Signed-off-by: Derrick Stolee <dstolee@xxxxxxxxxxxxx> --- midx.c | 96 ++++++++++++++++++++++++++++++++++++++++++++++++-- midx.h | 2 ++ object-store.h | 1 + packfile.c | 8 ++++- 4 files changed, 104 insertions(+), 3 deletions(-) diff --git a/midx.c b/midx.c index 5e9290ca8f..6eca8f1b12 100644 --- a/midx.c +++ b/midx.c @@ -3,6 +3,7 @@ #include "dir.h" #include "csum-file.h" #include "lockfile.h" +#include "sha1-lookup.h" #include "object-store.h" #include "packfile.h" #include "midx.h" @@ -64,7 +65,7 @@ struct midxed_git *load_midxed_git(const char *object_dir) m = xcalloc(1, sizeof(*m) + strlen(object_dir) + 1); strcpy(m->object_dir, object_dir); - m->data = midx_map; + m->data = (const unsigned char*)midx_map; m->signature = get_be32(m->data); if (m->signature != MIDX_SIGNATURE) { @@ -145,7 +146,9 @@ struct midxed_git *load_midxed_git(const char *object_dir) m->num_objects = ntohl(m->chunk_oid_fanout[255]); - m->pack_names = xcalloc(m->num_packs, sizeof(const char *)); + m->packs = xcalloc(m->num_packs, sizeof(*m->packs)); + + ALLOC_ARRAY(m->pack_names, m->num_packs); for (i = 0; i < m->num_packs; i++) { if (i) { if (ntohl(m->chunk_pack_lookup[i]) <= ntohl(m->chunk_pack_lookup[i - 1])) { @@ -175,6 +178,95 @@ struct midxed_git *load_midxed_git(const char *object_dir) exit(1); } +static int prepare_midx_pack(struct midxed_git *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_addstr(&pack_name, m->object_dir); + strbuf_addstr(&pack_name, "/pack/"); + strbuf_addstr(&pack_name, 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 midxed_git *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 midxed_git *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 midxed_git *m, uint32_t pos) +{ + return get_be32(m->chunk_object_offsets + pos * MIDX_CHUNK_OFFSET_WIDTH); +} + +static int nth_midxed_pack_entry(struct midxed_git *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 midxed_git *m) +{ + uint32_t pos; + + if (!bsearch_midx(oid, m, &pos)) + return 0; + + return nth_midxed_pack_entry(m, e, pos); +} + int prepare_midxed_git_one(struct repository *r, const char *object_dir) { struct midxed_git *m = r->objects->midxed_git; diff --git a/midx.h b/midx.h index 793203fc4a..0c66812229 100644 --- a/midx.h +++ b/midx.h @@ -8,6 +8,8 @@ #include "repository.h" struct midxed_git *load_midxed_git(const char *object_dir); +int bsearch_midx(const struct object_id *oid, struct midxed_git *m, uint32_t *result); +int fill_midx_entry(const struct object_id *oid, struct pack_entry *e, struct midxed_git *m); int prepare_midxed_git_one(struct repository *r, const char *object_dir); int write_midx_file(const char *object_dir); diff --git a/object-store.h b/object-store.h index 7908d46e34..5af2a852bc 100644 --- a/object-store.h +++ b/object-store.h @@ -108,6 +108,7 @@ struct midxed_git { const unsigned char *chunk_large_offsets; const char **pack_names; + struct packed_git **packs; char object_dir[FLEX_ARRAY]; }; diff --git a/packfile.c b/packfile.c index b91ca9b9f5..73f8cc28ee 100644 --- a/packfile.c +++ b/packfile.c @@ -1857,11 +1857,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 midxed_git *m; prepare_packed_git(r); - if (!r->objects->packed_git) + if (!r->objects->packed_git && !r->objects->midxed_git) return 0; + for (m = r->objects->midxed_git; 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)) { -- 2.18.0.rc1