The sixth batch
[alt-git.git] / midx.c
blobd91088efb87ca0d59b1ad215556a8cfe1612b8af
1 #define DISABLE_SIGN_COMPARE_WARNINGS
3 #include "git-compat-util.h"
4 #include "config.h"
5 #include "dir.h"
6 #include "hex.h"
7 #include "packfile.h"
8 #include "object-file.h"
9 #include "hash-lookup.h"
10 #include "midx.h"
11 #include "progress.h"
12 #include "trace2.h"
13 #include "chunk-format.h"
14 #include "pack-bitmap.h"
15 #include "pack-revindex.h"
17 int midx_checksum_valid(struct multi_pack_index *m);
18 void clear_midx_files_ext(const char *object_dir, const char *ext,
19 const char *keep_hash);
20 void clear_incremental_midx_files_ext(const char *object_dir, const char *ext,
21 char **keep_hashes,
22 uint32_t hashes_nr);
23 int cmp_idx_or_pack_name(const char *idx_or_pack_name,
24 const char *idx_name);
26 const unsigned char *get_midx_checksum(struct multi_pack_index *m)
28 return m->data + m->data_len - m->repo->hash_algo->rawsz;
31 void get_midx_filename(const struct git_hash_algo *hash_algo,
32 struct strbuf *out, const char *object_dir)
34 get_midx_filename_ext(hash_algo, out, object_dir, NULL, NULL);
37 void get_midx_filename_ext(const struct git_hash_algo *hash_algo,
38 struct strbuf *out, const char *object_dir,
39 const unsigned char *hash, const char *ext)
41 strbuf_addf(out, "%s/pack/multi-pack-index", object_dir);
42 if (ext)
43 strbuf_addf(out, "-%s.%s", hash_to_hex_algop(hash, hash_algo), ext);
46 static int midx_read_oid_fanout(const unsigned char *chunk_start,
47 size_t chunk_size, void *data)
49 int i;
50 struct multi_pack_index *m = data;
51 m->chunk_oid_fanout = (uint32_t *)chunk_start;
53 if (chunk_size != 4 * 256) {
54 error(_("multi-pack-index OID fanout is of the wrong size"));
55 return 1;
57 for (i = 0; i < 255; i++) {
58 uint32_t oid_fanout1 = ntohl(m->chunk_oid_fanout[i]);
59 uint32_t oid_fanout2 = ntohl(m->chunk_oid_fanout[i+1]);
61 if (oid_fanout1 > oid_fanout2) {
62 error(_("oid fanout out of order: fanout[%d] = %"PRIx32" > %"PRIx32" = fanout[%d]"),
63 i, oid_fanout1, oid_fanout2, i + 1);
64 return 1;
67 m->num_objects = ntohl(m->chunk_oid_fanout[255]);
68 return 0;
71 static int midx_read_oid_lookup(const unsigned char *chunk_start,
72 size_t chunk_size, void *data)
74 struct multi_pack_index *m = data;
75 m->chunk_oid_lookup = chunk_start;
77 if (chunk_size != st_mult(m->hash_len, m->num_objects)) {
78 error(_("multi-pack-index OID lookup chunk is the wrong size"));
79 return 1;
81 return 0;
84 static int midx_read_object_offsets(const unsigned char *chunk_start,
85 size_t chunk_size, void *data)
87 struct multi_pack_index *m = data;
88 m->chunk_object_offsets = chunk_start;
90 if (chunk_size != st_mult(m->num_objects, MIDX_CHUNK_OFFSET_WIDTH)) {
91 error(_("multi-pack-index object offset chunk is the wrong size"));
92 return 1;
94 return 0;
97 static struct multi_pack_index *load_multi_pack_index_one(struct repository *r,
98 const char *object_dir,
99 const char *midx_name,
100 int local)
102 struct multi_pack_index *m = NULL;
103 int fd;
104 struct stat st;
105 size_t midx_size;
106 void *midx_map = NULL;
107 uint32_t hash_version;
108 uint32_t i;
109 const char *cur_pack_name;
110 struct chunkfile *cf = NULL;
112 fd = git_open(midx_name);
114 if (fd < 0)
115 goto cleanup_fail;
116 if (fstat(fd, &st)) {
117 error_errno(_("failed to read %s"), midx_name);
118 goto cleanup_fail;
121 midx_size = xsize_t(st.st_size);
123 if (midx_size < (MIDX_HEADER_SIZE + r->hash_algo->rawsz)) {
124 error(_("multi-pack-index file %s is too small"), midx_name);
125 goto cleanup_fail;
128 midx_map = xmmap(NULL, midx_size, PROT_READ, MAP_PRIVATE, fd, 0);
129 close(fd);
131 FLEX_ALLOC_STR(m, object_dir, object_dir);
132 m->data = midx_map;
133 m->data_len = midx_size;
134 m->local = local;
135 m->repo = r;
137 m->signature = get_be32(m->data);
138 if (m->signature != MIDX_SIGNATURE)
139 die(_("multi-pack-index signature 0x%08x does not match signature 0x%08x"),
140 m->signature, MIDX_SIGNATURE);
142 m->version = m->data[MIDX_BYTE_FILE_VERSION];
143 if (m->version != MIDX_VERSION)
144 die(_("multi-pack-index version %d not recognized"),
145 m->version);
147 hash_version = m->data[MIDX_BYTE_HASH_VERSION];
148 if (hash_version != oid_version(r->hash_algo)) {
149 error(_("multi-pack-index hash version %u does not match version %u"),
150 hash_version, oid_version(r->hash_algo));
151 goto cleanup_fail;
153 m->hash_len = r->hash_algo->rawsz;
155 m->num_chunks = m->data[MIDX_BYTE_NUM_CHUNKS];
157 m->num_packs = get_be32(m->data + MIDX_BYTE_NUM_PACKS);
159 m->preferred_pack_idx = -1;
161 cf = init_chunkfile(NULL);
163 if (read_table_of_contents(cf, m->data, midx_size,
164 MIDX_HEADER_SIZE, m->num_chunks,
165 MIDX_CHUNK_ALIGNMENT))
166 goto cleanup_fail;
168 if (pair_chunk(cf, MIDX_CHUNKID_PACKNAMES, &m->chunk_pack_names, &m->chunk_pack_names_len))
169 die(_("multi-pack-index required pack-name chunk missing or corrupted"));
170 if (read_chunk(cf, MIDX_CHUNKID_OIDFANOUT, midx_read_oid_fanout, m))
171 die(_("multi-pack-index required OID fanout chunk missing or corrupted"));
172 if (read_chunk(cf, MIDX_CHUNKID_OIDLOOKUP, midx_read_oid_lookup, m))
173 die(_("multi-pack-index required OID lookup chunk missing or corrupted"));
174 if (read_chunk(cf, MIDX_CHUNKID_OBJECTOFFSETS, midx_read_object_offsets, m))
175 die(_("multi-pack-index required object offsets chunk missing or corrupted"));
177 pair_chunk(cf, MIDX_CHUNKID_LARGEOFFSETS, &m->chunk_large_offsets,
178 &m->chunk_large_offsets_len);
179 if (git_env_bool("GIT_TEST_MIDX_READ_BTMP", 1))
180 pair_chunk(cf, MIDX_CHUNKID_BITMAPPEDPACKS,
181 (const unsigned char **)&m->chunk_bitmapped_packs,
182 &m->chunk_bitmapped_packs_len);
184 if (git_env_bool("GIT_TEST_MIDX_READ_RIDX", 1))
185 pair_chunk(cf, MIDX_CHUNKID_REVINDEX, &m->chunk_revindex,
186 &m->chunk_revindex_len);
188 CALLOC_ARRAY(m->pack_names, m->num_packs);
189 CALLOC_ARRAY(m->packs, m->num_packs);
191 cur_pack_name = (const char *)m->chunk_pack_names;
192 for (i = 0; i < m->num_packs; i++) {
193 const char *end;
194 size_t avail = m->chunk_pack_names_len -
195 (cur_pack_name - (const char *)m->chunk_pack_names);
197 m->pack_names[i] = cur_pack_name;
199 end = memchr(cur_pack_name, '\0', avail);
200 if (!end)
201 die(_("multi-pack-index pack-name chunk is too short"));
202 cur_pack_name = end + 1;
204 if (i && strcmp(m->pack_names[i], m->pack_names[i - 1]) <= 0)
205 die(_("multi-pack-index pack names out of order: '%s' before '%s'"),
206 m->pack_names[i - 1],
207 m->pack_names[i]);
210 trace2_data_intmax("midx", r, "load/num_packs", m->num_packs);
211 trace2_data_intmax("midx", r, "load/num_objects", m->num_objects);
213 free_chunkfile(cf);
214 return m;
216 cleanup_fail:
217 free(m);
218 free_chunkfile(cf);
219 if (midx_map)
220 munmap(midx_map, midx_size);
221 if (0 <= fd)
222 close(fd);
223 return NULL;
226 void get_midx_chain_dirname(struct strbuf *buf, const char *object_dir)
228 strbuf_addf(buf, "%s/pack/multi-pack-index.d", object_dir);
231 void get_midx_chain_filename(struct strbuf *buf, const char *object_dir)
233 get_midx_chain_dirname(buf, object_dir);
234 strbuf_addstr(buf, "/multi-pack-index-chain");
237 void get_split_midx_filename_ext(const struct git_hash_algo *hash_algo,
238 struct strbuf *buf, const char *object_dir,
239 const unsigned char *hash, const char *ext)
241 get_midx_chain_dirname(buf, object_dir);
242 strbuf_addf(buf, "/multi-pack-index-%s.%s",
243 hash_to_hex_algop(hash, hash_algo), ext);
246 static int open_multi_pack_index_chain(const struct git_hash_algo *hash_algo,
247 const char *chain_file, int *fd,
248 struct stat *st)
250 *fd = git_open(chain_file);
251 if (*fd < 0)
252 return 0;
253 if (fstat(*fd, st)) {
254 close(*fd);
255 return 0;
257 if (st->st_size < hash_algo->hexsz) {
258 close(*fd);
259 if (!st->st_size) {
260 /* treat empty files the same as missing */
261 errno = ENOENT;
262 } else {
263 warning(_("multi-pack-index chain file too small"));
264 errno = EINVAL;
266 return 0;
268 return 1;
271 static int add_midx_to_chain(struct multi_pack_index *midx,
272 struct multi_pack_index *midx_chain)
274 if (midx_chain) {
275 if (unsigned_add_overflows(midx_chain->num_packs,
276 midx_chain->num_packs_in_base)) {
277 warning(_("pack count in base MIDX too high: %"PRIuMAX),
278 (uintmax_t)midx_chain->num_packs_in_base);
279 return 0;
281 if (unsigned_add_overflows(midx_chain->num_objects,
282 midx_chain->num_objects_in_base)) {
283 warning(_("object count in base MIDX too high: %"PRIuMAX),
284 (uintmax_t)midx_chain->num_objects_in_base);
285 return 0;
287 midx->num_packs_in_base = midx_chain->num_packs +
288 midx_chain->num_packs_in_base;
289 midx->num_objects_in_base = midx_chain->num_objects +
290 midx_chain->num_objects_in_base;
293 midx->base_midx = midx_chain;
294 midx->has_chain = 1;
296 return 1;
299 static struct multi_pack_index *load_midx_chain_fd_st(struct repository *r,
300 const char *object_dir,
301 int local,
302 int fd, struct stat *st,
303 int *incomplete_chain)
305 struct multi_pack_index *midx_chain = NULL;
306 struct strbuf buf = STRBUF_INIT;
307 int valid = 1;
308 uint32_t i, count;
309 FILE *fp = xfdopen(fd, "r");
311 count = st->st_size / (r->hash_algo->hexsz + 1);
313 for (i = 0; i < count; i++) {
314 struct multi_pack_index *m;
315 struct object_id layer;
317 if (strbuf_getline_lf(&buf, fp) == EOF)
318 break;
320 if (get_oid_hex_algop(buf.buf, &layer, r->hash_algo)) {
321 warning(_("invalid multi-pack-index chain: line '%s' "
322 "not a hash"),
323 buf.buf);
324 valid = 0;
325 break;
328 valid = 0;
330 strbuf_reset(&buf);
331 get_split_midx_filename_ext(r->hash_algo, &buf, object_dir,
332 layer.hash, MIDX_EXT_MIDX);
333 m = load_multi_pack_index_one(r, object_dir, buf.buf, local);
335 if (m) {
336 if (add_midx_to_chain(m, midx_chain)) {
337 midx_chain = m;
338 valid = 1;
339 } else {
340 close_midx(m);
343 if (!valid) {
344 warning(_("unable to find all multi-pack index files"));
345 break;
349 fclose(fp);
350 strbuf_release(&buf);
352 *incomplete_chain = !valid;
353 return midx_chain;
356 static struct multi_pack_index *load_multi_pack_index_chain(struct repository *r,
357 const char *object_dir,
358 int local)
360 struct strbuf chain_file = STRBUF_INIT;
361 struct stat st;
362 int fd;
363 struct multi_pack_index *m = NULL;
365 get_midx_chain_filename(&chain_file, object_dir);
366 if (open_multi_pack_index_chain(r->hash_algo, chain_file.buf, &fd, &st)) {
367 int incomplete;
368 /* ownership of fd is taken over by load function */
369 m = load_midx_chain_fd_st(r, object_dir, local, fd, &st,
370 &incomplete);
373 strbuf_release(&chain_file);
374 return m;
377 struct multi_pack_index *load_multi_pack_index(struct repository *r,
378 const char *object_dir,
379 int local)
381 struct strbuf midx_name = STRBUF_INIT;
382 struct multi_pack_index *m;
384 get_midx_filename(r->hash_algo, &midx_name, object_dir);
386 m = load_multi_pack_index_one(r, object_dir,
387 midx_name.buf, local);
388 if (!m)
389 m = load_multi_pack_index_chain(r, object_dir, local);
391 strbuf_release(&midx_name);
393 return m;
396 void close_midx(struct multi_pack_index *m)
398 uint32_t i;
400 if (!m)
401 return;
403 close_midx(m->next);
404 close_midx(m->base_midx);
406 munmap((unsigned char *)m->data, m->data_len);
408 for (i = 0; i < m->num_packs; i++) {
409 if (m->packs[i])
410 m->packs[i]->multi_pack_index = 0;
412 FREE_AND_NULL(m->packs);
413 FREE_AND_NULL(m->pack_names);
414 free(m);
417 static uint32_t midx_for_object(struct multi_pack_index **_m, uint32_t pos)
419 struct multi_pack_index *m = *_m;
420 while (m && pos < m->num_objects_in_base)
421 m = m->base_midx;
423 if (!m)
424 BUG("NULL multi-pack-index for object position: %"PRIu32, pos);
426 if (pos >= m->num_objects + m->num_objects_in_base)
427 die(_("invalid MIDX object position, MIDX is likely corrupt"));
429 *_m = m;
431 return pos - m->num_objects_in_base;
434 static uint32_t midx_for_pack(struct multi_pack_index **_m,
435 uint32_t pack_int_id)
437 struct multi_pack_index *m = *_m;
438 while (m && pack_int_id < m->num_packs_in_base)
439 m = m->base_midx;
441 if (!m)
442 BUG("NULL multi-pack-index for pack ID: %"PRIu32, pack_int_id);
444 if (pack_int_id >= m->num_packs + m->num_packs_in_base)
445 die(_("bad pack-int-id: %u (%u total packs)"),
446 pack_int_id, m->num_packs + m->num_packs_in_base);
448 *_m = m;
450 return pack_int_id - m->num_packs_in_base;
453 int prepare_midx_pack(struct repository *r, struct multi_pack_index *m,
454 uint32_t pack_int_id)
456 struct strbuf pack_name = STRBUF_INIT;
457 struct strbuf key = STRBUF_INIT;
458 struct packed_git *p;
460 pack_int_id = midx_for_pack(&m, pack_int_id);
462 if (m->packs[pack_int_id])
463 return 0;
465 strbuf_addf(&pack_name, "%s/pack/%s", m->object_dir,
466 m->pack_names[pack_int_id]);
468 /* pack_map holds the ".pack" name, but we have the .idx */
469 strbuf_addbuf(&key, &pack_name);
470 strbuf_strip_suffix(&key, ".idx");
471 strbuf_addstr(&key, ".pack");
472 p = hashmap_get_entry_from_hash(&r->objects->pack_map,
473 strhash(key.buf), key.buf,
474 struct packed_git, packmap_ent);
475 if (!p) {
476 p = add_packed_git(r, pack_name.buf, pack_name.len, m->local);
477 if (p) {
478 install_packed_git(r, p);
479 list_add_tail(&p->mru, &r->objects->packed_git_mru);
483 strbuf_release(&pack_name);
484 strbuf_release(&key);
486 if (!p)
487 return 1;
489 p->multi_pack_index = 1;
490 m->packs[pack_int_id] = p;
492 return 0;
495 struct packed_git *nth_midxed_pack(struct multi_pack_index *m,
496 uint32_t pack_int_id)
498 uint32_t local_pack_int_id = midx_for_pack(&m, pack_int_id);
499 return m->packs[local_pack_int_id];
502 #define MIDX_CHUNK_BITMAPPED_PACKS_WIDTH (2 * sizeof(uint32_t))
504 int nth_bitmapped_pack(struct repository *r, struct multi_pack_index *m,
505 struct bitmapped_pack *bp, uint32_t pack_int_id)
507 uint32_t local_pack_int_id = midx_for_pack(&m, pack_int_id);
509 if (!m->chunk_bitmapped_packs)
510 return error(_("MIDX does not contain the BTMP chunk"));
512 if (prepare_midx_pack(r, m, pack_int_id))
513 return error(_("could not load bitmapped pack %"PRIu32), pack_int_id);
515 bp->p = m->packs[local_pack_int_id];
516 bp->bitmap_pos = get_be32((char *)m->chunk_bitmapped_packs +
517 MIDX_CHUNK_BITMAPPED_PACKS_WIDTH * local_pack_int_id);
518 bp->bitmap_nr = get_be32((char *)m->chunk_bitmapped_packs +
519 MIDX_CHUNK_BITMAPPED_PACKS_WIDTH * local_pack_int_id +
520 sizeof(uint32_t));
521 bp->pack_int_id = pack_int_id;
522 bp->from_midx = m;
524 return 0;
527 int bsearch_one_midx(const struct object_id *oid, struct multi_pack_index *m,
528 uint32_t *result)
530 int ret = bsearch_hash(oid->hash, m->chunk_oid_fanout,
531 m->chunk_oid_lookup, m->repo->hash_algo->rawsz,
532 result);
533 if (result)
534 *result += m->num_objects_in_base;
535 return ret;
538 int bsearch_midx(const struct object_id *oid, struct multi_pack_index *m,
539 uint32_t *result)
541 for (; m; m = m->base_midx)
542 if (bsearch_one_midx(oid, m, result))
543 return 1;
544 return 0;
547 int midx_has_oid(struct multi_pack_index *m, const struct object_id *oid)
549 return bsearch_midx(oid, m, NULL);
552 struct object_id *nth_midxed_object_oid(struct object_id *oid,
553 struct multi_pack_index *m,
554 uint32_t n)
556 if (n >= m->num_objects + m->num_objects_in_base)
557 return NULL;
559 n = midx_for_object(&m, n);
561 oidread(oid, m->chunk_oid_lookup + st_mult(m->hash_len, n),
562 m->repo->hash_algo);
563 return oid;
566 off_t nth_midxed_offset(struct multi_pack_index *m, uint32_t pos)
568 const unsigned char *offset_data;
569 uint32_t offset32;
571 pos = midx_for_object(&m, pos);
573 offset_data = m->chunk_object_offsets + (off_t)pos * MIDX_CHUNK_OFFSET_WIDTH;
574 offset32 = get_be32(offset_data + sizeof(uint32_t));
576 if (m->chunk_large_offsets && offset32 & MIDX_LARGE_OFFSET_NEEDED) {
577 if (sizeof(off_t) < sizeof(uint64_t))
578 die(_("multi-pack-index stores a 64-bit offset, but off_t is too small"));
580 offset32 ^= MIDX_LARGE_OFFSET_NEEDED;
581 if (offset32 >= m->chunk_large_offsets_len / sizeof(uint64_t))
582 die(_("multi-pack-index large offset out of bounds"));
583 return get_be64(m->chunk_large_offsets + sizeof(uint64_t) * offset32);
586 return offset32;
589 uint32_t nth_midxed_pack_int_id(struct multi_pack_index *m, uint32_t pos)
591 pos = midx_for_object(&m, pos);
593 return m->num_packs_in_base + get_be32(m->chunk_object_offsets +
594 (off_t)pos * MIDX_CHUNK_OFFSET_WIDTH);
597 int fill_midx_entry(struct repository *r,
598 const struct object_id *oid,
599 struct pack_entry *e,
600 struct multi_pack_index *m)
602 uint32_t pos;
603 uint32_t pack_int_id;
604 struct packed_git *p;
606 if (!bsearch_midx(oid, m, &pos))
607 return 0;
609 midx_for_object(&m, pos);
610 pack_int_id = nth_midxed_pack_int_id(m, pos);
612 if (prepare_midx_pack(r, m, pack_int_id))
613 return 0;
614 p = m->packs[pack_int_id - m->num_packs_in_base];
617 * We are about to tell the caller where they can locate the
618 * requested object. We better make sure the packfile is
619 * still here and can be accessed before supplying that
620 * answer, as it may have been deleted since the MIDX was
621 * loaded!
623 if (!is_pack_valid(p))
624 return 0;
626 if (oidset_size(&p->bad_objects) &&
627 oidset_contains(&p->bad_objects, oid))
628 return 0;
630 e->offset = nth_midxed_offset(m, pos);
631 e->p = p;
633 return 1;
636 /* Match "foo.idx" against either "foo.pack" _or_ "foo.idx". */
637 int cmp_idx_or_pack_name(const char *idx_or_pack_name,
638 const char *idx_name)
640 /* Skip past any initial matching prefix. */
641 while (*idx_name && *idx_name == *idx_or_pack_name) {
642 idx_name++;
643 idx_or_pack_name++;
647 * If we didn't match completely, we may have matched "pack-1234." and
648 * be left with "idx" and "pack" respectively, which is also OK. We do
649 * not have to check for "idx" and "idx", because that would have been
650 * a complete match (and in that case these strcmps will be false, but
651 * we'll correctly return 0 from the final strcmp() below.
653 * Technically this matches "fooidx" and "foopack", but we'd never have
654 * such names in the first place.
656 if (!strcmp(idx_name, "idx") && !strcmp(idx_or_pack_name, "pack"))
657 return 0;
660 * This not only checks for a complete match, but also orders based on
661 * the first non-identical character, which means our ordering will
662 * match a raw strcmp(). That makes it OK to use this to binary search
663 * a naively-sorted list.
665 return strcmp(idx_or_pack_name, idx_name);
668 static int midx_contains_pack_1(struct multi_pack_index *m,
669 const char *idx_or_pack_name)
671 uint32_t first = 0, last = m->num_packs;
673 while (first < last) {
674 uint32_t mid = first + (last - first) / 2;
675 const char *current;
676 int cmp;
678 current = m->pack_names[mid];
679 cmp = cmp_idx_or_pack_name(idx_or_pack_name, current);
680 if (!cmp)
681 return 1;
682 if (cmp > 0) {
683 first = mid + 1;
684 continue;
686 last = mid;
689 return 0;
692 int midx_contains_pack(struct multi_pack_index *m, const char *idx_or_pack_name)
694 for (; m; m = m->base_midx)
695 if (midx_contains_pack_1(m, idx_or_pack_name))
696 return 1;
697 return 0;
700 int midx_preferred_pack(struct multi_pack_index *m, uint32_t *pack_int_id)
702 if (m->preferred_pack_idx == -1) {
703 uint32_t midx_pos;
704 if (load_midx_revindex(m) < 0) {
705 m->preferred_pack_idx = -2;
706 return -1;
709 midx_pos = pack_pos_to_midx(m, m->num_objects_in_base);
711 m->preferred_pack_idx = nth_midxed_pack_int_id(m, midx_pos);
713 } else if (m->preferred_pack_idx == -2)
714 return -1; /* no revindex */
716 *pack_int_id = m->preferred_pack_idx;
717 return 0;
720 int prepare_multi_pack_index_one(struct repository *r, const char *object_dir, int local)
722 struct multi_pack_index *m;
723 struct multi_pack_index *m_search;
725 prepare_repo_settings(r);
726 if (!r->settings.core_multi_pack_index)
727 return 0;
729 for (m_search = r->objects->multi_pack_index; m_search; m_search = m_search->next)
730 if (!strcmp(object_dir, m_search->object_dir))
731 return 1;
733 m = load_multi_pack_index(r, object_dir, local);
735 if (m) {
736 struct multi_pack_index *mp = r->objects->multi_pack_index;
737 if (mp) {
738 m->next = mp->next;
739 mp->next = m;
740 } else
741 r->objects->multi_pack_index = m;
742 return 1;
745 return 0;
748 int midx_checksum_valid(struct multi_pack_index *m)
750 return hashfile_checksum_valid(m->data, m->data_len);
753 struct clear_midx_data {
754 char **keep;
755 uint32_t keep_nr;
756 const char *ext;
759 static void clear_midx_file_ext(const char *full_path, size_t full_path_len UNUSED,
760 const char *file_name, void *_data)
762 struct clear_midx_data *data = _data;
763 uint32_t i;
765 if (!(starts_with(file_name, "multi-pack-index-") &&
766 ends_with(file_name, data->ext)))
767 return;
768 for (i = 0; i < data->keep_nr; i++) {
769 if (!strcmp(data->keep[i], file_name))
770 return;
772 if (unlink(full_path))
773 die_errno(_("failed to remove %s"), full_path);
776 void clear_midx_files_ext(const char *object_dir, const char *ext,
777 const char *keep_hash)
779 struct clear_midx_data data;
780 memset(&data, 0, sizeof(struct clear_midx_data));
782 if (keep_hash) {
783 ALLOC_ARRAY(data.keep, 1);
785 data.keep[0] = xstrfmt("multi-pack-index-%s.%s", keep_hash, ext);
786 data.keep_nr = 1;
788 data.ext = ext;
790 for_each_file_in_pack_dir(object_dir,
791 clear_midx_file_ext,
792 &data);
794 if (keep_hash)
795 free(data.keep[0]);
796 free(data.keep);
799 void clear_incremental_midx_files_ext(const char *object_dir, const char *ext,
800 char **keep_hashes,
801 uint32_t hashes_nr)
803 struct clear_midx_data data;
804 uint32_t i;
806 memset(&data, 0, sizeof(struct clear_midx_data));
808 ALLOC_ARRAY(data.keep, hashes_nr);
809 for (i = 0; i < hashes_nr; i++)
810 data.keep[i] = xstrfmt("multi-pack-index-%s.%s", keep_hashes[i],
811 ext);
812 data.keep_nr = hashes_nr;
813 data.ext = ext;
815 for_each_file_in_pack_subdir(object_dir, "multi-pack-index.d",
816 clear_midx_file_ext, &data);
818 for (i = 0; i < hashes_nr; i++)
819 free(data.keep[i]);
820 free(data.keep);
823 void clear_midx_file(struct repository *r)
825 struct strbuf midx = STRBUF_INIT;
827 get_midx_filename(r->hash_algo, &midx, r->objects->odb->path);
829 if (r->objects && r->objects->multi_pack_index) {
830 close_midx(r->objects->multi_pack_index);
831 r->objects->multi_pack_index = NULL;
834 if (remove_path(midx.buf))
835 die(_("failed to clear multi-pack-index at %s"), midx.buf);
837 clear_midx_files_ext(r->objects->odb->path, MIDX_EXT_BITMAP, NULL);
838 clear_midx_files_ext(r->objects->odb->path, MIDX_EXT_REV, NULL);
840 strbuf_release(&midx);
843 static int verify_midx_error;
845 __attribute__((format (printf, 1, 2)))
846 static void midx_report(const char *fmt, ...)
848 va_list ap;
849 verify_midx_error = 1;
850 va_start(ap, fmt);
851 vfprintf(stderr, fmt, ap);
852 fprintf(stderr, "\n");
853 va_end(ap);
856 struct pair_pos_vs_id
858 uint32_t pos;
859 uint32_t pack_int_id;
862 static int compare_pair_pos_vs_id(const void *_a, const void *_b)
864 struct pair_pos_vs_id *a = (struct pair_pos_vs_id *)_a;
865 struct pair_pos_vs_id *b = (struct pair_pos_vs_id *)_b;
867 return b->pack_int_id - a->pack_int_id;
871 * Limit calls to display_progress() for performance reasons.
872 * The interval here was arbitrarily chosen.
874 #define SPARSE_PROGRESS_INTERVAL (1 << 12)
875 #define midx_display_sparse_progress(progress, n) \
876 do { \
877 uint64_t _n = (n); \
878 if ((_n & (SPARSE_PROGRESS_INTERVAL - 1)) == 0) \
879 display_progress(progress, _n); \
880 } while (0)
882 int verify_midx_file(struct repository *r, const char *object_dir, unsigned flags)
884 struct pair_pos_vs_id *pairs = NULL;
885 uint32_t i;
886 struct progress *progress = NULL;
887 struct multi_pack_index *m = load_multi_pack_index(r, object_dir, 1);
888 struct multi_pack_index *curr;
889 verify_midx_error = 0;
891 if (!m) {
892 int result = 0;
893 struct stat sb;
894 struct strbuf filename = STRBUF_INIT;
896 get_midx_filename(r->hash_algo, &filename, object_dir);
898 if (!stat(filename.buf, &sb)) {
899 error(_("multi-pack-index file exists, but failed to parse"));
900 result = 1;
902 strbuf_release(&filename);
903 return result;
906 if (!midx_checksum_valid(m))
907 midx_report(_("incorrect checksum"));
909 if (flags & MIDX_PROGRESS)
910 progress = start_delayed_progress(r,
911 _("Looking for referenced packfiles"),
912 m->num_packs + m->num_packs_in_base);
913 for (i = 0; i < m->num_packs + m->num_packs_in_base; i++) {
914 if (prepare_midx_pack(r, m, i))
915 midx_report("failed to load pack in position %d", i);
917 display_progress(progress, i + 1);
919 stop_progress(&progress);
921 if (m->num_objects == 0) {
922 midx_report(_("the midx contains no oid"));
924 * Remaining tests assume that we have objects, so we can
925 * return here.
927 goto cleanup;
930 if (flags & MIDX_PROGRESS)
931 progress = start_sparse_progress(r,
932 _("Verifying OID order in multi-pack-index"),
933 m->num_objects - 1);
935 for (curr = m; curr; curr = curr->base_midx) {
936 for (i = 0; i < m->num_objects - 1; i++) {
937 struct object_id oid1, oid2;
939 nth_midxed_object_oid(&oid1, m, m->num_objects_in_base + i);
940 nth_midxed_object_oid(&oid2, m, m->num_objects_in_base + i + 1);
942 if (oidcmp(&oid1, &oid2) >= 0)
943 midx_report(_("oid lookup out of order: oid[%d] = %s >= %s = oid[%d]"),
944 i, oid_to_hex(&oid1), oid_to_hex(&oid2), i + 1);
946 midx_display_sparse_progress(progress, i + 1);
949 stop_progress(&progress);
952 * Create an array mapping each object to its packfile id. Sort it
953 * to group the objects by packfile. Use this permutation to visit
954 * each of the objects and only require 1 packfile to be open at a
955 * time.
957 ALLOC_ARRAY(pairs, m->num_objects + m->num_objects_in_base);
958 for (i = 0; i < m->num_objects + m->num_objects_in_base; i++) {
959 pairs[i].pos = i;
960 pairs[i].pack_int_id = nth_midxed_pack_int_id(m, i);
963 if (flags & MIDX_PROGRESS)
964 progress = start_sparse_progress(r,
965 _("Sorting objects by packfile"),
966 m->num_objects);
967 display_progress(progress, 0); /* TODO: Measure QSORT() progress */
968 QSORT(pairs, m->num_objects, compare_pair_pos_vs_id);
969 stop_progress(&progress);
971 if (flags & MIDX_PROGRESS)
972 progress = start_sparse_progress(r,
973 _("Verifying object offsets"),
974 m->num_objects);
975 for (i = 0; i < m->num_objects + m->num_objects_in_base; i++) {
976 struct object_id oid;
977 struct pack_entry e;
978 off_t m_offset, p_offset;
980 if (i > 0 && pairs[i-1].pack_int_id != pairs[i].pack_int_id &&
981 nth_midxed_pack(m, pairs[i-1].pack_int_id)) {
982 uint32_t pack_int_id = pairs[i-1].pack_int_id;
983 struct packed_git *p = nth_midxed_pack(m, pack_int_id);
985 close_pack_fd(p);
986 close_pack_index(p);
989 nth_midxed_object_oid(&oid, m, pairs[i].pos);
991 if (!fill_midx_entry(r, &oid, &e, m)) {
992 midx_report(_("failed to load pack entry for oid[%d] = %s"),
993 pairs[i].pos, oid_to_hex(&oid));
994 continue;
997 if (open_pack_index(e.p)) {
998 midx_report(_("failed to load pack-index for packfile %s"),
999 e.p->pack_name);
1000 break;
1003 m_offset = e.offset;
1004 p_offset = find_pack_entry_one(&oid, e.p);
1006 if (m_offset != p_offset)
1007 midx_report(_("incorrect object offset for oid[%d] = %s: %"PRIx64" != %"PRIx64),
1008 pairs[i].pos, oid_to_hex(&oid), m_offset, p_offset);
1010 midx_display_sparse_progress(progress, i + 1);
1012 stop_progress(&progress);
1014 cleanup:
1015 free(pairs);
1016 close_midx(m);
1018 return verify_midx_error;