]>
Commit | Line | Data |
---|---|---|
a3407730 | 1 | #include "cache.h" |
c4d25228 | 2 | #include "config.h" |
fc59e748 | 3 | #include "csum-file.h" |
396f2570 | 4 | #include "dir.h" |
fc59e748 | 5 | #include "lockfile.h" |
396f2570 | 6 | #include "packfile.h" |
4d80560c | 7 | #include "object-store.h" |
bc626927 | 8 | #include "hash-lookup.h" |
a3407730 | 9 | #include "midx.h" |
144d7033 | 10 | #include "progress.h" |
d829223a | 11 | #include "trace2.h" |
ce1e4a10 | 12 | #include "run-command.h" |
18e449f8 | 13 | #include "repository.h" |
63a8f0e9 | 14 | #include "chunk-format.h" |
38ff7cab | 15 | #include "pack.h" |
a3407730 | 16 | |
fc59e748 DS |
17 | #define MIDX_SIGNATURE 0x4d494458 /* "MIDX" */ |
18 | #define MIDX_VERSION 1 | |
4d80560c DS |
19 | #define MIDX_BYTE_FILE_VERSION 4 |
20 | #define MIDX_BYTE_HASH_VERSION 5 | |
21 | #define MIDX_BYTE_NUM_CHUNKS 6 | |
22 | #define MIDX_BYTE_NUM_PACKS 8 | |
fc59e748 | 23 | #define MIDX_HEADER_SIZE 12 |
aaa95dfa | 24 | #define MIDX_MIN_SIZE (MIDX_HEADER_SIZE + the_hash_algo->rawsz) |
fc59e748 | 25 | |
32f3c541 DS |
26 | #define MIDX_CHUNK_ALIGNMENT 4 |
27 | #define MIDX_CHUNKID_PACKNAMES 0x504e414d /* "PNAM" */ | |
d7cacf29 | 28 | #define MIDX_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */ |
0d5b3a5e | 29 | #define MIDX_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */ |
662148c4 DS |
30 | #define MIDX_CHUNKID_OBJECTOFFSETS 0x4f4f4646 /* "OOFF" */ |
31 | #define MIDX_CHUNKID_LARGEOFFSETS 0x4c4f4646 /* "LOFF" */ | |
d7cacf29 | 32 | #define MIDX_CHUNK_FANOUT_SIZE (sizeof(uint32_t) * 256) |
662148c4 DS |
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 | |
32f3c541 | 36 | |
19575c7c DS |
37 | #define PACK_EXPIRED UINT_MAX |
38 | ||
d9607542 DS |
39 | static uint8_t oid_version(void) |
40 | { | |
41 | switch (hash_algo_by_ptr(the_hash_algo)) { | |
42 | case GIT_HASH_SHA1: | |
43 | return 1; | |
44 | case GIT_HASH_SHA256: | |
45 | return 2; | |
46 | default: | |
47 | die(_("invalid hash version")); | |
48 | } | |
49 | } | |
50 | ||
f894081d TB |
51 | static const unsigned char *get_midx_checksum(struct multi_pack_index *m) |
52 | { | |
53 | return m->data + m->data_len - the_hash_algo->rawsz; | |
54 | } | |
55 | ||
fc59e748 DS |
56 | static char *get_midx_filename(const char *object_dir) |
57 | { | |
58 | return xstrfmt("%s/pack/multi-pack-index", object_dir); | |
59 | } | |
60 | ||
f894081d TB |
61 | char *get_midx_rev_filename(struct multi_pack_index *m) |
62 | { | |
63 | return xstrfmt("%s/pack/multi-pack-index-%s.rev", | |
64 | m->object_dir, hash_to_hex(get_midx_checksum(m))); | |
65 | } | |
66 | ||
6ab3b8b8 DS |
67 | static int midx_read_oid_fanout(const unsigned char *chunk_start, |
68 | size_t chunk_size, void *data) | |
69 | { | |
70 | struct multi_pack_index *m = data; | |
71 | m->chunk_oid_fanout = (uint32_t *)chunk_start; | |
72 | ||
73 | if (chunk_size != 4 * 256) { | |
74 | error(_("multi-pack-index OID fanout is of the wrong size")); | |
75 | return 1; | |
76 | } | |
77 | return 0; | |
78 | } | |
79 | ||
2cf489a3 | 80 | struct multi_pack_index *load_multi_pack_index(const char *object_dir, int local) |
4d80560c DS |
81 | { |
82 | struct multi_pack_index *m = NULL; | |
83 | int fd; | |
84 | struct stat st; | |
85 | size_t midx_size; | |
86 | void *midx_map = NULL; | |
87 | uint32_t hash_version; | |
88 | char *midx_name = get_midx_filename(object_dir); | |
32f3c541 | 89 | uint32_t i; |
3227565c | 90 | const char *cur_pack_name; |
6ab3b8b8 | 91 | struct chunkfile *cf = NULL; |
4d80560c DS |
92 | |
93 | fd = git_open(midx_name); | |
94 | ||
95 | if (fd < 0) | |
96 | goto cleanup_fail; | |
97 | if (fstat(fd, &st)) { | |
98 | error_errno(_("failed to read %s"), midx_name); | |
99 | goto cleanup_fail; | |
100 | } | |
101 | ||
102 | midx_size = xsize_t(st.st_size); | |
103 | ||
104 | if (midx_size < MIDX_MIN_SIZE) { | |
105 | error(_("multi-pack-index file %s is too small"), midx_name); | |
106 | goto cleanup_fail; | |
107 | } | |
108 | ||
109 | FREE_AND_NULL(midx_name); | |
110 | ||
111 | midx_map = xmmap(NULL, midx_size, PROT_READ, MAP_PRIVATE, fd, 0); | |
6c7ff7cf | 112 | close(fd); |
4d80560c | 113 | |
577314ca | 114 | FLEX_ALLOC_STR(m, object_dir, object_dir); |
4d80560c DS |
115 | m->data = midx_map; |
116 | m->data_len = midx_size; | |
2cf489a3 | 117 | m->local = local; |
4d80560c DS |
118 | |
119 | m->signature = get_be32(m->data); | |
53ad0407 DS |
120 | if (m->signature != MIDX_SIGNATURE) |
121 | die(_("multi-pack-index signature 0x%08x does not match signature 0x%08x"), | |
4d80560c | 122 | m->signature, MIDX_SIGNATURE); |
4d80560c DS |
123 | |
124 | m->version = m->data[MIDX_BYTE_FILE_VERSION]; | |
53ad0407 DS |
125 | if (m->version != MIDX_VERSION) |
126 | die(_("multi-pack-index version %d not recognized"), | |
4d80560c | 127 | m->version); |
4d80560c DS |
128 | |
129 | hash_version = m->data[MIDX_BYTE_HASH_VERSION]; | |
d9607542 DS |
130 | if (hash_version != oid_version()) { |
131 | error(_("multi-pack-index hash version %u does not match version %u"), | |
132 | hash_version, oid_version()); | |
133 | goto cleanup_fail; | |
134 | } | |
aaa95dfa | 135 | m->hash_len = the_hash_algo->rawsz; |
4d80560c DS |
136 | |
137 | m->num_chunks = m->data[MIDX_BYTE_NUM_CHUNKS]; | |
138 | ||
139 | m->num_packs = get_be32(m->data + MIDX_BYTE_NUM_PACKS); | |
140 | ||
6ab3b8b8 | 141 | cf = init_chunkfile(NULL); |
32f3c541 | 142 | |
6ab3b8b8 DS |
143 | if (read_table_of_contents(cf, m->data, midx_size, |
144 | MIDX_HEADER_SIZE, m->num_chunks)) | |
145 | goto cleanup_fail; | |
32f3c541 | 146 | |
6ab3b8b8 | 147 | if (pair_chunk(cf, MIDX_CHUNKID_PACKNAMES, &m->chunk_pack_names) == CHUNK_NOT_FOUND) |
32f3c541 | 148 | die(_("multi-pack-index missing required pack-name chunk")); |
6ab3b8b8 | 149 | if (read_chunk(cf, MIDX_CHUNKID_OIDFANOUT, midx_read_oid_fanout, m) == CHUNK_NOT_FOUND) |
d7cacf29 | 150 | die(_("multi-pack-index missing required OID fanout chunk")); |
6ab3b8b8 | 151 | if (pair_chunk(cf, MIDX_CHUNKID_OIDLOOKUP, &m->chunk_oid_lookup) == CHUNK_NOT_FOUND) |
0d5b3a5e | 152 | die(_("multi-pack-index missing required OID lookup chunk")); |
6ab3b8b8 | 153 | if (pair_chunk(cf, MIDX_CHUNKID_OBJECTOFFSETS, &m->chunk_object_offsets) == CHUNK_NOT_FOUND) |
662148c4 | 154 | die(_("multi-pack-index missing required object offsets chunk")); |
32f3c541 | 155 | |
6ab3b8b8 DS |
156 | pair_chunk(cf, MIDX_CHUNKID_LARGEOFFSETS, &m->chunk_large_offsets); |
157 | ||
d7cacf29 DS |
158 | m->num_objects = ntohl(m->chunk_oid_fanout[255]); |
159 | ||
3227565c | 160 | m->pack_names = xcalloc(m->num_packs, sizeof(*m->pack_names)); |
3715a633 | 161 | m->packs = xcalloc(m->num_packs, sizeof(*m->packs)); |
3227565c DS |
162 | |
163 | cur_pack_name = (const char *)m->chunk_pack_names; | |
164 | for (i = 0; i < m->num_packs; i++) { | |
165 | m->pack_names[i] = cur_pack_name; | |
166 | ||
167 | cur_pack_name += strlen(cur_pack_name) + 1; | |
168 | ||
8e72a3c3 DS |
169 | if (i && strcmp(m->pack_names[i], m->pack_names[i - 1]) <= 0) |
170 | die(_("multi-pack-index pack names out of order: '%s' before '%s'"), | |
3227565c DS |
171 | m->pack_names[i - 1], |
172 | m->pack_names[i]); | |
3227565c DS |
173 | } |
174 | ||
d829223a JH |
175 | trace2_data_intmax("midx", the_repository, "load/num_packs", m->num_packs); |
176 | trace2_data_intmax("midx", the_repository, "load/num_objects", m->num_objects); | |
177 | ||
4d80560c DS |
178 | return m; |
179 | ||
180 | cleanup_fail: | |
181 | free(m); | |
182 | free(midx_name); | |
6ab3b8b8 | 183 | free(cf); |
4d80560c DS |
184 | if (midx_map) |
185 | munmap(midx_map, midx_size); | |
186 | if (0 <= fd) | |
187 | close(fd); | |
188 | return NULL; | |
189 | } | |
190 | ||
1dcd9f20 | 191 | void close_midx(struct multi_pack_index *m) |
a40498a1 DS |
192 | { |
193 | uint32_t i; | |
1dcd9f20 DS |
194 | |
195 | if (!m) | |
196 | return; | |
197 | ||
a40498a1 | 198 | munmap((unsigned char *)m->data, m->data_len); |
a40498a1 DS |
199 | |
200 | for (i = 0; i < m->num_packs; i++) { | |
af96fe33 DS |
201 | if (m->packs[i]) |
202 | m->packs[i]->multi_pack_index = 0; | |
a40498a1 DS |
203 | } |
204 | FREE_AND_NULL(m->packs); | |
205 | FREE_AND_NULL(m->pack_names); | |
206 | } | |
207 | ||
64404a24 | 208 | int prepare_midx_pack(struct repository *r, struct multi_pack_index *m, uint32_t pack_int_id) |
3715a633 DS |
209 | { |
210 | struct strbuf pack_name = STRBUF_INIT; | |
af96fe33 | 211 | struct packed_git *p; |
3715a633 DS |
212 | |
213 | if (pack_int_id >= m->num_packs) | |
d355e46a | 214 | die(_("bad pack-int-id: %u (%u total packs)"), |
cc6af73c | 215 | pack_int_id, m->num_packs); |
3715a633 DS |
216 | |
217 | if (m->packs[pack_int_id]) | |
218 | return 0; | |
219 | ||
220 | strbuf_addf(&pack_name, "%s/pack/%s", m->object_dir, | |
221 | m->pack_names[pack_int_id]); | |
222 | ||
af96fe33 | 223 | p = add_packed_git(pack_name.buf, pack_name.len, m->local); |
3715a633 | 224 | strbuf_release(&pack_name); |
af96fe33 DS |
225 | |
226 | if (!p) | |
227 | return 1; | |
228 | ||
229 | p->multi_pack_index = 1; | |
230 | m->packs[pack_int_id] = p; | |
231 | install_packed_git(r, p); | |
232 | list_add_tail(&p->mru, &r->objects->packed_git_mru); | |
233 | ||
234 | return 0; | |
3715a633 DS |
235 | } |
236 | ||
237 | int bsearch_midx(const struct object_id *oid, struct multi_pack_index *m, uint32_t *result) | |
238 | { | |
239 | return bsearch_hash(oid->hash, m->chunk_oid_fanout, m->chunk_oid_lookup, | |
aaa95dfa | 240 | the_hash_algo->rawsz, result); |
3715a633 DS |
241 | } |
242 | ||
8aac67a1 DS |
243 | struct object_id *nth_midxed_object_oid(struct object_id *oid, |
244 | struct multi_pack_index *m, | |
245 | uint32_t n) | |
246 | { | |
247 | if (n >= m->num_objects) | |
248 | return NULL; | |
249 | ||
250 | hashcpy(oid->hash, m->chunk_oid_lookup + m->hash_len * n); | |
251 | return oid; | |
252 | } | |
253 | ||
62f2c1b5 | 254 | off_t nth_midxed_offset(struct multi_pack_index *m, uint32_t pos) |
3715a633 DS |
255 | { |
256 | const unsigned char *offset_data; | |
257 | uint32_t offset32; | |
258 | ||
329fac3a | 259 | offset_data = m->chunk_object_offsets + (off_t)pos * MIDX_CHUNK_OFFSET_WIDTH; |
3715a633 DS |
260 | offset32 = get_be32(offset_data + sizeof(uint32_t)); |
261 | ||
262 | if (m->chunk_large_offsets && offset32 & MIDX_LARGE_OFFSET_NEEDED) { | |
d8ac9ee1 | 263 | if (sizeof(off_t) < sizeof(uint64_t)) |
3715a633 DS |
264 | die(_("multi-pack-index stores a 64-bit offset, but off_t is too small")); |
265 | ||
266 | offset32 ^= MIDX_LARGE_OFFSET_NEEDED; | |
267 | return get_be64(m->chunk_large_offsets + sizeof(uint64_t) * offset32); | |
268 | } | |
269 | ||
270 | return offset32; | |
271 | } | |
272 | ||
62f2c1b5 | 273 | uint32_t nth_midxed_pack_int_id(struct multi_pack_index *m, uint32_t pos) |
3715a633 | 274 | { |
329fac3a DS |
275 | return get_be32(m->chunk_object_offsets + |
276 | (off_t)pos * MIDX_CHUNK_OFFSET_WIDTH); | |
3715a633 DS |
277 | } |
278 | ||
64404a24 DS |
279 | static int nth_midxed_pack_entry(struct repository *r, |
280 | struct multi_pack_index *m, | |
281 | struct pack_entry *e, | |
282 | uint32_t pos) | |
3715a633 DS |
283 | { |
284 | uint32_t pack_int_id; | |
285 | struct packed_git *p; | |
286 | ||
287 | if (pos >= m->num_objects) | |
288 | return 0; | |
289 | ||
290 | pack_int_id = nth_midxed_pack_int_id(m, pos); | |
291 | ||
64404a24 | 292 | if (prepare_midx_pack(r, m, pack_int_id)) |
506ec2fb | 293 | return 0; |
3715a633 DS |
294 | p = m->packs[pack_int_id]; |
295 | ||
296 | /* | |
297 | * We are about to tell the caller where they can locate the | |
298 | * requested object. We better make sure the packfile is | |
299 | * still here and can be accessed before supplying that | |
300 | * answer, as it may have been deleted since the MIDX was | |
301 | * loaded! | |
302 | */ | |
303 | if (!is_pack_valid(p)) | |
304 | return 0; | |
305 | ||
c39b02ae DS |
306 | if (p->num_bad_objects) { |
307 | uint32_t i; | |
308 | struct object_id oid; | |
309 | nth_midxed_object_oid(&oid, m, pos); | |
310 | for (i = 0; i < p->num_bad_objects; i++) | |
e43d2dcc JK |
311 | if (hasheq(oid.hash, |
312 | p->bad_object_sha1 + the_hash_algo->rawsz * i)) | |
c39b02ae DS |
313 | return 0; |
314 | } | |
315 | ||
3715a633 DS |
316 | e->offset = nth_midxed_offset(m, pos); |
317 | e->p = p; | |
318 | ||
319 | return 1; | |
320 | } | |
321 | ||
64404a24 DS |
322 | int fill_midx_entry(struct repository * r, |
323 | const struct object_id *oid, | |
324 | struct pack_entry *e, | |
325 | struct multi_pack_index *m) | |
3715a633 DS |
326 | { |
327 | uint32_t pos; | |
328 | ||
329 | if (!bsearch_midx(oid, m, &pos)) | |
330 | return 0; | |
331 | ||
64404a24 | 332 | return nth_midxed_pack_entry(r, m, e, pos); |
3715a633 DS |
333 | } |
334 | ||
013fd7ad JK |
335 | /* Match "foo.idx" against either "foo.pack" _or_ "foo.idx". */ |
336 | static int cmp_idx_or_pack_name(const char *idx_or_pack_name, | |
337 | const char *idx_name) | |
338 | { | |
339 | /* Skip past any initial matching prefix. */ | |
340 | while (*idx_name && *idx_name == *idx_or_pack_name) { | |
341 | idx_name++; | |
342 | idx_or_pack_name++; | |
343 | } | |
344 | ||
345 | /* | |
346 | * If we didn't match completely, we may have matched "pack-1234." and | |
347 | * be left with "idx" and "pack" respectively, which is also OK. We do | |
348 | * not have to check for "idx" and "idx", because that would have been | |
349 | * a complete match (and in that case these strcmps will be false, but | |
350 | * we'll correctly return 0 from the final strcmp() below. | |
351 | * | |
352 | * Technically this matches "fooidx" and "foopack", but we'd never have | |
353 | * such names in the first place. | |
354 | */ | |
355 | if (!strcmp(idx_name, "idx") && !strcmp(idx_or_pack_name, "pack")) | |
356 | return 0; | |
357 | ||
358 | /* | |
359 | * This not only checks for a complete match, but also orders based on | |
360 | * the first non-identical character, which means our ordering will | |
361 | * match a raw strcmp(). That makes it OK to use this to binary search | |
362 | * a naively-sorted list. | |
363 | */ | |
364 | return strcmp(idx_or_pack_name, idx_name); | |
365 | } | |
366 | ||
367 | int midx_contains_pack(struct multi_pack_index *m, const char *idx_or_pack_name) | |
a40498a1 DS |
368 | { |
369 | uint32_t first = 0, last = m->num_packs; | |
370 | ||
371 | while (first < last) { | |
372 | uint32_t mid = first + (last - first) / 2; | |
373 | const char *current; | |
374 | int cmp; | |
375 | ||
376 | current = m->pack_names[mid]; | |
013fd7ad | 377 | cmp = cmp_idx_or_pack_name(idx_or_pack_name, current); |
a40498a1 DS |
378 | if (!cmp) |
379 | return 1; | |
380 | if (cmp > 0) { | |
381 | first = mid + 1; | |
382 | continue; | |
383 | } | |
384 | last = mid; | |
385 | } | |
386 | ||
387 | return 0; | |
388 | } | |
389 | ||
2cf489a3 | 390 | int prepare_multi_pack_index_one(struct repository *r, const char *object_dir, int local) |
c4d25228 | 391 | { |
29e2016b | 392 | struct multi_pack_index *m; |
c4d25228 | 393 | struct multi_pack_index *m_search; |
c4d25228 | 394 | |
18e449f8 DS |
395 | prepare_repo_settings(r); |
396 | if (!r->settings.core_multi_pack_index) | |
c4d25228 DS |
397 | return 0; |
398 | ||
29e2016b | 399 | for (m_search = r->objects->multi_pack_index; m_search; m_search = m_search->next) |
c4d25228 DS |
400 | if (!strcmp(object_dir, m_search->object_dir)) |
401 | return 1; | |
402 | ||
29e2016b | 403 | m = load_multi_pack_index(object_dir, local); |
c4d25228 | 404 | |
29e2016b | 405 | if (m) { |
59552fb3 TB |
406 | struct multi_pack_index *mp = r->objects->multi_pack_index; |
407 | if (mp) { | |
408 | m->next = mp->next; | |
409 | mp->next = m; | |
410 | } else | |
411 | r->objects->multi_pack_index = m; | |
c4d25228 DS |
412 | return 1; |
413 | } | |
414 | ||
415 | return 0; | |
416 | } | |
417 | ||
fc59e748 DS |
418 | static size_t write_midx_header(struct hashfile *f, |
419 | unsigned char num_chunks, | |
420 | uint32_t num_packs) | |
421 | { | |
fc59e748 | 422 | hashwrite_be32(f, MIDX_SIGNATURE); |
014f1447 RS |
423 | hashwrite_u8(f, MIDX_VERSION); |
424 | hashwrite_u8(f, oid_version()); | |
425 | hashwrite_u8(f, num_chunks); | |
426 | hashwrite_u8(f, 0); /* unused */ | |
fc59e748 DS |
427 | hashwrite_be32(f, num_packs); |
428 | ||
429 | return MIDX_HEADER_SIZE; | |
430 | } | |
431 | ||
d01bf2e6 DS |
432 | struct pack_info { |
433 | uint32_t orig_pack_int_id; | |
434 | char *pack_name; | |
435 | struct packed_git *p; | |
19575c7c | 436 | unsigned expired : 1; |
d01bf2e6 DS |
437 | }; |
438 | ||
439 | static int pack_info_compare(const void *_a, const void *_b) | |
440 | { | |
441 | struct pack_info *a = (struct pack_info *)_a; | |
442 | struct pack_info *b = (struct pack_info *)_b; | |
443 | return strcmp(a->pack_name, b->pack_name); | |
444 | } | |
445 | ||
9218c6a4 TB |
446 | static int idx_or_pack_name_cmp(const void *_va, const void *_vb) |
447 | { | |
448 | const char *pack_name = _va; | |
449 | const struct pack_info *compar = _vb; | |
450 | ||
451 | return cmp_idx_or_pack_name(pack_name, compar->pack_name); | |
452 | } | |
453 | ||
577dc496 | 454 | struct write_midx_context { |
d01bf2e6 | 455 | struct pack_info *info; |
396f2570 | 456 | uint32_t nr; |
d01bf2e6 | 457 | uint32_t alloc; |
a40498a1 | 458 | struct multi_pack_index *m; |
840cef0c WB |
459 | struct progress *progress; |
460 | unsigned pack_paths_checked; | |
31bda9a2 DS |
461 | |
462 | struct pack_midx_entry *entries; | |
463 | uint32_t entries_nr; | |
7a3ada11 DS |
464 | |
465 | uint32_t *pack_perm; | |
38ff7cab | 466 | uint32_t *pack_order; |
7a3ada11 | 467 | unsigned large_offsets_needed:1; |
980f525c | 468 | uint32_t num_large_offsets; |
9218c6a4 TB |
469 | |
470 | int preferred_pack_idx; | |
396f2570 DS |
471 | }; |
472 | ||
473 | static void add_pack_to_midx(const char *full_path, size_t full_path_len, | |
474 | const char *file_name, void *data) | |
475 | { | |
577dc496 | 476 | struct write_midx_context *ctx = data; |
396f2570 DS |
477 | |
478 | if (ends_with(file_name, ".idx")) { | |
577dc496 DS |
479 | display_progress(ctx->progress, ++ctx->pack_paths_checked); |
480 | if (ctx->m && midx_contains_pack(ctx->m, file_name)) | |
a40498a1 DS |
481 | return; |
482 | ||
577dc496 | 483 | ALLOC_GROW(ctx->info, ctx->nr + 1, ctx->alloc); |
396f2570 | 484 | |
577dc496 DS |
485 | ctx->info[ctx->nr].p = add_packed_git(full_path, |
486 | full_path_len, | |
487 | 0); | |
fe1ed56f | 488 | |
577dc496 | 489 | if (!ctx->info[ctx->nr].p) { |
396f2570 DS |
490 | warning(_("failed to add packfile '%s'"), |
491 | full_path); | |
492 | return; | |
493 | } | |
494 | ||
577dc496 | 495 | if (open_pack_index(ctx->info[ctx->nr].p)) { |
fe1ed56f DS |
496 | warning(_("failed to open pack-index '%s'"), |
497 | full_path); | |
577dc496 DS |
498 | close_pack(ctx->info[ctx->nr].p); |
499 | FREE_AND_NULL(ctx->info[ctx->nr].p); | |
fe1ed56f DS |
500 | return; |
501 | } | |
502 | ||
577dc496 DS |
503 | ctx->info[ctx->nr].pack_name = xstrdup(file_name); |
504 | ctx->info[ctx->nr].orig_pack_int_id = ctx->nr; | |
505 | ctx->info[ctx->nr].expired = 0; | |
506 | ctx->nr++; | |
396f2570 DS |
507 | } |
508 | } | |
509 | ||
fe1ed56f DS |
510 | struct pack_midx_entry { |
511 | struct object_id oid; | |
512 | uint32_t pack_int_id; | |
513 | time_t pack_mtime; | |
514 | uint64_t offset; | |
9218c6a4 | 515 | unsigned preferred : 1; |
fe1ed56f DS |
516 | }; |
517 | ||
518 | static int midx_oid_compare(const void *_a, const void *_b) | |
519 | { | |
520 | const struct pack_midx_entry *a = (const struct pack_midx_entry *)_a; | |
521 | const struct pack_midx_entry *b = (const struct pack_midx_entry *)_b; | |
522 | int cmp = oidcmp(&a->oid, &b->oid); | |
523 | ||
524 | if (cmp) | |
525 | return cmp; | |
526 | ||
9218c6a4 TB |
527 | /* Sort objects in a preferred pack first when multiple copies exist. */ |
528 | if (a->preferred > b->preferred) | |
529 | return -1; | |
530 | if (a->preferred < b->preferred) | |
531 | return 1; | |
532 | ||
fe1ed56f DS |
533 | if (a->pack_mtime > b->pack_mtime) |
534 | return -1; | |
535 | else if (a->pack_mtime < b->pack_mtime) | |
536 | return 1; | |
537 | ||
538 | return a->pack_int_id - b->pack_int_id; | |
539 | } | |
540 | ||
a40498a1 | 541 | static int nth_midxed_pack_midx_entry(struct multi_pack_index *m, |
a40498a1 DS |
542 | struct pack_midx_entry *e, |
543 | uint32_t pos) | |
544 | { | |
545 | if (pos >= m->num_objects) | |
546 | return 1; | |
547 | ||
548 | nth_midxed_object_oid(&e->oid, m, pos); | |
d01bf2e6 | 549 | e->pack_int_id = nth_midxed_pack_int_id(m, pos); |
a40498a1 DS |
550 | e->offset = nth_midxed_offset(m, pos); |
551 | ||
552 | /* consider objects in midx to be from "old" packs */ | |
553 | e->pack_mtime = 0; | |
554 | return 0; | |
555 | } | |
556 | ||
fe1ed56f DS |
557 | static void fill_pack_entry(uint32_t pack_int_id, |
558 | struct packed_git *p, | |
559 | uint32_t cur_object, | |
9218c6a4 TB |
560 | struct pack_midx_entry *entry, |
561 | int preferred) | |
fe1ed56f | 562 | { |
0763671b | 563 | if (nth_packed_object_id(&entry->oid, p, cur_object) < 0) |
fe1ed56f DS |
564 | die(_("failed to locate object %d in packfile"), cur_object); |
565 | ||
566 | entry->pack_int_id = pack_int_id; | |
567 | entry->pack_mtime = p->mtime; | |
568 | ||
569 | entry->offset = nth_packed_object_offset(p, cur_object); | |
9218c6a4 | 570 | entry->preferred = !!preferred; |
fe1ed56f DS |
571 | } |
572 | ||
573 | /* | |
574 | * It is possible to artificially get into a state where there are many | |
575 | * duplicate copies of objects. That can create high memory pressure if | |
576 | * we are to create a list of all objects before de-duplication. To reduce | |
577 | * this memory pressure without a significant performance drop, automatically | |
578 | * group objects by the first byte of their object id. Use the IDX fanout | |
579 | * tables to group the data, copy to a local array, then sort. | |
580 | * | |
581 | * Copy only the de-duplicated entries (selected by most-recent modified time | |
582 | * of a packfile containing the object). | |
583 | */ | |
a40498a1 | 584 | static struct pack_midx_entry *get_sorted_entries(struct multi_pack_index *m, |
d01bf2e6 | 585 | struct pack_info *info, |
fe1ed56f | 586 | uint32_t nr_packs, |
9218c6a4 TB |
587 | uint32_t *nr_objects, |
588 | int preferred_pack) | |
fe1ed56f DS |
589 | { |
590 | uint32_t cur_fanout, cur_pack, cur_object; | |
591 | uint32_t alloc_fanout, alloc_objects, total_objects = 0; | |
592 | struct pack_midx_entry *entries_by_fanout = NULL; | |
593 | struct pack_midx_entry *deduplicated_entries = NULL; | |
a40498a1 | 594 | uint32_t start_pack = m ? m->num_packs : 0; |
fe1ed56f | 595 | |
a40498a1 | 596 | for (cur_pack = start_pack; cur_pack < nr_packs; cur_pack++) |
d01bf2e6 | 597 | total_objects += info[cur_pack].p->num_objects; |
fe1ed56f DS |
598 | |
599 | /* | |
600 | * As we de-duplicate by fanout value, we expect the fanout | |
601 | * slices to be evenly distributed, with some noise. Hence, | |
602 | * allocate slightly more than one 256th. | |
603 | */ | |
604 | alloc_objects = alloc_fanout = total_objects > 3200 ? total_objects / 200 : 16; | |
605 | ||
606 | ALLOC_ARRAY(entries_by_fanout, alloc_fanout); | |
607 | ALLOC_ARRAY(deduplicated_entries, alloc_objects); | |
608 | *nr_objects = 0; | |
609 | ||
610 | for (cur_fanout = 0; cur_fanout < 256; cur_fanout++) { | |
611 | uint32_t nr_fanout = 0; | |
612 | ||
a40498a1 DS |
613 | if (m) { |
614 | uint32_t start = 0, end; | |
615 | ||
616 | if (cur_fanout) | |
617 | start = ntohl(m->chunk_oid_fanout[cur_fanout - 1]); | |
618 | end = ntohl(m->chunk_oid_fanout[cur_fanout]); | |
619 | ||
620 | for (cur_object = start; cur_object < end; cur_object++) { | |
621 | ALLOC_GROW(entries_by_fanout, nr_fanout + 1, alloc_fanout); | |
d01bf2e6 | 622 | nth_midxed_pack_midx_entry(m, |
a40498a1 DS |
623 | &entries_by_fanout[nr_fanout], |
624 | cur_object); | |
9218c6a4 TB |
625 | if (nth_midxed_pack_int_id(m, cur_object) == preferred_pack) |
626 | entries_by_fanout[nr_fanout].preferred = 1; | |
627 | else | |
628 | entries_by_fanout[nr_fanout].preferred = 0; | |
a40498a1 DS |
629 | nr_fanout++; |
630 | } | |
631 | } | |
632 | ||
633 | for (cur_pack = start_pack; cur_pack < nr_packs; cur_pack++) { | |
fe1ed56f | 634 | uint32_t start = 0, end; |
9218c6a4 | 635 | int preferred = cur_pack == preferred_pack; |
fe1ed56f DS |
636 | |
637 | if (cur_fanout) | |
d01bf2e6 DS |
638 | start = get_pack_fanout(info[cur_pack].p, cur_fanout - 1); |
639 | end = get_pack_fanout(info[cur_pack].p, cur_fanout); | |
fe1ed56f DS |
640 | |
641 | for (cur_object = start; cur_object < end; cur_object++) { | |
642 | ALLOC_GROW(entries_by_fanout, nr_fanout + 1, alloc_fanout); | |
9218c6a4 TB |
643 | fill_pack_entry(cur_pack, |
644 | info[cur_pack].p, | |
645 | cur_object, | |
646 | &entries_by_fanout[nr_fanout], | |
647 | preferred); | |
fe1ed56f DS |
648 | nr_fanout++; |
649 | } | |
650 | } | |
651 | ||
652 | QSORT(entries_by_fanout, nr_fanout, midx_oid_compare); | |
653 | ||
654 | /* | |
655 | * The batch is now sorted by OID and then mtime (descending). | |
656 | * Take only the first duplicate. | |
657 | */ | |
658 | for (cur_object = 0; cur_object < nr_fanout; cur_object++) { | |
e43d2dcc JK |
659 | if (cur_object && oideq(&entries_by_fanout[cur_object - 1].oid, |
660 | &entries_by_fanout[cur_object].oid)) | |
fe1ed56f DS |
661 | continue; |
662 | ||
663 | ALLOC_GROW(deduplicated_entries, *nr_objects + 1, alloc_objects); | |
664 | memcpy(&deduplicated_entries[*nr_objects], | |
665 | &entries_by_fanout[cur_object], | |
666 | sizeof(struct pack_midx_entry)); | |
667 | (*nr_objects)++; | |
668 | } | |
669 | } | |
670 | ||
671 | free(entries_by_fanout); | |
672 | return deduplicated_entries; | |
673 | } | |
674 | ||
0ccd713c | 675 | static int write_midx_pack_names(struct hashfile *f, void *data) |
32f3c541 | 676 | { |
b4d94142 | 677 | struct write_midx_context *ctx = data; |
32f3c541 DS |
678 | uint32_t i; |
679 | unsigned char padding[MIDX_CHUNK_ALIGNMENT]; | |
680 | size_t written = 0; | |
681 | ||
b4d94142 | 682 | for (i = 0; i < ctx->nr; i++) { |
19575c7c DS |
683 | size_t writelen; |
684 | ||
b4d94142 | 685 | if (ctx->info[i].expired) |
19575c7c | 686 | continue; |
32f3c541 | 687 | |
b4d94142 | 688 | if (i && strcmp(ctx->info[i].pack_name, ctx->info[i - 1].pack_name) <= 0) |
32f3c541 | 689 | BUG("incorrect pack-file order: %s before %s", |
b4d94142 DS |
690 | ctx->info[i - 1].pack_name, |
691 | ctx->info[i].pack_name); | |
32f3c541 | 692 | |
b4d94142 DS |
693 | writelen = strlen(ctx->info[i].pack_name) + 1; |
694 | hashwrite(f, ctx->info[i].pack_name, writelen); | |
32f3c541 DS |
695 | written += writelen; |
696 | } | |
697 | ||
698 | /* add padding to be aligned */ | |
699 | i = MIDX_CHUNK_ALIGNMENT - (written % MIDX_CHUNK_ALIGNMENT); | |
700 | if (i < MIDX_CHUNK_ALIGNMENT) { | |
701 | memset(padding, 0, sizeof(padding)); | |
702 | hashwrite(f, padding, i); | |
32f3c541 DS |
703 | } |
704 | ||
0ccd713c | 705 | return 0; |
32f3c541 DS |
706 | } |
707 | ||
0ccd713c DS |
708 | static int write_midx_oid_fanout(struct hashfile *f, |
709 | void *data) | |
d7cacf29 | 710 | { |
31bda9a2 DS |
711 | struct write_midx_context *ctx = data; |
712 | struct pack_midx_entry *list = ctx->entries; | |
713 | struct pack_midx_entry *last = ctx->entries + ctx->entries_nr; | |
d7cacf29 DS |
714 | uint32_t count = 0; |
715 | uint32_t i; | |
716 | ||
717 | /* | |
718 | * Write the first-level table (the list is sorted, | |
719 | * but we use a 256-entry lookup to be able to avoid | |
720 | * having to do eight extra binary search iterations). | |
721 | */ | |
722 | for (i = 0; i < 256; i++) { | |
723 | struct pack_midx_entry *next = list; | |
724 | ||
725 | while (next < last && next->oid.hash[0] == i) { | |
726 | count++; | |
727 | next++; | |
728 | } | |
729 | ||
730 | hashwrite_be32(f, count); | |
731 | list = next; | |
732 | } | |
733 | ||
0ccd713c | 734 | return 0; |
d7cacf29 DS |
735 | } |
736 | ||
0ccd713c DS |
737 | static int write_midx_oid_lookup(struct hashfile *f, |
738 | void *data) | |
0d5b3a5e | 739 | { |
31bda9a2 DS |
740 | struct write_midx_context *ctx = data; |
741 | unsigned char hash_len = the_hash_algo->rawsz; | |
742 | struct pack_midx_entry *list = ctx->entries; | |
0d5b3a5e | 743 | uint32_t i; |
0d5b3a5e | 744 | |
31bda9a2 | 745 | for (i = 0; i < ctx->entries_nr; i++) { |
0d5b3a5e DS |
746 | struct pack_midx_entry *obj = list++; |
747 | ||
31bda9a2 | 748 | if (i < ctx->entries_nr - 1) { |
0d5b3a5e DS |
749 | struct pack_midx_entry *next = list; |
750 | if (oidcmp(&obj->oid, &next->oid) >= 0) | |
751 | BUG("OIDs not in order: %s >= %s", | |
752 | oid_to_hex(&obj->oid), | |
753 | oid_to_hex(&next->oid)); | |
754 | } | |
755 | ||
756 | hashwrite(f, obj->oid.hash, (int)hash_len); | |
0d5b3a5e DS |
757 | } |
758 | ||
0ccd713c | 759 | return 0; |
0d5b3a5e DS |
760 | } |
761 | ||
0ccd713c DS |
762 | static int write_midx_object_offsets(struct hashfile *f, |
763 | void *data) | |
662148c4 | 764 | { |
7a3ada11 DS |
765 | struct write_midx_context *ctx = data; |
766 | struct pack_midx_entry *list = ctx->entries; | |
662148c4 | 767 | uint32_t i, nr_large_offset = 0; |
662148c4 | 768 | |
7a3ada11 | 769 | for (i = 0; i < ctx->entries_nr; i++) { |
662148c4 DS |
770 | struct pack_midx_entry *obj = list++; |
771 | ||
7a3ada11 | 772 | if (ctx->pack_perm[obj->pack_int_id] == PACK_EXPIRED) |
19575c7c DS |
773 | BUG("object %s is in an expired pack with int-id %d", |
774 | oid_to_hex(&obj->oid), | |
775 | obj->pack_int_id); | |
776 | ||
7a3ada11 | 777 | hashwrite_be32(f, ctx->pack_perm[obj->pack_int_id]); |
662148c4 | 778 | |
7a3ada11 | 779 | if (ctx->large_offsets_needed && obj->offset >> 31) |
662148c4 | 780 | hashwrite_be32(f, MIDX_LARGE_OFFSET_NEEDED | nr_large_offset++); |
7a3ada11 | 781 | else if (!ctx->large_offsets_needed && obj->offset >> 32) |
662148c4 DS |
782 | BUG("object %s requires a large offset (%"PRIx64") but the MIDX is not writing large offsets!", |
783 | oid_to_hex(&obj->oid), | |
784 | obj->offset); | |
785 | else | |
786 | hashwrite_be32(f, (uint32_t)obj->offset); | |
662148c4 DS |
787 | } |
788 | ||
0ccd713c | 789 | return 0; |
662148c4 DS |
790 | } |
791 | ||
0ccd713c DS |
792 | static int write_midx_large_offsets(struct hashfile *f, |
793 | void *data) | |
662148c4 | 794 | { |
980f525c DS |
795 | struct write_midx_context *ctx = data; |
796 | struct pack_midx_entry *list = ctx->entries; | |
797 | struct pack_midx_entry *end = ctx->entries + ctx->entries_nr; | |
980f525c | 798 | uint32_t nr_large_offset = ctx->num_large_offsets; |
662148c4 DS |
799 | |
800 | while (nr_large_offset) { | |
61b0fcbb JK |
801 | struct pack_midx_entry *obj; |
802 | uint64_t offset; | |
803 | ||
804 | if (list >= end) | |
805 | BUG("too many large-offset objects"); | |
806 | ||
807 | obj = list++; | |
808 | offset = obj->offset; | |
662148c4 DS |
809 | |
810 | if (!(offset >> 31)) | |
811 | continue; | |
812 | ||
0ccd713c | 813 | hashwrite_be64(f, offset); |
662148c4 DS |
814 | |
815 | nr_large_offset--; | |
816 | } | |
817 | ||
0ccd713c | 818 | return 0; |
662148c4 DS |
819 | } |
820 | ||
38ff7cab TB |
821 | static int midx_pack_order_cmp(const void *va, const void *vb, void *_ctx) |
822 | { | |
823 | struct write_midx_context *ctx = _ctx; | |
824 | ||
825 | struct pack_midx_entry *a = &ctx->entries[*(const uint32_t *)va]; | |
826 | struct pack_midx_entry *b = &ctx->entries[*(const uint32_t *)vb]; | |
827 | ||
828 | uint32_t perm_a = ctx->pack_perm[a->pack_int_id]; | |
829 | uint32_t perm_b = ctx->pack_perm[b->pack_int_id]; | |
830 | ||
831 | /* Sort objects in the preferred pack ahead of any others. */ | |
832 | if (a->preferred > b->preferred) | |
833 | return -1; | |
834 | if (a->preferred < b->preferred) | |
835 | return 1; | |
836 | ||
837 | /* Then, order objects by which packs they appear in. */ | |
838 | if (perm_a < perm_b) | |
839 | return -1; | |
840 | if (perm_a > perm_b) | |
841 | return 1; | |
842 | ||
843 | /* Then, disambiguate by their offset within each pack. */ | |
844 | if (a->offset < b->offset) | |
845 | return -1; | |
846 | if (a->offset > b->offset) | |
847 | return 1; | |
848 | ||
849 | return 0; | |
850 | } | |
851 | ||
852 | static uint32_t *midx_pack_order(struct write_midx_context *ctx) | |
853 | { | |
854 | uint32_t *pack_order; | |
855 | uint32_t i; | |
856 | ||
857 | ALLOC_ARRAY(pack_order, ctx->entries_nr); | |
858 | for (i = 0; i < ctx->entries_nr; i++) | |
859 | pack_order[i] = i; | |
860 | QSORT_S(pack_order, ctx->entries_nr, midx_pack_order_cmp, ctx); | |
861 | ||
862 | return pack_order; | |
863 | } | |
864 | ||
865 | static void write_midx_reverse_index(char *midx_name, unsigned char *midx_hash, | |
866 | struct write_midx_context *ctx) | |
867 | { | |
868 | struct strbuf buf = STRBUF_INIT; | |
869 | const char *tmp_file; | |
870 | ||
871 | strbuf_addf(&buf, "%s-%s.rev", midx_name, hash_to_hex(midx_hash)); | |
872 | ||
873 | tmp_file = write_rev_file_order(NULL, ctx->pack_order, ctx->entries_nr, | |
874 | midx_hash, WRITE_REV); | |
875 | ||
876 | if (finalize_object_file(tmp_file, buf.buf)) | |
877 | die(_("cannot store reverse index file")); | |
878 | ||
879 | strbuf_release(&buf); | |
880 | } | |
881 | ||
882 | static void clear_midx_files_ext(struct repository *r, const char *ext, | |
883 | unsigned char *keep_hash); | |
884 | ||
19575c7c | 885 | static int write_midx_internal(const char *object_dir, struct multi_pack_index *m, |
9218c6a4 TB |
886 | struct string_list *packs_to_drop, |
887 | const char *preferred_pack_name, | |
888 | unsigned flags) | |
a3407730 | 889 | { |
fc59e748 | 890 | char *midx_name; |
9f191611 | 891 | unsigned char midx_hash[GIT_MAX_RAWSZ]; |
396f2570 | 892 | uint32_t i; |
fc59e748 DS |
893 | struct hashfile *f = NULL; |
894 | struct lock_file lk; | |
577dc496 | 895 | struct write_midx_context ctx = { 0 }; |
dba6175c | 896 | int pack_name_concat_len = 0; |
19575c7c DS |
897 | int dropped_packs = 0; |
898 | int result = 0; | |
63a8f0e9 | 899 | struct chunkfile *cf; |
fc59e748 DS |
900 | |
901 | midx_name = get_midx_filename(object_dir); | |
d5e1961c | 902 | if (safe_create_leading_directories(midx_name)) |
fc59e748 DS |
903 | die_errno(_("unable to create leading directories of %s"), |
904 | midx_name); | |
fc59e748 | 905 | |
19575c7c | 906 | if (m) |
577dc496 | 907 | ctx.m = m; |
19575c7c | 908 | else |
577dc496 DS |
909 | ctx.m = load_multi_pack_index(object_dir, 1); |
910 | ||
911 | ctx.nr = 0; | |
912 | ctx.alloc = ctx.m ? ctx.m->num_packs : 16; | |
913 | ctx.info = NULL; | |
914 | ALLOC_ARRAY(ctx.info, ctx.alloc); | |
915 | ||
916 | if (ctx.m) { | |
917 | for (i = 0; i < ctx.m->num_packs; i++) { | |
918 | ALLOC_GROW(ctx.info, ctx.nr + 1, ctx.alloc); | |
919 | ||
920 | ctx.info[ctx.nr].orig_pack_int_id = i; | |
921 | ctx.info[ctx.nr].pack_name = xstrdup(ctx.m->pack_names[i]); | |
922 | ctx.info[ctx.nr].p = NULL; | |
923 | ctx.info[ctx.nr].expired = 0; | |
924 | ctx.nr++; | |
a40498a1 DS |
925 | } |
926 | } | |
927 | ||
577dc496 | 928 | ctx.pack_paths_checked = 0; |
840cef0c | 929 | if (flags & MIDX_PROGRESS) |
577dc496 | 930 | ctx.progress = start_delayed_progress(_("Adding packfiles to multi-pack-index"), 0); |
840cef0c | 931 | else |
577dc496 | 932 | ctx.progress = NULL; |
840cef0c | 933 | |
577dc496 DS |
934 | for_each_file_in_pack_dir(object_dir, add_pack_to_midx, &ctx); |
935 | stop_progress(&ctx.progress); | |
396f2570 | 936 | |
577dc496 | 937 | if (ctx.m && ctx.nr == ctx.m->num_packs && !packs_to_drop) |
a40498a1 DS |
938 | goto cleanup; |
939 | ||
9218c6a4 TB |
940 | ctx.preferred_pack_idx = -1; |
941 | if (preferred_pack_name) { | |
942 | for (i = 0; i < ctx.nr; i++) { | |
943 | if (!cmp_idx_or_pack_name(preferred_pack_name, | |
944 | ctx.info[i].pack_name)) { | |
945 | ctx.preferred_pack_idx = i; | |
946 | break; | |
947 | } | |
948 | } | |
949 | } | |
950 | ||
951 | ctx.entries = get_sorted_entries(ctx.m, ctx.info, ctx.nr, &ctx.entries_nr, | |
952 | ctx.preferred_pack_idx); | |
a40498a1 | 953 | |
7a3ada11 | 954 | ctx.large_offsets_needed = 0; |
31bda9a2 DS |
955 | for (i = 0; i < ctx.entries_nr; i++) { |
956 | if (ctx.entries[i].offset > 0x7fffffff) | |
980f525c | 957 | ctx.num_large_offsets++; |
31bda9a2 | 958 | if (ctx.entries[i].offset > 0xffffffff) |
7a3ada11 | 959 | ctx.large_offsets_needed = 1; |
662148c4 | 960 | } |
fe1ed56f | 961 | |
577dc496 | 962 | QSORT(ctx.info, ctx.nr, pack_info_compare); |
d01bf2e6 | 963 | |
19575c7c DS |
964 | if (packs_to_drop && packs_to_drop->nr) { |
965 | int drop_index = 0; | |
966 | int missing_drops = 0; | |
967 | ||
577dc496 DS |
968 | for (i = 0; i < ctx.nr && drop_index < packs_to_drop->nr; i++) { |
969 | int cmp = strcmp(ctx.info[i].pack_name, | |
19575c7c DS |
970 | packs_to_drop->items[drop_index].string); |
971 | ||
972 | if (!cmp) { | |
973 | drop_index++; | |
577dc496 | 974 | ctx.info[i].expired = 1; |
19575c7c DS |
975 | } else if (cmp > 0) { |
976 | error(_("did not see pack-file %s to drop"), | |
977 | packs_to_drop->items[drop_index].string); | |
978 | drop_index++; | |
979 | missing_drops++; | |
980 | i--; | |
981 | } else { | |
577dc496 | 982 | ctx.info[i].expired = 0; |
19575c7c DS |
983 | } |
984 | } | |
985 | ||
986 | if (missing_drops) { | |
987 | result = 1; | |
988 | goto cleanup; | |
989 | } | |
990 | } | |
991 | ||
d01bf2e6 DS |
992 | /* |
993 | * pack_perm stores a permutation between pack-int-ids from the | |
994 | * previous multi-pack-index to the new one we are writing: | |
995 | * | |
996 | * pack_perm[old_id] = new_id | |
997 | */ | |
7a3ada11 | 998 | ALLOC_ARRAY(ctx.pack_perm, ctx.nr); |
577dc496 DS |
999 | for (i = 0; i < ctx.nr; i++) { |
1000 | if (ctx.info[i].expired) { | |
19575c7c | 1001 | dropped_packs++; |
7a3ada11 | 1002 | ctx.pack_perm[ctx.info[i].orig_pack_int_id] = PACK_EXPIRED; |
19575c7c | 1003 | } else { |
7a3ada11 | 1004 | ctx.pack_perm[ctx.info[i].orig_pack_int_id] = i - dropped_packs; |
19575c7c | 1005 | } |
d01bf2e6 DS |
1006 | } |
1007 | ||
577dc496 DS |
1008 | for (i = 0; i < ctx.nr; i++) { |
1009 | if (!ctx.info[i].expired) | |
1010 | pack_name_concat_len += strlen(ctx.info[i].pack_name) + 1; | |
19575c7c | 1011 | } |
dba6175c | 1012 | |
9218c6a4 TB |
1013 | /* Check that the preferred pack wasn't expired (if given). */ |
1014 | if (preferred_pack_name) { | |
1015 | struct pack_info *preferred = bsearch(preferred_pack_name, | |
1016 | ctx.info, ctx.nr, | |
1017 | sizeof(*ctx.info), | |
1018 | idx_or_pack_name_cmp); | |
1019 | ||
1020 | if (!preferred) | |
1021 | warning(_("unknown preferred pack: '%s'"), | |
1022 | preferred_pack_name); | |
1023 | else { | |
1024 | uint32_t perm = ctx.pack_perm[preferred->orig_pack_int_id]; | |
1025 | if (perm == PACK_EXPIRED) | |
1026 | warning(_("preferred pack '%s' is expired"), | |
1027 | preferred_pack_name); | |
1028 | } | |
1029 | } | |
1030 | ||
dba6175c DS |
1031 | if (pack_name_concat_len % MIDX_CHUNK_ALIGNMENT) |
1032 | pack_name_concat_len += MIDX_CHUNK_ALIGNMENT - | |
1033 | (pack_name_concat_len % MIDX_CHUNK_ALIGNMENT); | |
1034 | ||
fc59e748 | 1035 | hold_lock_file_for_update(&lk, midx_name, LOCK_DIE_ON_ERROR); |
acd71602 | 1036 | f = hashfd(get_lock_file_fd(&lk), get_lock_file_path(&lk)); |
fc59e748 | 1037 | |
577dc496 DS |
1038 | if (ctx.m) |
1039 | close_midx(ctx.m); | |
a40498a1 | 1040 | |
577dc496 | 1041 | if (ctx.nr - dropped_packs == 0) { |
796d61cd DR |
1042 | error(_("no pack files to index.")); |
1043 | result = 1; | |
1044 | goto cleanup; | |
1045 | } | |
1046 | ||
63a8f0e9 | 1047 | cf = init_chunkfile(f); |
32f3c541 | 1048 | |
63a8f0e9 DS |
1049 | add_chunk(cf, MIDX_CHUNKID_PACKNAMES, pack_name_concat_len, |
1050 | write_midx_pack_names); | |
1051 | add_chunk(cf, MIDX_CHUNKID_OIDFANOUT, MIDX_CHUNK_FANOUT_SIZE, | |
1052 | write_midx_oid_fanout); | |
1053 | add_chunk(cf, MIDX_CHUNKID_OIDLOOKUP, | |
329fac3a | 1054 | (size_t)ctx.entries_nr * the_hash_algo->rawsz, |
63a8f0e9 DS |
1055 | write_midx_oid_lookup); |
1056 | add_chunk(cf, MIDX_CHUNKID_OBJECTOFFSETS, | |
329fac3a | 1057 | (size_t)ctx.entries_nr * MIDX_CHUNK_OFFSET_WIDTH, |
63a8f0e9 | 1058 | write_midx_object_offsets); |
32f3c541 | 1059 | |
63a8f0e9 DS |
1060 | if (ctx.large_offsets_needed) |
1061 | add_chunk(cf, MIDX_CHUNKID_LARGEOFFSETS, | |
329fac3a | 1062 | (size_t)ctx.num_large_offsets * MIDX_CHUNK_LARGE_OFFSET_WIDTH, |
63a8f0e9 | 1063 | write_midx_large_offsets); |
32f3c541 | 1064 | |
63a8f0e9 DS |
1065 | write_midx_header(f, get_num_chunks(cf), ctx.nr - dropped_packs); |
1066 | write_chunkfile(cf, &ctx); | |
fc59e748 | 1067 | |
9f191611 | 1068 | finalize_hashfile(f, midx_hash, CSUM_FSYNC | CSUM_HASH_IN_STREAM); |
63a8f0e9 | 1069 | free_chunkfile(cf); |
38ff7cab TB |
1070 | |
1071 | if (flags & MIDX_WRITE_REV_INDEX) | |
1072 | ctx.pack_order = midx_pack_order(&ctx); | |
1073 | ||
1074 | if (flags & MIDX_WRITE_REV_INDEX) | |
1075 | write_midx_reverse_index(midx_name, midx_hash, &ctx); | |
1076 | clear_midx_files_ext(the_repository, ".rev", midx_hash); | |
1077 | ||
fc59e748 DS |
1078 | commit_lock_file(&lk); |
1079 | ||
a40498a1 | 1080 | cleanup: |
577dc496 DS |
1081 | for (i = 0; i < ctx.nr; i++) { |
1082 | if (ctx.info[i].p) { | |
1083 | close_pack(ctx.info[i].p); | |
1084 | free(ctx.info[i].p); | |
396f2570 | 1085 | } |
577dc496 | 1086 | free(ctx.info[i].pack_name); |
396f2570 DS |
1087 | } |
1088 | ||
577dc496 | 1089 | free(ctx.info); |
31bda9a2 | 1090 | free(ctx.entries); |
7a3ada11 | 1091 | free(ctx.pack_perm); |
38ff7cab | 1092 | free(ctx.pack_order); |
a40498a1 | 1093 | free(midx_name); |
19575c7c DS |
1094 | return result; |
1095 | } | |
1096 | ||
9218c6a4 TB |
1097 | int write_midx_file(const char *object_dir, |
1098 | const char *preferred_pack_name, | |
1099 | unsigned flags) | |
19575c7c | 1100 | { |
9218c6a4 TB |
1101 | return write_midx_internal(object_dir, NULL, NULL, preferred_pack_name, |
1102 | flags); | |
a3407730 | 1103 | } |
525e18c0 | 1104 | |
38ff7cab TB |
1105 | struct clear_midx_data { |
1106 | char *keep; | |
1107 | const char *ext; | |
1108 | }; | |
1109 | ||
1110 | static void clear_midx_file_ext(const char *full_path, size_t full_path_len, | |
1111 | const char *file_name, void *_data) | |
1112 | { | |
1113 | struct clear_midx_data *data = _data; | |
1114 | ||
1115 | if (!(starts_with(file_name, "multi-pack-index-") && | |
1116 | ends_with(file_name, data->ext))) | |
1117 | return; | |
1118 | if (data->keep && !strcmp(data->keep, file_name)) | |
1119 | return; | |
1120 | ||
1121 | if (unlink(full_path)) | |
1122 | die_errno(_("failed to remove %s"), full_path); | |
1123 | } | |
1124 | ||
1125 | static void clear_midx_files_ext(struct repository *r, const char *ext, | |
1126 | unsigned char *keep_hash) | |
1127 | { | |
1128 | struct clear_midx_data data; | |
1129 | memset(&data, 0, sizeof(struct clear_midx_data)); | |
1130 | ||
1131 | if (keep_hash) | |
1132 | data.keep = xstrfmt("multi-pack-index-%s%s", | |
1133 | hash_to_hex(keep_hash), ext); | |
1134 | data.ext = ext; | |
1135 | ||
1136 | for_each_file_in_pack_dir(r->objects->odb->path, | |
1137 | clear_midx_file_ext, | |
1138 | &data); | |
1139 | ||
1140 | free(data.keep); | |
1141 | } | |
1142 | ||
1dcd9f20 | 1143 | void clear_midx_file(struct repository *r) |
525e18c0 | 1144 | { |
3b2f8a02 | 1145 | char *midx = get_midx_filename(r->objects->odb->path); |
1dcd9f20 DS |
1146 | |
1147 | if (r->objects && r->objects->multi_pack_index) { | |
1148 | close_midx(r->objects->multi_pack_index); | |
1149 | r->objects->multi_pack_index = NULL; | |
1150 | } | |
525e18c0 | 1151 | |
d5e1961c | 1152 | if (remove_path(midx)) |
525e18c0 | 1153 | die(_("failed to clear multi-pack-index at %s"), midx); |
525e18c0 | 1154 | |
38ff7cab TB |
1155 | clear_midx_files_ext(r, ".rev", NULL); |
1156 | ||
525e18c0 DS |
1157 | free(midx); |
1158 | } | |
56ee7ff1 DS |
1159 | |
1160 | static int verify_midx_error; | |
1161 | ||
d4bf1d88 DS |
1162 | static void midx_report(const char *fmt, ...) |
1163 | { | |
1164 | va_list ap; | |
1165 | verify_midx_error = 1; | |
1166 | va_start(ap, fmt); | |
1167 | vfprintf(stderr, fmt, ap); | |
1168 | fprintf(stderr, "\n"); | |
1169 | va_end(ap); | |
1170 | } | |
1171 | ||
5ae18df9 JH |
1172 | struct pair_pos_vs_id |
1173 | { | |
1174 | uint32_t pos; | |
1175 | uint32_t pack_int_id; | |
1176 | }; | |
1177 | ||
1178 | static int compare_pair_pos_vs_id(const void *_a, const void *_b) | |
1179 | { | |
1180 | struct pair_pos_vs_id *a = (struct pair_pos_vs_id *)_a; | |
1181 | struct pair_pos_vs_id *b = (struct pair_pos_vs_id *)_b; | |
1182 | ||
1183 | return b->pack_int_id - a->pack_int_id; | |
1184 | } | |
1185 | ||
430efb8a JH |
1186 | /* |
1187 | * Limit calls to display_progress() for performance reasons. | |
1188 | * The interval here was arbitrarily chosen. | |
1189 | */ | |
1190 | #define SPARSE_PROGRESS_INTERVAL (1 << 12) | |
1191 | #define midx_display_sparse_progress(progress, n) \ | |
1192 | do { \ | |
1193 | uint64_t _n = (n); \ | |
1194 | if ((_n & (SPARSE_PROGRESS_INTERVAL - 1)) == 0) \ | |
1195 | display_progress(progress, _n); \ | |
1196 | } while (0) | |
1197 | ||
efbc3aee | 1198 | int verify_midx_file(struct repository *r, const char *object_dir, unsigned flags) |
56ee7ff1 | 1199 | { |
5ae18df9 | 1200 | struct pair_pos_vs_id *pairs = NULL; |
d4bf1d88 | 1201 | uint32_t i; |
ad60096d | 1202 | struct progress *progress = NULL; |
56ee7ff1 DS |
1203 | struct multi_pack_index *m = load_multi_pack_index(object_dir, 1); |
1204 | verify_midx_error = 0; | |
1205 | ||
d9607542 DS |
1206 | if (!m) { |
1207 | int result = 0; | |
1208 | struct stat sb; | |
1209 | char *filename = get_midx_filename(object_dir); | |
1210 | if (!stat(filename, &sb)) { | |
1211 | error(_("multi-pack-index file exists, but failed to parse")); | |
1212 | result = 1; | |
1213 | } | |
1214 | free(filename); | |
1215 | return result; | |
1216 | } | |
56ee7ff1 | 1217 | |
ad60096d | 1218 | if (flags & MIDX_PROGRESS) |
efdd2f0d | 1219 | progress = start_delayed_progress(_("Looking for referenced packfiles"), |
ad60096d | 1220 | m->num_packs); |
d4bf1d88 | 1221 | for (i = 0; i < m->num_packs; i++) { |
64404a24 | 1222 | if (prepare_midx_pack(r, m, i)) |
d4bf1d88 | 1223 | midx_report("failed to load pack in position %d", i); |
430efb8a JH |
1224 | |
1225 | display_progress(progress, i + 1); | |
d4bf1d88 | 1226 | } |
430efb8a | 1227 | stop_progress(&progress); |
d4bf1d88 | 1228 | |
2f23d3f3 DS |
1229 | for (i = 0; i < 255; i++) { |
1230 | uint32_t oid_fanout1 = ntohl(m->chunk_oid_fanout[i]); | |
1231 | uint32_t oid_fanout2 = ntohl(m->chunk_oid_fanout[i + 1]); | |
1232 | ||
1233 | if (oid_fanout1 > oid_fanout2) | |
1234 | midx_report(_("oid fanout out of order: fanout[%d] = %"PRIx32" > %"PRIx32" = fanout[%d]"), | |
1235 | i, oid_fanout1, oid_fanout2, i + 1); | |
1236 | } | |
1237 | ||
796d61cd DR |
1238 | if (m->num_objects == 0) { |
1239 | midx_report(_("the midx contains no oid")); | |
1240 | /* | |
1241 | * Remaining tests assume that we have objects, so we can | |
1242 | * return here. | |
1243 | */ | |
1244 | return verify_midx_error; | |
1245 | } | |
1246 | ||
ad60096d WB |
1247 | if (flags & MIDX_PROGRESS) |
1248 | progress = start_sparse_progress(_("Verifying OID order in multi-pack-index"), | |
1249 | m->num_objects - 1); | |
55c5648d DS |
1250 | for (i = 0; i < m->num_objects - 1; i++) { |
1251 | struct object_id oid1, oid2; | |
1252 | ||
1253 | nth_midxed_object_oid(&oid1, m, i); | |
1254 | nth_midxed_object_oid(&oid2, m, i + 1); | |
1255 | ||
1256 | if (oidcmp(&oid1, &oid2) >= 0) | |
1257 | midx_report(_("oid lookup out of order: oid[%d] = %s >= %s = oid[%d]"), | |
1258 | i, oid_to_hex(&oid1), oid_to_hex(&oid2), i + 1); | |
430efb8a JH |
1259 | |
1260 | midx_display_sparse_progress(progress, i + 1); | |
55c5648d | 1261 | } |
430efb8a | 1262 | stop_progress(&progress); |
55c5648d | 1263 | |
5ae18df9 JH |
1264 | /* |
1265 | * Create an array mapping each object to its packfile id. Sort it | |
1266 | * to group the objects by packfile. Use this permutation to visit | |
1267 | * each of the objects and only require 1 packfile to be open at a | |
1268 | * time. | |
1269 | */ | |
1270 | ALLOC_ARRAY(pairs, m->num_objects); | |
1271 | for (i = 0; i < m->num_objects; i++) { | |
1272 | pairs[i].pos = i; | |
1273 | pairs[i].pack_int_id = nth_midxed_pack_int_id(m, i); | |
1274 | } | |
1275 | ||
ad60096d WB |
1276 | if (flags & MIDX_PROGRESS) |
1277 | progress = start_sparse_progress(_("Sorting objects by packfile"), | |
1278 | m->num_objects); | |
5ae18df9 JH |
1279 | display_progress(progress, 0); /* TODO: Measure QSORT() progress */ |
1280 | QSORT(pairs, m->num_objects, compare_pair_pos_vs_id); | |
1281 | stop_progress(&progress); | |
1282 | ||
ad60096d WB |
1283 | if (flags & MIDX_PROGRESS) |
1284 | progress = start_sparse_progress(_("Verifying object offsets"), m->num_objects); | |
cc6af73c DS |
1285 | for (i = 0; i < m->num_objects; i++) { |
1286 | struct object_id oid; | |
1287 | struct pack_entry e; | |
1288 | off_t m_offset, p_offset; | |
1289 | ||
5ae18df9 JH |
1290 | if (i > 0 && pairs[i-1].pack_int_id != pairs[i].pack_int_id && |
1291 | m->packs[pairs[i-1].pack_int_id]) | |
1292 | { | |
1293 | close_pack_fd(m->packs[pairs[i-1].pack_int_id]); | |
1294 | close_pack_index(m->packs[pairs[i-1].pack_int_id]); | |
1295 | } | |
1296 | ||
1297 | nth_midxed_object_oid(&oid, m, pairs[i].pos); | |
1298 | ||
64404a24 | 1299 | if (!fill_midx_entry(r, &oid, &e, m)) { |
cc6af73c | 1300 | midx_report(_("failed to load pack entry for oid[%d] = %s"), |
5ae18df9 | 1301 | pairs[i].pos, oid_to_hex(&oid)); |
cc6af73c DS |
1302 | continue; |
1303 | } | |
1304 | ||
1305 | if (open_pack_index(e.p)) { | |
1306 | midx_report(_("failed to load pack-index for packfile %s"), | |
1307 | e.p->pack_name); | |
1308 | break; | |
1309 | } | |
1310 | ||
1311 | m_offset = e.offset; | |
1312 | p_offset = find_pack_entry_one(oid.hash, e.p); | |
1313 | ||
1314 | if (m_offset != p_offset) | |
1315 | midx_report(_("incorrect object offset for oid[%d] = %s: %"PRIx64" != %"PRIx64), | |
5ae18df9 | 1316 | pairs[i].pos, oid_to_hex(&oid), m_offset, p_offset); |
144d7033 | 1317 | |
430efb8a | 1318 | midx_display_sparse_progress(progress, i + 1); |
cc6af73c | 1319 | } |
144d7033 | 1320 | stop_progress(&progress); |
cc6af73c | 1321 | |
5ae18df9 JH |
1322 | free(pairs); |
1323 | ||
56ee7ff1 DS |
1324 | return verify_midx_error; |
1325 | } | |
cff97116 | 1326 | |
efbc3aee | 1327 | int expire_midx_packs(struct repository *r, const char *object_dir, unsigned flags) |
cff97116 | 1328 | { |
19575c7c DS |
1329 | uint32_t i, *count, result = 0; |
1330 | struct string_list packs_to_drop = STRING_LIST_INIT_DUP; | |
1331 | struct multi_pack_index *m = load_multi_pack_index(object_dir, 1); | |
8dc18f89 | 1332 | struct progress *progress = NULL; |
19575c7c DS |
1333 | |
1334 | if (!m) | |
1335 | return 0; | |
1336 | ||
1337 | count = xcalloc(m->num_packs, sizeof(uint32_t)); | |
8dc18f89 WB |
1338 | |
1339 | if (flags & MIDX_PROGRESS) | |
efdd2f0d | 1340 | progress = start_delayed_progress(_("Counting referenced objects"), |
8dc18f89 | 1341 | m->num_objects); |
19575c7c DS |
1342 | for (i = 0; i < m->num_objects; i++) { |
1343 | int pack_int_id = nth_midxed_pack_int_id(m, i); | |
1344 | count[pack_int_id]++; | |
8dc18f89 | 1345 | display_progress(progress, i + 1); |
19575c7c | 1346 | } |
8dc18f89 | 1347 | stop_progress(&progress); |
19575c7c | 1348 | |
8dc18f89 | 1349 | if (flags & MIDX_PROGRESS) |
efdd2f0d | 1350 | progress = start_delayed_progress(_("Finding and deleting unreferenced packfiles"), |
8dc18f89 | 1351 | m->num_packs); |
19575c7c DS |
1352 | for (i = 0; i < m->num_packs; i++) { |
1353 | char *pack_name; | |
8dc18f89 | 1354 | display_progress(progress, i + 1); |
19575c7c DS |
1355 | |
1356 | if (count[i]) | |
1357 | continue; | |
1358 | ||
1359 | if (prepare_midx_pack(r, m, i)) | |
1360 | continue; | |
1361 | ||
1362 | if (m->packs[i]->pack_keep) | |
1363 | continue; | |
1364 | ||
1365 | pack_name = xstrdup(m->packs[i]->pack_name); | |
1366 | close_pack(m->packs[i]); | |
1367 | ||
1368 | string_list_insert(&packs_to_drop, m->pack_names[i]); | |
1369 | unlink_pack_path(pack_name, 0); | |
1370 | free(pack_name); | |
1371 | } | |
8dc18f89 | 1372 | stop_progress(&progress); |
19575c7c DS |
1373 | |
1374 | free(count); | |
1375 | ||
1376 | if (packs_to_drop.nr) | |
9218c6a4 | 1377 | result = write_midx_internal(object_dir, m, &packs_to_drop, NULL, flags); |
19575c7c DS |
1378 | |
1379 | string_list_clear(&packs_to_drop, 0); | |
1380 | return result; | |
cff97116 | 1381 | } |
2af890bb | 1382 | |
ce1e4a10 DS |
1383 | struct repack_info { |
1384 | timestamp_t mtime; | |
1385 | uint32_t referenced_objects; | |
1386 | uint32_t pack_int_id; | |
1387 | }; | |
1388 | ||
1389 | static int compare_by_mtime(const void *a_, const void *b_) | |
2af890bb | 1390 | { |
ce1e4a10 DS |
1391 | const struct repack_info *a, *b; |
1392 | ||
1393 | a = (const struct repack_info *)a_; | |
1394 | b = (const struct repack_info *)b_; | |
1395 | ||
1396 | if (a->mtime < b->mtime) | |
1397 | return -1; | |
1398 | if (a->mtime > b->mtime) | |
1399 | return 1; | |
1400 | return 0; | |
1401 | } | |
1402 | ||
3ce4ca0a DS |
1403 | static int fill_included_packs_all(struct repository *r, |
1404 | struct multi_pack_index *m, | |
ce1e4a10 DS |
1405 | unsigned char *include_pack) |
1406 | { | |
3ce4ca0a DS |
1407 | uint32_t i, count = 0; |
1408 | int pack_kept_objects = 0; | |
1409 | ||
1410 | repo_config_get_bool(r, "repack.packkeptobjects", &pack_kept_objects); | |
1411 | ||
1412 | for (i = 0; i < m->num_packs; i++) { | |
1413 | if (prepare_midx_pack(r, m, i)) | |
1414 | continue; | |
1415 | if (!pack_kept_objects && m->packs[i]->pack_keep) | |
1416 | continue; | |
ce1e4a10 | 1417 | |
ce1e4a10 | 1418 | include_pack[i] = 1; |
3ce4ca0a DS |
1419 | count++; |
1420 | } | |
ce1e4a10 | 1421 | |
3ce4ca0a | 1422 | return count < 2; |
ce1e4a10 DS |
1423 | } |
1424 | ||
1425 | static int fill_included_packs_batch(struct repository *r, | |
1426 | struct multi_pack_index *m, | |
1427 | unsigned char *include_pack, | |
1428 | size_t batch_size) | |
1429 | { | |
1430 | uint32_t i, packs_to_repack; | |
1431 | size_t total_size; | |
1432 | struct repack_info *pack_info = xcalloc(m->num_packs, sizeof(struct repack_info)); | |
3ce4ca0a DS |
1433 | int pack_kept_objects = 0; |
1434 | ||
1435 | repo_config_get_bool(r, "repack.packkeptobjects", &pack_kept_objects); | |
ce1e4a10 DS |
1436 | |
1437 | for (i = 0; i < m->num_packs; i++) { | |
1438 | pack_info[i].pack_int_id = i; | |
1439 | ||
1440 | if (prepare_midx_pack(r, m, i)) | |
1441 | continue; | |
1442 | ||
1443 | pack_info[i].mtime = m->packs[i]->mtime; | |
1444 | } | |
1445 | ||
1446 | for (i = 0; batch_size && i < m->num_objects; i++) { | |
1447 | uint32_t pack_int_id = nth_midxed_pack_int_id(m, i); | |
1448 | pack_info[pack_int_id].referenced_objects++; | |
1449 | } | |
1450 | ||
1451 | QSORT(pack_info, m->num_packs, compare_by_mtime); | |
1452 | ||
1453 | total_size = 0; | |
1454 | packs_to_repack = 0; | |
1455 | for (i = 0; total_size < batch_size && i < m->num_packs; i++) { | |
1456 | int pack_int_id = pack_info[i].pack_int_id; | |
1457 | struct packed_git *p = m->packs[pack_int_id]; | |
1458 | size_t expected_size; | |
1459 | ||
1460 | if (!p) | |
1461 | continue; | |
3ce4ca0a DS |
1462 | if (!pack_kept_objects && p->pack_keep) |
1463 | continue; | |
ce1e4a10 DS |
1464 | if (open_pack_index(p) || !p->num_objects) |
1465 | continue; | |
1466 | ||
1467 | expected_size = (size_t)(p->pack_size | |
1468 | * pack_info[i].referenced_objects); | |
1469 | expected_size /= p->num_objects; | |
1470 | ||
1471 | if (expected_size >= batch_size) | |
1472 | continue; | |
1473 | ||
1474 | packs_to_repack++; | |
1475 | total_size += expected_size; | |
1476 | include_pack[pack_int_id] = 1; | |
1477 | } | |
1478 | ||
1479 | free(pack_info); | |
1480 | ||
1eb22c7d | 1481 | if (packs_to_repack < 2) |
ce1e4a10 DS |
1482 | return 1; |
1483 | ||
2af890bb DS |
1484 | return 0; |
1485 | } | |
ce1e4a10 | 1486 | |
efbc3aee | 1487 | int midx_repack(struct repository *r, const char *object_dir, size_t batch_size, unsigned flags) |
ce1e4a10 DS |
1488 | { |
1489 | int result = 0; | |
1490 | uint32_t i; | |
1491 | unsigned char *include_pack; | |
1492 | struct child_process cmd = CHILD_PROCESS_INIT; | |
6af3b00a | 1493 | FILE *cmd_in; |
ce1e4a10 DS |
1494 | struct strbuf base_name = STRBUF_INIT; |
1495 | struct multi_pack_index *m = load_multi_pack_index(object_dir, 1); | |
1496 | ||
e11d86de SLN |
1497 | /* |
1498 | * When updating the default for these configuration | |
1499 | * variables in builtin/repack.c, these must be adjusted | |
1500 | * to match. | |
1501 | */ | |
1502 | int delta_base_offset = 1; | |
1503 | int use_delta_islands = 0; | |
1504 | ||
ce1e4a10 DS |
1505 | if (!m) |
1506 | return 0; | |
1507 | ||
1508 | include_pack = xcalloc(m->num_packs, sizeof(unsigned char)); | |
1509 | ||
1510 | if (batch_size) { | |
1511 | if (fill_included_packs_batch(r, m, include_pack, batch_size)) | |
1512 | goto cleanup; | |
3ce4ca0a | 1513 | } else if (fill_included_packs_all(r, m, include_pack)) |
ce1e4a10 DS |
1514 | goto cleanup; |
1515 | ||
e11d86de SLN |
1516 | repo_config_get_bool(r, "repack.usedeltabaseoffset", &delta_base_offset); |
1517 | repo_config_get_bool(r, "repack.usedeltaislands", &use_delta_islands); | |
1518 | ||
c972bf4c | 1519 | strvec_push(&cmd.args, "pack-objects"); |
ce1e4a10 DS |
1520 | |
1521 | strbuf_addstr(&base_name, object_dir); | |
1522 | strbuf_addstr(&base_name, "/pack/pack"); | |
c972bf4c | 1523 | strvec_push(&cmd.args, base_name.buf); |
64d80e7d | 1524 | |
e11d86de | 1525 | if (delta_base_offset) |
c972bf4c | 1526 | strvec_push(&cmd.args, "--delta-base-offset"); |
e11d86de | 1527 | if (use_delta_islands) |
c972bf4c | 1528 | strvec_push(&cmd.args, "--delta-islands"); |
e11d86de | 1529 | |
64d80e7d | 1530 | if (flags & MIDX_PROGRESS) |
c972bf4c | 1531 | strvec_push(&cmd.args, "--progress"); |
64d80e7d | 1532 | else |
c972bf4c | 1533 | strvec_push(&cmd.args, "-q"); |
64d80e7d | 1534 | |
ce1e4a10 DS |
1535 | strbuf_release(&base_name); |
1536 | ||
1537 | cmd.git_cmd = 1; | |
1538 | cmd.in = cmd.out = -1; | |
1539 | ||
1540 | if (start_command(&cmd)) { | |
1541 | error(_("could not start pack-objects")); | |
1542 | result = 1; | |
1543 | goto cleanup; | |
1544 | } | |
1545 | ||
6af3b00a RS |
1546 | cmd_in = xfdopen(cmd.in, "w"); |
1547 | ||
ce1e4a10 DS |
1548 | for (i = 0; i < m->num_objects; i++) { |
1549 | struct object_id oid; | |
1550 | uint32_t pack_int_id = nth_midxed_pack_int_id(m, i); | |
1551 | ||
1552 | if (!include_pack[pack_int_id]) | |
1553 | continue; | |
1554 | ||
1555 | nth_midxed_object_oid(&oid, m, i); | |
6af3b00a | 1556 | fprintf(cmd_in, "%s\n", oid_to_hex(&oid)); |
ce1e4a10 | 1557 | } |
6af3b00a | 1558 | fclose(cmd_in); |
ce1e4a10 DS |
1559 | |
1560 | if (finish_command(&cmd)) { | |
1561 | error(_("could not finish pack-objects")); | |
1562 | result = 1; | |
1563 | goto cleanup; | |
1564 | } | |
1565 | ||
9218c6a4 | 1566 | result = write_midx_internal(object_dir, m, NULL, NULL, flags); |
ce1e4a10 DS |
1567 | m = NULL; |
1568 | ||
1569 | cleanup: | |
1570 | if (m) | |
1571 | close_midx(m); | |
1572 | free(include_pack); | |
1573 | return result; | |
1574 | } |