at commit a0189536

1 #define DISABLE_SIGN_COMPARE_WARNINGS
2
3 #include "git-compat-util.h"
4 #include "config.h"
5 #include "dir.h"
6 #include "hex.h"
7 #include "packfile.h"
8 #include "hash-lookup.h"
9 #include "midx.h"
10 #include "progress.h"
11 #include "trace2.h"
12 #include "chunk-format.h"
13 #include "pack-bitmap.h"
14 #include "pack-revindex.h"
15 #include "strvec.h"
16
17 #define MIDX_PACK_ERROR ((void *)(intptr_t)-1)
18
19 int midx_checksum_valid(struct multi_pack_index *m);
20 void clear_midx_files_ext(struct odb_source_packed *source, const char *ext,
21 const char *keep_hash);
22 void clear_incremental_midx_files_ext(struct odb_source_packed *source, const char *ext,
23 const struct strvec *keep_hashes);
24 int cmp_idx_or_pack_name(const char *idx_or_pack_name,
25 const char *idx_name);
26
27 const char *midx_get_checksum_hex(const struct multi_pack_index *m)
28 {
29 return hash_to_hex_algop(midx_get_checksum_hash(m),
30 m->source->base.odb->repo->hash_algo);
31 }
32
33 const unsigned char *midx_get_checksum_hash(const struct multi_pack_index *m)
34 {
35 return m->data + m->data_len - m->source->base.odb->repo->hash_algo->rawsz;
36 }
37
38 void get_midx_filename(struct odb_source_packed *source, struct strbuf *out)
39 {
40 get_midx_filename_ext(source, out, NULL, NULL);
41 }
42
43 void get_midx_filename_ext(struct odb_source_packed *source, struct strbuf *out,
44 const unsigned char *hash, const char *ext)
45 {
46 strbuf_addf(out, "%s/pack/multi-pack-index", source->base.path);
47 if (ext)
48 strbuf_addf(out, "-%s.%s", hash_to_hex_algop(hash, source->base.odb->repo->hash_algo), ext);
49 }
50
51 static int midx_read_oid_fanout(const unsigned char *chunk_start,
52 size_t chunk_size, void *data)
53 {
54 int i;
55 struct multi_pack_index *m = data;
56 m->chunk_oid_fanout = (uint32_t *)chunk_start;
57
58 if (chunk_size != 4 * 256) {
59 error(_("multi-pack-index OID fanout is of the wrong size"));
60 return 1;
61 }
62 for (i = 0; i < 255; i++) {
63 uint32_t oid_fanout1 = ntohl(m->chunk_oid_fanout[i]);
64 uint32_t oid_fanout2 = ntohl(m->chunk_oid_fanout[i+1]);
65
66 if (oid_fanout1 > oid_fanout2) {
67 error(_("oid fanout out of order: fanout[%d] = %"PRIx32" > %"PRIx32" = fanout[%d]"),
68 i, oid_fanout1, oid_fanout2, i + 1);
69 return 1;
70 }
71 }
72 m->num_objects = ntohl(m->chunk_oid_fanout[255]);
73 return 0;
74 }
75
76 static int midx_read_oid_lookup(const unsigned char *chunk_start,
77 size_t chunk_size, void *data)
78 {
79 struct multi_pack_index *m = data;
80 m->chunk_oid_lookup = chunk_start;
81
82 if (chunk_size != st_mult(m->hash_len, m->num_objects)) {
83 error(_("multi-pack-index OID lookup chunk is the wrong size"));
84 return 1;
85 }
86 return 0;
87 }
88
89 static int midx_read_object_offsets(const unsigned char *chunk_start,
90 size_t chunk_size, void *data)
91 {
92 struct multi_pack_index *m = data;
93 m->chunk_object_offsets = chunk_start;
94
95 if (chunk_size != st_mult(m->num_objects, MIDX_CHUNK_OFFSET_WIDTH)) {
96 error(_("multi-pack-index object offset chunk is the wrong size"));
97 return 1;
98 }
99 return 0;
100 }
101
102 struct multi_pack_index *get_multi_pack_index(struct odb_source_packed *source)
103 {
104 odb_source_prepare(&source->base, 0);
105 return source->midx;
106 }
107
108 static struct multi_pack_index *load_multi_pack_index_one(struct odb_source_packed *source,
109 const char *midx_name)
110 {
111 struct repository *r = source->base.odb->repo;
112 struct multi_pack_index *m = NULL;
113 int fd;
114 struct stat st;
115 size_t midx_size;
116 void *midx_map = NULL;
117 uint32_t hash_version;
118 uint32_t i;
119 const char *cur_pack_name;
120 struct chunkfile *cf = NULL;
121
122 fd = git_open(midx_name);
123
124 if (fd < 0)
125 goto cleanup_fail;
126 if (fstat(fd, &st)) {
127 error_errno(_("failed to read %s"), midx_name);
128 goto cleanup_fail;
129 }
130
131 midx_size = xsize_t(st.st_size);
132
133 if (midx_size < (MIDX_HEADER_SIZE + r->hash_algo->rawsz)) {
134 error(_("multi-pack-index file %s is too small"), midx_name);
135 goto cleanup_fail;
136 }
137
138 midx_map = xmmap(NULL, midx_size, PROT_READ, MAP_PRIVATE, fd, 0);
139 close(fd);
140
141 CALLOC_ARRAY(m, 1);
142 m->data = midx_map;
143 m->data_len = midx_size;
144 m->source = source;
145
146 m->signature = get_be32(m->data);
147 if (m->signature != MIDX_SIGNATURE)
148 die(_("multi-pack-index signature 0x%08x does not match signature 0x%08x"),
149 m->signature, MIDX_SIGNATURE);
150
151 m->version = m->data[MIDX_BYTE_FILE_VERSION];
152 if (m->version != MIDX_VERSION_V1 && m->version != MIDX_VERSION_V2)
153 die(_("multi-pack-index version %d not recognized"),
154 m->version);
155
156 hash_version = m->data[MIDX_BYTE_HASH_VERSION];
157 if (hash_version != oid_version(r->hash_algo)) {
158 error(_("multi-pack-index hash version %u does not match version %u"),
159 hash_version, oid_version(r->hash_algo));
160 goto cleanup_fail;
161 }
162 m->hash_len = r->hash_algo->rawsz;
163
164 m->num_chunks = m->data[MIDX_BYTE_NUM_CHUNKS];
165
166 m->num_packs = get_be32(m->data + MIDX_BYTE_NUM_PACKS);
167
168 m->preferred_pack_idx = -1;
169
170 cf = init_chunkfile(NULL);
171
172 if (read_table_of_contents(cf, m->data, midx_size,
173 MIDX_HEADER_SIZE, m->num_chunks,
174 MIDX_CHUNK_ALIGNMENT))
175 goto cleanup_fail;
176
177 if (pair_chunk(cf, MIDX_CHUNKID_PACKNAMES, &m->chunk_pack_names, &m->chunk_pack_names_len))
178 die(_("multi-pack-index required pack-name chunk missing or corrupted"));
179 if (read_chunk(cf, MIDX_CHUNKID_OIDFANOUT, midx_read_oid_fanout, m))
180 die(_("multi-pack-index required OID fanout chunk missing or corrupted"));
181 if (read_chunk(cf, MIDX_CHUNKID_OIDLOOKUP, midx_read_oid_lookup, m))
182 die(_("multi-pack-index required OID lookup chunk missing or corrupted"));
183 if (read_chunk(cf, MIDX_CHUNKID_OBJECTOFFSETS, midx_read_object_offsets, m))
184 die(_("multi-pack-index required object offsets chunk missing or corrupted"));
185
186 pair_chunk(cf, MIDX_CHUNKID_LARGEOFFSETS, &m->chunk_large_offsets,
187 &m->chunk_large_offsets_len);
188 if (git_env_bool("GIT_TEST_MIDX_READ_BTMP", 1))
189 pair_chunk(cf, MIDX_CHUNKID_BITMAPPEDPACKS,
190 (const unsigned char **)&m->chunk_bitmapped_packs,
191 &m->chunk_bitmapped_packs_len);
192
193 if (git_env_bool("GIT_TEST_MIDX_READ_RIDX", 1))
194 pair_chunk(cf, MIDX_CHUNKID_REVINDEX, &m->chunk_revindex,
195 &m->chunk_revindex_len);
196
197 CALLOC_ARRAY(m->pack_names, m->num_packs);
198 CALLOC_ARRAY(m->packs, m->num_packs);
199
200 cur_pack_name = (const char *)m->chunk_pack_names;
201 for (i = 0; i < m->num_packs; i++) {
202 const char *end;
203 size_t avail = m->chunk_pack_names_len -
204 (cur_pack_name - (const char *)m->chunk_pack_names);
205
206 m->pack_names[i] = cur_pack_name;
207
208 end = memchr(cur_pack_name, '\0', avail);
209 if (!end)
210 die(_("multi-pack-index pack-name chunk is too short"));
211 cur_pack_name = end + 1;
212
213 if (m->version == MIDX_VERSION_V1 &&
214 i && strcmp(m->pack_names[i], m->pack_names[i - 1]) <= 0)
215 die(_("multi-pack-index pack names out of order: '%s' before '%s'"),
216 m->pack_names[i - 1],
217 m->pack_names[i]);
218 }
219
220 trace2_data_intmax("midx", r, "load/num_packs", m->num_packs);
221 trace2_data_intmax("midx", r, "load/num_objects", m->num_objects);
222
223 free_chunkfile(cf);
224 return m;
225
226 cleanup_fail:
227 free(m);
228 free_chunkfile(cf);
229 if (midx_map)
230 munmap(midx_map, midx_size);
231 if (0 <= fd)
232 close(fd);
233 return NULL;
234 }
235
236 void get_midx_chain_dirname(struct odb_source_packed *source, struct strbuf *buf)
237 {
238 strbuf_addf(buf, "%s/pack/multi-pack-index.d", source->base.path);
239 }
240
241 void get_midx_chain_filename(struct odb_source_packed *source, struct strbuf *buf)
242 {
243 get_midx_chain_dirname(source, buf);
244 strbuf_addstr(buf, "/multi-pack-index-chain");
245 }
246
247 void get_split_midx_filename_ext(struct odb_source_packed *source, struct strbuf *buf,
248 const unsigned char *hash, const char *ext)
249 {
250 get_midx_chain_dirname(source, buf);
251 strbuf_addf(buf, "/multi-pack-index-%s.%s",
252 hash_to_hex_algop(hash, source->base.odb->repo->hash_algo), ext);
253 }
254
255 static int open_multi_pack_index_chain(const struct git_hash_algo *hash_algo,
256 const char *chain_file, int *fd,
257 struct stat *st)
258 {
259 *fd = git_open(chain_file);
260 if (*fd < 0)
261 return 0;
262 if (fstat(*fd, st)) {
263 close(*fd);
264 return 0;
265 }
266 if (st->st_size < hash_algo->hexsz) {
267 close(*fd);
268 if (!st->st_size) {
269 /* treat empty files the same as missing */
270 errno = ENOENT;
271 } else {
272 warning(_("multi-pack-index chain file too small"));
273 errno = EINVAL;
274 }
275 return 0;
276 }
277 return 1;
278 }
279
280 static int add_midx_to_chain(struct multi_pack_index *midx,
281 struct multi_pack_index *midx_chain)
282 {
283 if (midx_chain) {
284 if (unsigned_add_overflows(midx_chain->num_packs,
285 midx_chain->num_packs_in_base)) {
286 warning(_("pack count in base MIDX too high: %"PRIuMAX),
287 (uintmax_t)midx_chain->num_packs_in_base);
288 return 0;
289 }
290 if (unsigned_add_overflows(midx_chain->num_objects,
291 midx_chain->num_objects_in_base)) {
292 warning(_("object count in base MIDX too high: %"PRIuMAX),
293 (uintmax_t)midx_chain->num_objects_in_base);
294 return 0;
295 }
296 midx->num_packs_in_base = midx_chain->num_packs +
297 midx_chain->num_packs_in_base;
298 midx->num_objects_in_base = midx_chain->num_objects +
299 midx_chain->num_objects_in_base;
300 }
301
302 midx->base_midx = midx_chain;
303 midx->has_chain = 1;
304
305 return 1;
306 }
307
308 static struct multi_pack_index *load_midx_chain_fd_st(struct odb_source_packed *source,
309 int fd, struct stat *st,
310 int *incomplete_chain)
311 {
312 const struct git_hash_algo *hash_algo = source->base.odb->repo->hash_algo;
313 struct multi_pack_index *midx_chain = NULL;
314 struct strbuf buf = STRBUF_INIT;
315 int valid = 1;
316 uint32_t i, count;
317 FILE *fp = xfdopen(fd, "r");
318
319 count = st->st_size / (hash_algo->hexsz + 1);
320
321 for (i = 0; i < count; i++) {
322 struct multi_pack_index *m;
323 struct object_id layer;
324
325 if (strbuf_getline_lf(&buf, fp) == EOF)
326 break;
327
328 if (get_oid_hex_algop(buf.buf, &layer, hash_algo)) {
329 warning(_("invalid multi-pack-index chain: line '%s' "
330 "not a hash"),
331 buf.buf);
332 valid = 0;
333 break;
334 }
335
336 valid = 0;
337
338 strbuf_reset(&buf);
339 get_split_midx_filename_ext(source, &buf,
340 layer.hash, MIDX_EXT_MIDX);
341 m = load_multi_pack_index_one(source, buf.buf);
342
343 if (m) {
344 if (add_midx_to_chain(m, midx_chain)) {
345 midx_chain = m;
346 valid = 1;
347 } else {
348 close_midx(m);
349 }
350 }
351 if (!valid) {
352 warning(_("unable to find all multi-pack index files"));
353 break;
354 }
355 }
356
357 fclose(fp);
358 strbuf_release(&buf);
359
360 *incomplete_chain = !valid;
361 return midx_chain;
362 }
363
364 static struct multi_pack_index *load_multi_pack_index_chain(struct odb_source_packed *source)
365 {
366 struct strbuf chain_file = STRBUF_INIT;
367 struct stat st;
368 int fd;
369 struct multi_pack_index *m = NULL;
370
371 get_midx_chain_filename(source, &chain_file);
372 if (open_multi_pack_index_chain(source->base.odb->repo->hash_algo,
373 chain_file.buf, &fd, &st)) {
374 int incomplete;
375 /* ownership of fd is taken over by load function */
376 m = load_midx_chain_fd_st(source, fd, &st, &incomplete);
377 }
378
379 strbuf_release(&chain_file);
380 return m;
381 }
382
383 struct multi_pack_index *load_multi_pack_index(struct odb_source_packed *source)
384 {
385 struct strbuf midx_name = STRBUF_INIT;
386 struct multi_pack_index *m;
387
388 get_midx_filename(source, &midx_name);
389
390 m = load_multi_pack_index_one(source, midx_name.buf);
391 if (!m)
392 m = load_multi_pack_index_chain(source);
393
394 strbuf_release(&midx_name);
395
396 return m;
397 }
398
399 void close_midx(struct multi_pack_index *m)
400 {
401 uint32_t i;
402
403 if (!m)
404 return;
405
406 close_midx(m->base_midx);
407
408 munmap((unsigned char *)m->data, m->data_len);
409
410 for (i = 0; i < m->num_packs; i++) {
411 if (m->packs[i] && m->packs[i] != MIDX_PACK_ERROR)
412 m->packs[i]->multi_pack_index = 0;
413 }
414 FREE_AND_NULL(m->packs);
415 FREE_AND_NULL(m->pack_names);
416 FREE_AND_NULL(m->pack_names_sorted);
417 free(m);
418 }
419
420 static uint32_t midx_for_object(struct multi_pack_index **_m, uint32_t pos)
421 {
422 struct multi_pack_index *m = *_m;
423 while (m && pos < m->num_objects_in_base)
424 m = m->base_midx;
425
426 if (!m)
427 BUG("NULL multi-pack-index for object position: %"PRIu32, pos);
428
429 if (pos >= m->num_objects + m->num_objects_in_base)
430 die(_("invalid MIDX object position, MIDX is likely corrupt"));
431
432 *_m = m;
433
434 return pos - m->num_objects_in_base;
435 }
436
437 static uint32_t midx_for_pack(struct multi_pack_index **_m,
438 uint32_t pack_int_id)
439 {
440 struct multi_pack_index *m = *_m;
441 while (m && pack_int_id < m->num_packs_in_base)
442 m = m->base_midx;
443
444 if (!m)
445 BUG("NULL multi-pack-index for pack ID: %"PRIu32, pack_int_id);
446
447 if (pack_int_id >= m->num_packs + m->num_packs_in_base)
448 die(_("bad pack-int-id: %u (%u total packs)"),
449 pack_int_id, m->num_packs + m->num_packs_in_base);
450
451 *_m = m;
452
453 return pack_int_id - m->num_packs_in_base;
454 }
455
456 int prepare_midx_pack(struct multi_pack_index *m,
457 uint32_t pack_int_id)
458 {
459 struct odb_source_packed *packed = m->source;
460 struct strbuf pack_name = STRBUF_INIT;
461 struct packed_git *p;
462
463 pack_int_id = midx_for_pack(&m, pack_int_id);
464
465 if (m->packs[pack_int_id] == MIDX_PACK_ERROR)
466 return 1;
467 if (m->packs[pack_int_id])
468 return 0;
469
470 strbuf_addf(&pack_name, "%s/pack/%s", packed->base.path,
471 m->pack_names[pack_int_id]);
472 p = packfile_store_load_pack(packed,
473 pack_name.buf, packed->base.local);
474 strbuf_release(&pack_name);
475
476 if (!p) {
477 m->packs[pack_int_id] = MIDX_PACK_ERROR;
478 return 1;
479 }
480
481 p->multi_pack_index = 1;
482 m->packs[pack_int_id] = p;
483
484 return 0;
485 }
486
487 struct packed_git *nth_midxed_pack(struct multi_pack_index *m,
488 uint32_t pack_int_id)
489 {
490 uint32_t local_pack_int_id = midx_for_pack(&m, pack_int_id);
491 if (m->packs[local_pack_int_id] == MIDX_PACK_ERROR)
492 return NULL;
493 return m->packs[local_pack_int_id];
494 }
495
496 #define MIDX_CHUNK_BITMAPPED_PACKS_WIDTH (2 * sizeof(uint32_t))
497
498 int nth_bitmapped_pack(struct multi_pack_index *m,
499 struct bitmapped_pack *bp, uint32_t pack_int_id)
500 {
501 uint32_t local_pack_int_id = midx_for_pack(&m, pack_int_id);
502
503 if (!m->chunk_bitmapped_packs)
504 return error(_("MIDX does not contain the BTMP chunk"));
505
506 if (prepare_midx_pack(m, pack_int_id))
507 return error(_("could not load bitmapped pack %"PRIu32), pack_int_id);
508
509 bp->p = m->packs[local_pack_int_id];
510 bp->bitmap_pos = get_be32((char *)m->chunk_bitmapped_packs +
511 MIDX_CHUNK_BITMAPPED_PACKS_WIDTH * local_pack_int_id);
512 bp->bitmap_nr = get_be32((char *)m->chunk_bitmapped_packs +
513 MIDX_CHUNK_BITMAPPED_PACKS_WIDTH * local_pack_int_id +
514 sizeof(uint32_t));
515 bp->pack_int_id = pack_int_id;
516 bp->from_midx = m;
517
518 return 0;
519 }
520
521 int bsearch_one_midx(const struct object_id *oid, struct multi_pack_index *m,
522 uint32_t *result)
523 {
524 int ret = bsearch_hash(oid->hash, m->chunk_oid_fanout,
525 m->chunk_oid_lookup,
526 m->source->base.odb->repo->hash_algo->rawsz,
527 result);
528 if (result)
529 *result += m->num_objects_in_base;
530 return ret;
531 }
532
533 int bsearch_midx(const struct object_id *oid, struct multi_pack_index *m,
534 uint32_t *result)
535 {
536 for (; m; m = m->base_midx)
537 if (bsearch_one_midx(oid, m, result))
538 return 1;
539 return 0;
540 }
541
542 int midx_has_oid(struct multi_pack_index *m, const struct object_id *oid)
543 {
544 return bsearch_midx(oid, m, NULL);
545 }
546
547 struct object_id *nth_midxed_object_oid(struct object_id *oid,
548 struct multi_pack_index *m,
549 uint32_t n)
550 {
551 if (n >= m->num_objects + m->num_objects_in_base)
552 return NULL;
553
554 n = midx_for_object(&m, n);
555
556 oidread(oid, m->chunk_oid_lookup + st_mult(m->hash_len, n),
557 m->source->base.odb->repo->hash_algo);
558 return oid;
559 }
560
561 off_t nth_midxed_offset(struct multi_pack_index *m, uint32_t pos)
562 {
563 const unsigned char *offset_data;
564 uint32_t offset32;
565
566 pos = midx_for_object(&m, pos);
567
568 offset_data = m->chunk_object_offsets + (off_t)pos * MIDX_CHUNK_OFFSET_WIDTH;
569 offset32 = get_be32(offset_data + sizeof(uint32_t));
570
571 if (m->chunk_large_offsets && offset32 & MIDX_LARGE_OFFSET_NEEDED) {
572 if (sizeof(off_t) < sizeof(uint64_t))
573 die(_("multi-pack-index stores a 64-bit offset, but off_t is too small"));
574
575 offset32 ^= MIDX_LARGE_OFFSET_NEEDED;
576 if (offset32 >= m->chunk_large_offsets_len / sizeof(uint64_t))
577 die(_("multi-pack-index large offset out of bounds"));
578 return get_be64(m->chunk_large_offsets + sizeof(uint64_t) * offset32);
579 }
580
581 return offset32;
582 }
583
584 uint32_t nth_midxed_pack_int_id(struct multi_pack_index *m, uint32_t pos)
585 {
586 pos = midx_for_object(&m, pos);
587
588 return m->num_packs_in_base + get_be32(m->chunk_object_offsets +
589 (off_t)pos * MIDX_CHUNK_OFFSET_WIDTH);
590 }
591
592 enum midx_fill_result midx_fill_entry(struct multi_pack_index *m,
593 const struct object_id *oid,
594 struct pack_entry *e,
595 struct packed_git **bad_pack)
596 {
597 uint32_t pos;
598 uint32_t pack_int_id;
599 struct packed_git *p;
600
601 if (!bsearch_midx(oid, m, &pos))
602 return MIDX_FILL_MISS;
603
604 midx_for_object(&m, pos);
605 pack_int_id = nth_midxed_pack_int_id(m, pos);
606
607 if (prepare_midx_pack(m, pack_int_id))
608 return MIDX_FILL_OWNER_UNAVAILABLE;
609 p = m->packs[pack_int_id - m->num_packs_in_base];
610
611 /*
612 * We are about to tell the caller where they can locate the
613 * requested object. We better make sure the packfile is
614 * still here and can be accessed before supplying that
615 * answer, as it may have been deleted since the MIDX was
616 * loaded!
617 */
618 if (!is_pack_valid(p))
619 return MIDX_FILL_OWNER_UNAVAILABLE;
620
621 if (oidset_size(&p->bad_objects) &&
622 oidset_contains(&p->bad_objects, oid)) {
623 if (bad_pack && !*bad_pack)
624 *bad_pack = p;
625 return MIDX_FILL_MISS;
626 }
627
628 e->offset = nth_midxed_offset(m, pos);
629 e->p = p;
630
631 return MIDX_FILL_HIT;
632 }
633
634 /* Match "foo.idx" against either "foo.pack" _or_ "foo.idx". */
635 int cmp_idx_or_pack_name(const char *idx_or_pack_name,
636 const char *idx_name)
637 {
638 /* Skip past any initial matching prefix. */
639 while (*idx_name && *idx_name == *idx_or_pack_name) {
640 idx_name++;
641 idx_or_pack_name++;
642 }
643
644 /*
645 * If we didn't match completely, we may have matched "pack-1234." and
646 * be left with "idx" and "pack" respectively, which is also OK. We do
647 * not have to check for "idx" and "idx", because that would have been
648 * a complete match (and in that case these strcmps will be false, but
649 * we'll correctly return 0 from the final strcmp() below.
650 *
651 * Technically this matches "fooidx" and "foopack", but we'd never have
652 * such names in the first place.
653 */
654 if (!strcmp(idx_name, "idx") && !strcmp(idx_or_pack_name, "pack"))
655 return 0;
656
657 /*
658 * This not only checks for a complete match, but also orders based on
659 * the first non-identical character, which means our ordering will
660 * match a raw strcmp(). That makes it OK to use this to binary search
661 * a naively-sorted list.
662 */
663 return strcmp(idx_or_pack_name, idx_name);
664 }
665
666
667 static int midx_pack_names_cmp(const void *a, const void *b, void *m_)
668 {
669 struct multi_pack_index *m = m_;
670 return strcmp(m->pack_names[*(const size_t *)a],
671 m->pack_names[*(const size_t *)b]);
672 }
673
674 int midx_layer_contains_pack(struct multi_pack_index *m,
675 const char *idx_or_pack_name)
676 {
677 uint32_t first = 0, last = m->num_packs;
678
679 if (m->version == MIDX_VERSION_V2 && !m->pack_names_sorted) {
680 uint32_t i;
681
682 ALLOC_ARRAY(m->pack_names_sorted, m->num_packs);
683
684 for (i = 0; i < m->num_packs; i++)
685 m->pack_names_sorted[i] = i;
686
687 QSORT_S(m->pack_names_sorted, m->num_packs, midx_pack_names_cmp,
688 m);
689 }
690
691 while (first < last) {
692 uint32_t mid = first + (last - first) / 2;
693 const char *current;
694 int cmp;
695
696 if (m->pack_names_sorted)
697 current = m->pack_names[m->pack_names_sorted[mid]];
698 else
699 current = m->pack_names[mid];
700 cmp = cmp_idx_or_pack_name(idx_or_pack_name, current);
701 if (!cmp)
702 return 1;
703 if (cmp > 0) {
704 first = mid + 1;
705 continue;
706 }
707 last = mid;
708 }
709
710 return 0;
711 }
712
713 int midx_contains_pack(struct multi_pack_index *m, const char *idx_or_pack_name)
714 {
715 for (; m; m = m->base_midx)
716 if (midx_layer_contains_pack(m, idx_or_pack_name))
717 return 1;
718 return 0;
719 }
720
721 int midx_preferred_pack(struct multi_pack_index *m, uint32_t *pack_int_id)
722 {
723 if (m->preferred_pack_idx == -1) {
724 uint32_t midx_pos;
725 if (load_midx_revindex(m)) {
726 m->preferred_pack_idx = -2;
727 return -1;
728 }
729
730 midx_pos = pack_pos_to_midx(m, m->num_objects_in_base);
731
732 m->preferred_pack_idx = nth_midxed_pack_int_id(m, midx_pos);
733
734 } else if (m->preferred_pack_idx == -2)
735 return -1; /* no revindex */
736
737 *pack_int_id = m->preferred_pack_idx;
738 return 0;
739 }
740
741 int prepare_multi_pack_index_one(struct odb_source_packed *source)
742 {
743 struct repository *r = source->base.odb->repo;
744
745 prepare_repo_settings(r);
746 if (!r->settings.core_multi_pack_index)
747 return 0;
748
749 if (source->midx)
750 return 1;
751
752 source->midx = load_multi_pack_index(source);
753
754 return !!source->midx;
755 }
756
757 int midx_checksum_valid(struct multi_pack_index *m)
758 {
759 return hashfile_checksum_valid(m->source->base.odb->repo->hash_algo,
760 m->data, m->data_len);
761 }
762
763 struct clear_midx_data {
764 struct strset keep;
765 const char *ext;
766 };
767
768 static void clear_midx_file_ext(const char *full_path, size_t full_path_len UNUSED,
769 const char *file_name, void *_data)
770 {
771 struct clear_midx_data *data = _data;
772
773 if (!(starts_with(file_name, "multi-pack-index-") &&
774 ends_with(file_name, data->ext)))
775 return;
776 if (strset_contains(&data->keep, file_name))
777 return;
778 if (unlink(full_path))
779 die_errno(_("failed to remove %s"), full_path);
780 }
781
782 void clear_midx_files_ext(struct odb_source_packed *source, const char *ext,
783 const char *keep_hash)
784 {
785 struct clear_midx_data data = {
786 .keep = STRSET_INIT,
787 .ext = ext,
788 };
789
790 if (keep_hash) {
791 struct strbuf buf = STRBUF_INIT;
792 strbuf_addf(&buf, "multi-pack-index-%s.%s", keep_hash, ext);
793
794 strset_add(&data.keep, buf.buf);
795
796 strbuf_release(&buf);
797 }
798
799 for_each_file_in_pack_dir(source->base.path, clear_midx_file_ext, &data);
800
801 strset_clear(&data.keep);
802 }
803
804 void clear_incremental_midx_files_ext(struct odb_source_packed *source, const char *ext,
805 const struct strvec *keep_hashes)
806 {
807 struct clear_midx_data data = {
808 .keep = STRSET_INIT,
809 .ext = ext,
810 };
811 struct strbuf buf = STRBUF_INIT;
812
813 if (keep_hashes) {
814 for (size_t i = 0; i < keep_hashes->nr; i++) {
815 strbuf_reset(&buf);
816 strbuf_addf(&buf, "multi-pack-index-%s.%s",
817 keep_hashes->v[i], ext);
818
819 strset_add(&data.keep, buf.buf);
820 }
821 }
822
823 for_each_file_in_pack_subdir(source->base.path, "multi-pack-index.d",
824 clear_midx_file_ext, &data);
825
826 strbuf_release(&buf);
827 strset_clear(&data.keep);
828 }
829
830 void clear_midx_file(struct repository *r)
831 {
832 struct odb_source_files *files;
833 struct strbuf midx = STRBUF_INIT;
834
835 if (r->objects) {
836 struct odb_source *source;
837
838 for (source = r->objects->sources; source; source = source->next) {
839 files = odb_source_files_downcast(source);
840 if (files->packed->midx)
841 close_midx(files->packed->midx);
842 files->packed->midx = NULL;
843 }
844 }
845
846 files = odb_source_files_downcast(r->objects->sources);
847 get_midx_filename(files->packed, &midx);
848
849 if (remove_path(midx.buf))
850 die(_("failed to clear multi-pack-index at %s"), midx.buf);
851
852 clear_midx_files_ext(files->packed, MIDX_EXT_BITMAP, NULL);
853 clear_midx_files_ext(files->packed, MIDX_EXT_REV, NULL);
854
855 strbuf_release(&midx);
856 }
857
858 void clear_incremental_midx_files(struct repository *r,
859 const struct strvec *keep_hashes)
860 {
861 struct odb_source_files *files;
862 struct odb_source *source;
863 struct strbuf chain = STRBUF_INIT;
864
865 for (source = r->objects->sources; source; source = source->next) {
866 files = odb_source_files_downcast(source);
867 if (files->packed->midx)
868 close_midx(files->packed->midx);
869 files->packed->midx = NULL;
870 }
871
872 files = odb_source_files_downcast(r->objects->sources);
873 get_midx_chain_filename(files->packed, &chain);
874
875 if (!keep_hashes && remove_path(chain.buf))
876 die(_("failed to clear multi-pack-index chain at %s"),
877 chain.buf);
878
879 clear_incremental_midx_files_ext(files->packed, MIDX_EXT_BITMAP, keep_hashes);
880 clear_incremental_midx_files_ext(files->packed, MIDX_EXT_REV, keep_hashes);
881 clear_incremental_midx_files_ext(files->packed, MIDX_EXT_MIDX, keep_hashes);
882
883 strbuf_release(&chain);
884 }
885
886 static int verify_midx_error;
887
888 __attribute__((format (printf, 1, 2)))
889 static void midx_report(const char *fmt, ...)
890 {
891 va_list ap;
892 verify_midx_error = 1;
893 va_start(ap, fmt);
894 vfprintf(stderr, fmt, ap);
895 fprintf(stderr, "\n");
896 va_end(ap);
897 }
898
899 struct pair_pos_vs_id
900 {
901 uint32_t pos;
902 uint32_t pack_int_id;
903 };
904
905 static int compare_pair_pos_vs_id(const void *_a, const void *_b)
906 {
907 struct pair_pos_vs_id *a = (struct pair_pos_vs_id *)_a;
908 struct pair_pos_vs_id *b = (struct pair_pos_vs_id *)_b;
909
910 return b->pack_int_id - a->pack_int_id;
911 }
912
913 /*
914 * Limit calls to display_progress() for performance reasons.
915 * The interval here was arbitrarily chosen.
916 */
917 #define SPARSE_PROGRESS_INTERVAL (1 << 12)
918 #define midx_display_sparse_progress(progress, n) \
919 do { \
920 uint64_t _n = (n); \
921 if ((_n & (SPARSE_PROGRESS_INTERVAL - 1)) == 0) \
922 display_progress(progress, _n); \
923 } while (0)
924
925 int verify_midx_file(struct odb_source_packed *source, unsigned flags)
926 {
927 struct repository *r = source->base.odb->repo;
928 struct pair_pos_vs_id *pairs = NULL;
929 uint32_t i;
930 struct progress *progress = NULL;
931 struct multi_pack_index *m = load_multi_pack_index(source);
932 struct multi_pack_index *curr;
933 verify_midx_error = 0;
934
935 if (!m) {
936 int result = 0;
937 struct stat sb;
938 struct strbuf filename = STRBUF_INIT;
939
940 get_midx_filename(source, &filename);
941
942 if (!stat(filename.buf, &sb)) {
943 error(_("multi-pack-index file exists, but failed to parse"));
944 result = 1;
945 }
946 strbuf_release(&filename);
947 return result;
948 }
949
950 if (!midx_checksum_valid(m))
951 midx_report(_("incorrect checksum"));
952
953 if (flags & MIDX_PROGRESS)
954 progress = start_delayed_progress(r,
955 _("Looking for referenced packfiles"),
956 m->num_packs + m->num_packs_in_base);
957 for (i = 0; i < m->num_packs + m->num_packs_in_base; i++) {
958 if (prepare_midx_pack(m, i))
959 midx_report("failed to load pack in position %d", i);
960
961 display_progress(progress, i + 1);
962 }
963 stop_progress(&progress);
964
965 if (m->num_objects == 0) {
966 midx_report(_("the midx contains no oid"));
967 /*
968 * Remaining tests assume that we have objects, so we can
969 * return here.
970 */
971 goto cleanup;
972 }
973
974 if (flags & MIDX_PROGRESS)
975 progress = start_sparse_progress(r,
976 _("Verifying OID order in multi-pack-index"),
977 m->num_objects - 1);
978
979 for (curr = m; curr; curr = curr->base_midx) {
980 for (i = 0; i < m->num_objects - 1; i++) {
981 struct object_id oid1, oid2;
982
983 nth_midxed_object_oid(&oid1, m, m->num_objects_in_base + i);
984 nth_midxed_object_oid(&oid2, m, m->num_objects_in_base + i + 1);
985
986 if (oidcmp(&oid1, &oid2) >= 0)
987 midx_report(_("oid lookup out of order: oid[%d] = %s >= %s = oid[%d]"),
988 i, oid_to_hex(&oid1), oid_to_hex(&oid2), i + 1);
989
990 midx_display_sparse_progress(progress, i + 1);
991 }
992 }
993 stop_progress(&progress);
994
995 /*
996 * Create an array mapping each object to its packfile id. Sort it
997 * to group the objects by packfile. Use this permutation to visit
998 * each of the objects and only require 1 packfile to be open at a
999 * time.
1000 */
1001 ALLOC_ARRAY(pairs, m->num_objects + m->num_objects_in_base);
1002 for (i = 0; i < m->num_objects + m->num_objects_in_base; i++) {
1003 pairs[i].pos = i;
1004 pairs[i].pack_int_id = nth_midxed_pack_int_id(m, i);
1005 }
1006
1007 if (flags & MIDX_PROGRESS)
1008 progress = start_sparse_progress(r,
1009 _("Sorting objects by packfile"),
1010 m->num_objects);
1011 display_progress(progress, 0); /* TODO: Measure QSORT() progress */
1012 QSORT(pairs, m->num_objects, compare_pair_pos_vs_id);
1013 stop_progress(&progress);
1014
1015 if (flags & MIDX_PROGRESS)
1016 progress = start_sparse_progress(r,
1017 _("Verifying object offsets"),
1018 m->num_objects);
1019 for (i = 0; i < m->num_objects + m->num_objects_in_base; i++) {
1020 struct object_id oid;
1021 struct pack_entry e;
1022 off_t m_offset, p_offset;
1023
1024 if (i > 0 && pairs[i-1].pack_int_id != pairs[i].pack_int_id &&
1025 nth_midxed_pack(m, pairs[i-1].pack_int_id)) {
1026 uint32_t pack_int_id = pairs[i-1].pack_int_id;
1027 struct packed_git *p = nth_midxed_pack(m, pack_int_id);
1028
1029 close_pack_fd(p);
1030 close_pack_index(p);
1031 }
1032
1033 nth_midxed_object_oid(&oid, m, pairs[i].pos);
1034
1035 if (midx_fill_entry(m, &oid, &e, NULL) != MIDX_FILL_HIT) {
1036 midx_report(_("failed to load pack entry for oid[%d] = %s"),
1037 pairs[i].pos, oid_to_hex(&oid));
1038 continue;
1039 }
1040
1041 if (open_pack_index(e.p)) {
1042 midx_report(_("failed to load pack-index for packfile %s"),
1043 e.p->pack_name);
1044 break;
1045 }
1046
1047 m_offset = e.offset;
1048 p_offset = find_pack_entry_one(&oid, e.p);
1049
1050 if (m_offset != p_offset)
1051 midx_report(_("incorrect object offset for oid[%d] = %s: %"PRIx64" != %"PRIx64),
1052 pairs[i].pos, oid_to_hex(&oid), m_offset, p_offset);
1053
1054 midx_display_sparse_progress(progress, i + 1);
1055 }
1056 stop_progress(&progress);
1057
1058 cleanup:
1059 free(pairs);
1060 close_midx(m);
1061
1062 return verify_midx_error;
1063 }