7 #include "object-store.h"
8 #include "sha1-lookup.h"
12 #include "run-command.h"
14 #define MIDX_SIGNATURE 0x4d494458 /* "MIDX" */
15 #define MIDX_VERSION 1
16 #define MIDX_BYTE_FILE_VERSION 4
17 #define MIDX_BYTE_HASH_VERSION 5
18 #define MIDX_BYTE_NUM_CHUNKS 6
19 #define MIDX_BYTE_NUM_PACKS 8
20 #define MIDX_HEADER_SIZE 12
21 #define MIDX_MIN_SIZE (MIDX_HEADER_SIZE + the_hash_algo->rawsz)
23 #define MIDX_MAX_CHUNKS 5
24 #define MIDX_CHUNK_ALIGNMENT 4
25 #define MIDX_CHUNKID_PACKNAMES 0x504e414d /* "PNAM" */
26 #define MIDX_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
27 #define MIDX_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
28 #define MIDX_CHUNKID_OBJECTOFFSETS 0x4f4f4646 /* "OOFF" */
29 #define MIDX_CHUNKID_LARGEOFFSETS 0x4c4f4646 /* "LOFF" */
30 #define MIDX_CHUNKLOOKUP_WIDTH (sizeof(uint32_t) + sizeof(uint64_t))
31 #define MIDX_CHUNK_FANOUT_SIZE (sizeof(uint32_t) * 256)
32 #define MIDX_CHUNK_OFFSET_WIDTH (2 * sizeof(uint32_t))
33 #define MIDX_CHUNK_LARGE_OFFSET_WIDTH (sizeof(uint64_t))
34 #define MIDX_LARGE_OFFSET_NEEDED 0x80000000
36 #define PACK_EXPIRED UINT_MAX
38 static uint8_t oid_version(void)
40 switch (hash_algo_by_ptr(the_hash_algo
)) {
46 die(_("invalid hash version"));
50 static char *get_midx_filename(const char *object_dir
)
52 return xstrfmt("%s/pack/multi-pack-index", object_dir
);
55 struct multi_pack_index
*load_multi_pack_index(const char *object_dir
, int local
)
57 struct multi_pack_index
*m
= NULL
;
61 void *midx_map
= NULL
;
62 uint32_t hash_version
;
63 char *midx_name
= get_midx_filename(object_dir
);
65 const char *cur_pack_name
;
67 fd
= git_open(midx_name
);
72 error_errno(_("failed to read %s"), midx_name
);
76 midx_size
= xsize_t(st
.st_size
);
78 if (midx_size
< MIDX_MIN_SIZE
) {
79 error(_("multi-pack-index file %s is too small"), midx_name
);
83 FREE_AND_NULL(midx_name
);
85 midx_map
= xmmap(NULL
, midx_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
88 FLEX_ALLOC_STR(m
, object_dir
, object_dir
);
90 m
->data_len
= midx_size
;
93 m
->signature
= get_be32(m
->data
);
94 if (m
->signature
!= MIDX_SIGNATURE
)
95 die(_("multi-pack-index signature 0x%08x does not match signature 0x%08x"),
96 m
->signature
, MIDX_SIGNATURE
);
98 m
->version
= m
->data
[MIDX_BYTE_FILE_VERSION
];
99 if (m
->version
!= MIDX_VERSION
)
100 die(_("multi-pack-index version %d not recognized"),
103 hash_version
= m
->data
[MIDX_BYTE_HASH_VERSION
];
104 if (hash_version
!= oid_version()) {
105 error(_("multi-pack-index hash version %u does not match version %u"),
106 hash_version
, oid_version());
109 m
->hash_len
= the_hash_algo
->rawsz
;
111 m
->num_chunks
= m
->data
[MIDX_BYTE_NUM_CHUNKS
];
113 m
->num_packs
= get_be32(m
->data
+ MIDX_BYTE_NUM_PACKS
);
115 for (i
= 0; i
< m
->num_chunks
; i
++) {
116 uint32_t chunk_id
= get_be32(m
->data
+ MIDX_HEADER_SIZE
+
117 MIDX_CHUNKLOOKUP_WIDTH
* i
);
118 uint64_t chunk_offset
= get_be64(m
->data
+ MIDX_HEADER_SIZE
+ 4 +
119 MIDX_CHUNKLOOKUP_WIDTH
* i
);
121 if (chunk_offset
>= m
->data_len
)
122 die(_("invalid chunk offset (too large)"));
125 case MIDX_CHUNKID_PACKNAMES
:
126 m
->chunk_pack_names
= m
->data
+ chunk_offset
;
129 case MIDX_CHUNKID_OIDFANOUT
:
130 m
->chunk_oid_fanout
= (uint32_t *)(m
->data
+ chunk_offset
);
133 case MIDX_CHUNKID_OIDLOOKUP
:
134 m
->chunk_oid_lookup
= m
->data
+ chunk_offset
;
137 case MIDX_CHUNKID_OBJECTOFFSETS
:
138 m
->chunk_object_offsets
= m
->data
+ chunk_offset
;
141 case MIDX_CHUNKID_LARGEOFFSETS
:
142 m
->chunk_large_offsets
= m
->data
+ chunk_offset
;
146 die(_("terminating multi-pack-index chunk id appears earlier than expected"));
151 * Do nothing on unrecognized chunks, allowing future
152 * extensions to add optional chunks.
158 if (!m
->chunk_pack_names
)
159 die(_("multi-pack-index missing required pack-name chunk"));
160 if (!m
->chunk_oid_fanout
)
161 die(_("multi-pack-index missing required OID fanout chunk"));
162 if (!m
->chunk_oid_lookup
)
163 die(_("multi-pack-index missing required OID lookup chunk"));
164 if (!m
->chunk_object_offsets
)
165 die(_("multi-pack-index missing required object offsets chunk"));
167 m
->num_objects
= ntohl(m
->chunk_oid_fanout
[255]);
169 m
->pack_names
= xcalloc(m
->num_packs
, sizeof(*m
->pack_names
));
170 m
->packs
= xcalloc(m
->num_packs
, sizeof(*m
->packs
));
172 cur_pack_name
= (const char *)m
->chunk_pack_names
;
173 for (i
= 0; i
< m
->num_packs
; i
++) {
174 m
->pack_names
[i
] = cur_pack_name
;
176 cur_pack_name
+= strlen(cur_pack_name
) + 1;
178 if (i
&& strcmp(m
->pack_names
[i
], m
->pack_names
[i
- 1]) <= 0)
179 die(_("multi-pack-index pack names out of order: '%s' before '%s'"),
180 m
->pack_names
[i
- 1],
184 trace2_data_intmax("midx", the_repository
, "load/num_packs", m
->num_packs
);
185 trace2_data_intmax("midx", the_repository
, "load/num_objects", m
->num_objects
);
193 munmap(midx_map
, midx_size
);
199 void close_midx(struct multi_pack_index
*m
)
206 munmap((unsigned char *)m
->data
, m
->data_len
);
208 for (i
= 0; i
< m
->num_packs
; i
++) {
210 m
->packs
[i
]->multi_pack_index
= 0;
212 FREE_AND_NULL(m
->packs
);
213 FREE_AND_NULL(m
->pack_names
);
216 int prepare_midx_pack(struct repository
*r
, struct multi_pack_index
*m
, uint32_t pack_int_id
)
218 struct strbuf pack_name
= STRBUF_INIT
;
219 struct packed_git
*p
;
221 if (pack_int_id
>= m
->num_packs
)
222 die(_("bad pack-int-id: %u (%u total packs)"),
223 pack_int_id
, m
->num_packs
);
225 if (m
->packs
[pack_int_id
])
228 strbuf_addf(&pack_name
, "%s/pack/%s", m
->object_dir
,
229 m
->pack_names
[pack_int_id
]);
231 p
= add_packed_git(pack_name
.buf
, pack_name
.len
, m
->local
);
232 strbuf_release(&pack_name
);
237 p
->multi_pack_index
= 1;
238 m
->packs
[pack_int_id
] = p
;
239 install_packed_git(r
, p
);
240 list_add_tail(&p
->mru
, &r
->objects
->packed_git_mru
);
245 int bsearch_midx(const struct object_id
*oid
, struct multi_pack_index
*m
, uint32_t *result
)
247 return bsearch_hash(oid
->hash
, m
->chunk_oid_fanout
, m
->chunk_oid_lookup
,
248 the_hash_algo
->rawsz
, result
);
251 struct object_id
*nth_midxed_object_oid(struct object_id
*oid
,
252 struct multi_pack_index
*m
,
255 if (n
>= m
->num_objects
)
258 hashcpy(oid
->hash
, m
->chunk_oid_lookup
+ m
->hash_len
* n
);
262 static off_t
nth_midxed_offset(struct multi_pack_index
*m
, uint32_t pos
)
264 const unsigned char *offset_data
;
267 offset_data
= m
->chunk_object_offsets
+ pos
* MIDX_CHUNK_OFFSET_WIDTH
;
268 offset32
= get_be32(offset_data
+ sizeof(uint32_t));
270 if (m
->chunk_large_offsets
&& offset32
& MIDX_LARGE_OFFSET_NEEDED
) {
271 if (sizeof(off_t
) < sizeof(uint64_t))
272 die(_("multi-pack-index stores a 64-bit offset, but off_t is too small"));
274 offset32
^= MIDX_LARGE_OFFSET_NEEDED
;
275 return get_be64(m
->chunk_large_offsets
+ sizeof(uint64_t) * offset32
);
281 static uint32_t nth_midxed_pack_int_id(struct multi_pack_index
*m
, uint32_t pos
)
283 return get_be32(m
->chunk_object_offsets
+ pos
* MIDX_CHUNK_OFFSET_WIDTH
);
286 static int nth_midxed_pack_entry(struct repository
*r
,
287 struct multi_pack_index
*m
,
288 struct pack_entry
*e
,
291 uint32_t pack_int_id
;
292 struct packed_git
*p
;
294 if (pos
>= m
->num_objects
)
297 pack_int_id
= nth_midxed_pack_int_id(m
, pos
);
299 if (prepare_midx_pack(r
, m
, pack_int_id
))
300 die(_("error preparing packfile from multi-pack-index"));
301 p
= m
->packs
[pack_int_id
];
304 * We are about to tell the caller where they can locate the
305 * requested object. We better make sure the packfile is
306 * still here and can be accessed before supplying that
307 * answer, as it may have been deleted since the MIDX was
310 if (!is_pack_valid(p
))
313 if (p
->num_bad_objects
) {
315 struct object_id oid
;
316 nth_midxed_object_oid(&oid
, m
, pos
);
317 for (i
= 0; i
< p
->num_bad_objects
; i
++)
319 p
->bad_object_sha1
+ the_hash_algo
->rawsz
* i
))
323 e
->offset
= nth_midxed_offset(m
, pos
);
329 int fill_midx_entry(struct repository
* r
,
330 const struct object_id
*oid
,
331 struct pack_entry
*e
,
332 struct multi_pack_index
*m
)
336 if (!bsearch_midx(oid
, m
, &pos
))
339 return nth_midxed_pack_entry(r
, m
, e
, pos
);
342 /* Match "foo.idx" against either "foo.pack" _or_ "foo.idx". */
343 static int cmp_idx_or_pack_name(const char *idx_or_pack_name
,
344 const char *idx_name
)
346 /* Skip past any initial matching prefix. */
347 while (*idx_name
&& *idx_name
== *idx_or_pack_name
) {
353 * If we didn't match completely, we may have matched "pack-1234." and
354 * be left with "idx" and "pack" respectively, which is also OK. We do
355 * not have to check for "idx" and "idx", because that would have been
356 * a complete match (and in that case these strcmps will be false, but
357 * we'll correctly return 0 from the final strcmp() below.
359 * Technically this matches "fooidx" and "foopack", but we'd never have
360 * such names in the first place.
362 if (!strcmp(idx_name
, "idx") && !strcmp(idx_or_pack_name
, "pack"))
366 * This not only checks for a complete match, but also orders based on
367 * the first non-identical character, which means our ordering will
368 * match a raw strcmp(). That makes it OK to use this to binary search
369 * a naively-sorted list.
371 return strcmp(idx_or_pack_name
, idx_name
);
374 int midx_contains_pack(struct multi_pack_index
*m
, const char *idx_or_pack_name
)
376 uint32_t first
= 0, last
= m
->num_packs
;
378 while (first
< last
) {
379 uint32_t mid
= first
+ (last
- first
) / 2;
383 current
= m
->pack_names
[mid
];
384 cmp
= cmp_idx_or_pack_name(idx_or_pack_name
, current
);
397 int prepare_multi_pack_index_one(struct repository
*r
, const char *object_dir
, int local
)
399 struct multi_pack_index
*m
;
400 struct multi_pack_index
*m_search
;
402 static int env_value
= -1;
405 env_value
= git_env_bool(GIT_TEST_MULTI_PACK_INDEX
, 0);
408 (repo_config_get_bool(r
, "core.multipackindex", &config_value
) ||
412 for (m_search
= r
->objects
->multi_pack_index
; m_search
; m_search
= m_search
->next
)
413 if (!strcmp(object_dir
, m_search
->object_dir
))
416 m
= load_multi_pack_index(object_dir
, local
);
419 struct multi_pack_index
*mp
= r
->objects
->multi_pack_index
;
424 r
->objects
->multi_pack_index
= m
;
431 static size_t write_midx_header(struct hashfile
*f
,
432 unsigned char num_chunks
,
435 unsigned char byte_values
[4];
437 hashwrite_be32(f
, MIDX_SIGNATURE
);
438 byte_values
[0] = MIDX_VERSION
;
439 byte_values
[1] = oid_version();
440 byte_values
[2] = num_chunks
;
441 byte_values
[3] = 0; /* unused */
442 hashwrite(f
, byte_values
, sizeof(byte_values
));
443 hashwrite_be32(f
, num_packs
);
445 return MIDX_HEADER_SIZE
;
449 uint32_t orig_pack_int_id
;
451 struct packed_git
*p
;
452 unsigned expired
: 1;
455 static int pack_info_compare(const void *_a
, const void *_b
)
457 struct pack_info
*a
= (struct pack_info
*)_a
;
458 struct pack_info
*b
= (struct pack_info
*)_b
;
459 return strcmp(a
->pack_name
, b
->pack_name
);
463 struct pack_info
*info
;
466 struct multi_pack_index
*m
;
467 struct progress
*progress
;
468 unsigned pack_paths_checked
;
471 static void add_pack_to_midx(const char *full_path
, size_t full_path_len
,
472 const char *file_name
, void *data
)
474 struct pack_list
*packs
= (struct pack_list
*)data
;
476 if (ends_with(file_name
, ".idx")) {
477 display_progress(packs
->progress
, ++packs
->pack_paths_checked
);
478 if (packs
->m
&& midx_contains_pack(packs
->m
, file_name
))
481 ALLOC_GROW(packs
->info
, packs
->nr
+ 1, packs
->alloc
);
483 packs
->info
[packs
->nr
].p
= add_packed_git(full_path
,
487 if (!packs
->info
[packs
->nr
].p
) {
488 warning(_("failed to add packfile '%s'"),
493 if (open_pack_index(packs
->info
[packs
->nr
].p
)) {
494 warning(_("failed to open pack-index '%s'"),
496 close_pack(packs
->info
[packs
->nr
].p
);
497 FREE_AND_NULL(packs
->info
[packs
->nr
].p
);
501 packs
->info
[packs
->nr
].pack_name
= xstrdup(file_name
);
502 packs
->info
[packs
->nr
].orig_pack_int_id
= packs
->nr
;
503 packs
->info
[packs
->nr
].expired
= 0;
508 struct pack_midx_entry
{
509 struct object_id oid
;
510 uint32_t pack_int_id
;
515 static int midx_oid_compare(const void *_a
, const void *_b
)
517 const struct pack_midx_entry
*a
= (const struct pack_midx_entry
*)_a
;
518 const struct pack_midx_entry
*b
= (const struct pack_midx_entry
*)_b
;
519 int cmp
= oidcmp(&a
->oid
, &b
->oid
);
524 if (a
->pack_mtime
> b
->pack_mtime
)
526 else if (a
->pack_mtime
< b
->pack_mtime
)
529 return a
->pack_int_id
- b
->pack_int_id
;
532 static int nth_midxed_pack_midx_entry(struct multi_pack_index
*m
,
533 struct pack_midx_entry
*e
,
536 if (pos
>= m
->num_objects
)
539 nth_midxed_object_oid(&e
->oid
, m
, pos
);
540 e
->pack_int_id
= nth_midxed_pack_int_id(m
, pos
);
541 e
->offset
= nth_midxed_offset(m
, pos
);
543 /* consider objects in midx to be from "old" packs */
548 static void fill_pack_entry(uint32_t pack_int_id
,
549 struct packed_git
*p
,
551 struct pack_midx_entry
*entry
)
553 if (nth_packed_object_id(&entry
->oid
, p
, cur_object
) < 0)
554 die(_("failed to locate object %d in packfile"), cur_object
);
556 entry
->pack_int_id
= pack_int_id
;
557 entry
->pack_mtime
= p
->mtime
;
559 entry
->offset
= nth_packed_object_offset(p
, cur_object
);
563 * It is possible to artificially get into a state where there are many
564 * duplicate copies of objects. That can create high memory pressure if
565 * we are to create a list of all objects before de-duplication. To reduce
566 * this memory pressure without a significant performance drop, automatically
567 * group objects by the first byte of their object id. Use the IDX fanout
568 * tables to group the data, copy to a local array, then sort.
570 * Copy only the de-duplicated entries (selected by most-recent modified time
571 * of a packfile containing the object).
573 static struct pack_midx_entry
*get_sorted_entries(struct multi_pack_index
*m
,
574 struct pack_info
*info
,
576 uint32_t *nr_objects
)
578 uint32_t cur_fanout
, cur_pack
, cur_object
;
579 uint32_t alloc_fanout
, alloc_objects
, total_objects
= 0;
580 struct pack_midx_entry
*entries_by_fanout
= NULL
;
581 struct pack_midx_entry
*deduplicated_entries
= NULL
;
582 uint32_t start_pack
= m
? m
->num_packs
: 0;
584 for (cur_pack
= start_pack
; cur_pack
< nr_packs
; cur_pack
++)
585 total_objects
+= info
[cur_pack
].p
->num_objects
;
588 * As we de-duplicate by fanout value, we expect the fanout
589 * slices to be evenly distributed, with some noise. Hence,
590 * allocate slightly more than one 256th.
592 alloc_objects
= alloc_fanout
= total_objects
> 3200 ? total_objects
/ 200 : 16;
594 ALLOC_ARRAY(entries_by_fanout
, alloc_fanout
);
595 ALLOC_ARRAY(deduplicated_entries
, alloc_objects
);
598 for (cur_fanout
= 0; cur_fanout
< 256; cur_fanout
++) {
599 uint32_t nr_fanout
= 0;
602 uint32_t start
= 0, end
;
605 start
= ntohl(m
->chunk_oid_fanout
[cur_fanout
- 1]);
606 end
= ntohl(m
->chunk_oid_fanout
[cur_fanout
]);
608 for (cur_object
= start
; cur_object
< end
; cur_object
++) {
609 ALLOC_GROW(entries_by_fanout
, nr_fanout
+ 1, alloc_fanout
);
610 nth_midxed_pack_midx_entry(m
,
611 &entries_by_fanout
[nr_fanout
],
617 for (cur_pack
= start_pack
; cur_pack
< nr_packs
; cur_pack
++) {
618 uint32_t start
= 0, end
;
621 start
= get_pack_fanout(info
[cur_pack
].p
, cur_fanout
- 1);
622 end
= get_pack_fanout(info
[cur_pack
].p
, cur_fanout
);
624 for (cur_object
= start
; cur_object
< end
; cur_object
++) {
625 ALLOC_GROW(entries_by_fanout
, nr_fanout
+ 1, alloc_fanout
);
626 fill_pack_entry(cur_pack
, info
[cur_pack
].p
, cur_object
, &entries_by_fanout
[nr_fanout
]);
631 QSORT(entries_by_fanout
, nr_fanout
, midx_oid_compare
);
634 * The batch is now sorted by OID and then mtime (descending).
635 * Take only the first duplicate.
637 for (cur_object
= 0; cur_object
< nr_fanout
; cur_object
++) {
638 if (cur_object
&& oideq(&entries_by_fanout
[cur_object
- 1].oid
,
639 &entries_by_fanout
[cur_object
].oid
))
642 ALLOC_GROW(deduplicated_entries
, *nr_objects
+ 1, alloc_objects
);
643 memcpy(&deduplicated_entries
[*nr_objects
],
644 &entries_by_fanout
[cur_object
],
645 sizeof(struct pack_midx_entry
));
650 free(entries_by_fanout
);
651 return deduplicated_entries
;
654 static size_t write_midx_pack_names(struct hashfile
*f
,
655 struct pack_info
*info
,
659 unsigned char padding
[MIDX_CHUNK_ALIGNMENT
];
662 for (i
= 0; i
< num_packs
; i
++) {
668 if (i
&& strcmp(info
[i
].pack_name
, info
[i
- 1].pack_name
) <= 0)
669 BUG("incorrect pack-file order: %s before %s",
670 info
[i
- 1].pack_name
,
673 writelen
= strlen(info
[i
].pack_name
) + 1;
674 hashwrite(f
, info
[i
].pack_name
, 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
,
746 struct pack_midx_entry
*objects
, uint32_t nr_objects
)
748 struct pack_midx_entry
*list
= objects
;
749 uint32_t i
, nr_large_offset
= 0;
752 for (i
= 0; i
< nr_objects
; i
++) {
753 struct pack_midx_entry
*obj
= list
++;
755 if (perm
[obj
->pack_int_id
] == PACK_EXPIRED
)
756 BUG("object %s is in an expired pack with int-id %d",
757 oid_to_hex(&obj
->oid
),
760 hashwrite_be32(f
, perm
[obj
->pack_int_id
]);
762 if (large_offset_needed
&& obj
->offset
>> 31)
763 hashwrite_be32(f
, MIDX_LARGE_OFFSET_NEEDED
| nr_large_offset
++);
764 else if (!large_offset_needed
&& obj
->offset
>> 32)
765 BUG("object %s requires a large offset (%"PRIx64
") but the MIDX is not writing large offsets!",
766 oid_to_hex(&obj
->oid
),
769 hashwrite_be32(f
, (uint32_t)obj
->offset
);
771 written
+= MIDX_CHUNK_OFFSET_WIDTH
;
777 static size_t write_midx_large_offsets(struct hashfile
*f
, uint32_t nr_large_offset
,
778 struct pack_midx_entry
*objects
, uint32_t nr_objects
)
780 struct pack_midx_entry
*list
= objects
, *end
= objects
+ nr_objects
;
783 while (nr_large_offset
) {
784 struct pack_midx_entry
*obj
;
788 BUG("too many large-offset objects");
791 offset
= obj
->offset
;
796 hashwrite_be32(f
, offset
>> 32);
797 hashwrite_be32(f
, offset
& 0xffffffffUL
);
798 written
+= 2 * sizeof(uint32_t);
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 pack_list packs
;
815 uint32_t *pack_perm
= NULL
;
816 uint64_t written
= 0;
817 uint32_t chunk_ids
[MIDX_MAX_CHUNKS
+ 1];
818 uint64_t chunk_offsets
[MIDX_MAX_CHUNKS
+ 1];
819 uint32_t nr_entries
, num_large_offsets
= 0;
820 struct pack_midx_entry
*entries
= NULL
;
821 struct progress
*progress
= NULL
;
822 int large_offsets_needed
= 0;
823 int pack_name_concat_len
= 0;
824 int dropped_packs
= 0;
827 midx_name
= get_midx_filename(object_dir
);
828 if (safe_create_leading_directories(midx_name
))
829 die_errno(_("unable to create leading directories of %s"),
835 packs
.m
= load_multi_pack_index(object_dir
, 1);
838 packs
.alloc
= packs
.m
? packs
.m
->num_packs
: 16;
840 ALLOC_ARRAY(packs
.info
, packs
.alloc
);
843 for (i
= 0; i
< packs
.m
->num_packs
; i
++) {
844 ALLOC_GROW(packs
.info
, packs
.nr
+ 1, packs
.alloc
);
846 packs
.info
[packs
.nr
].orig_pack_int_id
= i
;
847 packs
.info
[packs
.nr
].pack_name
= xstrdup(packs
.m
->pack_names
[i
]);
848 packs
.info
[packs
.nr
].p
= NULL
;
849 packs
.info
[packs
.nr
].expired
= 0;
854 packs
.pack_paths_checked
= 0;
855 if (flags
& MIDX_PROGRESS
)
856 packs
.progress
= start_progress(_("Adding packfiles to multi-pack-index"), 0);
858 packs
.progress
= NULL
;
860 for_each_file_in_pack_dir(object_dir
, add_pack_to_midx
, &packs
);
861 stop_progress(&packs
.progress
);
863 if (packs
.m
&& packs
.nr
== packs
.m
->num_packs
&& !packs_to_drop
)
866 entries
= get_sorted_entries(packs
.m
, packs
.info
, packs
.nr
, &nr_entries
);
868 for (i
= 0; i
< nr_entries
; i
++) {
869 if (entries
[i
].offset
> 0x7fffffff)
871 if (entries
[i
].offset
> 0xffffffff)
872 large_offsets_needed
= 1;
875 QSORT(packs
.info
, packs
.nr
, pack_info_compare
);
877 if (packs_to_drop
&& packs_to_drop
->nr
) {
879 int missing_drops
= 0;
881 for (i
= 0; i
< packs
.nr
&& drop_index
< packs_to_drop
->nr
; i
++) {
882 int cmp
= strcmp(packs
.info
[i
].pack_name
,
883 packs_to_drop
->items
[drop_index
].string
);
887 packs
.info
[i
].expired
= 1;
888 } else if (cmp
> 0) {
889 error(_("did not see pack-file %s to drop"),
890 packs_to_drop
->items
[drop_index
].string
);
895 packs
.info
[i
].expired
= 0;
906 * pack_perm stores a permutation between pack-int-ids from the
907 * previous multi-pack-index to the new one we are writing:
909 * pack_perm[old_id] = new_id
911 ALLOC_ARRAY(pack_perm
, packs
.nr
);
912 for (i
= 0; i
< packs
.nr
; i
++) {
913 if (packs
.info
[i
].expired
) {
915 pack_perm
[packs
.info
[i
].orig_pack_int_id
] = PACK_EXPIRED
;
917 pack_perm
[packs
.info
[i
].orig_pack_int_id
] = i
- dropped_packs
;
921 for (i
= 0; i
< packs
.nr
; i
++) {
922 if (!packs
.info
[i
].expired
)
923 pack_name_concat_len
+= strlen(packs
.info
[i
].pack_name
) + 1;
926 if (pack_name_concat_len
% MIDX_CHUNK_ALIGNMENT
)
927 pack_name_concat_len
+= MIDX_CHUNK_ALIGNMENT
-
928 (pack_name_concat_len
% MIDX_CHUNK_ALIGNMENT
);
930 hold_lock_file_for_update(&lk
, midx_name
, LOCK_DIE_ON_ERROR
);
931 f
= hashfd(lk
.tempfile
->fd
, lk
.tempfile
->filename
.buf
);
932 FREE_AND_NULL(midx_name
);
938 num_chunks
= large_offsets_needed
? 5 : 4;
940 if (packs
.nr
- dropped_packs
== 0) {
941 error(_("no pack files to index."));
946 written
= write_midx_header(f
, num_chunks
, packs
.nr
- dropped_packs
);
948 chunk_ids
[cur_chunk
] = MIDX_CHUNKID_PACKNAMES
;
949 chunk_offsets
[cur_chunk
] = written
+ (num_chunks
+ 1) * MIDX_CHUNKLOOKUP_WIDTH
;
952 chunk_ids
[cur_chunk
] = MIDX_CHUNKID_OIDFANOUT
;
953 chunk_offsets
[cur_chunk
] = chunk_offsets
[cur_chunk
- 1] + pack_name_concat_len
;
956 chunk_ids
[cur_chunk
] = MIDX_CHUNKID_OIDLOOKUP
;
957 chunk_offsets
[cur_chunk
] = chunk_offsets
[cur_chunk
- 1] + MIDX_CHUNK_FANOUT_SIZE
;
960 chunk_ids
[cur_chunk
] = MIDX_CHUNKID_OBJECTOFFSETS
;
961 chunk_offsets
[cur_chunk
] = chunk_offsets
[cur_chunk
- 1] + nr_entries
* the_hash_algo
->rawsz
;
964 chunk_offsets
[cur_chunk
] = chunk_offsets
[cur_chunk
- 1] + nr_entries
* MIDX_CHUNK_OFFSET_WIDTH
;
965 if (large_offsets_needed
) {
966 chunk_ids
[cur_chunk
] = MIDX_CHUNKID_LARGEOFFSETS
;
969 chunk_offsets
[cur_chunk
] = chunk_offsets
[cur_chunk
- 1] +
970 num_large_offsets
* MIDX_CHUNK_LARGE_OFFSET_WIDTH
;
973 chunk_ids
[cur_chunk
] = 0;
975 for (i
= 0; i
<= num_chunks
; i
++) {
976 if (i
&& chunk_offsets
[i
] < chunk_offsets
[i
- 1])
977 BUG("incorrect chunk offsets: %"PRIu64
" before %"PRIu64
,
978 chunk_offsets
[i
- 1],
981 if (chunk_offsets
[i
] % MIDX_CHUNK_ALIGNMENT
)
982 BUG("chunk offset %"PRIu64
" is not properly aligned",
985 hashwrite_be32(f
, chunk_ids
[i
]);
986 hashwrite_be32(f
, chunk_offsets
[i
] >> 32);
987 hashwrite_be32(f
, chunk_offsets
[i
]);
989 written
+= MIDX_CHUNKLOOKUP_WIDTH
;
992 if (flags
& MIDX_PROGRESS
)
993 progress
= start_progress(_("Writing chunks to multi-pack-index"),
995 for (i
= 0; i
< num_chunks
; i
++) {
996 if (written
!= chunk_offsets
[i
])
997 BUG("incorrect chunk offset (%"PRIu64
" != %"PRIu64
") for chunk id %"PRIx32
,
1002 switch (chunk_ids
[i
]) {
1003 case MIDX_CHUNKID_PACKNAMES
:
1004 written
+= write_midx_pack_names(f
, packs
.info
, packs
.nr
);
1007 case MIDX_CHUNKID_OIDFANOUT
:
1008 written
+= write_midx_oid_fanout(f
, entries
, nr_entries
);
1011 case MIDX_CHUNKID_OIDLOOKUP
:
1012 written
+= write_midx_oid_lookup(f
, the_hash_algo
->rawsz
, entries
, nr_entries
);
1015 case MIDX_CHUNKID_OBJECTOFFSETS
:
1016 written
+= write_midx_object_offsets(f
, large_offsets_needed
, pack_perm
, entries
, nr_entries
);
1019 case MIDX_CHUNKID_LARGEOFFSETS
:
1020 written
+= write_midx_large_offsets(f
, num_large_offsets
, entries
, nr_entries
);
1024 BUG("trying to write unknown chunk id %"PRIx32
,
1028 display_progress(progress
, i
+ 1);
1030 stop_progress(&progress
);
1032 if (written
!= chunk_offsets
[num_chunks
])
1033 BUG("incorrect final offset %"PRIu64
" != %"PRIu64
,
1035 chunk_offsets
[num_chunks
]);
1037 finalize_hashfile(f
, NULL
, CSUM_FSYNC
| CSUM_HASH_IN_STREAM
);
1038 commit_lock_file(&lk
);
1041 for (i
= 0; i
< packs
.nr
; i
++) {
1042 if (packs
.info
[i
].p
) {
1043 close_pack(packs
.info
[i
].p
);
1044 free(packs
.info
[i
].p
);
1046 free(packs
.info
[i
].pack_name
);
1056 int write_midx_file(const char *object_dir
, unsigned flags
)
1058 return write_midx_internal(object_dir
, NULL
, NULL
, flags
);
1061 void clear_midx_file(struct repository
*r
)
1063 char *midx
= get_midx_filename(r
->objects
->odb
->path
);
1065 if (r
->objects
&& r
->objects
->multi_pack_index
) {
1066 close_midx(r
->objects
->multi_pack_index
);
1067 r
->objects
->multi_pack_index
= NULL
;
1070 if (remove_path(midx
))
1071 die(_("failed to clear multi-pack-index at %s"), midx
);
1076 static int verify_midx_error
;
1078 static void midx_report(const char *fmt
, ...)
1081 verify_midx_error
= 1;
1083 vfprintf(stderr
, fmt
, ap
);
1084 fprintf(stderr
, "\n");
1088 struct pair_pos_vs_id
1091 uint32_t pack_int_id
;
1094 static int compare_pair_pos_vs_id(const void *_a
, const void *_b
)
1096 struct pair_pos_vs_id
*a
= (struct pair_pos_vs_id
*)_a
;
1097 struct pair_pos_vs_id
*b
= (struct pair_pos_vs_id
*)_b
;
1099 return b
->pack_int_id
- a
->pack_int_id
;
1103 * Limit calls to display_progress() for performance reasons.
1104 * The interval here was arbitrarily chosen.
1106 #define SPARSE_PROGRESS_INTERVAL (1 << 12)
1107 #define midx_display_sparse_progress(progress, n) \
1109 uint64_t _n = (n); \
1110 if ((_n & (SPARSE_PROGRESS_INTERVAL - 1)) == 0) \
1111 display_progress(progress, _n); \
1114 int verify_midx_file(struct repository
*r
, const char *object_dir
, unsigned flags
)
1116 struct pair_pos_vs_id
*pairs
= NULL
;
1118 struct progress
*progress
= NULL
;
1119 struct multi_pack_index
*m
= load_multi_pack_index(object_dir
, 1);
1120 verify_midx_error
= 0;
1125 char *filename
= get_midx_filename(object_dir
);
1126 if (!stat(filename
, &sb
)) {
1127 error(_("multi-pack-index file exists, but failed to parse"));
1134 if (flags
& MIDX_PROGRESS
)
1135 progress
= start_progress(_("Looking for referenced packfiles"),
1137 for (i
= 0; i
< m
->num_packs
; i
++) {
1138 if (prepare_midx_pack(r
, m
, i
))
1139 midx_report("failed to load pack in position %d", i
);
1141 display_progress(progress
, i
+ 1);
1143 stop_progress(&progress
);
1145 for (i
= 0; i
< 255; i
++) {
1146 uint32_t oid_fanout1
= ntohl(m
->chunk_oid_fanout
[i
]);
1147 uint32_t oid_fanout2
= ntohl(m
->chunk_oid_fanout
[i
+ 1]);
1149 if (oid_fanout1
> oid_fanout2
)
1150 midx_report(_("oid fanout out of order: fanout[%d] = %"PRIx32
" > %"PRIx32
" = fanout[%d]"),
1151 i
, oid_fanout1
, oid_fanout2
, i
+ 1);
1154 if (m
->num_objects
== 0) {
1155 midx_report(_("the midx contains no oid"));
1157 * Remaining tests assume that we have objects, so we can
1160 return verify_midx_error
;
1163 if (flags
& MIDX_PROGRESS
)
1164 progress
= start_sparse_progress(_("Verifying OID order in multi-pack-index"),
1165 m
->num_objects
- 1);
1166 for (i
= 0; i
< m
->num_objects
- 1; i
++) {
1167 struct object_id oid1
, oid2
;
1169 nth_midxed_object_oid(&oid1
, m
, i
);
1170 nth_midxed_object_oid(&oid2
, m
, i
+ 1);
1172 if (oidcmp(&oid1
, &oid2
) >= 0)
1173 midx_report(_("oid lookup out of order: oid[%d] = %s >= %s = oid[%d]"),
1174 i
, oid_to_hex(&oid1
), oid_to_hex(&oid2
), i
+ 1);
1176 midx_display_sparse_progress(progress
, i
+ 1);
1178 stop_progress(&progress
);
1181 * Create an array mapping each object to its packfile id. Sort it
1182 * to group the objects by packfile. Use this permutation to visit
1183 * each of the objects and only require 1 packfile to be open at a
1186 ALLOC_ARRAY(pairs
, m
->num_objects
);
1187 for (i
= 0; i
< m
->num_objects
; i
++) {
1189 pairs
[i
].pack_int_id
= nth_midxed_pack_int_id(m
, i
);
1192 if (flags
& MIDX_PROGRESS
)
1193 progress
= start_sparse_progress(_("Sorting objects by packfile"),
1195 display_progress(progress
, 0); /* TODO: Measure QSORT() progress */
1196 QSORT(pairs
, m
->num_objects
, compare_pair_pos_vs_id
);
1197 stop_progress(&progress
);
1199 if (flags
& MIDX_PROGRESS
)
1200 progress
= start_sparse_progress(_("Verifying object offsets"), m
->num_objects
);
1201 for (i
= 0; i
< m
->num_objects
; i
++) {
1202 struct object_id oid
;
1203 struct pack_entry e
;
1204 off_t m_offset
, p_offset
;
1206 if (i
> 0 && pairs
[i
-1].pack_int_id
!= pairs
[i
].pack_int_id
&&
1207 m
->packs
[pairs
[i
-1].pack_int_id
])
1209 close_pack_fd(m
->packs
[pairs
[i
-1].pack_int_id
]);
1210 close_pack_index(m
->packs
[pairs
[i
-1].pack_int_id
]);
1213 nth_midxed_object_oid(&oid
, m
, pairs
[i
].pos
);
1215 if (!fill_midx_entry(r
, &oid
, &e
, m
)) {
1216 midx_report(_("failed to load pack entry for oid[%d] = %s"),
1217 pairs
[i
].pos
, oid_to_hex(&oid
));
1221 if (open_pack_index(e
.p
)) {
1222 midx_report(_("failed to load pack-index for packfile %s"),
1227 m_offset
= e
.offset
;
1228 p_offset
= find_pack_entry_one(oid
.hash
, e
.p
);
1230 if (m_offset
!= p_offset
)
1231 midx_report(_("incorrect object offset for oid[%d] = %s: %"PRIx64
" != %"PRIx64
),
1232 pairs
[i
].pos
, oid_to_hex(&oid
), m_offset
, p_offset
);
1234 midx_display_sparse_progress(progress
, i
+ 1);
1236 stop_progress(&progress
);
1240 return verify_midx_error
;
1243 int expire_midx_packs(struct repository
*r
, const char *object_dir
, unsigned flags
)
1245 uint32_t i
, *count
, result
= 0;
1246 struct string_list packs_to_drop
= STRING_LIST_INIT_DUP
;
1247 struct multi_pack_index
*m
= load_multi_pack_index(object_dir
, 1);
1248 struct progress
*progress
= NULL
;
1253 count
= xcalloc(m
->num_packs
, sizeof(uint32_t));
1255 if (flags
& MIDX_PROGRESS
)
1256 progress
= start_progress(_("Counting referenced objects"),
1258 for (i
= 0; i
< m
->num_objects
; i
++) {
1259 int pack_int_id
= nth_midxed_pack_int_id(m
, i
);
1260 count
[pack_int_id
]++;
1261 display_progress(progress
, i
+ 1);
1263 stop_progress(&progress
);
1265 if (flags
& MIDX_PROGRESS
)
1266 progress
= start_progress(_("Finding and deleting unreferenced packfiles"),
1268 for (i
= 0; i
< m
->num_packs
; i
++) {
1270 display_progress(progress
, i
+ 1);
1275 if (prepare_midx_pack(r
, m
, i
))
1278 if (m
->packs
[i
]->pack_keep
)
1281 pack_name
= xstrdup(m
->packs
[i
]->pack_name
);
1282 close_pack(m
->packs
[i
]);
1284 string_list_insert(&packs_to_drop
, m
->pack_names
[i
]);
1285 unlink_pack_path(pack_name
, 0);
1288 stop_progress(&progress
);
1292 if (packs_to_drop
.nr
)
1293 result
= write_midx_internal(object_dir
, m
, &packs_to_drop
, flags
);
1295 string_list_clear(&packs_to_drop
, 0);
1299 struct repack_info
{
1301 uint32_t referenced_objects
;
1302 uint32_t pack_int_id
;
1305 static int compare_by_mtime(const void *a_
, const void *b_
)
1307 const struct repack_info
*a
, *b
;
1309 a
= (const struct repack_info
*)a_
;
1310 b
= (const struct repack_info
*)b_
;
1312 if (a
->mtime
< b
->mtime
)
1314 if (a
->mtime
> b
->mtime
)
1319 static int fill_included_packs_all(struct repository
*r
,
1320 struct multi_pack_index
*m
,
1321 unsigned char *include_pack
)
1323 uint32_t i
, count
= 0;
1324 int pack_kept_objects
= 0;
1326 repo_config_get_bool(r
, "repack.packkeptobjects", &pack_kept_objects
);
1328 for (i
= 0; i
< m
->num_packs
; i
++) {
1329 if (prepare_midx_pack(r
, m
, i
))
1331 if (!pack_kept_objects
&& m
->packs
[i
]->pack_keep
)
1334 include_pack
[i
] = 1;
1341 static int fill_included_packs_batch(struct repository
*r
,
1342 struct multi_pack_index
*m
,
1343 unsigned char *include_pack
,
1346 uint32_t i
, packs_to_repack
;
1348 struct repack_info
*pack_info
= xcalloc(m
->num_packs
, sizeof(struct repack_info
));
1349 int pack_kept_objects
= 0;
1351 repo_config_get_bool(r
, "repack.packkeptobjects", &pack_kept_objects
);
1353 for (i
= 0; i
< m
->num_packs
; i
++) {
1354 pack_info
[i
].pack_int_id
= i
;
1356 if (prepare_midx_pack(r
, m
, i
))
1359 pack_info
[i
].mtime
= m
->packs
[i
]->mtime
;
1362 for (i
= 0; batch_size
&& i
< m
->num_objects
; i
++) {
1363 uint32_t pack_int_id
= nth_midxed_pack_int_id(m
, i
);
1364 pack_info
[pack_int_id
].referenced_objects
++;
1367 QSORT(pack_info
, m
->num_packs
, compare_by_mtime
);
1370 packs_to_repack
= 0;
1371 for (i
= 0; total_size
< batch_size
&& i
< m
->num_packs
; i
++) {
1372 int pack_int_id
= pack_info
[i
].pack_int_id
;
1373 struct packed_git
*p
= m
->packs
[pack_int_id
];
1374 size_t expected_size
;
1378 if (!pack_kept_objects
&& p
->pack_keep
)
1380 if (open_pack_index(p
) || !p
->num_objects
)
1383 expected_size
= (size_t)(p
->pack_size
1384 * pack_info
[i
].referenced_objects
);
1385 expected_size
/= p
->num_objects
;
1387 if (expected_size
>= batch_size
)
1391 total_size
+= expected_size
;
1392 include_pack
[pack_int_id
] = 1;
1397 if (packs_to_repack
< 2)
1403 int midx_repack(struct repository
*r
, const char *object_dir
, size_t batch_size
, unsigned flags
)
1407 unsigned char *include_pack
;
1408 struct child_process cmd
= CHILD_PROCESS_INIT
;
1410 struct strbuf base_name
= STRBUF_INIT
;
1411 struct multi_pack_index
*m
= load_multi_pack_index(object_dir
, 1);
1414 * When updating the default for these configuration
1415 * variables in builtin/repack.c, these must be adjusted
1418 int delta_base_offset
= 1;
1419 int use_delta_islands
= 0;
1424 include_pack
= xcalloc(m
->num_packs
, sizeof(unsigned char));
1427 if (fill_included_packs_batch(r
, m
, include_pack
, batch_size
))
1429 } else if (fill_included_packs_all(r
, m
, include_pack
))
1432 repo_config_get_bool(r
, "repack.usedeltabaseoffset", &delta_base_offset
);
1433 repo_config_get_bool(r
, "repack.usedeltaislands", &use_delta_islands
);
1435 strvec_push(&cmd
.args
, "pack-objects");
1437 strbuf_addstr(&base_name
, object_dir
);
1438 strbuf_addstr(&base_name
, "/pack/pack");
1439 strvec_push(&cmd
.args
, base_name
.buf
);
1441 if (delta_base_offset
)
1442 strvec_push(&cmd
.args
, "--delta-base-offset");
1443 if (use_delta_islands
)
1444 strvec_push(&cmd
.args
, "--delta-islands");
1446 if (flags
& MIDX_PROGRESS
)
1447 strvec_push(&cmd
.args
, "--progress");
1449 strvec_push(&cmd
.args
, "-q");
1451 strbuf_release(&base_name
);
1454 cmd
.in
= cmd
.out
= -1;
1456 if (start_command(&cmd
)) {
1457 error(_("could not start pack-objects"));
1462 cmd_in
= xfdopen(cmd
.in
, "w");
1464 for (i
= 0; i
< m
->num_objects
; i
++) {
1465 struct object_id oid
;
1466 uint32_t pack_int_id
= nth_midxed_pack_int_id(m
, i
);
1468 if (!include_pack
[pack_int_id
])
1471 nth_midxed_object_oid(&oid
, m
, i
);
1472 fprintf(cmd_in
, "%s\n", oid_to_hex(&oid
));
1476 if (finish_command(&cmd
)) {
1477 error(_("could not finish pack-objects"));
1482 result
= write_midx_internal(object_dir
, m
, NULL
, flags
);