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_STR(m
, object_dir
, 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 m
->packs
[i
]->multi_pack_index
= 0;
198 FREE_AND_NULL(m
->packs
);
199 FREE_AND_NULL(m
->pack_names
);
202 int prepare_midx_pack(struct repository
*r
, struct multi_pack_index
*m
, uint32_t pack_int_id
)
204 struct strbuf pack_name
= STRBUF_INIT
;
205 struct packed_git
*p
;
207 if (pack_int_id
>= m
->num_packs
)
208 die(_("bad pack-int-id: %u (%u total packs)"),
209 pack_int_id
, m
->num_packs
);
211 if (m
->packs
[pack_int_id
])
214 strbuf_addf(&pack_name
, "%s/pack/%s", m
->object_dir
,
215 m
->pack_names
[pack_int_id
]);
217 p
= add_packed_git(pack_name
.buf
, pack_name
.len
, m
->local
);
218 strbuf_release(&pack_name
);
223 p
->multi_pack_index
= 1;
224 m
->packs
[pack_int_id
] = p
;
225 install_packed_git(r
, p
);
226 list_add_tail(&p
->mru
, &r
->objects
->packed_git_mru
);
231 int bsearch_midx(const struct object_id
*oid
, struct multi_pack_index
*m
, uint32_t *result
)
233 return bsearch_hash(oid
->hash
, m
->chunk_oid_fanout
, m
->chunk_oid_lookup
,
234 MIDX_HASH_LEN
, result
);
237 struct object_id
*nth_midxed_object_oid(struct object_id
*oid
,
238 struct multi_pack_index
*m
,
241 if (n
>= m
->num_objects
)
244 hashcpy(oid
->hash
, m
->chunk_oid_lookup
+ m
->hash_len
* n
);
248 static off_t
nth_midxed_offset(struct multi_pack_index
*m
, uint32_t pos
)
250 const unsigned char *offset_data
;
253 offset_data
= m
->chunk_object_offsets
+ pos
* MIDX_CHUNK_OFFSET_WIDTH
;
254 offset32
= get_be32(offset_data
+ sizeof(uint32_t));
256 if (m
->chunk_large_offsets
&& offset32
& MIDX_LARGE_OFFSET_NEEDED
) {
257 if (sizeof(off_t
) < sizeof(uint64_t))
258 die(_("multi-pack-index stores a 64-bit offset, but off_t is too small"));
260 offset32
^= MIDX_LARGE_OFFSET_NEEDED
;
261 return get_be64(m
->chunk_large_offsets
+ sizeof(uint64_t) * offset32
);
267 static uint32_t nth_midxed_pack_int_id(struct multi_pack_index
*m
, uint32_t pos
)
269 return get_be32(m
->chunk_object_offsets
+ pos
* MIDX_CHUNK_OFFSET_WIDTH
);
272 static int nth_midxed_pack_entry(struct repository
*r
,
273 struct multi_pack_index
*m
,
274 struct pack_entry
*e
,
277 uint32_t pack_int_id
;
278 struct packed_git
*p
;
280 if (pos
>= m
->num_objects
)
283 pack_int_id
= nth_midxed_pack_int_id(m
, pos
);
285 if (prepare_midx_pack(r
, m
, pack_int_id
))
286 die(_("error preparing packfile from multi-pack-index"));
287 p
= m
->packs
[pack_int_id
];
290 * We are about to tell the caller where they can locate the
291 * requested object. We better make sure the packfile is
292 * still here and can be accessed before supplying that
293 * answer, as it may have been deleted since the MIDX was
296 if (!is_pack_valid(p
))
299 if (p
->num_bad_objects
) {
301 struct object_id oid
;
302 nth_midxed_object_oid(&oid
, m
, pos
);
303 for (i
= 0; i
< p
->num_bad_objects
; i
++)
305 p
->bad_object_sha1
+ the_hash_algo
->rawsz
* i
))
309 e
->offset
= nth_midxed_offset(m
, pos
);
315 int fill_midx_entry(struct repository
* r
,
316 const struct object_id
*oid
,
317 struct pack_entry
*e
,
318 struct multi_pack_index
*m
)
322 if (!bsearch_midx(oid
, m
, &pos
))
325 return nth_midxed_pack_entry(r
, m
, e
, pos
);
328 /* Match "foo.idx" against either "foo.pack" _or_ "foo.idx". */
329 static int cmp_idx_or_pack_name(const char *idx_or_pack_name
,
330 const char *idx_name
)
332 /* Skip past any initial matching prefix. */
333 while (*idx_name
&& *idx_name
== *idx_or_pack_name
) {
339 * If we didn't match completely, we may have matched "pack-1234." and
340 * be left with "idx" and "pack" respectively, which is also OK. We do
341 * not have to check for "idx" and "idx", because that would have been
342 * a complete match (and in that case these strcmps will be false, but
343 * we'll correctly return 0 from the final strcmp() below.
345 * Technically this matches "fooidx" and "foopack", but we'd never have
346 * such names in the first place.
348 if (!strcmp(idx_name
, "idx") && !strcmp(idx_or_pack_name
, "pack"))
352 * This not only checks for a complete match, but also orders based on
353 * the first non-identical character, which means our ordering will
354 * match a raw strcmp(). That makes it OK to use this to binary search
355 * a naively-sorted list.
357 return strcmp(idx_or_pack_name
, idx_name
);
360 int midx_contains_pack(struct multi_pack_index
*m
, const char *idx_or_pack_name
)
362 uint32_t first
= 0, last
= m
->num_packs
;
364 while (first
< last
) {
365 uint32_t mid
= first
+ (last
- first
) / 2;
369 current
= m
->pack_names
[mid
];
370 cmp
= cmp_idx_or_pack_name(idx_or_pack_name
, current
);
383 int prepare_multi_pack_index_one(struct repository
*r
, const char *object_dir
, int local
)
385 struct multi_pack_index
*m
;
386 struct multi_pack_index
*m_search
;
388 static int env_value
= -1;
391 env_value
= git_env_bool(GIT_TEST_MULTI_PACK_INDEX
, 0);
394 (repo_config_get_bool(r
, "core.multipackindex", &config_value
) ||
398 for (m_search
= r
->objects
->multi_pack_index
; m_search
; m_search
= m_search
->next
)
399 if (!strcmp(object_dir
, m_search
->object_dir
))
402 m
= load_multi_pack_index(object_dir
, local
);
405 m
->next
= r
->objects
->multi_pack_index
;
406 r
->objects
->multi_pack_index
= m
;
413 static size_t write_midx_header(struct hashfile
*f
,
414 unsigned char num_chunks
,
417 unsigned char byte_values
[4];
419 hashwrite_be32(f
, MIDX_SIGNATURE
);
420 byte_values
[0] = MIDX_VERSION
;
421 byte_values
[1] = MIDX_HASH_VERSION
;
422 byte_values
[2] = num_chunks
;
423 byte_values
[3] = 0; /* unused */
424 hashwrite(f
, byte_values
, sizeof(byte_values
));
425 hashwrite_be32(f
, num_packs
);
427 return MIDX_HEADER_SIZE
;
431 struct packed_git
**list
;
435 uint32_t alloc_names
;
436 size_t pack_name_concat_len
;
437 struct multi_pack_index
*m
;
440 static void add_pack_to_midx(const char *full_path
, size_t full_path_len
,
441 const char *file_name
, void *data
)
443 struct pack_list
*packs
= (struct pack_list
*)data
;
445 if (ends_with(file_name
, ".idx")) {
446 if (packs
->m
&& midx_contains_pack(packs
->m
, file_name
))
449 ALLOC_GROW(packs
->list
, packs
->nr
+ 1, packs
->alloc_list
);
450 ALLOC_GROW(packs
->names
, packs
->nr
+ 1, packs
->alloc_names
);
452 packs
->list
[packs
->nr
] = add_packed_git(full_path
,
456 if (!packs
->list
[packs
->nr
]) {
457 warning(_("failed to add packfile '%s'"),
462 if (open_pack_index(packs
->list
[packs
->nr
])) {
463 warning(_("failed to open pack-index '%s'"),
465 close_pack(packs
->list
[packs
->nr
]);
466 FREE_AND_NULL(packs
->list
[packs
->nr
]);
470 packs
->names
[packs
->nr
] = xstrdup(file_name
);
471 packs
->pack_name_concat_len
+= strlen(file_name
) + 1;
477 uint32_t pack_int_id
;
481 static int pack_pair_compare(const void *_a
, const void *_b
)
483 struct pack_pair
*a
= (struct pack_pair
*)_a
;
484 struct pack_pair
*b
= (struct pack_pair
*)_b
;
485 return strcmp(a
->pack_name
, b
->pack_name
);
488 static void sort_packs_by_name(char **pack_names
, uint32_t nr_packs
, uint32_t *perm
)
491 struct pack_pair
*pairs
;
493 ALLOC_ARRAY(pairs
, nr_packs
);
495 for (i
= 0; i
< nr_packs
; i
++) {
496 pairs
[i
].pack_int_id
= i
;
497 pairs
[i
].pack_name
= pack_names
[i
];
500 QSORT(pairs
, nr_packs
, pack_pair_compare
);
502 for (i
= 0; i
< nr_packs
; i
++) {
503 pack_names
[i
] = pairs
[i
].pack_name
;
504 perm
[pairs
[i
].pack_int_id
] = i
;
510 struct pack_midx_entry
{
511 struct object_id oid
;
512 uint32_t pack_int_id
;
517 static int midx_oid_compare(const void *_a
, const void *_b
)
519 const struct pack_midx_entry
*a
= (const struct pack_midx_entry
*)_a
;
520 const struct pack_midx_entry
*b
= (const struct pack_midx_entry
*)_b
;
521 int cmp
= oidcmp(&a
->oid
, &b
->oid
);
526 if (a
->pack_mtime
> b
->pack_mtime
)
528 else if (a
->pack_mtime
< b
->pack_mtime
)
531 return a
->pack_int_id
- b
->pack_int_id
;
534 static int nth_midxed_pack_midx_entry(struct multi_pack_index
*m
,
536 struct pack_midx_entry
*e
,
539 if (pos
>= m
->num_objects
)
542 nth_midxed_object_oid(&e
->oid
, m
, pos
);
543 e
->pack_int_id
= pack_perm
[nth_midxed_pack_int_id(m
, pos
)];
544 e
->offset
= nth_midxed_offset(m
, pos
);
546 /* consider objects in midx to be from "old" packs */
551 static void fill_pack_entry(uint32_t pack_int_id
,
552 struct packed_git
*p
,
554 struct pack_midx_entry
*entry
)
556 if (!nth_packed_object_oid(&entry
->oid
, p
, cur_object
))
557 die(_("failed to locate object %d in packfile"), cur_object
);
559 entry
->pack_int_id
= pack_int_id
;
560 entry
->pack_mtime
= p
->mtime
;
562 entry
->offset
= nth_packed_object_offset(p
, cur_object
);
566 * It is possible to artificially get into a state where there are many
567 * duplicate copies of objects. That can create high memory pressure if
568 * we are to create a list of all objects before de-duplication. To reduce
569 * this memory pressure without a significant performance drop, automatically
570 * group objects by the first byte of their object id. Use the IDX fanout
571 * tables to group the data, copy to a local array, then sort.
573 * Copy only the de-duplicated entries (selected by most-recent modified time
574 * of a packfile containing the object).
576 static struct pack_midx_entry
*get_sorted_entries(struct multi_pack_index
*m
,
577 struct packed_git
**p
,
580 uint32_t *nr_objects
)
582 uint32_t cur_fanout
, cur_pack
, cur_object
;
583 uint32_t alloc_fanout
, alloc_objects
, total_objects
= 0;
584 struct pack_midx_entry
*entries_by_fanout
= NULL
;
585 struct pack_midx_entry
*deduplicated_entries
= NULL
;
586 uint32_t start_pack
= m
? m
->num_packs
: 0;
588 for (cur_pack
= start_pack
; cur_pack
< nr_packs
; cur_pack
++)
589 total_objects
+= p
[cur_pack
]->num_objects
;
592 * As we de-duplicate by fanout value, we expect the fanout
593 * slices to be evenly distributed, with some noise. Hence,
594 * allocate slightly more than one 256th.
596 alloc_objects
= alloc_fanout
= total_objects
> 3200 ? total_objects
/ 200 : 16;
598 ALLOC_ARRAY(entries_by_fanout
, alloc_fanout
);
599 ALLOC_ARRAY(deduplicated_entries
, alloc_objects
);
602 for (cur_fanout
= 0; cur_fanout
< 256; cur_fanout
++) {
603 uint32_t nr_fanout
= 0;
606 uint32_t start
= 0, end
;
609 start
= ntohl(m
->chunk_oid_fanout
[cur_fanout
- 1]);
610 end
= ntohl(m
->chunk_oid_fanout
[cur_fanout
]);
612 for (cur_object
= start
; cur_object
< end
; cur_object
++) {
613 ALLOC_GROW(entries_by_fanout
, nr_fanout
+ 1, alloc_fanout
);
614 nth_midxed_pack_midx_entry(m
, perm
,
615 &entries_by_fanout
[nr_fanout
],
621 for (cur_pack
= start_pack
; cur_pack
< nr_packs
; cur_pack
++) {
622 uint32_t start
= 0, end
;
625 start
= get_pack_fanout(p
[cur_pack
], cur_fanout
- 1);
626 end
= get_pack_fanout(p
[cur_pack
], cur_fanout
);
628 for (cur_object
= start
; cur_object
< end
; cur_object
++) {
629 ALLOC_GROW(entries_by_fanout
, nr_fanout
+ 1, alloc_fanout
);
630 fill_pack_entry(perm
[cur_pack
], p
[cur_pack
], cur_object
, &entries_by_fanout
[nr_fanout
]);
635 QSORT(entries_by_fanout
, nr_fanout
, midx_oid_compare
);
638 * The batch is now sorted by OID and then mtime (descending).
639 * Take only the first duplicate.
641 for (cur_object
= 0; cur_object
< nr_fanout
; cur_object
++) {
642 if (cur_object
&& oideq(&entries_by_fanout
[cur_object
- 1].oid
,
643 &entries_by_fanout
[cur_object
].oid
))
646 ALLOC_GROW(deduplicated_entries
, *nr_objects
+ 1, alloc_objects
);
647 memcpy(&deduplicated_entries
[*nr_objects
],
648 &entries_by_fanout
[cur_object
],
649 sizeof(struct pack_midx_entry
));
654 free(entries_by_fanout
);
655 return deduplicated_entries
;
658 static size_t write_midx_pack_names(struct hashfile
*f
,
663 unsigned char padding
[MIDX_CHUNK_ALIGNMENT
];
666 for (i
= 0; i
< num_packs
; i
++) {
667 size_t writelen
= strlen(pack_names
[i
]) + 1;
669 if (i
&& strcmp(pack_names
[i
], pack_names
[i
- 1]) <= 0)
670 BUG("incorrect pack-file order: %s before %s",
674 hashwrite(f
, pack_names
[i
], writelen
);
678 /* add padding to be aligned */
679 i
= MIDX_CHUNK_ALIGNMENT
- (written
% MIDX_CHUNK_ALIGNMENT
);
680 if (i
< MIDX_CHUNK_ALIGNMENT
) {
681 memset(padding
, 0, sizeof(padding
));
682 hashwrite(f
, padding
, i
);
689 static size_t write_midx_oid_fanout(struct hashfile
*f
,
690 struct pack_midx_entry
*objects
,
693 struct pack_midx_entry
*list
= objects
;
694 struct pack_midx_entry
*last
= objects
+ nr_objects
;
699 * Write the first-level table (the list is sorted,
700 * but we use a 256-entry lookup to be able to avoid
701 * having to do eight extra binary search iterations).
703 for (i
= 0; i
< 256; i
++) {
704 struct pack_midx_entry
*next
= list
;
706 while (next
< last
&& next
->oid
.hash
[0] == i
) {
711 hashwrite_be32(f
, count
);
715 return MIDX_CHUNK_FANOUT_SIZE
;
718 static size_t write_midx_oid_lookup(struct hashfile
*f
, unsigned char hash_len
,
719 struct pack_midx_entry
*objects
,
722 struct pack_midx_entry
*list
= objects
;
726 for (i
= 0; i
< nr_objects
; i
++) {
727 struct pack_midx_entry
*obj
= list
++;
729 if (i
< nr_objects
- 1) {
730 struct pack_midx_entry
*next
= list
;
731 if (oidcmp(&obj
->oid
, &next
->oid
) >= 0)
732 BUG("OIDs not in order: %s >= %s",
733 oid_to_hex(&obj
->oid
),
734 oid_to_hex(&next
->oid
));
737 hashwrite(f
, obj
->oid
.hash
, (int)hash_len
);
744 static size_t write_midx_object_offsets(struct hashfile
*f
, int large_offset_needed
,
745 struct pack_midx_entry
*objects
, uint32_t nr_objects
)
747 struct pack_midx_entry
*list
= objects
;
748 uint32_t i
, nr_large_offset
= 0;
751 for (i
= 0; i
< nr_objects
; i
++) {
752 struct pack_midx_entry
*obj
= list
++;
754 hashwrite_be32(f
, obj
->pack_int_id
);
756 if (large_offset_needed
&& obj
->offset
>> 31)
757 hashwrite_be32(f
, MIDX_LARGE_OFFSET_NEEDED
| nr_large_offset
++);
758 else if (!large_offset_needed
&& obj
->offset
>> 32)
759 BUG("object %s requires a large offset (%"PRIx64
") but the MIDX is not writing large offsets!",
760 oid_to_hex(&obj
->oid
),
763 hashwrite_be32(f
, (uint32_t)obj
->offset
);
765 written
+= MIDX_CHUNK_OFFSET_WIDTH
;
771 static size_t write_midx_large_offsets(struct hashfile
*f
, uint32_t nr_large_offset
,
772 struct pack_midx_entry
*objects
, uint32_t nr_objects
)
774 struct pack_midx_entry
*list
= objects
, *end
= objects
+ nr_objects
;
777 while (nr_large_offset
) {
778 struct pack_midx_entry
*obj
;
782 BUG("too many large-offset objects");
785 offset
= obj
->offset
;
790 hashwrite_be32(f
, offset
>> 32);
791 hashwrite_be32(f
, offset
& 0xffffffffUL
);
792 written
+= 2 * sizeof(uint32_t);
800 int write_midx_file(const char *object_dir
)
802 unsigned char cur_chunk
, num_chunks
= 0;
805 struct hashfile
*f
= NULL
;
807 struct pack_list packs
;
808 uint32_t *pack_perm
= NULL
;
809 uint64_t written
= 0;
810 uint32_t chunk_ids
[MIDX_MAX_CHUNKS
+ 1];
811 uint64_t chunk_offsets
[MIDX_MAX_CHUNKS
+ 1];
812 uint32_t nr_entries
, num_large_offsets
= 0;
813 struct pack_midx_entry
*entries
= NULL
;
814 int large_offsets_needed
= 0;
816 midx_name
= get_midx_filename(object_dir
);
817 if (safe_create_leading_directories(midx_name
)) {
819 die_errno(_("unable to create leading directories of %s"),
823 packs
.m
= load_multi_pack_index(object_dir
, 1);
826 packs
.alloc_list
= packs
.m
? packs
.m
->num_packs
: 16;
827 packs
.alloc_names
= packs
.alloc_list
;
830 packs
.pack_name_concat_len
= 0;
831 ALLOC_ARRAY(packs
.list
, packs
.alloc_list
);
832 ALLOC_ARRAY(packs
.names
, packs
.alloc_names
);
835 for (i
= 0; i
< packs
.m
->num_packs
; i
++) {
836 ALLOC_GROW(packs
.list
, packs
.nr
+ 1, packs
.alloc_list
);
837 ALLOC_GROW(packs
.names
, packs
.nr
+ 1, packs
.alloc_names
);
839 packs
.list
[packs
.nr
] = NULL
;
840 packs
.names
[packs
.nr
] = xstrdup(packs
.m
->pack_names
[i
]);
841 packs
.pack_name_concat_len
+= strlen(packs
.names
[packs
.nr
]) + 1;
846 for_each_file_in_pack_dir(object_dir
, add_pack_to_midx
, &packs
);
848 if (packs
.m
&& packs
.nr
== packs
.m
->num_packs
)
851 if (packs
.pack_name_concat_len
% MIDX_CHUNK_ALIGNMENT
)
852 packs
.pack_name_concat_len
+= MIDX_CHUNK_ALIGNMENT
-
853 (packs
.pack_name_concat_len
% MIDX_CHUNK_ALIGNMENT
);
855 ALLOC_ARRAY(pack_perm
, packs
.nr
);
856 sort_packs_by_name(packs
.names
, packs
.nr
, pack_perm
);
858 entries
= get_sorted_entries(packs
.m
, packs
.list
, pack_perm
, packs
.nr
, &nr_entries
);
860 for (i
= 0; i
< nr_entries
; i
++) {
861 if (entries
[i
].offset
> 0x7fffffff)
863 if (entries
[i
].offset
> 0xffffffff)
864 large_offsets_needed
= 1;
867 hold_lock_file_for_update(&lk
, midx_name
, LOCK_DIE_ON_ERROR
);
868 f
= hashfd(lk
.tempfile
->fd
, lk
.tempfile
->filename
.buf
);
869 FREE_AND_NULL(midx_name
);
875 num_chunks
= large_offsets_needed
? 5 : 4;
877 written
= write_midx_header(f
, num_chunks
, packs
.nr
);
879 chunk_ids
[cur_chunk
] = MIDX_CHUNKID_PACKNAMES
;
880 chunk_offsets
[cur_chunk
] = written
+ (num_chunks
+ 1) * MIDX_CHUNKLOOKUP_WIDTH
;
883 chunk_ids
[cur_chunk
] = MIDX_CHUNKID_OIDFANOUT
;
884 chunk_offsets
[cur_chunk
] = chunk_offsets
[cur_chunk
- 1] + packs
.pack_name_concat_len
;
887 chunk_ids
[cur_chunk
] = MIDX_CHUNKID_OIDLOOKUP
;
888 chunk_offsets
[cur_chunk
] = chunk_offsets
[cur_chunk
- 1] + MIDX_CHUNK_FANOUT_SIZE
;
891 chunk_ids
[cur_chunk
] = MIDX_CHUNKID_OBJECTOFFSETS
;
892 chunk_offsets
[cur_chunk
] = chunk_offsets
[cur_chunk
- 1] + nr_entries
* MIDX_HASH_LEN
;
895 chunk_offsets
[cur_chunk
] = chunk_offsets
[cur_chunk
- 1] + nr_entries
* MIDX_CHUNK_OFFSET_WIDTH
;
896 if (large_offsets_needed
) {
897 chunk_ids
[cur_chunk
] = MIDX_CHUNKID_LARGEOFFSETS
;
900 chunk_offsets
[cur_chunk
] = chunk_offsets
[cur_chunk
- 1] +
901 num_large_offsets
* MIDX_CHUNK_LARGE_OFFSET_WIDTH
;
904 chunk_ids
[cur_chunk
] = 0;
906 for (i
= 0; i
<= num_chunks
; i
++) {
907 if (i
&& chunk_offsets
[i
] < chunk_offsets
[i
- 1])
908 BUG("incorrect chunk offsets: %"PRIu64
" before %"PRIu64
,
909 chunk_offsets
[i
- 1],
912 if (chunk_offsets
[i
] % MIDX_CHUNK_ALIGNMENT
)
913 BUG("chunk offset %"PRIu64
" is not properly aligned",
916 hashwrite_be32(f
, chunk_ids
[i
]);
917 hashwrite_be32(f
, chunk_offsets
[i
] >> 32);
918 hashwrite_be32(f
, chunk_offsets
[i
]);
920 written
+= MIDX_CHUNKLOOKUP_WIDTH
;
923 for (i
= 0; i
< num_chunks
; i
++) {
924 if (written
!= chunk_offsets
[i
])
925 BUG("incorrect chunk offset (%"PRIu64
" != %"PRIu64
") for chunk id %"PRIx32
,
930 switch (chunk_ids
[i
]) {
931 case MIDX_CHUNKID_PACKNAMES
:
932 written
+= write_midx_pack_names(f
, packs
.names
, packs
.nr
);
935 case MIDX_CHUNKID_OIDFANOUT
:
936 written
+= write_midx_oid_fanout(f
, entries
, nr_entries
);
939 case MIDX_CHUNKID_OIDLOOKUP
:
940 written
+= write_midx_oid_lookup(f
, MIDX_HASH_LEN
, entries
, nr_entries
);
943 case MIDX_CHUNKID_OBJECTOFFSETS
:
944 written
+= write_midx_object_offsets(f
, large_offsets_needed
, entries
, nr_entries
);
947 case MIDX_CHUNKID_LARGEOFFSETS
:
948 written
+= write_midx_large_offsets(f
, num_large_offsets
, entries
, nr_entries
);
952 BUG("trying to write unknown chunk id %"PRIx32
,
957 if (written
!= chunk_offsets
[num_chunks
])
958 BUG("incorrect final offset %"PRIu64
" != %"PRIu64
,
960 chunk_offsets
[num_chunks
]);
962 finalize_hashfile(f
, NULL
, CSUM_FSYNC
| CSUM_HASH_IN_STREAM
);
963 commit_lock_file(&lk
);
966 for (i
= 0; i
< packs
.nr
; i
++) {
968 close_pack(packs
.list
[i
]);
971 free(packs
.names
[i
]);
982 void clear_midx_file(struct repository
*r
)
984 char *midx
= get_midx_filename(r
->objects
->odb
->path
);
986 if (r
->objects
&& r
->objects
->multi_pack_index
) {
987 close_midx(r
->objects
->multi_pack_index
);
988 r
->objects
->multi_pack_index
= NULL
;
991 if (remove_path(midx
)) {
993 die(_("failed to clear multi-pack-index at %s"), midx
);
999 static int verify_midx_error
;
1001 static void midx_report(const char *fmt
, ...)
1004 verify_midx_error
= 1;
1006 vfprintf(stderr
, fmt
, ap
);
1007 fprintf(stderr
, "\n");
1011 struct pair_pos_vs_id
1014 uint32_t pack_int_id
;
1017 static int compare_pair_pos_vs_id(const void *_a
, const void *_b
)
1019 struct pair_pos_vs_id
*a
= (struct pair_pos_vs_id
*)_a
;
1020 struct pair_pos_vs_id
*b
= (struct pair_pos_vs_id
*)_b
;
1022 return b
->pack_int_id
- a
->pack_int_id
;
1026 * Limit calls to display_progress() for performance reasons.
1027 * The interval here was arbitrarily chosen.
1029 #define SPARSE_PROGRESS_INTERVAL (1 << 12)
1030 #define midx_display_sparse_progress(progress, n) \
1032 uint64_t _n = (n); \
1033 if ((_n & (SPARSE_PROGRESS_INTERVAL - 1)) == 0) \
1034 display_progress(progress, _n); \
1037 int verify_midx_file(struct repository
*r
, const char *object_dir
)
1039 struct pair_pos_vs_id
*pairs
= NULL
;
1041 struct progress
*progress
;
1042 struct multi_pack_index
*m
= load_multi_pack_index(object_dir
, 1);
1043 verify_midx_error
= 0;
1048 progress
= start_progress(_("Looking for referenced packfiles"),
1050 for (i
= 0; i
< m
->num_packs
; i
++) {
1051 if (prepare_midx_pack(r
, m
, i
))
1052 midx_report("failed to load pack in position %d", i
);
1054 display_progress(progress
, i
+ 1);
1056 stop_progress(&progress
);
1058 for (i
= 0; i
< 255; i
++) {
1059 uint32_t oid_fanout1
= ntohl(m
->chunk_oid_fanout
[i
]);
1060 uint32_t oid_fanout2
= ntohl(m
->chunk_oid_fanout
[i
+ 1]);
1062 if (oid_fanout1
> oid_fanout2
)
1063 midx_report(_("oid fanout out of order: fanout[%d] = %"PRIx32
" > %"PRIx32
" = fanout[%d]"),
1064 i
, oid_fanout1
, oid_fanout2
, i
+ 1);
1067 progress
= start_sparse_progress(_("Verifying OID order in MIDX"),
1068 m
->num_objects
- 1);
1069 for (i
= 0; i
< m
->num_objects
- 1; i
++) {
1070 struct object_id oid1
, oid2
;
1072 nth_midxed_object_oid(&oid1
, m
, i
);
1073 nth_midxed_object_oid(&oid2
, m
, i
+ 1);
1075 if (oidcmp(&oid1
, &oid2
) >= 0)
1076 midx_report(_("oid lookup out of order: oid[%d] = %s >= %s = oid[%d]"),
1077 i
, oid_to_hex(&oid1
), oid_to_hex(&oid2
), i
+ 1);
1079 midx_display_sparse_progress(progress
, i
+ 1);
1081 stop_progress(&progress
);
1084 * Create an array mapping each object to its packfile id. Sort it
1085 * to group the objects by packfile. Use this permutation to visit
1086 * each of the objects and only require 1 packfile to be open at a
1089 ALLOC_ARRAY(pairs
, m
->num_objects
);
1090 for (i
= 0; i
< m
->num_objects
; i
++) {
1092 pairs
[i
].pack_int_id
= nth_midxed_pack_int_id(m
, i
);
1095 progress
= start_sparse_progress(_("Sorting objects by packfile"),
1097 display_progress(progress
, 0); /* TODO: Measure QSORT() progress */
1098 QSORT(pairs
, m
->num_objects
, compare_pair_pos_vs_id
);
1099 stop_progress(&progress
);
1101 progress
= start_sparse_progress(_("Verifying object offsets"), m
->num_objects
);
1102 for (i
= 0; i
< m
->num_objects
; i
++) {
1103 struct object_id oid
;
1104 struct pack_entry e
;
1105 off_t m_offset
, p_offset
;
1107 if (i
> 0 && pairs
[i
-1].pack_int_id
!= pairs
[i
].pack_int_id
&&
1108 m
->packs
[pairs
[i
-1].pack_int_id
])
1110 close_pack_fd(m
->packs
[pairs
[i
-1].pack_int_id
]);
1111 close_pack_index(m
->packs
[pairs
[i
-1].pack_int_id
]);
1114 nth_midxed_object_oid(&oid
, m
, pairs
[i
].pos
);
1116 if (!fill_midx_entry(r
, &oid
, &e
, m
)) {
1117 midx_report(_("failed to load pack entry for oid[%d] = %s"),
1118 pairs
[i
].pos
, oid_to_hex(&oid
));
1122 if (open_pack_index(e
.p
)) {
1123 midx_report(_("failed to load pack-index for packfile %s"),
1128 m_offset
= e
.offset
;
1129 p_offset
= find_pack_entry_one(oid
.hash
, e
.p
);
1131 if (m_offset
!= p_offset
)
1132 midx_report(_("incorrect object offset for oid[%d] = %s: %"PRIx64
" != %"PRIx64
),
1133 pairs
[i
].pos
, oid_to_hex(&oid
), m_offset
, p_offset
);
1135 midx_display_sparse_progress(progress
, i
+ 1);
1137 stop_progress(&progress
);
1141 return verify_midx_error
;