]> git.ipfire.org Git - thirdparty/git.git/commitdiff
midx: read objects from multi-pack-index
authorDerrick Stolee <stolee@gmail.com>
Thu, 12 Jul 2018 19:39:34 +0000 (15:39 -0400)
committerJunio C Hamano <gitster@pobox.com>
Fri, 20 Jul 2018 18:27:28 +0000 (11:27 -0700)
Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
midx.c
midx.h
packfile.c

diff --git a/midx.c b/midx.c
index 4090cf4ca42fe5b384a5b70bb79bc405d15d4e80..182535933c33ed9a7874e38957e0d04d64a36c9d 100644 (file)
--- a/midx.c
+++ b/midx.c
@@ -5,7 +5,7 @@
 #include "lockfile.h"
 #include "packfile.h"
 #include "object-store.h"
-#include "packfile.h"
+#include "sha1-lookup.h"
 #include "midx.h"
 
 #define MIDX_SIGNATURE 0x4d494458 /* "MIDX" */
@@ -151,6 +151,7 @@ struct multi_pack_index *load_multi_pack_index(const char *object_dir)
        m->num_objects = ntohl(m->chunk_oid_fanout[255]);
 
        m->pack_names = xcalloc(m->num_packs, sizeof(*m->pack_names));
+       m->packs = xcalloc(m->num_packs, sizeof(*m->packs));
 
        cur_pack_name = (const char *)m->chunk_pack_names;
        for (i = 0; i < m->num_packs; i++) {
@@ -178,6 +179,94 @@ cleanup_fail:
        return NULL;
 }
 
+static int prepare_midx_pack(struct multi_pack_index *m, uint32_t pack_int_id)
+{
+       struct strbuf pack_name = STRBUF_INIT;
+
+       if (pack_int_id >= m->num_packs)
+               BUG("bad pack-int-id");
+
+       if (m->packs[pack_int_id])
+               return 0;
+
+       strbuf_addf(&pack_name, "%s/pack/%s", m->object_dir,
+                   m->pack_names[pack_int_id]);
+
+       m->packs[pack_int_id] = add_packed_git(pack_name.buf, pack_name.len, 1);
+       strbuf_release(&pack_name);
+       return !m->packs[pack_int_id];
+}
+
+int bsearch_midx(const struct object_id *oid, struct multi_pack_index *m, uint32_t *result)
+{
+       return bsearch_hash(oid->hash, m->chunk_oid_fanout, m->chunk_oid_lookup,
+                           MIDX_HASH_LEN, result);
+}
+
+static off_t nth_midxed_offset(struct multi_pack_index *m, uint32_t pos)
+{
+       const unsigned char *offset_data;
+       uint32_t offset32;
+
+       offset_data = m->chunk_object_offsets + pos * MIDX_CHUNK_OFFSET_WIDTH;
+       offset32 = get_be32(offset_data + sizeof(uint32_t));
+
+       if (m->chunk_large_offsets && offset32 & MIDX_LARGE_OFFSET_NEEDED) {
+               if (sizeof(offset32) < sizeof(uint64_t))
+                       die(_("multi-pack-index stores a 64-bit offset, but off_t is too small"));
+
+               offset32 ^= MIDX_LARGE_OFFSET_NEEDED;
+               return get_be64(m->chunk_large_offsets + sizeof(uint64_t) * offset32);
+       }
+
+       return offset32;
+}
+
+static uint32_t nth_midxed_pack_int_id(struct multi_pack_index *m, uint32_t pos)
+{
+       return get_be32(m->chunk_object_offsets + pos * MIDX_CHUNK_OFFSET_WIDTH);
+}
+
+static int nth_midxed_pack_entry(struct multi_pack_index *m, struct pack_entry *e, uint32_t pos)
+{
+       uint32_t pack_int_id;
+       struct packed_git *p;
+
+       if (pos >= m->num_objects)
+               return 0;
+
+       pack_int_id = nth_midxed_pack_int_id(m, pos);
+
+       if (prepare_midx_pack(m, pack_int_id))
+               die(_("error preparing packfile from multi-pack-index"));
+       p = m->packs[pack_int_id];
+
+       /*
+       * We are about to tell the caller where they can locate the
+       * requested object.  We better make sure the packfile is
+       * still here and can be accessed before supplying that
+       * answer, as it may have been deleted since the MIDX was
+       * loaded!
+       */
+       if (!is_pack_valid(p))
+               return 0;
+
+       e->offset = nth_midxed_offset(m, pos);
+       e->p = p;
+
+       return 1;
+}
+
+int fill_midx_entry(const struct object_id *oid, struct pack_entry *e, struct multi_pack_index *m)
+{
+       uint32_t pos;
+
+       if (!bsearch_midx(oid, m, &pos))
+               return 0;
+
+       return nth_midxed_pack_entry(m, e, pos);
+}
+
 int prepare_multi_pack_index_one(struct repository *r, const char *object_dir)
 {
        struct multi_pack_index *m = r->objects->multi_pack_index;
diff --git a/midx.h b/midx.h
index 9bcfc82d2ec3b275e1b23f01fd6c1dd0ebba858c..377838c9ca2724b34b1b85eeb1402b8b402dfc22 100644 (file)
--- a/midx.h
+++ b/midx.h
@@ -25,10 +25,13 @@ struct multi_pack_index {
        const unsigned char *chunk_large_offsets;
 
        const char **pack_names;
+       struct packed_git **packs;
        char object_dir[FLEX_ARRAY];
 };
 
 struct multi_pack_index *load_multi_pack_index(const char *object_dir);
+int bsearch_midx(const struct object_id *oid, struct multi_pack_index *m, uint32_t *result);
+int fill_midx_entry(const struct object_id *oid, struct pack_entry *e, struct multi_pack_index *m);
 int prepare_multi_pack_index_one(struct repository *r, const char *object_dir);
 
 int write_midx_file(const char *object_dir);
index 5d4493dbf4f9a4f047af54631541bd7138fa42af..bc763d91b9b64c8ad6e9edc79e23d3b7b1480b41 100644 (file)
@@ -1902,11 +1902,17 @@ static int fill_pack_entry(const struct object_id *oid,
 int find_pack_entry(struct repository *r, const struct object_id *oid, struct pack_entry *e)
 {
        struct list_head *pos;
+       struct multi_pack_index *m;
 
        prepare_packed_git(r);
-       if (!r->objects->packed_git)
+       if (!r->objects->packed_git && !r->objects->multi_pack_index)
                return 0;
 
+       for (m = r->objects->multi_pack_index; m; m = m->next) {
+               if (fill_midx_entry(oid, e, m))
+                       return 1;
+       }
+
        list_for_each(pos, &r->objects->packed_git_mru) {
                struct packed_git *p = list_entry(pos, struct packed_git, mru);
                if (fill_pack_entry(oid, e, p)) {