]> git.ipfire.org Git - thirdparty/git.git/blobdiff - pack-revindex.c
strmap: enable allocations to come from a mem_pool
[thirdparty/git.git] / pack-revindex.c
index bb521cf7fb2911bc2d3653f46224880f8540f9ab..d28a7e43d0bd80c3dc968439b3262da7c80117c1 100644 (file)
@@ -1,6 +1,7 @@
 #include "cache.h"
 #include "pack-revindex.h"
 #include "object-store.h"
+#include "packfile.h"
 
 /*
  * Pack index for existing packs give us easy access to the offsets into
@@ -119,19 +120,20 @@ static void sort_revindex(struct revindex_entry *entries, unsigned n, off_t max)
  */
 static void create_pack_revindex(struct packed_git *p)
 {
-       unsigned num_ent = p->num_objects;
+       const unsigned num_ent = p->num_objects;
        unsigned i;
        const char *index = p->index_data;
+       const unsigned hashsz = the_hash_algo->rawsz;
 
        ALLOC_ARRAY(p->revindex, num_ent + 1);
        index += 4 * 256;
 
        if (p->index_version > 1) {
                const uint32_t *off_32 =
-                       (uint32_t *)(index + 8 + p->num_objects * (20 + 4));
+                       (uint32_t *)(index + 8 + p->num_objects * (hashsz + 4));
                const uint32_t *off_64 = off_32 + p->num_objects;
                for (i = 0; i < num_ent; i++) {
-                       uint32_t off = ntohl(*off_32++);
+                       const uint32_t off = ntohl(*off_32++);
                        if (!(off & 0x80000000)) {
                                p->revindex[i].offset = off;
                        } else {
@@ -142,34 +144,39 @@ static void create_pack_revindex(struct packed_git *p)
                }
        } else {
                for (i = 0; i < num_ent; i++) {
-                       uint32_t hl = *((uint32_t *)(index + 24 * i));
+                       const uint32_t hl = *((uint32_t *)(index + (hashsz + 4) * i));
                        p->revindex[i].offset = ntohl(hl);
                        p->revindex[i].nr = i;
                }
        }
 
-       /* This knows the pack format -- the 20-byte trailer
+       /*
+        * This knows the pack format -- the hash trailer
         * follows immediately after the last object data.
         */
-       p->revindex[num_ent].offset = p->pack_size - 20;
+       p->revindex[num_ent].offset = p->pack_size - hashsz;
        p->revindex[num_ent].nr = -1;
        sort_revindex(p->revindex, num_ent, p->pack_size);
 }
 
-void load_pack_revindex(struct packed_git *p)
+int load_pack_revindex(struct packed_git *p)
 {
-       if (!p->revindex)
+       if (!p->revindex) {
+               if (open_pack_index(p))
+                       return -1;
                create_pack_revindex(p);
+       }
+       return 0;
 }
 
 int find_revindex_position(struct packed_git *p, off_t ofs)
 {
        int lo = 0;
        int hi = p->num_objects + 1;
-       struct revindex_entry *revindex = p->revindex;
+       const struct revindex_entry *revindex = p->revindex;
 
        do {
-               unsigned mi = lo + (hi - lo) / 2;
+               const unsigned mi = lo + (hi - lo) / 2;
                if (revindex[mi].offset == ofs) {
                        return mi;
                } else if (ofs < revindex[mi].offset)
@@ -186,7 +193,9 @@ struct revindex_entry *find_pack_revindex(struct packed_git *p, off_t ofs)
 {
        int pos;
 
-       load_pack_revindex(p);
+       if (load_pack_revindex(p))
+               return NULL;
+
        pos = find_revindex_position(p, ofs);
 
        if (pos < 0)