Merge https://github.com/j6t/git-gui
[git.git] / midx.c
blob3992b054658366148eb51e1cb6ac753d60988694
1 #define USE_THE_REPOSITORY_VARIABLE
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 unsigned char *keep_hash);
20 int cmp_idx_or_pack_name(const char *idx_or_pack_name,
21 const char *idx_name);
23 const unsigned char *get_midx_checksum(struct multi_pack_index *m)
25 return m->data + m->data_len - the_hash_algo->rawsz;
28 void get_midx_filename(struct strbuf *out, const char *object_dir)
30 get_midx_filename_ext(out, object_dir, NULL, NULL);
33 void get_midx_filename_ext(struct strbuf *out, const char *object_dir,
34 const unsigned char *hash, const char *ext)
36 strbuf_addf(out, "%s/pack/multi-pack-index", object_dir);
37 if (ext)
38 strbuf_addf(out, "-%s.%s", hash_to_hex(hash), ext);
41 static int midx_read_oid_fanout(const unsigned char *chunk_start,
42 size_t chunk_size, void *data)
44 int i;
45 struct multi_pack_index *m = data;
46 m->chunk_oid_fanout = (uint32_t *)chunk_start;
48 if (chunk_size != 4 * 256) {
49 error(_("multi-pack-index OID fanout is of the wrong size"));
50 return 1;
52 for (i = 0; i < 255; i++) {
53 uint32_t oid_fanout1 = ntohl(m->chunk_oid_fanout[i]);
54 uint32_t oid_fanout2 = ntohl(m->chunk_oid_fanout[i+1]);
56 if (oid_fanout1 > oid_fanout2) {
57 error(_("oid fanout out of order: fanout[%d] = %"PRIx32" > %"PRIx32" = fanout[%d]"),
58 i, oid_fanout1, oid_fanout2, i + 1);
59 return 1;
62 m->num_objects = ntohl(m->chunk_oid_fanout[255]);
63 return 0;
66 static int midx_read_oid_lookup(const unsigned char *chunk_start,
67 size_t chunk_size, void *data)
69 struct multi_pack_index *m = data;
70 m->chunk_oid_lookup = chunk_start;
72 if (chunk_size != st_mult(m->hash_len, m->num_objects)) {
73 error(_("multi-pack-index OID lookup chunk is the wrong size"));
74 return 1;
76 return 0;
79 static int midx_read_object_offsets(const unsigned char *chunk_start,
80 size_t chunk_size, void *data)
82 struct multi_pack_index *m = data;
83 m->chunk_object_offsets = chunk_start;
85 if (chunk_size != st_mult(m->num_objects, MIDX_CHUNK_OFFSET_WIDTH)) {
86 error(_("multi-pack-index object offset chunk is the wrong size"));
87 return 1;
89 return 0;
92 #define MIDX_MIN_SIZE (MIDX_HEADER_SIZE + the_hash_algo->rawsz)
94 struct multi_pack_index *load_multi_pack_index(const char *object_dir, int local)
96 struct multi_pack_index *m = NULL;
97 int fd;
98 struct stat st;
99 size_t midx_size;
100 void *midx_map = NULL;
101 uint32_t hash_version;
102 struct strbuf midx_name = STRBUF_INIT;
103 uint32_t i;
104 const char *cur_pack_name;
105 struct chunkfile *cf = NULL;
107 get_midx_filename(&midx_name, object_dir);
109 fd = git_open(midx_name.buf);
111 if (fd < 0)
112 goto cleanup_fail;
113 if (fstat(fd, &st)) {
114 error_errno(_("failed to read %s"), midx_name.buf);
115 goto cleanup_fail;
118 midx_size = xsize_t(st.st_size);
120 if (midx_size < MIDX_MIN_SIZE) {
121 error(_("multi-pack-index file %s is too small"), midx_name.buf);
122 goto cleanup_fail;
125 strbuf_release(&midx_name);
127 midx_map = xmmap(NULL, midx_size, PROT_READ, MAP_PRIVATE, fd, 0);
128 close(fd);
130 FLEX_ALLOC_STR(m, object_dir, object_dir);
131 m->data = midx_map;
132 m->data_len = midx_size;
133 m->local = local;
135 m->signature = get_be32(m->data);
136 if (m->signature != MIDX_SIGNATURE)
137 die(_("multi-pack-index signature 0x%08x does not match signature 0x%08x"),
138 m->signature, MIDX_SIGNATURE);
140 m->version = m->data[MIDX_BYTE_FILE_VERSION];
141 if (m->version != MIDX_VERSION)
142 die(_("multi-pack-index version %d not recognized"),
143 m->version);
145 hash_version = m->data[MIDX_BYTE_HASH_VERSION];
146 if (hash_version != oid_version(the_hash_algo)) {
147 error(_("multi-pack-index hash version %u does not match version %u"),
148 hash_version, oid_version(the_hash_algo));
149 goto cleanup_fail;
151 m->hash_len = the_hash_algo->rawsz;
153 m->num_chunks = m->data[MIDX_BYTE_NUM_CHUNKS];
155 m->num_packs = get_be32(m->data + MIDX_BYTE_NUM_PACKS);
157 m->preferred_pack_idx = -1;
159 cf = init_chunkfile(NULL);
161 if (read_table_of_contents(cf, m->data, midx_size,
162 MIDX_HEADER_SIZE, m->num_chunks,
163 MIDX_CHUNK_ALIGNMENT))
164 goto cleanup_fail;
166 if (pair_chunk(cf, MIDX_CHUNKID_PACKNAMES, &m->chunk_pack_names, &m->chunk_pack_names_len))
167 die(_("multi-pack-index required pack-name chunk missing or corrupted"));
168 if (read_chunk(cf, MIDX_CHUNKID_OIDFANOUT, midx_read_oid_fanout, m))
169 die(_("multi-pack-index required OID fanout chunk missing or corrupted"));
170 if (read_chunk(cf, MIDX_CHUNKID_OIDLOOKUP, midx_read_oid_lookup, m))
171 die(_("multi-pack-index required OID lookup chunk missing or corrupted"));
172 if (read_chunk(cf, MIDX_CHUNKID_OBJECTOFFSETS, midx_read_object_offsets, m))
173 die(_("multi-pack-index required object offsets chunk missing or corrupted"));
175 pair_chunk(cf, MIDX_CHUNKID_LARGEOFFSETS, &m->chunk_large_offsets,
176 &m->chunk_large_offsets_len);
177 if (git_env_bool("GIT_TEST_MIDX_READ_BTMP", 1))
178 pair_chunk(cf, MIDX_CHUNKID_BITMAPPEDPACKS,
179 (const unsigned char **)&m->chunk_bitmapped_packs,
180 &m->chunk_bitmapped_packs_len);
182 if (git_env_bool("GIT_TEST_MIDX_READ_RIDX", 1))
183 pair_chunk(cf, MIDX_CHUNKID_REVINDEX, &m->chunk_revindex,
184 &m->chunk_revindex_len);
186 CALLOC_ARRAY(m->pack_names, m->num_packs);
187 CALLOC_ARRAY(m->packs, m->num_packs);
189 cur_pack_name = (const char *)m->chunk_pack_names;
190 for (i = 0; i < m->num_packs; i++) {
191 const char *end;
192 size_t avail = m->chunk_pack_names_len -
193 (cur_pack_name - (const char *)m->chunk_pack_names);
195 m->pack_names[i] = cur_pack_name;
197 end = memchr(cur_pack_name, '\0', avail);
198 if (!end)
199 die(_("multi-pack-index pack-name chunk is too short"));
200 cur_pack_name = end + 1;
202 if (i && strcmp(m->pack_names[i], m->pack_names[i - 1]) <= 0)
203 die(_("multi-pack-index pack names out of order: '%s' before '%s'"),
204 m->pack_names[i - 1],
205 m->pack_names[i]);
208 trace2_data_intmax("midx", the_repository, "load/num_packs", m->num_packs);
209 trace2_data_intmax("midx", the_repository, "load/num_objects", m->num_objects);
211 free_chunkfile(cf);
212 return m;
214 cleanup_fail:
215 free(m);
216 strbuf_release(&midx_name);
217 free_chunkfile(cf);
218 if (midx_map)
219 munmap(midx_map, midx_size);
220 if (0 <= fd)
221 close(fd);
222 return NULL;
225 void close_midx(struct multi_pack_index *m)
227 uint32_t i;
229 if (!m)
230 return;
232 close_midx(m->next);
234 munmap((unsigned char *)m->data, m->data_len);
236 for (i = 0; i < m->num_packs; i++) {
237 if (m->packs[i])
238 m->packs[i]->multi_pack_index = 0;
240 FREE_AND_NULL(m->packs);
241 FREE_AND_NULL(m->pack_names);
242 free(m);
245 int prepare_midx_pack(struct repository *r, struct multi_pack_index *m, uint32_t pack_int_id)
247 struct strbuf pack_name = STRBUF_INIT;
248 struct packed_git *p;
250 if (pack_int_id >= m->num_packs)
251 die(_("bad pack-int-id: %u (%u total packs)"),
252 pack_int_id, m->num_packs);
254 if (m->packs[pack_int_id])
255 return 0;
257 strbuf_addf(&pack_name, "%s/pack/%s", m->object_dir,
258 m->pack_names[pack_int_id]);
260 p = add_packed_git(pack_name.buf, pack_name.len, m->local);
261 strbuf_release(&pack_name);
263 if (!p)
264 return 1;
266 p->multi_pack_index = 1;
267 m->packs[pack_int_id] = p;
268 install_packed_git(r, p);
269 list_add_tail(&p->mru, &r->objects->packed_git_mru);
271 return 0;
274 #define MIDX_CHUNK_BITMAPPED_PACKS_WIDTH (2 * sizeof(uint32_t))
276 int nth_bitmapped_pack(struct repository *r, struct multi_pack_index *m,
277 struct bitmapped_pack *bp, uint32_t pack_int_id)
279 if (!m->chunk_bitmapped_packs)
280 return error(_("MIDX does not contain the BTMP chunk"));
282 if (prepare_midx_pack(r, m, pack_int_id))
283 return error(_("could not load bitmapped pack %"PRIu32), pack_int_id);
285 bp->p = m->packs[pack_int_id];
286 bp->bitmap_pos = get_be32((char *)m->chunk_bitmapped_packs +
287 MIDX_CHUNK_BITMAPPED_PACKS_WIDTH * pack_int_id);
288 bp->bitmap_nr = get_be32((char *)m->chunk_bitmapped_packs +
289 MIDX_CHUNK_BITMAPPED_PACKS_WIDTH * pack_int_id +
290 sizeof(uint32_t));
291 bp->pack_int_id = pack_int_id;
293 return 0;
296 int bsearch_midx(const struct object_id *oid, struct multi_pack_index *m, uint32_t *result)
298 return bsearch_hash(oid->hash, m->chunk_oid_fanout, m->chunk_oid_lookup,
299 the_hash_algo->rawsz, result);
302 struct object_id *nth_midxed_object_oid(struct object_id *oid,
303 struct multi_pack_index *m,
304 uint32_t n)
306 if (n >= m->num_objects)
307 return NULL;
309 oidread(oid, m->chunk_oid_lookup + st_mult(m->hash_len, n),
310 the_repository->hash_algo);
311 return oid;
314 off_t nth_midxed_offset(struct multi_pack_index *m, uint32_t pos)
316 const unsigned char *offset_data;
317 uint32_t offset32;
319 offset_data = m->chunk_object_offsets + (off_t)pos * MIDX_CHUNK_OFFSET_WIDTH;
320 offset32 = get_be32(offset_data + sizeof(uint32_t));
322 if (m->chunk_large_offsets && offset32 & MIDX_LARGE_OFFSET_NEEDED) {
323 if (sizeof(off_t) < sizeof(uint64_t))
324 die(_("multi-pack-index stores a 64-bit offset, but off_t is too small"));
326 offset32 ^= MIDX_LARGE_OFFSET_NEEDED;
327 if (offset32 >= m->chunk_large_offsets_len / sizeof(uint64_t))
328 die(_("multi-pack-index large offset out of bounds"));
329 return get_be64(m->chunk_large_offsets + sizeof(uint64_t) * offset32);
332 return offset32;
335 uint32_t nth_midxed_pack_int_id(struct multi_pack_index *m, uint32_t pos)
337 return get_be32(m->chunk_object_offsets +
338 (off_t)pos * MIDX_CHUNK_OFFSET_WIDTH);
341 int fill_midx_entry(struct repository *r,
342 const struct object_id *oid,
343 struct pack_entry *e,
344 struct multi_pack_index *m)
346 uint32_t pos;
347 uint32_t pack_int_id;
348 struct packed_git *p;
350 if (!bsearch_midx(oid, m, &pos))
351 return 0;
353 if (pos >= m->num_objects)
354 return 0;
356 pack_int_id = nth_midxed_pack_int_id(m, pos);
358 if (prepare_midx_pack(r, m, pack_int_id))
359 return 0;
360 p = m->packs[pack_int_id];
363 * We are about to tell the caller where they can locate the
364 * requested object. We better make sure the packfile is
365 * still here and can be accessed before supplying that
366 * answer, as it may have been deleted since the MIDX was
367 * loaded!
369 if (!is_pack_valid(p))
370 return 0;
372 if (oidset_size(&p->bad_objects) &&
373 oidset_contains(&p->bad_objects, oid))
374 return 0;
376 e->offset = nth_midxed_offset(m, pos);
377 e->p = p;
379 return 1;
382 /* Match "foo.idx" against either "foo.pack" _or_ "foo.idx". */
383 int cmp_idx_or_pack_name(const char *idx_or_pack_name,
384 const char *idx_name)
386 /* Skip past any initial matching prefix. */
387 while (*idx_name && *idx_name == *idx_or_pack_name) {
388 idx_name++;
389 idx_or_pack_name++;
393 * If we didn't match completely, we may have matched "pack-1234." and
394 * be left with "idx" and "pack" respectively, which is also OK. We do
395 * not have to check for "idx" and "idx", because that would have been
396 * a complete match (and in that case these strcmps will be false, but
397 * we'll correctly return 0 from the final strcmp() below.
399 * Technically this matches "fooidx" and "foopack", but we'd never have
400 * such names in the first place.
402 if (!strcmp(idx_name, "idx") && !strcmp(idx_or_pack_name, "pack"))
403 return 0;
406 * This not only checks for a complete match, but also orders based on
407 * the first non-identical character, which means our ordering will
408 * match a raw strcmp(). That makes it OK to use this to binary search
409 * a naively-sorted list.
411 return strcmp(idx_or_pack_name, idx_name);
414 int midx_locate_pack(struct multi_pack_index *m, const char *idx_or_pack_name,
415 uint32_t *pos)
417 uint32_t first = 0, last = m->num_packs;
419 while (first < last) {
420 uint32_t mid = first + (last - first) / 2;
421 const char *current;
422 int cmp;
424 current = m->pack_names[mid];
425 cmp = cmp_idx_or_pack_name(idx_or_pack_name, current);
426 if (!cmp) {
427 if (pos)
428 *pos = mid;
429 return 1;
431 if (cmp > 0) {
432 first = mid + 1;
433 continue;
435 last = mid;
438 return 0;
441 int midx_contains_pack(struct multi_pack_index *m, const char *idx_or_pack_name)
443 return midx_locate_pack(m, idx_or_pack_name, NULL);
446 int midx_preferred_pack(struct multi_pack_index *m, uint32_t *pack_int_id)
448 if (m->preferred_pack_idx == -1) {
449 if (load_midx_revindex(m) < 0) {
450 m->preferred_pack_idx = -2;
451 return -1;
454 m->preferred_pack_idx =
455 nth_midxed_pack_int_id(m, pack_pos_to_midx(m, 0));
456 } else if (m->preferred_pack_idx == -2)
457 return -1; /* no revindex */
459 *pack_int_id = m->preferred_pack_idx;
460 return 0;
463 int prepare_multi_pack_index_one(struct repository *r, const char *object_dir, int local)
465 struct multi_pack_index *m;
466 struct multi_pack_index *m_search;
468 prepare_repo_settings(r);
469 if (!r->settings.core_multi_pack_index)
470 return 0;
472 for (m_search = r->objects->multi_pack_index; m_search; m_search = m_search->next)
473 if (!strcmp(object_dir, m_search->object_dir))
474 return 1;
476 m = load_multi_pack_index(object_dir, local);
478 if (m) {
479 struct multi_pack_index *mp = r->objects->multi_pack_index;
480 if (mp) {
481 m->next = mp->next;
482 mp->next = m;
483 } else
484 r->objects->multi_pack_index = m;
485 return 1;
488 return 0;
491 int midx_checksum_valid(struct multi_pack_index *m)
493 return hashfile_checksum_valid(m->data, m->data_len);
496 struct clear_midx_data {
497 char *keep;
498 const char *ext;
501 static void clear_midx_file_ext(const char *full_path, size_t full_path_len UNUSED,
502 const char *file_name, void *_data)
504 struct clear_midx_data *data = _data;
506 if (!(starts_with(file_name, "multi-pack-index-") &&
507 ends_with(file_name, data->ext)))
508 return;
509 if (data->keep && !strcmp(data->keep, file_name))
510 return;
512 if (unlink(full_path))
513 die_errno(_("failed to remove %s"), full_path);
516 void clear_midx_files_ext(const char *object_dir, const char *ext,
517 unsigned char *keep_hash)
519 struct clear_midx_data data;
520 memset(&data, 0, sizeof(struct clear_midx_data));
522 if (keep_hash)
523 data.keep = xstrfmt("multi-pack-index-%s%s",
524 hash_to_hex(keep_hash), ext);
525 data.ext = ext;
527 for_each_file_in_pack_dir(object_dir,
528 clear_midx_file_ext,
529 &data);
531 free(data.keep);
534 void clear_midx_file(struct repository *r)
536 struct strbuf midx = STRBUF_INIT;
538 get_midx_filename(&midx, r->objects->odb->path);
540 if (r->objects && r->objects->multi_pack_index) {
541 close_midx(r->objects->multi_pack_index);
542 r->objects->multi_pack_index = NULL;
545 if (remove_path(midx.buf))
546 die(_("failed to clear multi-pack-index at %s"), midx.buf);
548 clear_midx_files_ext(r->objects->odb->path, ".bitmap", NULL);
549 clear_midx_files_ext(r->objects->odb->path, ".rev", NULL);
551 strbuf_release(&midx);
554 static int verify_midx_error;
556 __attribute__((format (printf, 1, 2)))
557 static void midx_report(const char *fmt, ...)
559 va_list ap;
560 verify_midx_error = 1;
561 va_start(ap, fmt);
562 vfprintf(stderr, fmt, ap);
563 fprintf(stderr, "\n");
564 va_end(ap);
567 struct pair_pos_vs_id
569 uint32_t pos;
570 uint32_t pack_int_id;
573 static int compare_pair_pos_vs_id(const void *_a, const void *_b)
575 struct pair_pos_vs_id *a = (struct pair_pos_vs_id *)_a;
576 struct pair_pos_vs_id *b = (struct pair_pos_vs_id *)_b;
578 return b->pack_int_id - a->pack_int_id;
582 * Limit calls to display_progress() for performance reasons.
583 * The interval here was arbitrarily chosen.
585 #define SPARSE_PROGRESS_INTERVAL (1 << 12)
586 #define midx_display_sparse_progress(progress, n) \
587 do { \
588 uint64_t _n = (n); \
589 if ((_n & (SPARSE_PROGRESS_INTERVAL - 1)) == 0) \
590 display_progress(progress, _n); \
591 } while (0)
593 int verify_midx_file(struct repository *r, const char *object_dir, unsigned flags)
595 struct pair_pos_vs_id *pairs = NULL;
596 uint32_t i;
597 struct progress *progress = NULL;
598 struct multi_pack_index *m = load_multi_pack_index(object_dir, 1);
599 verify_midx_error = 0;
601 if (!m) {
602 int result = 0;
603 struct stat sb;
604 struct strbuf filename = STRBUF_INIT;
606 get_midx_filename(&filename, object_dir);
608 if (!stat(filename.buf, &sb)) {
609 error(_("multi-pack-index file exists, but failed to parse"));
610 result = 1;
612 strbuf_release(&filename);
613 return result;
616 if (!midx_checksum_valid(m))
617 midx_report(_("incorrect checksum"));
619 if (flags & MIDX_PROGRESS)
620 progress = start_delayed_progress(_("Looking for referenced packfiles"),
621 m->num_packs);
622 for (i = 0; i < m->num_packs; i++) {
623 if (prepare_midx_pack(r, m, i))
624 midx_report("failed to load pack in position %d", i);
626 display_progress(progress, i + 1);
628 stop_progress(&progress);
630 if (m->num_objects == 0) {
631 midx_report(_("the midx contains no oid"));
633 * Remaining tests assume that we have objects, so we can
634 * return here.
636 goto cleanup;
639 if (flags & MIDX_PROGRESS)
640 progress = start_sparse_progress(_("Verifying OID order in multi-pack-index"),
641 m->num_objects - 1);
642 for (i = 0; i < m->num_objects - 1; i++) {
643 struct object_id oid1, oid2;
645 nth_midxed_object_oid(&oid1, m, i);
646 nth_midxed_object_oid(&oid2, m, i + 1);
648 if (oidcmp(&oid1, &oid2) >= 0)
649 midx_report(_("oid lookup out of order: oid[%d] = %s >= %s = oid[%d]"),
650 i, oid_to_hex(&oid1), oid_to_hex(&oid2), i + 1);
652 midx_display_sparse_progress(progress, i + 1);
654 stop_progress(&progress);
657 * Create an array mapping each object to its packfile id. Sort it
658 * to group the objects by packfile. Use this permutation to visit
659 * each of the objects and only require 1 packfile to be open at a
660 * time.
662 ALLOC_ARRAY(pairs, m->num_objects);
663 for (i = 0; i < m->num_objects; i++) {
664 pairs[i].pos = i;
665 pairs[i].pack_int_id = nth_midxed_pack_int_id(m, i);
668 if (flags & MIDX_PROGRESS)
669 progress = start_sparse_progress(_("Sorting objects by packfile"),
670 m->num_objects);
671 display_progress(progress, 0); /* TODO: Measure QSORT() progress */
672 QSORT(pairs, m->num_objects, compare_pair_pos_vs_id);
673 stop_progress(&progress);
675 if (flags & MIDX_PROGRESS)
676 progress = start_sparse_progress(_("Verifying object offsets"), m->num_objects);
677 for (i = 0; i < m->num_objects; i++) {
678 struct object_id oid;
679 struct pack_entry e;
680 off_t m_offset, p_offset;
682 if (i > 0 && pairs[i-1].pack_int_id != pairs[i].pack_int_id &&
683 m->packs[pairs[i-1].pack_int_id])
685 close_pack_fd(m->packs[pairs[i-1].pack_int_id]);
686 close_pack_index(m->packs[pairs[i-1].pack_int_id]);
689 nth_midxed_object_oid(&oid, m, pairs[i].pos);
691 if (!fill_midx_entry(r, &oid, &e, m)) {
692 midx_report(_("failed to load pack entry for oid[%d] = %s"),
693 pairs[i].pos, oid_to_hex(&oid));
694 continue;
697 if (open_pack_index(e.p)) {
698 midx_report(_("failed to load pack-index for packfile %s"),
699 e.p->pack_name);
700 break;
703 m_offset = e.offset;
704 p_offset = find_pack_entry_one(oid.hash, e.p);
706 if (m_offset != p_offset)
707 midx_report(_("incorrect object offset for oid[%d] = %s: %"PRIx64" != %"PRIx64),
708 pairs[i].pos, oid_to_hex(&oid), m_offset, p_offset);
710 midx_display_sparse_progress(progress, i + 1);
712 stop_progress(&progress);
714 cleanup:
715 free(pairs);
716 close_midx(m);
718 return verify_midx_error;