7 #include "object-store.h"
8 #include "sha1-lookup.h"
13 #define MIDX_SIGNATURE 0x4d494458 /* "MIDX" */
14 #define MIDX_VERSION 1
15 #define MIDX_BYTE_FILE_VERSION 4
16 #define MIDX_BYTE_HASH_VERSION 5
17 #define MIDX_BYTE_NUM_CHUNKS 6
18 #define MIDX_BYTE_NUM_PACKS 8
19 #define MIDX_HASH_VERSION 1
20 #define MIDX_HEADER_SIZE 12
21 #define MIDX_HASH_LEN 20
22 #define MIDX_MIN_SIZE (MIDX_HEADER_SIZE + MIDX_HASH_LEN)
24 #define MIDX_MAX_CHUNKS 5
25 #define MIDX_CHUNK_ALIGNMENT 4
26 #define MIDX_CHUNKID_PACKNAMES 0x504e414d /* "PNAM" */
27 #define MIDX_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
28 #define MIDX_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
29 #define MIDX_CHUNKID_OBJECTOFFSETS 0x4f4f4646 /* "OOFF" */
30 #define MIDX_CHUNKID_LARGEOFFSETS 0x4c4f4646 /* "LOFF" */
31 #define MIDX_CHUNKLOOKUP_WIDTH (sizeof(uint32_t) + sizeof(uint64_t))
32 #define MIDX_CHUNK_FANOUT_SIZE (sizeof(uint32_t) * 256)
33 #define MIDX_CHUNK_OFFSET_WIDTH (2 * sizeof(uint32_t))
34 #define MIDX_CHUNK_LARGE_OFFSET_WIDTH (sizeof(uint64_t))
35 #define MIDX_LARGE_OFFSET_NEEDED 0x80000000
37 static char *get_midx_filename(const char *object_dir
)
39 return xstrfmt("%s/pack/multi-pack-index", object_dir
);
42 struct multi_pack_index
*load_multi_pack_index(const char *object_dir
, int local
)
44 struct multi_pack_index
*m
= NULL
;
48 void *midx_map
= NULL
;
49 uint32_t hash_version
;
50 char *midx_name
= get_midx_filename(object_dir
);
52 const char *cur_pack_name
;
54 fd
= git_open(midx_name
);
59 error_errno(_("failed to read %s"), midx_name
);
63 midx_size
= xsize_t(st
.st_size
);
65 if (midx_size
< MIDX_MIN_SIZE
) {
66 error(_("multi-pack-index file %s is too small"), midx_name
);
70 FREE_AND_NULL(midx_name
);
72 midx_map
= xmmap(NULL
, midx_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
74 FLEX_ALLOC_MEM(m
, object_dir
, object_dir
, strlen(object_dir
));
77 m
->data_len
= midx_size
;
80 m
->signature
= get_be32(m
->data
);
81 if (m
->signature
!= MIDX_SIGNATURE
)
82 die(_("multi-pack-index signature 0x%08x does not match signature 0x%08x"),
83 m
->signature
, MIDX_SIGNATURE
);
85 m
->version
= m
->data
[MIDX_BYTE_FILE_VERSION
];
86 if (m
->version
!= MIDX_VERSION
)
87 die(_("multi-pack-index version %d not recognized"),
90 hash_version
= m
->data
[MIDX_BYTE_HASH_VERSION
];
91 if (hash_version
!= MIDX_HASH_VERSION
)
92 die(_("hash version %u does not match"), hash_version
);
93 m
->hash_len
= MIDX_HASH_LEN
;
95 m
->num_chunks
= m
->data
[MIDX_BYTE_NUM_CHUNKS
];
97 m
->num_packs
= get_be32(m
->data
+ MIDX_BYTE_NUM_PACKS
);
99 for (i
= 0; i
< m
->num_chunks
; i
++) {
100 uint32_t chunk_id
= get_be32(m
->data
+ MIDX_HEADER_SIZE
+
101 MIDX_CHUNKLOOKUP_WIDTH
* i
);
102 uint64_t chunk_offset
= get_be64(m
->data
+ MIDX_HEADER_SIZE
+ 4 +
103 MIDX_CHUNKLOOKUP_WIDTH
* i
);
105 if (chunk_offset
>= m
->data_len
)
106 die(_("invalid chunk offset (too large)"));
109 case MIDX_CHUNKID_PACKNAMES
:
110 m
->chunk_pack_names
= m
->data
+ chunk_offset
;
113 case MIDX_CHUNKID_OIDFANOUT
:
114 m
->chunk_oid_fanout
= (uint32_t *)(m
->data
+ chunk_offset
);
117 case MIDX_CHUNKID_OIDLOOKUP
:
118 m
->chunk_oid_lookup
= m
->data
+ chunk_offset
;
121 case MIDX_CHUNKID_OBJECTOFFSETS
:
122 m
->chunk_object_offsets
= m
->data
+ chunk_offset
;
125 case MIDX_CHUNKID_LARGEOFFSETS
:
126 m
->chunk_large_offsets
= m
->data
+ chunk_offset
;
130 die(_("terminating multi-pack-index chunk id appears earlier than expected"));
135 * Do nothing on unrecognized chunks, allowing future
136 * extensions to add optional chunks.
142 if (!m
->chunk_pack_names
)
143 die(_("multi-pack-index missing required pack-name chunk"));
144 if (!m
->chunk_oid_fanout
)
145 die(_("multi-pack-index missing required OID fanout chunk"));
146 if (!m
->chunk_oid_lookup
)
147 die(_("multi-pack-index missing required OID lookup chunk"));
148 if (!m
->chunk_object_offsets
)
149 die(_("multi-pack-index missing required object offsets chunk"));
151 m
->num_objects
= ntohl(m
->chunk_oid_fanout
[255]);
153 m
->pack_names
= xcalloc(m
->num_packs
, sizeof(*m
->pack_names
));
154 m
->packs
= xcalloc(m
->num_packs
, sizeof(*m
->packs
));
156 cur_pack_name
= (const char *)m
->chunk_pack_names
;
157 for (i
= 0; i
< m
->num_packs
; i
++) {
158 m
->pack_names
[i
] = cur_pack_name
;
160 cur_pack_name
+= strlen(cur_pack_name
) + 1;
162 if (i
&& strcmp(m
->pack_names
[i
], m
->pack_names
[i
- 1]) <= 0)
163 die(_("multi-pack-index pack names out of order: '%s' before '%s'"),
164 m
->pack_names
[i
- 1],
168 trace2_data_intmax("midx", the_repository
, "load/num_packs", m
->num_packs
);
169 trace2_data_intmax("midx", the_repository
, "load/num_objects", m
->num_objects
);
177 munmap(midx_map
, midx_size
);
183 void close_midx(struct multi_pack_index
*m
)
190 munmap((unsigned char *)m
->data
, m
->data_len
);
194 for (i
= 0; i
< m
->num_packs
; i
++) {
196 close_pack(m
->packs
[i
]);
200 FREE_AND_NULL(m
->packs
);
201 FREE_AND_NULL(m
->pack_names
);
204 int prepare_midx_pack(struct multi_pack_index
*m
, uint32_t pack_int_id
)
206 struct strbuf pack_name
= STRBUF_INIT
;
208 if (pack_int_id
>= m
->num_packs
)
209 die(_("bad pack-int-id: %u (%u total packs)"),
210 pack_int_id
, m
->num_packs
);
212 if (m
->packs
[pack_int_id
])
215 strbuf_addf(&pack_name
, "%s/pack/%s", m
->object_dir
,
216 m
->pack_names
[pack_int_id
]);
218 m
->packs
[pack_int_id
] = add_packed_git(pack_name
.buf
, pack_name
.len
, m
->local
);
219 strbuf_release(&pack_name
);
220 return !m
->packs
[pack_int_id
];
223 int bsearch_midx(const struct object_id
*oid
, struct multi_pack_index
*m
, uint32_t *result
)
225 return bsearch_hash(oid
->hash
, m
->chunk_oid_fanout
, m
->chunk_oid_lookup
,
226 MIDX_HASH_LEN
, result
);
229 struct object_id
*nth_midxed_object_oid(struct object_id
*oid
,
230 struct multi_pack_index
*m
,
233 if (n
>= m
->num_objects
)
236 hashcpy(oid
->hash
, m
->chunk_oid_lookup
+ m
->hash_len
* n
);
240 static off_t
nth_midxed_offset(struct multi_pack_index
*m
, uint32_t pos
)
242 const unsigned char *offset_data
;
245 offset_data
= m
->chunk_object_offsets
+ pos
* MIDX_CHUNK_OFFSET_WIDTH
;
246 offset32
= get_be32(offset_data
+ sizeof(uint32_t));
248 if (m
->chunk_large_offsets
&& offset32
& MIDX_LARGE_OFFSET_NEEDED
) {
249 if (sizeof(off_t
) < sizeof(uint64_t))
250 die(_("multi-pack-index stores a 64-bit offset, but off_t is too small"));
252 offset32
^= MIDX_LARGE_OFFSET_NEEDED
;
253 return get_be64(m
->chunk_large_offsets
+ sizeof(uint64_t) * offset32
);
259 static uint32_t nth_midxed_pack_int_id(struct multi_pack_index
*m
, uint32_t pos
)
261 return get_be32(m
->chunk_object_offsets
+ pos
* MIDX_CHUNK_OFFSET_WIDTH
);
264 static int nth_midxed_pack_entry(struct multi_pack_index
*m
, struct pack_entry
*e
, uint32_t pos
)
266 uint32_t pack_int_id
;
267 struct packed_git
*p
;
269 if (pos
>= m
->num_objects
)
272 pack_int_id
= nth_midxed_pack_int_id(m
, pos
);
274 if (prepare_midx_pack(m
, pack_int_id
))
275 die(_("error preparing packfile from multi-pack-index"));
276 p
= m
->packs
[pack_int_id
];
279 * We are about to tell the caller where they can locate the
280 * requested object. We better make sure the packfile is
281 * still here and can be accessed before supplying that
282 * answer, as it may have been deleted since the MIDX was
285 if (!is_pack_valid(p
))
288 if (p
->num_bad_objects
) {
290 struct object_id oid
;
291 nth_midxed_object_oid(&oid
, m
, pos
);
292 for (i
= 0; i
< p
->num_bad_objects
; i
++)
294 p
->bad_object_sha1
+ the_hash_algo
->rawsz
* i
))
298 e
->offset
= nth_midxed_offset(m
, pos
);
304 int fill_midx_entry(const struct object_id
*oid
, struct pack_entry
*e
, struct multi_pack_index
*m
)
308 if (!bsearch_midx(oid
, m
, &pos
))
311 return nth_midxed_pack_entry(m
, e
, pos
);
314 int midx_contains_pack(struct multi_pack_index
*m
, const char *idx_name
)
316 uint32_t first
= 0, last
= m
->num_packs
;
318 while (first
< last
) {
319 uint32_t mid
= first
+ (last
- first
) / 2;
323 current
= m
->pack_names
[mid
];
324 cmp
= strcmp(idx_name
, current
);
337 int prepare_multi_pack_index_one(struct repository
*r
, const char *object_dir
, int local
)
339 struct multi_pack_index
*m
;
340 struct multi_pack_index
*m_search
;
342 static int env_value
= -1;
345 env_value
= git_env_bool(GIT_TEST_MULTI_PACK_INDEX
, 0);
348 (repo_config_get_bool(r
, "core.multipackindex", &config_value
) ||
352 for (m_search
= r
->objects
->multi_pack_index
; m_search
; m_search
= m_search
->next
)
353 if (!strcmp(object_dir
, m_search
->object_dir
))
356 m
= load_multi_pack_index(object_dir
, local
);
359 m
->next
= r
->objects
->multi_pack_index
;
360 r
->objects
->multi_pack_index
= m
;
367 static size_t write_midx_header(struct hashfile
*f
,
368 unsigned char num_chunks
,
371 unsigned char byte_values
[4];
373 hashwrite_be32(f
, MIDX_SIGNATURE
);
374 byte_values
[0] = MIDX_VERSION
;
375 byte_values
[1] = MIDX_HASH_VERSION
;
376 byte_values
[2] = num_chunks
;
377 byte_values
[3] = 0; /* unused */
378 hashwrite(f
, byte_values
, sizeof(byte_values
));
379 hashwrite_be32(f
, num_packs
);
381 return MIDX_HEADER_SIZE
;
385 struct packed_git
**list
;
389 uint32_t alloc_names
;
390 size_t pack_name_concat_len
;
391 struct multi_pack_index
*m
;
394 static void add_pack_to_midx(const char *full_path
, size_t full_path_len
,
395 const char *file_name
, void *data
)
397 struct pack_list
*packs
= (struct pack_list
*)data
;
399 if (ends_with(file_name
, ".idx")) {
400 if (packs
->m
&& midx_contains_pack(packs
->m
, file_name
))
403 ALLOC_GROW(packs
->list
, packs
->nr
+ 1, packs
->alloc_list
);
404 ALLOC_GROW(packs
->names
, packs
->nr
+ 1, packs
->alloc_names
);
406 packs
->list
[packs
->nr
] = add_packed_git(full_path
,
410 if (!packs
->list
[packs
->nr
]) {
411 warning(_("failed to add packfile '%s'"),
416 if (open_pack_index(packs
->list
[packs
->nr
])) {
417 warning(_("failed to open pack-index '%s'"),
419 close_pack(packs
->list
[packs
->nr
]);
420 FREE_AND_NULL(packs
->list
[packs
->nr
]);
424 packs
->names
[packs
->nr
] = xstrdup(file_name
);
425 packs
->pack_name_concat_len
+= strlen(file_name
) + 1;
431 uint32_t pack_int_id
;
435 static int pack_pair_compare(const void *_a
, const void *_b
)
437 struct pack_pair
*a
= (struct pack_pair
*)_a
;
438 struct pack_pair
*b
= (struct pack_pair
*)_b
;
439 return strcmp(a
->pack_name
, b
->pack_name
);
442 static void sort_packs_by_name(char **pack_names
, uint32_t nr_packs
, uint32_t *perm
)
445 struct pack_pair
*pairs
;
447 ALLOC_ARRAY(pairs
, nr_packs
);
449 for (i
= 0; i
< nr_packs
; i
++) {
450 pairs
[i
].pack_int_id
= i
;
451 pairs
[i
].pack_name
= pack_names
[i
];
454 QSORT(pairs
, nr_packs
, pack_pair_compare
);
456 for (i
= 0; i
< nr_packs
; i
++) {
457 pack_names
[i
] = pairs
[i
].pack_name
;
458 perm
[pairs
[i
].pack_int_id
] = i
;
464 struct pack_midx_entry
{
465 struct object_id oid
;
466 uint32_t pack_int_id
;
471 static int midx_oid_compare(const void *_a
, const void *_b
)
473 const struct pack_midx_entry
*a
= (const struct pack_midx_entry
*)_a
;
474 const struct pack_midx_entry
*b
= (const struct pack_midx_entry
*)_b
;
475 int cmp
= oidcmp(&a
->oid
, &b
->oid
);
480 if (a
->pack_mtime
> b
->pack_mtime
)
482 else if (a
->pack_mtime
< b
->pack_mtime
)
485 return a
->pack_int_id
- b
->pack_int_id
;
488 static int nth_midxed_pack_midx_entry(struct multi_pack_index
*m
,
490 struct pack_midx_entry
*e
,
493 if (pos
>= m
->num_objects
)
496 nth_midxed_object_oid(&e
->oid
, m
, pos
);
497 e
->pack_int_id
= pack_perm
[nth_midxed_pack_int_id(m
, pos
)];
498 e
->offset
= nth_midxed_offset(m
, pos
);
500 /* consider objects in midx to be from "old" packs */
505 static void fill_pack_entry(uint32_t pack_int_id
,
506 struct packed_git
*p
,
508 struct pack_midx_entry
*entry
)
510 if (!nth_packed_object_oid(&entry
->oid
, p
, cur_object
))
511 die(_("failed to locate object %d in packfile"), cur_object
);
513 entry
->pack_int_id
= pack_int_id
;
514 entry
->pack_mtime
= p
->mtime
;
516 entry
->offset
= nth_packed_object_offset(p
, cur_object
);
520 * It is possible to artificially get into a state where there are many
521 * duplicate copies of objects. That can create high memory pressure if
522 * we are to create a list of all objects before de-duplication. To reduce
523 * this memory pressure without a significant performance drop, automatically
524 * group objects by the first byte of their object id. Use the IDX fanout
525 * tables to group the data, copy to a local array, then sort.
527 * Copy only the de-duplicated entries (selected by most-recent modified time
528 * of a packfile containing the object).
530 static struct pack_midx_entry
*get_sorted_entries(struct multi_pack_index
*m
,
531 struct packed_git
**p
,
534 uint32_t *nr_objects
)
536 uint32_t cur_fanout
, cur_pack
, cur_object
;
537 uint32_t alloc_fanout
, alloc_objects
, total_objects
= 0;
538 struct pack_midx_entry
*entries_by_fanout
= NULL
;
539 struct pack_midx_entry
*deduplicated_entries
= NULL
;
540 uint32_t start_pack
= m
? m
->num_packs
: 0;
542 for (cur_pack
= start_pack
; cur_pack
< nr_packs
; cur_pack
++)
543 total_objects
+= p
[cur_pack
]->num_objects
;
546 * As we de-duplicate by fanout value, we expect the fanout
547 * slices to be evenly distributed, with some noise. Hence,
548 * allocate slightly more than one 256th.
550 alloc_objects
= alloc_fanout
= total_objects
> 3200 ? total_objects
/ 200 : 16;
552 ALLOC_ARRAY(entries_by_fanout
, alloc_fanout
);
553 ALLOC_ARRAY(deduplicated_entries
, alloc_objects
);
556 for (cur_fanout
= 0; cur_fanout
< 256; cur_fanout
++) {
557 uint32_t nr_fanout
= 0;
560 uint32_t start
= 0, end
;
563 start
= ntohl(m
->chunk_oid_fanout
[cur_fanout
- 1]);
564 end
= ntohl(m
->chunk_oid_fanout
[cur_fanout
]);
566 for (cur_object
= start
; cur_object
< end
; cur_object
++) {
567 ALLOC_GROW(entries_by_fanout
, nr_fanout
+ 1, alloc_fanout
);
568 nth_midxed_pack_midx_entry(m
, perm
,
569 &entries_by_fanout
[nr_fanout
],
575 for (cur_pack
= start_pack
; cur_pack
< nr_packs
; cur_pack
++) {
576 uint32_t start
= 0, end
;
579 start
= get_pack_fanout(p
[cur_pack
], cur_fanout
- 1);
580 end
= get_pack_fanout(p
[cur_pack
], cur_fanout
);
582 for (cur_object
= start
; cur_object
< end
; cur_object
++) {
583 ALLOC_GROW(entries_by_fanout
, nr_fanout
+ 1, alloc_fanout
);
584 fill_pack_entry(perm
[cur_pack
], p
[cur_pack
], cur_object
, &entries_by_fanout
[nr_fanout
]);
589 QSORT(entries_by_fanout
, nr_fanout
, midx_oid_compare
);
592 * The batch is now sorted by OID and then mtime (descending).
593 * Take only the first duplicate.
595 for (cur_object
= 0; cur_object
< nr_fanout
; cur_object
++) {
596 if (cur_object
&& oideq(&entries_by_fanout
[cur_object
- 1].oid
,
597 &entries_by_fanout
[cur_object
].oid
))
600 ALLOC_GROW(deduplicated_entries
, *nr_objects
+ 1, alloc_objects
);
601 memcpy(&deduplicated_entries
[*nr_objects
],
602 &entries_by_fanout
[cur_object
],
603 sizeof(struct pack_midx_entry
));
608 free(entries_by_fanout
);
609 return deduplicated_entries
;
612 static size_t write_midx_pack_names(struct hashfile
*f
,
617 unsigned char padding
[MIDX_CHUNK_ALIGNMENT
];
620 for (i
= 0; i
< num_packs
; i
++) {
621 size_t writelen
= strlen(pack_names
[i
]) + 1;
623 if (i
&& strcmp(pack_names
[i
], pack_names
[i
- 1]) <= 0)
624 BUG("incorrect pack-file order: %s before %s",
628 hashwrite(f
, pack_names
[i
], writelen
);
632 /* add padding to be aligned */
633 i
= MIDX_CHUNK_ALIGNMENT
- (written
% MIDX_CHUNK_ALIGNMENT
);
634 if (i
< MIDX_CHUNK_ALIGNMENT
) {
635 memset(padding
, 0, sizeof(padding
));
636 hashwrite(f
, padding
, i
);
643 static size_t write_midx_oid_fanout(struct hashfile
*f
,
644 struct pack_midx_entry
*objects
,
647 struct pack_midx_entry
*list
= objects
;
648 struct pack_midx_entry
*last
= objects
+ nr_objects
;
653 * Write the first-level table (the list is sorted,
654 * but we use a 256-entry lookup to be able to avoid
655 * having to do eight extra binary search iterations).
657 for (i
= 0; i
< 256; i
++) {
658 struct pack_midx_entry
*next
= list
;
660 while (next
< last
&& next
->oid
.hash
[0] == i
) {
665 hashwrite_be32(f
, count
);
669 return MIDX_CHUNK_FANOUT_SIZE
;
672 static size_t write_midx_oid_lookup(struct hashfile
*f
, unsigned char hash_len
,
673 struct pack_midx_entry
*objects
,
676 struct pack_midx_entry
*list
= objects
;
680 for (i
= 0; i
< nr_objects
; i
++) {
681 struct pack_midx_entry
*obj
= list
++;
683 if (i
< nr_objects
- 1) {
684 struct pack_midx_entry
*next
= list
;
685 if (oidcmp(&obj
->oid
, &next
->oid
) >= 0)
686 BUG("OIDs not in order: %s >= %s",
687 oid_to_hex(&obj
->oid
),
688 oid_to_hex(&next
->oid
));
691 hashwrite(f
, obj
->oid
.hash
, (int)hash_len
);
698 static size_t write_midx_object_offsets(struct hashfile
*f
, int large_offset_needed
,
699 struct pack_midx_entry
*objects
, uint32_t nr_objects
)
701 struct pack_midx_entry
*list
= objects
;
702 uint32_t i
, nr_large_offset
= 0;
705 for (i
= 0; i
< nr_objects
; i
++) {
706 struct pack_midx_entry
*obj
= list
++;
708 hashwrite_be32(f
, obj
->pack_int_id
);
710 if (large_offset_needed
&& obj
->offset
>> 31)
711 hashwrite_be32(f
, MIDX_LARGE_OFFSET_NEEDED
| nr_large_offset
++);
712 else if (!large_offset_needed
&& obj
->offset
>> 32)
713 BUG("object %s requires a large offset (%"PRIx64
") but the MIDX is not writing large offsets!",
714 oid_to_hex(&obj
->oid
),
717 hashwrite_be32(f
, (uint32_t)obj
->offset
);
719 written
+= MIDX_CHUNK_OFFSET_WIDTH
;
725 static size_t write_midx_large_offsets(struct hashfile
*f
, uint32_t nr_large_offset
,
726 struct pack_midx_entry
*objects
, uint32_t nr_objects
)
728 struct pack_midx_entry
*list
= objects
, *end
= objects
+ nr_objects
;
731 while (nr_large_offset
) {
732 struct pack_midx_entry
*obj
;
736 BUG("too many large-offset objects");
739 offset
= obj
->offset
;
744 hashwrite_be32(f
, offset
>> 32);
745 hashwrite_be32(f
, offset
& 0xffffffffUL
);
746 written
+= 2 * sizeof(uint32_t);
754 int write_midx_file(const char *object_dir
)
756 unsigned char cur_chunk
, num_chunks
= 0;
759 struct hashfile
*f
= NULL
;
761 struct pack_list packs
;
762 uint32_t *pack_perm
= NULL
;
763 uint64_t written
= 0;
764 uint32_t chunk_ids
[MIDX_MAX_CHUNKS
+ 1];
765 uint64_t chunk_offsets
[MIDX_MAX_CHUNKS
+ 1];
766 uint32_t nr_entries
, num_large_offsets
= 0;
767 struct pack_midx_entry
*entries
= NULL
;
768 int large_offsets_needed
= 0;
770 midx_name
= get_midx_filename(object_dir
);
771 if (safe_create_leading_directories(midx_name
)) {
773 die_errno(_("unable to create leading directories of %s"),
777 packs
.m
= load_multi_pack_index(object_dir
, 1);
780 packs
.alloc_list
= packs
.m
? packs
.m
->num_packs
: 16;
781 packs
.alloc_names
= packs
.alloc_list
;
784 packs
.pack_name_concat_len
= 0;
785 ALLOC_ARRAY(packs
.list
, packs
.alloc_list
);
786 ALLOC_ARRAY(packs
.names
, packs
.alloc_names
);
789 for (i
= 0; i
< packs
.m
->num_packs
; i
++) {
790 ALLOC_GROW(packs
.list
, packs
.nr
+ 1, packs
.alloc_list
);
791 ALLOC_GROW(packs
.names
, packs
.nr
+ 1, packs
.alloc_names
);
793 packs
.list
[packs
.nr
] = NULL
;
794 packs
.names
[packs
.nr
] = xstrdup(packs
.m
->pack_names
[i
]);
795 packs
.pack_name_concat_len
+= strlen(packs
.names
[packs
.nr
]) + 1;
800 for_each_file_in_pack_dir(object_dir
, add_pack_to_midx
, &packs
);
802 if (packs
.m
&& packs
.nr
== packs
.m
->num_packs
)
805 if (packs
.pack_name_concat_len
% MIDX_CHUNK_ALIGNMENT
)
806 packs
.pack_name_concat_len
+= MIDX_CHUNK_ALIGNMENT
-
807 (packs
.pack_name_concat_len
% MIDX_CHUNK_ALIGNMENT
);
809 ALLOC_ARRAY(pack_perm
, packs
.nr
);
810 sort_packs_by_name(packs
.names
, packs
.nr
, pack_perm
);
812 entries
= get_sorted_entries(packs
.m
, packs
.list
, pack_perm
, packs
.nr
, &nr_entries
);
814 for (i
= 0; i
< nr_entries
; i
++) {
815 if (entries
[i
].offset
> 0x7fffffff)
817 if (entries
[i
].offset
> 0xffffffff)
818 large_offsets_needed
= 1;
821 hold_lock_file_for_update(&lk
, midx_name
, LOCK_DIE_ON_ERROR
);
822 f
= hashfd(lk
.tempfile
->fd
, lk
.tempfile
->filename
.buf
);
823 FREE_AND_NULL(midx_name
);
829 num_chunks
= large_offsets_needed
? 5 : 4;
831 written
= write_midx_header(f
, num_chunks
, packs
.nr
);
833 chunk_ids
[cur_chunk
] = MIDX_CHUNKID_PACKNAMES
;
834 chunk_offsets
[cur_chunk
] = written
+ (num_chunks
+ 1) * MIDX_CHUNKLOOKUP_WIDTH
;
837 chunk_ids
[cur_chunk
] = MIDX_CHUNKID_OIDFANOUT
;
838 chunk_offsets
[cur_chunk
] = chunk_offsets
[cur_chunk
- 1] + packs
.pack_name_concat_len
;
841 chunk_ids
[cur_chunk
] = MIDX_CHUNKID_OIDLOOKUP
;
842 chunk_offsets
[cur_chunk
] = chunk_offsets
[cur_chunk
- 1] + MIDX_CHUNK_FANOUT_SIZE
;
845 chunk_ids
[cur_chunk
] = MIDX_CHUNKID_OBJECTOFFSETS
;
846 chunk_offsets
[cur_chunk
] = chunk_offsets
[cur_chunk
- 1] + nr_entries
* MIDX_HASH_LEN
;
849 chunk_offsets
[cur_chunk
] = chunk_offsets
[cur_chunk
- 1] + nr_entries
* MIDX_CHUNK_OFFSET_WIDTH
;
850 if (large_offsets_needed
) {
851 chunk_ids
[cur_chunk
] = MIDX_CHUNKID_LARGEOFFSETS
;
854 chunk_offsets
[cur_chunk
] = chunk_offsets
[cur_chunk
- 1] +
855 num_large_offsets
* MIDX_CHUNK_LARGE_OFFSET_WIDTH
;
858 chunk_ids
[cur_chunk
] = 0;
860 for (i
= 0; i
<= num_chunks
; i
++) {
861 if (i
&& chunk_offsets
[i
] < chunk_offsets
[i
- 1])
862 BUG("incorrect chunk offsets: %"PRIu64
" before %"PRIu64
,
863 chunk_offsets
[i
- 1],
866 if (chunk_offsets
[i
] % MIDX_CHUNK_ALIGNMENT
)
867 BUG("chunk offset %"PRIu64
" is not properly aligned",
870 hashwrite_be32(f
, chunk_ids
[i
]);
871 hashwrite_be32(f
, chunk_offsets
[i
] >> 32);
872 hashwrite_be32(f
, chunk_offsets
[i
]);
874 written
+= MIDX_CHUNKLOOKUP_WIDTH
;
877 for (i
= 0; i
< num_chunks
; i
++) {
878 if (written
!= chunk_offsets
[i
])
879 BUG("incorrect chunk offset (%"PRIu64
" != %"PRIu64
") for chunk id %"PRIx32
,
884 switch (chunk_ids
[i
]) {
885 case MIDX_CHUNKID_PACKNAMES
:
886 written
+= write_midx_pack_names(f
, packs
.names
, packs
.nr
);
889 case MIDX_CHUNKID_OIDFANOUT
:
890 written
+= write_midx_oid_fanout(f
, entries
, nr_entries
);
893 case MIDX_CHUNKID_OIDLOOKUP
:
894 written
+= write_midx_oid_lookup(f
, MIDX_HASH_LEN
, entries
, nr_entries
);
897 case MIDX_CHUNKID_OBJECTOFFSETS
:
898 written
+= write_midx_object_offsets(f
, large_offsets_needed
, entries
, nr_entries
);
901 case MIDX_CHUNKID_LARGEOFFSETS
:
902 written
+= write_midx_large_offsets(f
, num_large_offsets
, entries
, nr_entries
);
906 BUG("trying to write unknown chunk id %"PRIx32
,
911 if (written
!= chunk_offsets
[num_chunks
])
912 BUG("incorrect final offset %"PRIu64
" != %"PRIu64
,
914 chunk_offsets
[num_chunks
]);
916 finalize_hashfile(f
, NULL
, CSUM_FSYNC
| CSUM_HASH_IN_STREAM
);
917 commit_lock_file(&lk
);
920 for (i
= 0; i
< packs
.nr
; i
++) {
922 close_pack(packs
.list
[i
]);
925 free(packs
.names
[i
]);
936 void clear_midx_file(struct repository
*r
)
938 char *midx
= get_midx_filename(r
->objects
->odb
->path
);
940 if (r
->objects
&& r
->objects
->multi_pack_index
) {
941 close_midx(r
->objects
->multi_pack_index
);
942 r
->objects
->multi_pack_index
= NULL
;
945 if (remove_path(midx
)) {
947 die(_("failed to clear multi-pack-index at %s"), midx
);
953 static int verify_midx_error
;
955 static void midx_report(const char *fmt
, ...)
958 verify_midx_error
= 1;
960 vfprintf(stderr
, fmt
, ap
);
961 fprintf(stderr
, "\n");
965 struct pair_pos_vs_id
968 uint32_t pack_int_id
;
971 static int compare_pair_pos_vs_id(const void *_a
, const void *_b
)
973 struct pair_pos_vs_id
*a
= (struct pair_pos_vs_id
*)_a
;
974 struct pair_pos_vs_id
*b
= (struct pair_pos_vs_id
*)_b
;
976 return b
->pack_int_id
- a
->pack_int_id
;
980 * Limit calls to display_progress() for performance reasons.
981 * The interval here was arbitrarily chosen.
983 #define SPARSE_PROGRESS_INTERVAL (1 << 12)
984 #define midx_display_sparse_progress(progress, n) \
987 if ((_n & (SPARSE_PROGRESS_INTERVAL - 1)) == 0) \
988 display_progress(progress, _n); \
991 int verify_midx_file(const char *object_dir
)
993 struct pair_pos_vs_id
*pairs
= NULL
;
995 struct progress
*progress
;
996 struct multi_pack_index
*m
= load_multi_pack_index(object_dir
, 1);
997 verify_midx_error
= 0;
1002 progress
= start_progress(_("Looking for referenced packfiles"),
1004 for (i
= 0; i
< m
->num_packs
; i
++) {
1005 if (prepare_midx_pack(m
, i
))
1006 midx_report("failed to load pack in position %d", i
);
1008 display_progress(progress
, i
+ 1);
1010 stop_progress(&progress
);
1012 for (i
= 0; i
< 255; i
++) {
1013 uint32_t oid_fanout1
= ntohl(m
->chunk_oid_fanout
[i
]);
1014 uint32_t oid_fanout2
= ntohl(m
->chunk_oid_fanout
[i
+ 1]);
1016 if (oid_fanout1
> oid_fanout2
)
1017 midx_report(_("oid fanout out of order: fanout[%d] = %"PRIx32
" > %"PRIx32
" = fanout[%d]"),
1018 i
, oid_fanout1
, oid_fanout2
, i
+ 1);
1021 progress
= start_sparse_progress(_("Verifying OID order in MIDX"),
1022 m
->num_objects
- 1);
1023 for (i
= 0; i
< m
->num_objects
- 1; i
++) {
1024 struct object_id oid1
, oid2
;
1026 nth_midxed_object_oid(&oid1
, m
, i
);
1027 nth_midxed_object_oid(&oid2
, m
, i
+ 1);
1029 if (oidcmp(&oid1
, &oid2
) >= 0)
1030 midx_report(_("oid lookup out of order: oid[%d] = %s >= %s = oid[%d]"),
1031 i
, oid_to_hex(&oid1
), oid_to_hex(&oid2
), i
+ 1);
1033 midx_display_sparse_progress(progress
, i
+ 1);
1035 stop_progress(&progress
);
1038 * Create an array mapping each object to its packfile id. Sort it
1039 * to group the objects by packfile. Use this permutation to visit
1040 * each of the objects and only require 1 packfile to be open at a
1043 ALLOC_ARRAY(pairs
, m
->num_objects
);
1044 for (i
= 0; i
< m
->num_objects
; i
++) {
1046 pairs
[i
].pack_int_id
= nth_midxed_pack_int_id(m
, i
);
1049 progress
= start_sparse_progress(_("Sorting objects by packfile"),
1051 display_progress(progress
, 0); /* TODO: Measure QSORT() progress */
1052 QSORT(pairs
, m
->num_objects
, compare_pair_pos_vs_id
);
1053 stop_progress(&progress
);
1055 progress
= start_sparse_progress(_("Verifying object offsets"), m
->num_objects
);
1056 for (i
= 0; i
< m
->num_objects
; i
++) {
1057 struct object_id oid
;
1058 struct pack_entry e
;
1059 off_t m_offset
, p_offset
;
1061 if (i
> 0 && pairs
[i
-1].pack_int_id
!= pairs
[i
].pack_int_id
&&
1062 m
->packs
[pairs
[i
-1].pack_int_id
])
1064 close_pack_fd(m
->packs
[pairs
[i
-1].pack_int_id
]);
1065 close_pack_index(m
->packs
[pairs
[i
-1].pack_int_id
]);
1068 nth_midxed_object_oid(&oid
, m
, pairs
[i
].pos
);
1070 if (!fill_midx_entry(&oid
, &e
, m
)) {
1071 midx_report(_("failed to load pack entry for oid[%d] = %s"),
1072 pairs
[i
].pos
, oid_to_hex(&oid
));
1076 if (open_pack_index(e
.p
)) {
1077 midx_report(_("failed to load pack-index for packfile %s"),
1082 m_offset
= e
.offset
;
1083 p_offset
= find_pack_entry_one(oid
.hash
, e
.p
);
1085 if (m_offset
!= p_offset
)
1086 midx_report(_("incorrect object offset for oid[%d] = %s: %"PRIx64
" != %"PRIx64
),
1087 pairs
[i
].pos
, oid_to_hex(&oid
), m_offset
, p_offset
);
1089 midx_display_sparse_progress(progress
, i
+ 1);
1091 stop_progress(&progress
);
1095 return verify_midx_error
;