Merge branch 'rs/remove-unused-find-header-mem'
[git.git] / midx.c
blobbc4797196f02e60f566668867d6896b79ff952e4
1 #include "git-compat-util.h"
2 #include "config.h"
3 #include "dir.h"
4 #include "hex.h"
5 #include "packfile.h"
6 #include "object-file.h"
7 #include "hash-lookup.h"
8 #include "midx.h"
9 #include "progress.h"
10 #include "trace2.h"
11 #include "chunk-format.h"
12 #include "pack-bitmap.h"
13 #include "pack-revindex.h"
15 int midx_checksum_valid(struct multi_pack_index *m);
16 void clear_midx_files_ext(const char *object_dir, const char *ext,
17 unsigned char *keep_hash);
18 int cmp_idx_or_pack_name(const char *idx_or_pack_name,
19 const char *idx_name);
21 const unsigned char *get_midx_checksum(struct multi_pack_index *m)
23 return m->data + m->data_len - the_hash_algo->rawsz;
26 void get_midx_filename(struct strbuf *out, const char *object_dir)
28 get_midx_filename_ext(out, object_dir, NULL, NULL);
31 void get_midx_filename_ext(struct strbuf *out, const char *object_dir,
32 const unsigned char *hash, const char *ext)
34 strbuf_addf(out, "%s/pack/multi-pack-index", object_dir);
35 if (ext)
36 strbuf_addf(out, "-%s.%s", hash_to_hex(hash), ext);
39 static int midx_read_oid_fanout(const unsigned char *chunk_start,
40 size_t chunk_size, void *data)
42 int i;
43 struct multi_pack_index *m = data;
44 m->chunk_oid_fanout = (uint32_t *)chunk_start;
46 if (chunk_size != 4 * 256) {
47 error(_("multi-pack-index OID fanout is of the wrong size"));
48 return 1;
50 for (i = 0; i < 255; i++) {
51 uint32_t oid_fanout1 = ntohl(m->chunk_oid_fanout[i]);
52 uint32_t oid_fanout2 = ntohl(m->chunk_oid_fanout[i+1]);
54 if (oid_fanout1 > oid_fanout2) {
55 error(_("oid fanout out of order: fanout[%d] = %"PRIx32" > %"PRIx32" = fanout[%d]"),
56 i, oid_fanout1, oid_fanout2, i + 1);
57 return 1;
60 m->num_objects = ntohl(m->chunk_oid_fanout[255]);
61 return 0;
64 static int midx_read_oid_lookup(const unsigned char *chunk_start,
65 size_t chunk_size, void *data)
67 struct multi_pack_index *m = data;
68 m->chunk_oid_lookup = chunk_start;
70 if (chunk_size != st_mult(m->hash_len, m->num_objects)) {
71 error(_("multi-pack-index OID lookup chunk is the wrong size"));
72 return 1;
74 return 0;
77 static int midx_read_object_offsets(const unsigned char *chunk_start,
78 size_t chunk_size, void *data)
80 struct multi_pack_index *m = data;
81 m->chunk_object_offsets = chunk_start;
83 if (chunk_size != st_mult(m->num_objects, MIDX_CHUNK_OFFSET_WIDTH)) {
84 error(_("multi-pack-index object offset chunk is the wrong size"));
85 return 1;
87 return 0;
90 #define MIDX_MIN_SIZE (MIDX_HEADER_SIZE + the_hash_algo->rawsz)
92 struct multi_pack_index *load_multi_pack_index(const char *object_dir, int local)
94 struct multi_pack_index *m = NULL;
95 int fd;
96 struct stat st;
97 size_t midx_size;
98 void *midx_map = NULL;
99 uint32_t hash_version;
100 struct strbuf midx_name = STRBUF_INIT;
101 uint32_t i;
102 const char *cur_pack_name;
103 struct chunkfile *cf = NULL;
105 get_midx_filename(&midx_name, object_dir);
107 fd = git_open(midx_name.buf);
109 if (fd < 0)
110 goto cleanup_fail;
111 if (fstat(fd, &st)) {
112 error_errno(_("failed to read %s"), midx_name.buf);
113 goto cleanup_fail;
116 midx_size = xsize_t(st.st_size);
118 if (midx_size < MIDX_MIN_SIZE) {
119 error(_("multi-pack-index file %s is too small"), midx_name.buf);
120 goto cleanup_fail;
123 strbuf_release(&midx_name);
125 midx_map = xmmap(NULL, midx_size, PROT_READ, MAP_PRIVATE, fd, 0);
126 close(fd);
128 FLEX_ALLOC_STR(m, object_dir, object_dir);
129 m->data = midx_map;
130 m->data_len = midx_size;
131 m->local = local;
133 m->signature = get_be32(m->data);
134 if (m->signature != MIDX_SIGNATURE)
135 die(_("multi-pack-index signature 0x%08x does not match signature 0x%08x"),
136 m->signature, MIDX_SIGNATURE);
138 m->version = m->data[MIDX_BYTE_FILE_VERSION];
139 if (m->version != MIDX_VERSION)
140 die(_("multi-pack-index version %d not recognized"),
141 m->version);
143 hash_version = m->data[MIDX_BYTE_HASH_VERSION];
144 if (hash_version != oid_version(the_hash_algo)) {
145 error(_("multi-pack-index hash version %u does not match version %u"),
146 hash_version, oid_version(the_hash_algo));
147 goto cleanup_fail;
149 m->hash_len = the_hash_algo->rawsz;
151 m->num_chunks = m->data[MIDX_BYTE_NUM_CHUNKS];
153 m->num_packs = get_be32(m->data + MIDX_BYTE_NUM_PACKS);
155 m->preferred_pack_idx = -1;
157 cf = init_chunkfile(NULL);
159 if (read_table_of_contents(cf, m->data, midx_size,
160 MIDX_HEADER_SIZE, m->num_chunks,
161 MIDX_CHUNK_ALIGNMENT))
162 goto cleanup_fail;
164 if (pair_chunk(cf, MIDX_CHUNKID_PACKNAMES, &m->chunk_pack_names, &m->chunk_pack_names_len))
165 die(_("multi-pack-index required pack-name chunk missing or corrupted"));
166 if (read_chunk(cf, MIDX_CHUNKID_OIDFANOUT, midx_read_oid_fanout, m))
167 die(_("multi-pack-index required OID fanout chunk missing or corrupted"));
168 if (read_chunk(cf, MIDX_CHUNKID_OIDLOOKUP, midx_read_oid_lookup, m))
169 die(_("multi-pack-index required OID lookup chunk missing or corrupted"));
170 if (read_chunk(cf, MIDX_CHUNKID_OBJECTOFFSETS, midx_read_object_offsets, m))
171 die(_("multi-pack-index required object offsets chunk missing or corrupted"));
173 pair_chunk(cf, MIDX_CHUNKID_LARGEOFFSETS, &m->chunk_large_offsets,
174 &m->chunk_large_offsets_len);
175 if (git_env_bool("GIT_TEST_MIDX_READ_BTMP", 1))
176 pair_chunk(cf, MIDX_CHUNKID_BITMAPPEDPACKS,
177 (const unsigned char **)&m->chunk_bitmapped_packs,
178 &m->chunk_bitmapped_packs_len);
180 if (git_env_bool("GIT_TEST_MIDX_READ_RIDX", 1))
181 pair_chunk(cf, MIDX_CHUNKID_REVINDEX, &m->chunk_revindex,
182 &m->chunk_revindex_len);
184 CALLOC_ARRAY(m->pack_names, m->num_packs);
185 CALLOC_ARRAY(m->packs, m->num_packs);
187 cur_pack_name = (const char *)m->chunk_pack_names;
188 for (i = 0; i < m->num_packs; i++) {
189 const char *end;
190 size_t avail = m->chunk_pack_names_len -
191 (cur_pack_name - (const char *)m->chunk_pack_names);
193 m->pack_names[i] = cur_pack_name;
195 end = memchr(cur_pack_name, '\0', avail);
196 if (!end)
197 die(_("multi-pack-index pack-name chunk is too short"));
198 cur_pack_name = end + 1;
200 if (i && strcmp(m->pack_names[i], m->pack_names[i - 1]) <= 0)
201 die(_("multi-pack-index pack names out of order: '%s' before '%s'"),
202 m->pack_names[i - 1],
203 m->pack_names[i]);
206 trace2_data_intmax("midx", the_repository, "load/num_packs", m->num_packs);
207 trace2_data_intmax("midx", the_repository, "load/num_objects", m->num_objects);
209 free_chunkfile(cf);
210 return m;
212 cleanup_fail:
213 free(m);
214 strbuf_release(&midx_name);
215 free_chunkfile(cf);
216 if (midx_map)
217 munmap(midx_map, midx_size);
218 if (0 <= fd)
219 close(fd);
220 return NULL;
223 void close_midx(struct multi_pack_index *m)
225 uint32_t i;
227 if (!m)
228 return;
230 close_midx(m->next);
232 munmap((unsigned char *)m->data, m->data_len);
234 for (i = 0; i < m->num_packs; i++) {
235 if (m->packs[i])
236 m->packs[i]->multi_pack_index = 0;
238 FREE_AND_NULL(m->packs);
239 FREE_AND_NULL(m->pack_names);
240 free(m);
243 int prepare_midx_pack(struct repository *r, struct multi_pack_index *m, uint32_t pack_int_id)
245 struct strbuf pack_name = STRBUF_INIT;
246 struct packed_git *p;
248 if (pack_int_id >= m->num_packs)
249 die(_("bad pack-int-id: %u (%u total packs)"),
250 pack_int_id, m->num_packs);
252 if (m->packs[pack_int_id])
253 return 0;
255 strbuf_addf(&pack_name, "%s/pack/%s", m->object_dir,
256 m->pack_names[pack_int_id]);
258 p = add_packed_git(pack_name.buf, pack_name.len, m->local);
259 strbuf_release(&pack_name);
261 if (!p)
262 return 1;
264 p->multi_pack_index = 1;
265 m->packs[pack_int_id] = p;
266 install_packed_git(r, p);
267 list_add_tail(&p->mru, &r->objects->packed_git_mru);
269 return 0;
272 #define MIDX_CHUNK_BITMAPPED_PACKS_WIDTH (2 * sizeof(uint32_t))
274 int nth_bitmapped_pack(struct repository *r, struct multi_pack_index *m,
275 struct bitmapped_pack *bp, uint32_t pack_int_id)
277 if (!m->chunk_bitmapped_packs)
278 return error(_("MIDX does not contain the BTMP chunk"));
280 if (prepare_midx_pack(r, m, pack_int_id))
281 return error(_("could not load bitmapped pack %"PRIu32), pack_int_id);
283 bp->p = m->packs[pack_int_id];
284 bp->bitmap_pos = get_be32((char *)m->chunk_bitmapped_packs +
285 MIDX_CHUNK_BITMAPPED_PACKS_WIDTH * pack_int_id);
286 bp->bitmap_nr = get_be32((char *)m->chunk_bitmapped_packs +
287 MIDX_CHUNK_BITMAPPED_PACKS_WIDTH * pack_int_id +
288 sizeof(uint32_t));
289 bp->pack_int_id = pack_int_id;
291 return 0;
294 int bsearch_midx(const struct object_id *oid, struct multi_pack_index *m, uint32_t *result)
296 return bsearch_hash(oid->hash, m->chunk_oid_fanout, m->chunk_oid_lookup,
297 the_hash_algo->rawsz, result);
300 struct object_id *nth_midxed_object_oid(struct object_id *oid,
301 struct multi_pack_index *m,
302 uint32_t n)
304 if (n >= m->num_objects)
305 return NULL;
307 oidread(oid, m->chunk_oid_lookup + st_mult(m->hash_len, n));
308 return oid;
311 off_t nth_midxed_offset(struct multi_pack_index *m, uint32_t pos)
313 const unsigned char *offset_data;
314 uint32_t offset32;
316 offset_data = m->chunk_object_offsets + (off_t)pos * MIDX_CHUNK_OFFSET_WIDTH;
317 offset32 = get_be32(offset_data + sizeof(uint32_t));
319 if (m->chunk_large_offsets && offset32 & MIDX_LARGE_OFFSET_NEEDED) {
320 if (sizeof(off_t) < sizeof(uint64_t))
321 die(_("multi-pack-index stores a 64-bit offset, but off_t is too small"));
323 offset32 ^= MIDX_LARGE_OFFSET_NEEDED;
324 if (offset32 >= m->chunk_large_offsets_len / sizeof(uint64_t))
325 die(_("multi-pack-index large offset out of bounds"));
326 return get_be64(m->chunk_large_offsets + sizeof(uint64_t) * offset32);
329 return offset32;
332 uint32_t nth_midxed_pack_int_id(struct multi_pack_index *m, uint32_t pos)
334 return get_be32(m->chunk_object_offsets +
335 (off_t)pos * MIDX_CHUNK_OFFSET_WIDTH);
338 int fill_midx_entry(struct repository *r,
339 const struct object_id *oid,
340 struct pack_entry *e,
341 struct multi_pack_index *m)
343 uint32_t pos;
344 uint32_t pack_int_id;
345 struct packed_git *p;
347 if (!bsearch_midx(oid, m, &pos))
348 return 0;
350 if (pos >= m->num_objects)
351 return 0;
353 pack_int_id = nth_midxed_pack_int_id(m, pos);
355 if (prepare_midx_pack(r, m, pack_int_id))
356 return 0;
357 p = m->packs[pack_int_id];
360 * We are about to tell the caller where they can locate the
361 * requested object. We better make sure the packfile is
362 * still here and can be accessed before supplying that
363 * answer, as it may have been deleted since the MIDX was
364 * loaded!
366 if (!is_pack_valid(p))
367 return 0;
369 if (oidset_size(&p->bad_objects) &&
370 oidset_contains(&p->bad_objects, oid))
371 return 0;
373 e->offset = nth_midxed_offset(m, pos);
374 e->p = p;
376 return 1;
379 /* Match "foo.idx" against either "foo.pack" _or_ "foo.idx". */
380 int cmp_idx_or_pack_name(const char *idx_or_pack_name,
381 const char *idx_name)
383 /* Skip past any initial matching prefix. */
384 while (*idx_name && *idx_name == *idx_or_pack_name) {
385 idx_name++;
386 idx_or_pack_name++;
390 * If we didn't match completely, we may have matched "pack-1234." and
391 * be left with "idx" and "pack" respectively, which is also OK. We do
392 * not have to check for "idx" and "idx", because that would have been
393 * a complete match (and in that case these strcmps will be false, but
394 * we'll correctly return 0 from the final strcmp() below.
396 * Technically this matches "fooidx" and "foopack", but we'd never have
397 * such names in the first place.
399 if (!strcmp(idx_name, "idx") && !strcmp(idx_or_pack_name, "pack"))
400 return 0;
403 * This not only checks for a complete match, but also orders based on
404 * the first non-identical character, which means our ordering will
405 * match a raw strcmp(). That makes it OK to use this to binary search
406 * a naively-sorted list.
408 return strcmp(idx_or_pack_name, idx_name);
411 int midx_locate_pack(struct multi_pack_index *m, const char *idx_or_pack_name,
412 uint32_t *pos)
414 uint32_t first = 0, last = m->num_packs;
416 while (first < last) {
417 uint32_t mid = first + (last - first) / 2;
418 const char *current;
419 int cmp;
421 current = m->pack_names[mid];
422 cmp = cmp_idx_or_pack_name(idx_or_pack_name, current);
423 if (!cmp) {
424 if (pos)
425 *pos = mid;
426 return 1;
428 if (cmp > 0) {
429 first = mid + 1;
430 continue;
432 last = mid;
435 return 0;
438 int midx_contains_pack(struct multi_pack_index *m, const char *idx_or_pack_name)
440 return midx_locate_pack(m, idx_or_pack_name, NULL);
443 int midx_preferred_pack(struct multi_pack_index *m, uint32_t *pack_int_id)
445 if (m->preferred_pack_idx == -1) {
446 if (load_midx_revindex(m) < 0) {
447 m->preferred_pack_idx = -2;
448 return -1;
451 m->preferred_pack_idx =
452 nth_midxed_pack_int_id(m, pack_pos_to_midx(m, 0));
453 } else if (m->preferred_pack_idx == -2)
454 return -1; /* no revindex */
456 *pack_int_id = m->preferred_pack_idx;
457 return 0;
460 int prepare_multi_pack_index_one(struct repository *r, const char *object_dir, int local)
462 struct multi_pack_index *m;
463 struct multi_pack_index *m_search;
465 prepare_repo_settings(r);
466 if (!r->settings.core_multi_pack_index)
467 return 0;
469 for (m_search = r->objects->multi_pack_index; m_search; m_search = m_search->next)
470 if (!strcmp(object_dir, m_search->object_dir))
471 return 1;
473 m = load_multi_pack_index(object_dir, local);
475 if (m) {
476 struct multi_pack_index *mp = r->objects->multi_pack_index;
477 if (mp) {
478 m->next = mp->next;
479 mp->next = m;
480 } else
481 r->objects->multi_pack_index = m;
482 return 1;
485 return 0;
488 int midx_checksum_valid(struct multi_pack_index *m)
490 return hashfile_checksum_valid(m->data, m->data_len);
493 struct clear_midx_data {
494 char *keep;
495 const char *ext;
498 static void clear_midx_file_ext(const char *full_path, size_t full_path_len UNUSED,
499 const char *file_name, void *_data)
501 struct clear_midx_data *data = _data;
503 if (!(starts_with(file_name, "multi-pack-index-") &&
504 ends_with(file_name, data->ext)))
505 return;
506 if (data->keep && !strcmp(data->keep, file_name))
507 return;
509 if (unlink(full_path))
510 die_errno(_("failed to remove %s"), full_path);
513 void clear_midx_files_ext(const char *object_dir, const char *ext,
514 unsigned char *keep_hash)
516 struct clear_midx_data data;
517 memset(&data, 0, sizeof(struct clear_midx_data));
519 if (keep_hash)
520 data.keep = xstrfmt("multi-pack-index-%s%s",
521 hash_to_hex(keep_hash), ext);
522 data.ext = ext;
524 for_each_file_in_pack_dir(object_dir,
525 clear_midx_file_ext,
526 &data);
528 free(data.keep);
531 void clear_midx_file(struct repository *r)
533 struct strbuf midx = STRBUF_INIT;
535 get_midx_filename(&midx, r->objects->odb->path);
537 if (r->objects && r->objects->multi_pack_index) {
538 close_midx(r->objects->multi_pack_index);
539 r->objects->multi_pack_index = NULL;
542 if (remove_path(midx.buf))
543 die(_("failed to clear multi-pack-index at %s"), midx.buf);
545 clear_midx_files_ext(r->objects->odb->path, ".bitmap", NULL);
546 clear_midx_files_ext(r->objects->odb->path, ".rev", NULL);
548 strbuf_release(&midx);
551 static int verify_midx_error;
553 __attribute__((format (printf, 1, 2)))
554 static void midx_report(const char *fmt, ...)
556 va_list ap;
557 verify_midx_error = 1;
558 va_start(ap, fmt);
559 vfprintf(stderr, fmt, ap);
560 fprintf(stderr, "\n");
561 va_end(ap);
564 struct pair_pos_vs_id
566 uint32_t pos;
567 uint32_t pack_int_id;
570 static int compare_pair_pos_vs_id(const void *_a, const void *_b)
572 struct pair_pos_vs_id *a = (struct pair_pos_vs_id *)_a;
573 struct pair_pos_vs_id *b = (struct pair_pos_vs_id *)_b;
575 return b->pack_int_id - a->pack_int_id;
579 * Limit calls to display_progress() for performance reasons.
580 * The interval here was arbitrarily chosen.
582 #define SPARSE_PROGRESS_INTERVAL (1 << 12)
583 #define midx_display_sparse_progress(progress, n) \
584 do { \
585 uint64_t _n = (n); \
586 if ((_n & (SPARSE_PROGRESS_INTERVAL - 1)) == 0) \
587 display_progress(progress, _n); \
588 } while (0)
590 int verify_midx_file(struct repository *r, const char *object_dir, unsigned flags)
592 struct pair_pos_vs_id *pairs = NULL;
593 uint32_t i;
594 struct progress *progress = NULL;
595 struct multi_pack_index *m = load_multi_pack_index(object_dir, 1);
596 verify_midx_error = 0;
598 if (!m) {
599 int result = 0;
600 struct stat sb;
601 struct strbuf filename = STRBUF_INIT;
603 get_midx_filename(&filename, object_dir);
605 if (!stat(filename.buf, &sb)) {
606 error(_("multi-pack-index file exists, but failed to parse"));
607 result = 1;
609 strbuf_release(&filename);
610 return result;
613 if (!midx_checksum_valid(m))
614 midx_report(_("incorrect checksum"));
616 if (flags & MIDX_PROGRESS)
617 progress = start_delayed_progress(_("Looking for referenced packfiles"),
618 m->num_packs);
619 for (i = 0; i < m->num_packs; i++) {
620 if (prepare_midx_pack(r, m, i))
621 midx_report("failed to load pack in position %d", i);
623 display_progress(progress, i + 1);
625 stop_progress(&progress);
627 if (m->num_objects == 0) {
628 midx_report(_("the midx contains no oid"));
630 * Remaining tests assume that we have objects, so we can
631 * return here.
633 goto cleanup;
636 if (flags & MIDX_PROGRESS)
637 progress = start_sparse_progress(_("Verifying OID order in multi-pack-index"),
638 m->num_objects - 1);
639 for (i = 0; i < m->num_objects - 1; i++) {
640 struct object_id oid1, oid2;
642 nth_midxed_object_oid(&oid1, m, i);
643 nth_midxed_object_oid(&oid2, m, i + 1);
645 if (oidcmp(&oid1, &oid2) >= 0)
646 midx_report(_("oid lookup out of order: oid[%d] = %s >= %s = oid[%d]"),
647 i, oid_to_hex(&oid1), oid_to_hex(&oid2), i + 1);
649 midx_display_sparse_progress(progress, i + 1);
651 stop_progress(&progress);
654 * Create an array mapping each object to its packfile id. Sort it
655 * to group the objects by packfile. Use this permutation to visit
656 * each of the objects and only require 1 packfile to be open at a
657 * time.
659 ALLOC_ARRAY(pairs, m->num_objects);
660 for (i = 0; i < m->num_objects; i++) {
661 pairs[i].pos = i;
662 pairs[i].pack_int_id = nth_midxed_pack_int_id(m, i);
665 if (flags & MIDX_PROGRESS)
666 progress = start_sparse_progress(_("Sorting objects by packfile"),
667 m->num_objects);
668 display_progress(progress, 0); /* TODO: Measure QSORT() progress */
669 QSORT(pairs, m->num_objects, compare_pair_pos_vs_id);
670 stop_progress(&progress);
672 if (flags & MIDX_PROGRESS)
673 progress = start_sparse_progress(_("Verifying object offsets"), m->num_objects);
674 for (i = 0; i < m->num_objects; i++) {
675 struct object_id oid;
676 struct pack_entry e;
677 off_t m_offset, p_offset;
679 if (i > 0 && pairs[i-1].pack_int_id != pairs[i].pack_int_id &&
680 m->packs[pairs[i-1].pack_int_id])
682 close_pack_fd(m->packs[pairs[i-1].pack_int_id]);
683 close_pack_index(m->packs[pairs[i-1].pack_int_id]);
686 nth_midxed_object_oid(&oid, m, pairs[i].pos);
688 if (!fill_midx_entry(r, &oid, &e, m)) {
689 midx_report(_("failed to load pack entry for oid[%d] = %s"),
690 pairs[i].pos, oid_to_hex(&oid));
691 continue;
694 if (open_pack_index(e.p)) {
695 midx_report(_("failed to load pack-index for packfile %s"),
696 e.p->pack_name);
697 break;
700 m_offset = e.offset;
701 p_offset = find_pack_entry_one(oid.hash, e.p);
703 if (m_offset != p_offset)
704 midx_report(_("incorrect object offset for oid[%d] = %s: %"PRIx64" != %"PRIx64),
705 pairs[i].pos, oid_to_hex(&oid), m_offset, p_offset);
707 midx_display_sparse_progress(progress, i + 1);
709 stop_progress(&progress);
711 cleanup:
712 free(pairs);
713 close_midx(m);
715 return verify_midx_error;