1 #define DISABLE_SIGN_COMPARE_WARNINGS
3 #include "git-compat-util.h"
8 #include "object-file.h"
9 #include "hash-lookup.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
,
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
);
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
)
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"));
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);
67 m
->num_objects
= ntohl(m
->chunk_oid_fanout
[255]);
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"));
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"));
97 static struct multi_pack_index
*load_multi_pack_index_one(struct repository
*r
,
98 const char *object_dir
,
99 const char *midx_name
,
102 struct multi_pack_index
*m
= NULL
;
106 void *midx_map
= NULL
;
107 uint32_t hash_version
;
109 const char *cur_pack_name
;
110 struct chunkfile
*cf
= NULL
;
112 fd
= git_open(midx_name
);
116 if (fstat(fd
, &st
)) {
117 error_errno(_("failed to read %s"), midx_name
);
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
);
128 midx_map
= xmmap(NULL
, midx_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
131 FLEX_ALLOC_STR(m
, object_dir
, object_dir
);
133 m
->data_len
= midx_size
;
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"),
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
));
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
))
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
++) {
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
);
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],
210 trace2_data_intmax("midx", r
, "load/num_packs", m
->num_packs
);
211 trace2_data_intmax("midx", r
, "load/num_objects", m
->num_objects
);
220 munmap(midx_map
, midx_size
);
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
,
250 *fd
= git_open(chain_file
);
253 if (fstat(*fd
, st
)) {
257 if (st
->st_size
< hash_algo
->hexsz
) {
260 /* treat empty files the same as missing */
263 warning(_("multi-pack-index chain file too small"));
271 static int add_midx_to_chain(struct multi_pack_index
*midx
,
272 struct multi_pack_index
*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
);
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
);
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
;
299 static struct multi_pack_index
*load_midx_chain_fd_st(struct repository
*r
,
300 const char *object_dir
,
302 int fd
, struct stat
*st
,
303 int *incomplete_chain
)
305 struct multi_pack_index
*midx_chain
= NULL
;
306 struct strbuf buf
= STRBUF_INIT
;
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
)
320 if (get_oid_hex_algop(buf
.buf
, &layer
, r
->hash_algo
)) {
321 warning(_("invalid multi-pack-index chain: line '%s' "
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
);
336 if (add_midx_to_chain(m
, midx_chain
)) {
344 warning(_("unable to find all multi-pack index files"));
350 strbuf_release(&buf
);
352 *incomplete_chain
= !valid
;
356 static struct multi_pack_index
*load_multi_pack_index_chain(struct repository
*r
,
357 const char *object_dir
,
360 struct strbuf chain_file
= STRBUF_INIT
;
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
)) {
368 /* ownership of fd is taken over by load function */
369 m
= load_midx_chain_fd_st(r
, object_dir
, local
, fd
, &st
,
373 strbuf_release(&chain_file
);
377 struct multi_pack_index
*load_multi_pack_index(struct repository
*r
,
378 const char *object_dir
,
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
);
389 m
= load_multi_pack_index_chain(r
, object_dir
, local
);
391 strbuf_release(&midx_name
);
396 void close_midx(struct multi_pack_index
*m
)
404 close_midx(m
->base_midx
);
406 munmap((unsigned char *)m
->data
, m
->data_len
);
408 for (i
= 0; i
< m
->num_packs
; i
++) {
410 m
->packs
[i
]->multi_pack_index
= 0;
412 FREE_AND_NULL(m
->packs
);
413 FREE_AND_NULL(m
->pack_names
);
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
)
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"));
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
)
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
);
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
])
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
);
476 p
= add_packed_git(r
, pack_name
.buf
, pack_name
.len
, m
->local
);
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
);
489 p
->multi_pack_index
= 1;
490 m
->packs
[pack_int_id
] = p
;
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
+
521 bp
->pack_int_id
= pack_int_id
;
527 int bsearch_one_midx(const struct object_id
*oid
, struct multi_pack_index
*m
,
530 int ret
= bsearch_hash(oid
->hash
, m
->chunk_oid_fanout
,
531 m
->chunk_oid_lookup
, m
->repo
->hash_algo
->rawsz
,
534 *result
+= m
->num_objects_in_base
;
538 int bsearch_midx(const struct object_id
*oid
, struct multi_pack_index
*m
,
541 for (; m
; m
= m
->base_midx
)
542 if (bsearch_one_midx(oid
, m
, result
))
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
,
556 if (n
>= m
->num_objects
+ m
->num_objects_in_base
)
559 n
= midx_for_object(&m
, n
);
561 oidread(oid
, m
->chunk_oid_lookup
+ st_mult(m
->hash_len
, n
),
566 off_t
nth_midxed_offset(struct multi_pack_index
*m
, uint32_t pos
)
568 const unsigned char *offset_data
;
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
);
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
)
603 uint32_t pack_int_id
;
604 struct packed_git
*p
;
606 if (!bsearch_midx(oid
, m
, &pos
))
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
))
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
623 if (!is_pack_valid(p
))
626 if (oidset_size(&p
->bad_objects
) &&
627 oidset_contains(&p
->bad_objects
, oid
))
630 e
->offset
= nth_midxed_offset(m
, pos
);
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
) {
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"))
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;
678 current
= m
->pack_names
[mid
];
679 cmp
= cmp_idx_or_pack_name(idx_or_pack_name
, current
);
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
))
700 int midx_preferred_pack(struct multi_pack_index
*m
, uint32_t *pack_int_id
)
702 if (m
->preferred_pack_idx
== -1) {
704 if (load_midx_revindex(m
) < 0) {
705 m
->preferred_pack_idx
= -2;
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
;
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
)
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
))
733 m
= load_multi_pack_index(r
, object_dir
, local
);
736 struct multi_pack_index
*mp
= r
->objects
->multi_pack_index
;
741 r
->objects
->multi_pack_index
= m
;
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
{
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
;
765 if (!(starts_with(file_name
, "multi-pack-index-") &&
766 ends_with(file_name
, data
->ext
)))
768 for (i
= 0; i
< data
->keep_nr
; i
++) {
769 if (!strcmp(data
->keep
[i
], file_name
))
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
));
783 ALLOC_ARRAY(data
.keep
, 1);
785 data
.keep
[0] = xstrfmt("multi-pack-index-%s.%s", keep_hash
, ext
);
790 for_each_file_in_pack_dir(object_dir
,
799 void clear_incremental_midx_files_ext(const char *object_dir
, const char *ext
,
803 struct clear_midx_data data
;
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
],
812 data
.keep_nr
= hashes_nr
;
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
++)
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
, ...)
849 verify_midx_error
= 1;
851 vfprintf(stderr
, fmt
, ap
);
852 fprintf(stderr
, "\n");
856 struct pair_pos_vs_id
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) \
878 if ((_n & (SPARSE_PROGRESS_INTERVAL - 1)) == 0) \
879 display_progress(progress, _n); \
882 int verify_midx_file(struct repository
*r
, const char *object_dir
, unsigned flags
)
884 struct pair_pos_vs_id
*pairs
= NULL
;
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;
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"));
902 strbuf_release(&filename
);
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
930 if (flags
& MIDX_PROGRESS
)
931 progress
= start_sparse_progress(r
,
932 _("Verifying OID order in multi-pack-index"),
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
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
++) {
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"),
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"),
975 for (i
= 0; i
< m
->num_objects
+ m
->num_objects_in_base
; i
++) {
976 struct object_id oid
;
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
);
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
));
997 if (open_pack_index(e
.p
)) {
998 midx_report(_("failed to load pack-index for packfile %s"),
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
);
1018 return verify_midx_error
;