1 #define USE_THE_REPOSITORY_VARIABLE
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 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
);
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
)
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"));
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);
62 m
->num_objects
= ntohl(m
->chunk_oid_fanout
[255]);
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"));
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"));
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
;
100 void *midx_map
= NULL
;
101 uint32_t hash_version
;
102 struct strbuf midx_name
= STRBUF_INIT
;
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
);
113 if (fstat(fd
, &st
)) {
114 error_errno(_("failed to read %s"), midx_name
.buf
);
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
);
125 strbuf_release(&midx_name
);
127 midx_map
= xmmap(NULL
, midx_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
130 FLEX_ALLOC_STR(m
, object_dir
, object_dir
);
132 m
->data_len
= midx_size
;
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"),
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
));
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
))
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
++) {
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
);
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],
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
);
216 strbuf_release(&midx_name
);
219 munmap(midx_map
, midx_size
);
225 void close_midx(struct multi_pack_index
*m
)
234 munmap((unsigned char *)m
->data
, m
->data_len
);
236 for (i
= 0; i
< m
->num_packs
; i
++) {
238 m
->packs
[i
]->multi_pack_index
= 0;
240 FREE_AND_NULL(m
->packs
);
241 FREE_AND_NULL(m
->pack_names
);
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
])
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
);
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
);
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
+
291 bp
->pack_int_id
= pack_int_id
;
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
,
306 if (n
>= m
->num_objects
)
309 oidread(oid
, m
->chunk_oid_lookup
+ st_mult(m
->hash_len
, n
),
310 the_repository
->hash_algo
);
314 off_t
nth_midxed_offset(struct multi_pack_index
*m
, uint32_t pos
)
316 const unsigned char *offset_data
;
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
);
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
)
347 uint32_t pack_int_id
;
348 struct packed_git
*p
;
350 if (!bsearch_midx(oid
, m
, &pos
))
353 if (pos
>= m
->num_objects
)
356 pack_int_id
= nth_midxed_pack_int_id(m
, pos
);
358 if (prepare_midx_pack(r
, m
, pack_int_id
))
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
369 if (!is_pack_valid(p
))
372 if (oidset_size(&p
->bad_objects
) &&
373 oidset_contains(&p
->bad_objects
, oid
))
376 e
->offset
= nth_midxed_offset(m
, pos
);
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
) {
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"))
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
,
417 uint32_t first
= 0, last
= m
->num_packs
;
419 while (first
< last
) {
420 uint32_t mid
= first
+ (last
- first
) / 2;
424 current
= m
->pack_names
[mid
];
425 cmp
= cmp_idx_or_pack_name(idx_or_pack_name
, current
);
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;
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
;
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
)
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
))
476 m
= load_multi_pack_index(object_dir
, local
);
479 struct multi_pack_index
*mp
= r
->objects
->multi_pack_index
;
484 r
->objects
->multi_pack_index
= m
;
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
{
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
)))
509 if (data
->keep
&& !strcmp(data
->keep
, file_name
))
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
));
523 data
.keep
= xstrfmt("multi-pack-index-%s%s",
524 hash_to_hex(keep_hash
), ext
);
527 for_each_file_in_pack_dir(object_dir
,
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
, ...)
560 verify_midx_error
= 1;
562 vfprintf(stderr
, fmt
, ap
);
563 fprintf(stderr
, "\n");
567 struct pair_pos_vs_id
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) \
589 if ((_n & (SPARSE_PROGRESS_INTERVAL - 1)) == 0) \
590 display_progress(progress, _n); \
593 int verify_midx_file(struct repository
*r
, const char *object_dir
, unsigned flags
)
595 struct pair_pos_vs_id
*pairs
= NULL
;
597 struct progress
*progress
= NULL
;
598 struct multi_pack_index
*m
= load_multi_pack_index(object_dir
, 1);
599 verify_midx_error
= 0;
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"));
612 strbuf_release(&filename
);
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"),
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
639 if (flags
& MIDX_PROGRESS
)
640 progress
= start_sparse_progress(_("Verifying OID order in multi-pack-index"),
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
662 ALLOC_ARRAY(pairs
, m
->num_objects
);
663 for (i
= 0; i
< m
->num_objects
; 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"),
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
;
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
));
697 if (open_pack_index(e
.p
)) {
698 midx_report(_("failed to load pack-index for packfile %s"),
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
);
718 return verify_midx_error
;