7 #include "object-store.h"
8 #include "sha1-lookup.h"
12 #include "run-command.h"
13 #include "repository.h"
15 #define MIDX_SIGNATURE 0x4d494458 /* "MIDX" */
16 #define MIDX_VERSION 1
17 #define MIDX_BYTE_FILE_VERSION 4
18 #define MIDX_BYTE_HASH_VERSION 5
19 #define MIDX_BYTE_NUM_CHUNKS 6
20 #define MIDX_BYTE_NUM_PACKS 8
21 #define MIDX_HEADER_SIZE 12
22 #define MIDX_MIN_SIZE (MIDX_HEADER_SIZE + the_hash_algo->rawsz)
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 #define PACK_EXPIRED UINT_MAX
39 static uint8_t oid_version(void)
41 switch (hash_algo_by_ptr(the_hash_algo
)) {
47 die(_("invalid hash version"));
51 static char *get_midx_filename(const char *object_dir
)
53 return xstrfmt("%s/pack/multi-pack-index", object_dir
);
56 struct multi_pack_index
*load_multi_pack_index(const char *object_dir
, int local
)
58 struct multi_pack_index
*m
= NULL
;
62 void *midx_map
= NULL
;
63 uint32_t hash_version
;
64 char *midx_name
= get_midx_filename(object_dir
);
66 const char *cur_pack_name
;
68 fd
= git_open(midx_name
);
73 error_errno(_("failed to read %s"), midx_name
);
77 midx_size
= xsize_t(st
.st_size
);
79 if (midx_size
< MIDX_MIN_SIZE
) {
80 error(_("multi-pack-index file %s is too small"), midx_name
);
84 FREE_AND_NULL(midx_name
);
86 midx_map
= xmmap(NULL
, midx_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
89 FLEX_ALLOC_STR(m
, object_dir
, object_dir
);
91 m
->data_len
= midx_size
;
94 m
->signature
= get_be32(m
->data
);
95 if (m
->signature
!= MIDX_SIGNATURE
)
96 die(_("multi-pack-index signature 0x%08x does not match signature 0x%08x"),
97 m
->signature
, MIDX_SIGNATURE
);
99 m
->version
= m
->data
[MIDX_BYTE_FILE_VERSION
];
100 if (m
->version
!= MIDX_VERSION
)
101 die(_("multi-pack-index version %d not recognized"),
104 hash_version
= m
->data
[MIDX_BYTE_HASH_VERSION
];
105 if (hash_version
!= oid_version()) {
106 error(_("multi-pack-index hash version %u does not match version %u"),
107 hash_version
, oid_version());
110 m
->hash_len
= the_hash_algo
->rawsz
;
112 m
->num_chunks
= m
->data
[MIDX_BYTE_NUM_CHUNKS
];
114 m
->num_packs
= get_be32(m
->data
+ MIDX_BYTE_NUM_PACKS
);
116 for (i
= 0; i
< m
->num_chunks
; i
++) {
117 uint32_t chunk_id
= get_be32(m
->data
+ MIDX_HEADER_SIZE
+
118 MIDX_CHUNKLOOKUP_WIDTH
* i
);
119 uint64_t chunk_offset
= get_be64(m
->data
+ MIDX_HEADER_SIZE
+ 4 +
120 MIDX_CHUNKLOOKUP_WIDTH
* i
);
122 if (chunk_offset
>= m
->data_len
)
123 die(_("invalid chunk offset (too large)"));
126 case MIDX_CHUNKID_PACKNAMES
:
127 m
->chunk_pack_names
= m
->data
+ chunk_offset
;
130 case MIDX_CHUNKID_OIDFANOUT
:
131 m
->chunk_oid_fanout
= (uint32_t *)(m
->data
+ chunk_offset
);
134 case MIDX_CHUNKID_OIDLOOKUP
:
135 m
->chunk_oid_lookup
= m
->data
+ chunk_offset
;
138 case MIDX_CHUNKID_OBJECTOFFSETS
:
139 m
->chunk_object_offsets
= m
->data
+ chunk_offset
;
142 case MIDX_CHUNKID_LARGEOFFSETS
:
143 m
->chunk_large_offsets
= m
->data
+ chunk_offset
;
147 die(_("terminating multi-pack-index chunk id appears earlier than expected"));
152 * Do nothing on unrecognized chunks, allowing future
153 * extensions to add optional chunks.
159 if (!m
->chunk_pack_names
)
160 die(_("multi-pack-index missing required pack-name chunk"));
161 if (!m
->chunk_oid_fanout
)
162 die(_("multi-pack-index missing required OID fanout chunk"));
163 if (!m
->chunk_oid_lookup
)
164 die(_("multi-pack-index missing required OID lookup chunk"));
165 if (!m
->chunk_object_offsets
)
166 die(_("multi-pack-index missing required object offsets chunk"));
168 m
->num_objects
= ntohl(m
->chunk_oid_fanout
[255]);
170 m
->pack_names
= xcalloc(m
->num_packs
, sizeof(*m
->pack_names
));
171 m
->packs
= xcalloc(m
->num_packs
, sizeof(*m
->packs
));
173 cur_pack_name
= (const char *)m
->chunk_pack_names
;
174 for (i
= 0; i
< m
->num_packs
; i
++) {
175 m
->pack_names
[i
] = cur_pack_name
;
177 cur_pack_name
+= strlen(cur_pack_name
) + 1;
179 if (i
&& strcmp(m
->pack_names
[i
], m
->pack_names
[i
- 1]) <= 0)
180 die(_("multi-pack-index pack names out of order: '%s' before '%s'"),
181 m
->pack_names
[i
- 1],
185 trace2_data_intmax("midx", the_repository
, "load/num_packs", m
->num_packs
);
186 trace2_data_intmax("midx", the_repository
, "load/num_objects", m
->num_objects
);
194 munmap(midx_map
, midx_size
);
200 void close_midx(struct multi_pack_index
*m
)
207 munmap((unsigned char *)m
->data
, m
->data_len
);
209 for (i
= 0; i
< m
->num_packs
; i
++) {
211 m
->packs
[i
]->multi_pack_index
= 0;
213 FREE_AND_NULL(m
->packs
);
214 FREE_AND_NULL(m
->pack_names
);
217 int prepare_midx_pack(struct repository
*r
, struct multi_pack_index
*m
, uint32_t pack_int_id
)
219 struct strbuf pack_name
= STRBUF_INIT
;
220 struct packed_git
*p
;
222 if (pack_int_id
>= m
->num_packs
)
223 die(_("bad pack-int-id: %u (%u total packs)"),
224 pack_int_id
, m
->num_packs
);
226 if (m
->packs
[pack_int_id
])
229 strbuf_addf(&pack_name
, "%s/pack/%s", m
->object_dir
,
230 m
->pack_names
[pack_int_id
]);
232 p
= add_packed_git(pack_name
.buf
, pack_name
.len
, m
->local
);
233 strbuf_release(&pack_name
);
238 p
->multi_pack_index
= 1;
239 m
->packs
[pack_int_id
] = p
;
240 install_packed_git(r
, p
);
241 list_add_tail(&p
->mru
, &r
->objects
->packed_git_mru
);
246 int bsearch_midx(const struct object_id
*oid
, struct multi_pack_index
*m
, uint32_t *result
)
248 return bsearch_hash(oid
->hash
, m
->chunk_oid_fanout
, m
->chunk_oid_lookup
,
249 the_hash_algo
->rawsz
, result
);
252 struct object_id
*nth_midxed_object_oid(struct object_id
*oid
,
253 struct multi_pack_index
*m
,
256 if (n
>= m
->num_objects
)
259 hashcpy(oid
->hash
, m
->chunk_oid_lookup
+ m
->hash_len
* n
);
263 static off_t
nth_midxed_offset(struct multi_pack_index
*m
, uint32_t pos
)
265 const unsigned char *offset_data
;
268 offset_data
= m
->chunk_object_offsets
+ pos
* MIDX_CHUNK_OFFSET_WIDTH
;
269 offset32
= get_be32(offset_data
+ sizeof(uint32_t));
271 if (m
->chunk_large_offsets
&& offset32
& MIDX_LARGE_OFFSET_NEEDED
) {
272 if (sizeof(off_t
) < sizeof(uint64_t))
273 die(_("multi-pack-index stores a 64-bit offset, but off_t is too small"));
275 offset32
^= MIDX_LARGE_OFFSET_NEEDED
;
276 return get_be64(m
->chunk_large_offsets
+ sizeof(uint64_t) * offset32
);
282 static uint32_t nth_midxed_pack_int_id(struct multi_pack_index
*m
, uint32_t pos
)
284 return get_be32(m
->chunk_object_offsets
+ pos
* MIDX_CHUNK_OFFSET_WIDTH
);
287 static int nth_midxed_pack_entry(struct repository
*r
,
288 struct multi_pack_index
*m
,
289 struct pack_entry
*e
,
292 uint32_t pack_int_id
;
293 struct packed_git
*p
;
295 if (pos
>= m
->num_objects
)
298 pack_int_id
= nth_midxed_pack_int_id(m
, pos
);
300 if (prepare_midx_pack(r
, m
, pack_int_id
))
302 p
= m
->packs
[pack_int_id
];
305 * We are about to tell the caller where they can locate the
306 * requested object. We better make sure the packfile is
307 * still here and can be accessed before supplying that
308 * answer, as it may have been deleted since the MIDX was
311 if (!is_pack_valid(p
))
314 if (p
->num_bad_objects
) {
316 struct object_id oid
;
317 nth_midxed_object_oid(&oid
, m
, pos
);
318 for (i
= 0; i
< p
->num_bad_objects
; i
++)
320 p
->bad_object_sha1
+ the_hash_algo
->rawsz
* i
))
324 e
->offset
= nth_midxed_offset(m
, pos
);
330 int fill_midx_entry(struct repository
* r
,
331 const struct object_id
*oid
,
332 struct pack_entry
*e
,
333 struct multi_pack_index
*m
)
337 if (!bsearch_midx(oid
, m
, &pos
))
340 return nth_midxed_pack_entry(r
, m
, e
, pos
);
343 /* Match "foo.idx" against either "foo.pack" _or_ "foo.idx". */
344 static int cmp_idx_or_pack_name(const char *idx_or_pack_name
,
345 const char *idx_name
)
347 /* Skip past any initial matching prefix. */
348 while (*idx_name
&& *idx_name
== *idx_or_pack_name
) {
354 * If we didn't match completely, we may have matched "pack-1234." and
355 * be left with "idx" and "pack" respectively, which is also OK. We do
356 * not have to check for "idx" and "idx", because that would have been
357 * a complete match (and in that case these strcmps will be false, but
358 * we'll correctly return 0 from the final strcmp() below.
360 * Technically this matches "fooidx" and "foopack", but we'd never have
361 * such names in the first place.
363 if (!strcmp(idx_name
, "idx") && !strcmp(idx_or_pack_name
, "pack"))
367 * This not only checks for a complete match, but also orders based on
368 * the first non-identical character, which means our ordering will
369 * match a raw strcmp(). That makes it OK to use this to binary search
370 * a naively-sorted list.
372 return strcmp(idx_or_pack_name
, idx_name
);
375 int midx_contains_pack(struct multi_pack_index
*m
, const char *idx_or_pack_name
)
377 uint32_t first
= 0, last
= m
->num_packs
;
379 while (first
< last
) {
380 uint32_t mid
= first
+ (last
- first
) / 2;
384 current
= m
->pack_names
[mid
];
385 cmp
= cmp_idx_or_pack_name(idx_or_pack_name
, current
);
398 int prepare_multi_pack_index_one(struct repository
*r
, const char *object_dir
, int local
)
400 struct multi_pack_index
*m
;
401 struct multi_pack_index
*m_search
;
403 prepare_repo_settings(r
);
404 if (!r
->settings
.core_multi_pack_index
)
407 for (m_search
= r
->objects
->multi_pack_index
; m_search
; m_search
= m_search
->next
)
408 if (!strcmp(object_dir
, m_search
->object_dir
))
411 m
= load_multi_pack_index(object_dir
, local
);
414 struct multi_pack_index
*mp
= r
->objects
->multi_pack_index
;
419 r
->objects
->multi_pack_index
= m
;
426 static size_t write_midx_header(struct hashfile
*f
,
427 unsigned char num_chunks
,
430 hashwrite_be32(f
, MIDX_SIGNATURE
);
431 hashwrite_u8(f
, MIDX_VERSION
);
432 hashwrite_u8(f
, oid_version());
433 hashwrite_u8(f
, num_chunks
);
434 hashwrite_u8(f
, 0); /* unused */
435 hashwrite_be32(f
, num_packs
);
437 return MIDX_HEADER_SIZE
;
441 uint32_t orig_pack_int_id
;
443 struct packed_git
*p
;
444 unsigned expired
: 1;
447 static int pack_info_compare(const void *_a
, const void *_b
)
449 struct pack_info
*a
= (struct pack_info
*)_a
;
450 struct pack_info
*b
= (struct pack_info
*)_b
;
451 return strcmp(a
->pack_name
, b
->pack_name
);
454 struct write_midx_context
{
455 struct pack_info
*info
;
458 struct multi_pack_index
*m
;
459 struct progress
*progress
;
460 unsigned pack_paths_checked
;
462 struct pack_midx_entry
*entries
;
466 unsigned large_offsets_needed
:1;
467 uint32_t num_large_offsets
;
470 static void add_pack_to_midx(const char *full_path
, size_t full_path_len
,
471 const char *file_name
, void *data
)
473 struct write_midx_context
*ctx
= data
;
475 if (ends_with(file_name
, ".idx")) {
476 display_progress(ctx
->progress
, ++ctx
->pack_paths_checked
);
477 if (ctx
->m
&& midx_contains_pack(ctx
->m
, file_name
))
480 ALLOC_GROW(ctx
->info
, ctx
->nr
+ 1, ctx
->alloc
);
482 ctx
->info
[ctx
->nr
].p
= add_packed_git(full_path
,
486 if (!ctx
->info
[ctx
->nr
].p
) {
487 warning(_("failed to add packfile '%s'"),
492 if (open_pack_index(ctx
->info
[ctx
->nr
].p
)) {
493 warning(_("failed to open pack-index '%s'"),
495 close_pack(ctx
->info
[ctx
->nr
].p
);
496 FREE_AND_NULL(ctx
->info
[ctx
->nr
].p
);
500 ctx
->info
[ctx
->nr
].pack_name
= xstrdup(file_name
);
501 ctx
->info
[ctx
->nr
].orig_pack_int_id
= ctx
->nr
;
502 ctx
->info
[ctx
->nr
].expired
= 0;
507 struct pack_midx_entry
{
508 struct object_id oid
;
509 uint32_t pack_int_id
;
514 static int midx_oid_compare(const void *_a
, const void *_b
)
516 const struct pack_midx_entry
*a
= (const struct pack_midx_entry
*)_a
;
517 const struct pack_midx_entry
*b
= (const struct pack_midx_entry
*)_b
;
518 int cmp
= oidcmp(&a
->oid
, &b
->oid
);
523 if (a
->pack_mtime
> b
->pack_mtime
)
525 else if (a
->pack_mtime
< b
->pack_mtime
)
528 return a
->pack_int_id
- b
->pack_int_id
;
531 static int nth_midxed_pack_midx_entry(struct multi_pack_index
*m
,
532 struct pack_midx_entry
*e
,
535 if (pos
>= m
->num_objects
)
538 nth_midxed_object_oid(&e
->oid
, m
, pos
);
539 e
->pack_int_id
= nth_midxed_pack_int_id(m
, pos
);
540 e
->offset
= nth_midxed_offset(m
, pos
);
542 /* consider objects in midx to be from "old" packs */
547 static void fill_pack_entry(uint32_t pack_int_id
,
548 struct packed_git
*p
,
550 struct pack_midx_entry
*entry
)
552 if (nth_packed_object_id(&entry
->oid
, p
, cur_object
) < 0)
553 die(_("failed to locate object %d in packfile"), cur_object
);
555 entry
->pack_int_id
= pack_int_id
;
556 entry
->pack_mtime
= p
->mtime
;
558 entry
->offset
= nth_packed_object_offset(p
, cur_object
);
562 * It is possible to artificially get into a state where there are many
563 * duplicate copies of objects. That can create high memory pressure if
564 * we are to create a list of all objects before de-duplication. To reduce
565 * this memory pressure without a significant performance drop, automatically
566 * group objects by the first byte of their object id. Use the IDX fanout
567 * tables to group the data, copy to a local array, then sort.
569 * Copy only the de-duplicated entries (selected by most-recent modified time
570 * of a packfile containing the object).
572 static struct pack_midx_entry
*get_sorted_entries(struct multi_pack_index
*m
,
573 struct pack_info
*info
,
575 uint32_t *nr_objects
)
577 uint32_t cur_fanout
, cur_pack
, cur_object
;
578 uint32_t alloc_fanout
, alloc_objects
, total_objects
= 0;
579 struct pack_midx_entry
*entries_by_fanout
= NULL
;
580 struct pack_midx_entry
*deduplicated_entries
= NULL
;
581 uint32_t start_pack
= m
? m
->num_packs
: 0;
583 for (cur_pack
= start_pack
; cur_pack
< nr_packs
; cur_pack
++)
584 total_objects
+= info
[cur_pack
].p
->num_objects
;
587 * As we de-duplicate by fanout value, we expect the fanout
588 * slices to be evenly distributed, with some noise. Hence,
589 * allocate slightly more than one 256th.
591 alloc_objects
= alloc_fanout
= total_objects
> 3200 ? total_objects
/ 200 : 16;
593 ALLOC_ARRAY(entries_by_fanout
, alloc_fanout
);
594 ALLOC_ARRAY(deduplicated_entries
, alloc_objects
);
597 for (cur_fanout
= 0; cur_fanout
< 256; cur_fanout
++) {
598 uint32_t nr_fanout
= 0;
601 uint32_t start
= 0, end
;
604 start
= ntohl(m
->chunk_oid_fanout
[cur_fanout
- 1]);
605 end
= ntohl(m
->chunk_oid_fanout
[cur_fanout
]);
607 for (cur_object
= start
; cur_object
< end
; cur_object
++) {
608 ALLOC_GROW(entries_by_fanout
, nr_fanout
+ 1, alloc_fanout
);
609 nth_midxed_pack_midx_entry(m
,
610 &entries_by_fanout
[nr_fanout
],
616 for (cur_pack
= start_pack
; cur_pack
< nr_packs
; cur_pack
++) {
617 uint32_t start
= 0, end
;
620 start
= get_pack_fanout(info
[cur_pack
].p
, cur_fanout
- 1);
621 end
= get_pack_fanout(info
[cur_pack
].p
, cur_fanout
);
623 for (cur_object
= start
; cur_object
< end
; cur_object
++) {
624 ALLOC_GROW(entries_by_fanout
, nr_fanout
+ 1, alloc_fanout
);
625 fill_pack_entry(cur_pack
, info
[cur_pack
].p
, cur_object
, &entries_by_fanout
[nr_fanout
]);
630 QSORT(entries_by_fanout
, nr_fanout
, midx_oid_compare
);
633 * The batch is now sorted by OID and then mtime (descending).
634 * Take only the first duplicate.
636 for (cur_object
= 0; cur_object
< nr_fanout
; cur_object
++) {
637 if (cur_object
&& oideq(&entries_by_fanout
[cur_object
- 1].oid
,
638 &entries_by_fanout
[cur_object
].oid
))
641 ALLOC_GROW(deduplicated_entries
, *nr_objects
+ 1, alloc_objects
);
642 memcpy(&deduplicated_entries
[*nr_objects
],
643 &entries_by_fanout
[cur_object
],
644 sizeof(struct pack_midx_entry
));
649 free(entries_by_fanout
);
650 return deduplicated_entries
;
653 static size_t write_midx_pack_names(struct hashfile
*f
, void *data
)
655 struct write_midx_context
*ctx
= data
;
657 unsigned char padding
[MIDX_CHUNK_ALIGNMENT
];
660 for (i
= 0; i
< ctx
->nr
; i
++) {
663 if (ctx
->info
[i
].expired
)
666 if (i
&& strcmp(ctx
->info
[i
].pack_name
, ctx
->info
[i
- 1].pack_name
) <= 0)
667 BUG("incorrect pack-file order: %s before %s",
668 ctx
->info
[i
- 1].pack_name
,
669 ctx
->info
[i
].pack_name
);
671 writelen
= strlen(ctx
->info
[i
].pack_name
) + 1;
672 hashwrite(f
, ctx
->info
[i
].pack_name
, writelen
);
676 /* add padding to be aligned */
677 i
= MIDX_CHUNK_ALIGNMENT
- (written
% MIDX_CHUNK_ALIGNMENT
);
678 if (i
< MIDX_CHUNK_ALIGNMENT
) {
679 memset(padding
, 0, sizeof(padding
));
680 hashwrite(f
, padding
, i
);
687 static size_t write_midx_oid_fanout(struct hashfile
*f
,
690 struct write_midx_context
*ctx
= data
;
691 struct pack_midx_entry
*list
= ctx
->entries
;
692 struct pack_midx_entry
*last
= ctx
->entries
+ ctx
->entries_nr
;
697 * Write the first-level table (the list is sorted,
698 * but we use a 256-entry lookup to be able to avoid
699 * having to do eight extra binary search iterations).
701 for (i
= 0; i
< 256; i
++) {
702 struct pack_midx_entry
*next
= list
;
704 while (next
< last
&& next
->oid
.hash
[0] == i
) {
709 hashwrite_be32(f
, count
);
713 return MIDX_CHUNK_FANOUT_SIZE
;
716 static size_t write_midx_oid_lookup(struct hashfile
*f
,
719 struct write_midx_context
*ctx
= data
;
720 unsigned char hash_len
= the_hash_algo
->rawsz
;
721 struct pack_midx_entry
*list
= ctx
->entries
;
725 for (i
= 0; i
< ctx
->entries_nr
; i
++) {
726 struct pack_midx_entry
*obj
= list
++;
728 if (i
< ctx
->entries_nr
- 1) {
729 struct pack_midx_entry
*next
= list
;
730 if (oidcmp(&obj
->oid
, &next
->oid
) >= 0)
731 BUG("OIDs not in order: %s >= %s",
732 oid_to_hex(&obj
->oid
),
733 oid_to_hex(&next
->oid
));
736 hashwrite(f
, obj
->oid
.hash
, (int)hash_len
);
743 static size_t write_midx_object_offsets(struct hashfile
*f
,
746 struct write_midx_context
*ctx
= data
;
747 struct pack_midx_entry
*list
= ctx
->entries
;
748 uint32_t i
, nr_large_offset
= 0;
751 for (i
= 0; i
< ctx
->entries_nr
; i
++) {
752 struct pack_midx_entry
*obj
= list
++;
754 if (ctx
->pack_perm
[obj
->pack_int_id
] == PACK_EXPIRED
)
755 BUG("object %s is in an expired pack with int-id %d",
756 oid_to_hex(&obj
->oid
),
759 hashwrite_be32(f
, ctx
->pack_perm
[obj
->pack_int_id
]);
761 if (ctx
->large_offsets_needed
&& obj
->offset
>> 31)
762 hashwrite_be32(f
, MIDX_LARGE_OFFSET_NEEDED
| nr_large_offset
++);
763 else if (!ctx
->large_offsets_needed
&& obj
->offset
>> 32)
764 BUG("object %s requires a large offset (%"PRIx64
") but the MIDX is not writing large offsets!",
765 oid_to_hex(&obj
->oid
),
768 hashwrite_be32(f
, (uint32_t)obj
->offset
);
770 written
+= MIDX_CHUNK_OFFSET_WIDTH
;
776 static size_t write_midx_large_offsets(struct hashfile
*f
,
779 struct write_midx_context
*ctx
= data
;
780 struct pack_midx_entry
*list
= ctx
->entries
;
781 struct pack_midx_entry
*end
= ctx
->entries
+ ctx
->entries_nr
;
783 uint32_t nr_large_offset
= ctx
->num_large_offsets
;
785 while (nr_large_offset
) {
786 struct pack_midx_entry
*obj
;
790 BUG("too many large-offset objects");
793 offset
= obj
->offset
;
798 written
+= hashwrite_be64(f
, offset
);
806 static int write_midx_internal(const char *object_dir
, struct multi_pack_index
*m
,
807 struct string_list
*packs_to_drop
, unsigned flags
)
809 unsigned char cur_chunk
, num_chunks
= 0;
812 struct hashfile
*f
= NULL
;
814 struct write_midx_context ctx
= { 0 };
815 uint64_t written
= 0;
816 uint32_t chunk_ids
[MIDX_MAX_CHUNKS
+ 1];
817 uint64_t chunk_offsets
[MIDX_MAX_CHUNKS
+ 1];
818 struct progress
*progress
= NULL
;
819 int pack_name_concat_len
= 0;
820 int dropped_packs
= 0;
823 midx_name
= get_midx_filename(object_dir
);
824 if (safe_create_leading_directories(midx_name
))
825 die_errno(_("unable to create leading directories of %s"),
831 ctx
.m
= load_multi_pack_index(object_dir
, 1);
834 ctx
.alloc
= ctx
.m
? ctx
.m
->num_packs
: 16;
836 ALLOC_ARRAY(ctx
.info
, ctx
.alloc
);
839 for (i
= 0; i
< ctx
.m
->num_packs
; i
++) {
840 ALLOC_GROW(ctx
.info
, ctx
.nr
+ 1, ctx
.alloc
);
842 ctx
.info
[ctx
.nr
].orig_pack_int_id
= i
;
843 ctx
.info
[ctx
.nr
].pack_name
= xstrdup(ctx
.m
->pack_names
[i
]);
844 ctx
.info
[ctx
.nr
].p
= NULL
;
845 ctx
.info
[ctx
.nr
].expired
= 0;
850 ctx
.pack_paths_checked
= 0;
851 if (flags
& MIDX_PROGRESS
)
852 ctx
.progress
= start_delayed_progress(_("Adding packfiles to multi-pack-index"), 0);
856 for_each_file_in_pack_dir(object_dir
, add_pack_to_midx
, &ctx
);
857 stop_progress(&ctx
.progress
);
859 if (ctx
.m
&& ctx
.nr
== ctx
.m
->num_packs
&& !packs_to_drop
)
862 ctx
.entries
= get_sorted_entries(ctx
.m
, ctx
.info
, ctx
.nr
, &ctx
.entries_nr
);
864 ctx
.large_offsets_needed
= 0;
865 for (i
= 0; i
< ctx
.entries_nr
; i
++) {
866 if (ctx
.entries
[i
].offset
> 0x7fffffff)
867 ctx
.num_large_offsets
++;
868 if (ctx
.entries
[i
].offset
> 0xffffffff)
869 ctx
.large_offsets_needed
= 1;
872 QSORT(ctx
.info
, ctx
.nr
, pack_info_compare
);
874 if (packs_to_drop
&& packs_to_drop
->nr
) {
876 int missing_drops
= 0;
878 for (i
= 0; i
< ctx
.nr
&& drop_index
< packs_to_drop
->nr
; i
++) {
879 int cmp
= strcmp(ctx
.info
[i
].pack_name
,
880 packs_to_drop
->items
[drop_index
].string
);
884 ctx
.info
[i
].expired
= 1;
885 } else if (cmp
> 0) {
886 error(_("did not see pack-file %s to drop"),
887 packs_to_drop
->items
[drop_index
].string
);
892 ctx
.info
[i
].expired
= 0;
903 * pack_perm stores a permutation between pack-int-ids from the
904 * previous multi-pack-index to the new one we are writing:
906 * pack_perm[old_id] = new_id
908 ALLOC_ARRAY(ctx
.pack_perm
, ctx
.nr
);
909 for (i
= 0; i
< ctx
.nr
; i
++) {
910 if (ctx
.info
[i
].expired
) {
912 ctx
.pack_perm
[ctx
.info
[i
].orig_pack_int_id
] = PACK_EXPIRED
;
914 ctx
.pack_perm
[ctx
.info
[i
].orig_pack_int_id
] = i
- dropped_packs
;
918 for (i
= 0; i
< ctx
.nr
; i
++) {
919 if (!ctx
.info
[i
].expired
)
920 pack_name_concat_len
+= strlen(ctx
.info
[i
].pack_name
) + 1;
923 if (pack_name_concat_len
% MIDX_CHUNK_ALIGNMENT
)
924 pack_name_concat_len
+= MIDX_CHUNK_ALIGNMENT
-
925 (pack_name_concat_len
% MIDX_CHUNK_ALIGNMENT
);
927 hold_lock_file_for_update(&lk
, midx_name
, LOCK_DIE_ON_ERROR
);
928 f
= hashfd(lk
.tempfile
->fd
, lk
.tempfile
->filename
.buf
);
929 FREE_AND_NULL(midx_name
);
935 num_chunks
= ctx
.large_offsets_needed
? 5 : 4;
937 if (ctx
.nr
- dropped_packs
== 0) {
938 error(_("no pack files to index."));
943 written
= write_midx_header(f
, num_chunks
, ctx
.nr
- dropped_packs
);
945 chunk_ids
[cur_chunk
] = MIDX_CHUNKID_PACKNAMES
;
946 chunk_offsets
[cur_chunk
] = written
+ (num_chunks
+ 1) * MIDX_CHUNKLOOKUP_WIDTH
;
949 chunk_ids
[cur_chunk
] = MIDX_CHUNKID_OIDFANOUT
;
950 chunk_offsets
[cur_chunk
] = chunk_offsets
[cur_chunk
- 1] + pack_name_concat_len
;
953 chunk_ids
[cur_chunk
] = MIDX_CHUNKID_OIDLOOKUP
;
954 chunk_offsets
[cur_chunk
] = chunk_offsets
[cur_chunk
- 1] + MIDX_CHUNK_FANOUT_SIZE
;
957 chunk_ids
[cur_chunk
] = MIDX_CHUNKID_OBJECTOFFSETS
;
958 chunk_offsets
[cur_chunk
] = chunk_offsets
[cur_chunk
- 1] + ctx
.entries_nr
* the_hash_algo
->rawsz
;
961 chunk_offsets
[cur_chunk
] = chunk_offsets
[cur_chunk
- 1] + ctx
.entries_nr
* MIDX_CHUNK_OFFSET_WIDTH
;
962 if (ctx
.large_offsets_needed
) {
963 chunk_ids
[cur_chunk
] = MIDX_CHUNKID_LARGEOFFSETS
;
966 chunk_offsets
[cur_chunk
] = chunk_offsets
[cur_chunk
- 1] +
967 ctx
.num_large_offsets
* MIDX_CHUNK_LARGE_OFFSET_WIDTH
;
970 chunk_ids
[cur_chunk
] = 0;
972 for (i
= 0; i
<= num_chunks
; i
++) {
973 if (i
&& chunk_offsets
[i
] < chunk_offsets
[i
- 1])
974 BUG("incorrect chunk offsets: %"PRIu64
" before %"PRIu64
,
975 chunk_offsets
[i
- 1],
978 if (chunk_offsets
[i
] % MIDX_CHUNK_ALIGNMENT
)
979 BUG("chunk offset %"PRIu64
" is not properly aligned",
982 hashwrite_be32(f
, chunk_ids
[i
]);
983 hashwrite_be64(f
, chunk_offsets
[i
]);
985 written
+= MIDX_CHUNKLOOKUP_WIDTH
;
988 if (flags
& MIDX_PROGRESS
)
989 progress
= start_delayed_progress(_("Writing chunks to multi-pack-index"),
991 for (i
= 0; i
< num_chunks
; i
++) {
992 if (written
!= chunk_offsets
[i
])
993 BUG("incorrect chunk offset (%"PRIu64
" != %"PRIu64
") for chunk id %"PRIx32
,
998 switch (chunk_ids
[i
]) {
999 case MIDX_CHUNKID_PACKNAMES
:
1000 written
+= write_midx_pack_names(f
, &ctx
);
1003 case MIDX_CHUNKID_OIDFANOUT
:
1004 written
+= write_midx_oid_fanout(f
, &ctx
);
1007 case MIDX_CHUNKID_OIDLOOKUP
:
1008 written
+= write_midx_oid_lookup(f
, &ctx
);
1011 case MIDX_CHUNKID_OBJECTOFFSETS
:
1012 written
+= write_midx_object_offsets(f
, &ctx
);
1015 case MIDX_CHUNKID_LARGEOFFSETS
:
1016 written
+= write_midx_large_offsets(f
, &ctx
);
1020 BUG("trying to write unknown chunk id %"PRIx32
,
1024 display_progress(progress
, i
+ 1);
1026 stop_progress(&progress
);
1028 if (written
!= chunk_offsets
[num_chunks
])
1029 BUG("incorrect final offset %"PRIu64
" != %"PRIu64
,
1031 chunk_offsets
[num_chunks
]);
1033 finalize_hashfile(f
, NULL
, CSUM_FSYNC
| CSUM_HASH_IN_STREAM
);
1034 commit_lock_file(&lk
);
1037 for (i
= 0; i
< ctx
.nr
; i
++) {
1038 if (ctx
.info
[i
].p
) {
1039 close_pack(ctx
.info
[i
].p
);
1040 free(ctx
.info
[i
].p
);
1042 free(ctx
.info
[i
].pack_name
);
1047 free(ctx
.pack_perm
);
1052 int write_midx_file(const char *object_dir
, unsigned flags
)
1054 return write_midx_internal(object_dir
, NULL
, NULL
, flags
);
1057 void clear_midx_file(struct repository
*r
)
1059 char *midx
= get_midx_filename(r
->objects
->odb
->path
);
1061 if (r
->objects
&& r
->objects
->multi_pack_index
) {
1062 close_midx(r
->objects
->multi_pack_index
);
1063 r
->objects
->multi_pack_index
= NULL
;
1066 if (remove_path(midx
))
1067 die(_("failed to clear multi-pack-index at %s"), midx
);
1072 static int verify_midx_error
;
1074 static void midx_report(const char *fmt
, ...)
1077 verify_midx_error
= 1;
1079 vfprintf(stderr
, fmt
, ap
);
1080 fprintf(stderr
, "\n");
1084 struct pair_pos_vs_id
1087 uint32_t pack_int_id
;
1090 static int compare_pair_pos_vs_id(const void *_a
, const void *_b
)
1092 struct pair_pos_vs_id
*a
= (struct pair_pos_vs_id
*)_a
;
1093 struct pair_pos_vs_id
*b
= (struct pair_pos_vs_id
*)_b
;
1095 return b
->pack_int_id
- a
->pack_int_id
;
1099 * Limit calls to display_progress() for performance reasons.
1100 * The interval here was arbitrarily chosen.
1102 #define SPARSE_PROGRESS_INTERVAL (1 << 12)
1103 #define midx_display_sparse_progress(progress, n) \
1105 uint64_t _n = (n); \
1106 if ((_n & (SPARSE_PROGRESS_INTERVAL - 1)) == 0) \
1107 display_progress(progress, _n); \
1110 int verify_midx_file(struct repository
*r
, const char *object_dir
, unsigned flags
)
1112 struct pair_pos_vs_id
*pairs
= NULL
;
1114 struct progress
*progress
= NULL
;
1115 struct multi_pack_index
*m
= load_multi_pack_index(object_dir
, 1);
1116 verify_midx_error
= 0;
1121 char *filename
= get_midx_filename(object_dir
);
1122 if (!stat(filename
, &sb
)) {
1123 error(_("multi-pack-index file exists, but failed to parse"));
1130 if (flags
& MIDX_PROGRESS
)
1131 progress
= start_delayed_progress(_("Looking for referenced packfiles"),
1133 for (i
= 0; i
< m
->num_packs
; i
++) {
1134 if (prepare_midx_pack(r
, m
, i
))
1135 midx_report("failed to load pack in position %d", i
);
1137 display_progress(progress
, i
+ 1);
1139 stop_progress(&progress
);
1141 for (i
= 0; i
< 255; i
++) {
1142 uint32_t oid_fanout1
= ntohl(m
->chunk_oid_fanout
[i
]);
1143 uint32_t oid_fanout2
= ntohl(m
->chunk_oid_fanout
[i
+ 1]);
1145 if (oid_fanout1
> oid_fanout2
)
1146 midx_report(_("oid fanout out of order: fanout[%d] = %"PRIx32
" > %"PRIx32
" = fanout[%d]"),
1147 i
, oid_fanout1
, oid_fanout2
, i
+ 1);
1150 if (m
->num_objects
== 0) {
1151 midx_report(_("the midx contains no oid"));
1153 * Remaining tests assume that we have objects, so we can
1156 return verify_midx_error
;
1159 if (flags
& MIDX_PROGRESS
)
1160 progress
= start_sparse_progress(_("Verifying OID order in multi-pack-index"),
1161 m
->num_objects
- 1);
1162 for (i
= 0; i
< m
->num_objects
- 1; i
++) {
1163 struct object_id oid1
, oid2
;
1165 nth_midxed_object_oid(&oid1
, m
, i
);
1166 nth_midxed_object_oid(&oid2
, m
, i
+ 1);
1168 if (oidcmp(&oid1
, &oid2
) >= 0)
1169 midx_report(_("oid lookup out of order: oid[%d] = %s >= %s = oid[%d]"),
1170 i
, oid_to_hex(&oid1
), oid_to_hex(&oid2
), i
+ 1);
1172 midx_display_sparse_progress(progress
, i
+ 1);
1174 stop_progress(&progress
);
1177 * Create an array mapping each object to its packfile id. Sort it
1178 * to group the objects by packfile. Use this permutation to visit
1179 * each of the objects and only require 1 packfile to be open at a
1182 ALLOC_ARRAY(pairs
, m
->num_objects
);
1183 for (i
= 0; i
< m
->num_objects
; i
++) {
1185 pairs
[i
].pack_int_id
= nth_midxed_pack_int_id(m
, i
);
1188 if (flags
& MIDX_PROGRESS
)
1189 progress
= start_sparse_progress(_("Sorting objects by packfile"),
1191 display_progress(progress
, 0); /* TODO: Measure QSORT() progress */
1192 QSORT(pairs
, m
->num_objects
, compare_pair_pos_vs_id
);
1193 stop_progress(&progress
);
1195 if (flags
& MIDX_PROGRESS
)
1196 progress
= start_sparse_progress(_("Verifying object offsets"), m
->num_objects
);
1197 for (i
= 0; i
< m
->num_objects
; i
++) {
1198 struct object_id oid
;
1199 struct pack_entry e
;
1200 off_t m_offset
, p_offset
;
1202 if (i
> 0 && pairs
[i
-1].pack_int_id
!= pairs
[i
].pack_int_id
&&
1203 m
->packs
[pairs
[i
-1].pack_int_id
])
1205 close_pack_fd(m
->packs
[pairs
[i
-1].pack_int_id
]);
1206 close_pack_index(m
->packs
[pairs
[i
-1].pack_int_id
]);
1209 nth_midxed_object_oid(&oid
, m
, pairs
[i
].pos
);
1211 if (!fill_midx_entry(r
, &oid
, &e
, m
)) {
1212 midx_report(_("failed to load pack entry for oid[%d] = %s"),
1213 pairs
[i
].pos
, oid_to_hex(&oid
));
1217 if (open_pack_index(e
.p
)) {
1218 midx_report(_("failed to load pack-index for packfile %s"),
1223 m_offset
= e
.offset
;
1224 p_offset
= find_pack_entry_one(oid
.hash
, e
.p
);
1226 if (m_offset
!= p_offset
)
1227 midx_report(_("incorrect object offset for oid[%d] = %s: %"PRIx64
" != %"PRIx64
),
1228 pairs
[i
].pos
, oid_to_hex(&oid
), m_offset
, p_offset
);
1230 midx_display_sparse_progress(progress
, i
+ 1);
1232 stop_progress(&progress
);
1236 return verify_midx_error
;
1239 int expire_midx_packs(struct repository
*r
, const char *object_dir
, unsigned flags
)
1241 uint32_t i
, *count
, result
= 0;
1242 struct string_list packs_to_drop
= STRING_LIST_INIT_DUP
;
1243 struct multi_pack_index
*m
= load_multi_pack_index(object_dir
, 1);
1244 struct progress
*progress
= NULL
;
1249 count
= xcalloc(m
->num_packs
, sizeof(uint32_t));
1251 if (flags
& MIDX_PROGRESS
)
1252 progress
= start_delayed_progress(_("Counting referenced objects"),
1254 for (i
= 0; i
< m
->num_objects
; i
++) {
1255 int pack_int_id
= nth_midxed_pack_int_id(m
, i
);
1256 count
[pack_int_id
]++;
1257 display_progress(progress
, i
+ 1);
1259 stop_progress(&progress
);
1261 if (flags
& MIDX_PROGRESS
)
1262 progress
= start_delayed_progress(_("Finding and deleting unreferenced packfiles"),
1264 for (i
= 0; i
< m
->num_packs
; i
++) {
1266 display_progress(progress
, i
+ 1);
1271 if (prepare_midx_pack(r
, m
, i
))
1274 if (m
->packs
[i
]->pack_keep
)
1277 pack_name
= xstrdup(m
->packs
[i
]->pack_name
);
1278 close_pack(m
->packs
[i
]);
1280 string_list_insert(&packs_to_drop
, m
->pack_names
[i
]);
1281 unlink_pack_path(pack_name
, 0);
1284 stop_progress(&progress
);
1288 if (packs_to_drop
.nr
)
1289 result
= write_midx_internal(object_dir
, m
, &packs_to_drop
, flags
);
1291 string_list_clear(&packs_to_drop
, 0);
1295 struct repack_info
{
1297 uint32_t referenced_objects
;
1298 uint32_t pack_int_id
;
1301 static int compare_by_mtime(const void *a_
, const void *b_
)
1303 const struct repack_info
*a
, *b
;
1305 a
= (const struct repack_info
*)a_
;
1306 b
= (const struct repack_info
*)b_
;
1308 if (a
->mtime
< b
->mtime
)
1310 if (a
->mtime
> b
->mtime
)
1315 static int fill_included_packs_all(struct repository
*r
,
1316 struct multi_pack_index
*m
,
1317 unsigned char *include_pack
)
1319 uint32_t i
, count
= 0;
1320 int pack_kept_objects
= 0;
1322 repo_config_get_bool(r
, "repack.packkeptobjects", &pack_kept_objects
);
1324 for (i
= 0; i
< m
->num_packs
; i
++) {
1325 if (prepare_midx_pack(r
, m
, i
))
1327 if (!pack_kept_objects
&& m
->packs
[i
]->pack_keep
)
1330 include_pack
[i
] = 1;
1337 static int fill_included_packs_batch(struct repository
*r
,
1338 struct multi_pack_index
*m
,
1339 unsigned char *include_pack
,
1342 uint32_t i
, packs_to_repack
;
1344 struct repack_info
*pack_info
= xcalloc(m
->num_packs
, sizeof(struct repack_info
));
1345 int pack_kept_objects
= 0;
1347 repo_config_get_bool(r
, "repack.packkeptobjects", &pack_kept_objects
);
1349 for (i
= 0; i
< m
->num_packs
; i
++) {
1350 pack_info
[i
].pack_int_id
= i
;
1352 if (prepare_midx_pack(r
, m
, i
))
1355 pack_info
[i
].mtime
= m
->packs
[i
]->mtime
;
1358 for (i
= 0; batch_size
&& i
< m
->num_objects
; i
++) {
1359 uint32_t pack_int_id
= nth_midxed_pack_int_id(m
, i
);
1360 pack_info
[pack_int_id
].referenced_objects
++;
1363 QSORT(pack_info
, m
->num_packs
, compare_by_mtime
);
1366 packs_to_repack
= 0;
1367 for (i
= 0; total_size
< batch_size
&& i
< m
->num_packs
; i
++) {
1368 int pack_int_id
= pack_info
[i
].pack_int_id
;
1369 struct packed_git
*p
= m
->packs
[pack_int_id
];
1370 size_t expected_size
;
1374 if (!pack_kept_objects
&& p
->pack_keep
)
1376 if (open_pack_index(p
) || !p
->num_objects
)
1379 expected_size
= (size_t)(p
->pack_size
1380 * pack_info
[i
].referenced_objects
);
1381 expected_size
/= p
->num_objects
;
1383 if (expected_size
>= batch_size
)
1387 total_size
+= expected_size
;
1388 include_pack
[pack_int_id
] = 1;
1393 if (packs_to_repack
< 2)
1399 int midx_repack(struct repository
*r
, const char *object_dir
, size_t batch_size
, unsigned flags
)
1403 unsigned char *include_pack
;
1404 struct child_process cmd
= CHILD_PROCESS_INIT
;
1406 struct strbuf base_name
= STRBUF_INIT
;
1407 struct multi_pack_index
*m
= load_multi_pack_index(object_dir
, 1);
1410 * When updating the default for these configuration
1411 * variables in builtin/repack.c, these must be adjusted
1414 int delta_base_offset
= 1;
1415 int use_delta_islands
= 0;
1420 include_pack
= xcalloc(m
->num_packs
, sizeof(unsigned char));
1423 if (fill_included_packs_batch(r
, m
, include_pack
, batch_size
))
1425 } else if (fill_included_packs_all(r
, m
, include_pack
))
1428 repo_config_get_bool(r
, "repack.usedeltabaseoffset", &delta_base_offset
);
1429 repo_config_get_bool(r
, "repack.usedeltaislands", &use_delta_islands
);
1431 strvec_push(&cmd
.args
, "pack-objects");
1433 strbuf_addstr(&base_name
, object_dir
);
1434 strbuf_addstr(&base_name
, "/pack/pack");
1435 strvec_push(&cmd
.args
, base_name
.buf
);
1437 if (delta_base_offset
)
1438 strvec_push(&cmd
.args
, "--delta-base-offset");
1439 if (use_delta_islands
)
1440 strvec_push(&cmd
.args
, "--delta-islands");
1442 if (flags
& MIDX_PROGRESS
)
1443 strvec_push(&cmd
.args
, "--progress");
1445 strvec_push(&cmd
.args
, "-q");
1447 strbuf_release(&base_name
);
1450 cmd
.in
= cmd
.out
= -1;
1452 if (start_command(&cmd
)) {
1453 error(_("could not start pack-objects"));
1458 cmd_in
= xfdopen(cmd
.in
, "w");
1460 for (i
= 0; i
< m
->num_objects
; i
++) {
1461 struct object_id oid
;
1462 uint32_t pack_int_id
= nth_midxed_pack_int_id(m
, i
);
1464 if (!include_pack
[pack_int_id
])
1467 nth_midxed_object_oid(&oid
, m
, i
);
1468 fprintf(cmd_in
, "%s\n", oid_to_hex(&oid
));
1472 if (finish_command(&cmd
)) {
1473 error(_("could not finish pack-objects"));
1478 result
= write_midx_internal(object_dir
, m
, NULL
, flags
);