]>
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" |
c528e179 TB |
16 | #include "pack-bitmap.h" |
17 | #include "refs.h" | |
18 | #include "revision.h" | |
19 | #include "list-objects.h" | |
a3407730 | 20 | |
fc59e748 DS |
21 | #define MIDX_SIGNATURE 0x4d494458 /* "MIDX" */ |
22 | #define MIDX_VERSION 1 | |
4d80560c DS |
23 | #define MIDX_BYTE_FILE_VERSION 4 |
24 | #define MIDX_BYTE_HASH_VERSION 5 | |
25 | #define MIDX_BYTE_NUM_CHUNKS 6 | |
26 | #define MIDX_BYTE_NUM_PACKS 8 | |
fc59e748 | 27 | #define MIDX_HEADER_SIZE 12 |
aaa95dfa | 28 | #define MIDX_MIN_SIZE (MIDX_HEADER_SIZE + the_hash_algo->rawsz) |
fc59e748 | 29 | |
32f3c541 DS |
30 | #define MIDX_CHUNK_ALIGNMENT 4 |
31 | #define MIDX_CHUNKID_PACKNAMES 0x504e414d /* "PNAM" */ | |
d7cacf29 | 32 | #define MIDX_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */ |
0d5b3a5e | 33 | #define MIDX_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */ |
662148c4 DS |
34 | #define MIDX_CHUNKID_OBJECTOFFSETS 0x4f4f4646 /* "OOFF" */ |
35 | #define MIDX_CHUNKID_LARGEOFFSETS 0x4c4f4646 /* "LOFF" */ | |
d7cacf29 | 36 | #define MIDX_CHUNK_FANOUT_SIZE (sizeof(uint32_t) * 256) |
662148c4 DS |
37 | #define MIDX_CHUNK_OFFSET_WIDTH (2 * sizeof(uint32_t)) |
38 | #define MIDX_CHUNK_LARGE_OFFSET_WIDTH (sizeof(uint64_t)) | |
39 | #define MIDX_LARGE_OFFSET_NEEDED 0x80000000 | |
32f3c541 | 40 | |
19575c7c DS |
41 | #define PACK_EXPIRED UINT_MAX |
42 | ||
d9607542 DS |
43 | static uint8_t oid_version(void) |
44 | { | |
45 | switch (hash_algo_by_ptr(the_hash_algo)) { | |
46 | case GIT_HASH_SHA1: | |
47 | return 1; | |
48 | case GIT_HASH_SHA256: | |
49 | return 2; | |
50 | default: | |
51 | die(_("invalid hash version")); | |
52 | } | |
53 | } | |
54 | ||
0f533c72 | 55 | const unsigned char *get_midx_checksum(struct multi_pack_index *m) |
f894081d TB |
56 | { |
57 | return m->data + m->data_len - the_hash_algo->rawsz; | |
58 | } | |
59 | ||
0f533c72 | 60 | char *get_midx_filename(const char *object_dir) |
fc59e748 DS |
61 | { |
62 | return xstrfmt("%s/pack/multi-pack-index", object_dir); | |
63 | } | |
64 | ||
f894081d TB |
65 | char *get_midx_rev_filename(struct multi_pack_index *m) |
66 | { | |
67 | return xstrfmt("%s/pack/multi-pack-index-%s.rev", | |
68 | m->object_dir, hash_to_hex(get_midx_checksum(m))); | |
69 | } | |
70 | ||
6ab3b8b8 DS |
71 | static int midx_read_oid_fanout(const unsigned char *chunk_start, |
72 | size_t chunk_size, void *data) | |
73 | { | |
74 | struct multi_pack_index *m = data; | |
75 | m->chunk_oid_fanout = (uint32_t *)chunk_start; | |
76 | ||
77 | if (chunk_size != 4 * 256) { | |
78 | error(_("multi-pack-index OID fanout is of the wrong size")); | |
79 | return 1; | |
80 | } | |
81 | return 0; | |
82 | } | |
83 | ||
2cf489a3 | 84 | struct multi_pack_index *load_multi_pack_index(const char *object_dir, int local) |
4d80560c DS |
85 | { |
86 | struct multi_pack_index *m = NULL; | |
87 | int fd; | |
88 | struct stat st; | |
89 | size_t midx_size; | |
90 | void *midx_map = NULL; | |
91 | uint32_t hash_version; | |
92 | char *midx_name = get_midx_filename(object_dir); | |
32f3c541 | 93 | uint32_t i; |
3227565c | 94 | const char *cur_pack_name; |
6ab3b8b8 | 95 | struct chunkfile *cf = NULL; |
4d80560c DS |
96 | |
97 | fd = git_open(midx_name); | |
98 | ||
99 | if (fd < 0) | |
100 | goto cleanup_fail; | |
101 | if (fstat(fd, &st)) { | |
102 | error_errno(_("failed to read %s"), midx_name); | |
103 | goto cleanup_fail; | |
104 | } | |
105 | ||
106 | midx_size = xsize_t(st.st_size); | |
107 | ||
108 | if (midx_size < MIDX_MIN_SIZE) { | |
109 | error(_("multi-pack-index file %s is too small"), midx_name); | |
110 | goto cleanup_fail; | |
111 | } | |
112 | ||
113 | FREE_AND_NULL(midx_name); | |
114 | ||
115 | midx_map = xmmap(NULL, midx_size, PROT_READ, MAP_PRIVATE, fd, 0); | |
6c7ff7cf | 116 | close(fd); |
4d80560c | 117 | |
577314ca | 118 | FLEX_ALLOC_STR(m, object_dir, object_dir); |
4d80560c DS |
119 | m->data = midx_map; |
120 | m->data_len = midx_size; | |
2cf489a3 | 121 | m->local = local; |
4d80560c DS |
122 | |
123 | m->signature = get_be32(m->data); | |
53ad0407 DS |
124 | if (m->signature != MIDX_SIGNATURE) |
125 | die(_("multi-pack-index signature 0x%08x does not match signature 0x%08x"), | |
4d80560c | 126 | m->signature, MIDX_SIGNATURE); |
4d80560c DS |
127 | |
128 | m->version = m->data[MIDX_BYTE_FILE_VERSION]; | |
53ad0407 DS |
129 | if (m->version != MIDX_VERSION) |
130 | die(_("multi-pack-index version %d not recognized"), | |
4d80560c | 131 | m->version); |
4d80560c DS |
132 | |
133 | hash_version = m->data[MIDX_BYTE_HASH_VERSION]; | |
d9607542 DS |
134 | if (hash_version != oid_version()) { |
135 | error(_("multi-pack-index hash version %u does not match version %u"), | |
136 | hash_version, oid_version()); | |
137 | goto cleanup_fail; | |
138 | } | |
aaa95dfa | 139 | m->hash_len = the_hash_algo->rawsz; |
4d80560c DS |
140 | |
141 | m->num_chunks = m->data[MIDX_BYTE_NUM_CHUNKS]; | |
142 | ||
143 | m->num_packs = get_be32(m->data + MIDX_BYTE_NUM_PACKS); | |
144 | ||
6ab3b8b8 | 145 | cf = init_chunkfile(NULL); |
32f3c541 | 146 | |
6ab3b8b8 DS |
147 | if (read_table_of_contents(cf, m->data, midx_size, |
148 | MIDX_HEADER_SIZE, m->num_chunks)) | |
149 | goto cleanup_fail; | |
32f3c541 | 150 | |
6ab3b8b8 | 151 | if (pair_chunk(cf, MIDX_CHUNKID_PACKNAMES, &m->chunk_pack_names) == CHUNK_NOT_FOUND) |
32f3c541 | 152 | die(_("multi-pack-index missing required pack-name chunk")); |
6ab3b8b8 | 153 | if (read_chunk(cf, MIDX_CHUNKID_OIDFANOUT, midx_read_oid_fanout, m) == CHUNK_NOT_FOUND) |
d7cacf29 | 154 | die(_("multi-pack-index missing required OID fanout chunk")); |
6ab3b8b8 | 155 | if (pair_chunk(cf, MIDX_CHUNKID_OIDLOOKUP, &m->chunk_oid_lookup) == CHUNK_NOT_FOUND) |
0d5b3a5e | 156 | die(_("multi-pack-index missing required OID lookup chunk")); |
6ab3b8b8 | 157 | if (pair_chunk(cf, MIDX_CHUNKID_OBJECTOFFSETS, &m->chunk_object_offsets) == CHUNK_NOT_FOUND) |
662148c4 | 158 | die(_("multi-pack-index missing required object offsets chunk")); |
32f3c541 | 159 | |
6ab3b8b8 DS |
160 | pair_chunk(cf, MIDX_CHUNKID_LARGEOFFSETS, &m->chunk_large_offsets); |
161 | ||
d7cacf29 DS |
162 | m->num_objects = ntohl(m->chunk_oid_fanout[255]); |
163 | ||
ca56dadb RS |
164 | CALLOC_ARRAY(m->pack_names, m->num_packs); |
165 | CALLOC_ARRAY(m->packs, m->num_packs); | |
3227565c DS |
166 | |
167 | cur_pack_name = (const char *)m->chunk_pack_names; | |
168 | for (i = 0; i < m->num_packs; i++) { | |
169 | m->pack_names[i] = cur_pack_name; | |
170 | ||
171 | cur_pack_name += strlen(cur_pack_name) + 1; | |
172 | ||
8e72a3c3 DS |
173 | if (i && strcmp(m->pack_names[i], m->pack_names[i - 1]) <= 0) |
174 | die(_("multi-pack-index pack names out of order: '%s' before '%s'"), | |
3227565c DS |
175 | m->pack_names[i - 1], |
176 | m->pack_names[i]); | |
3227565c DS |
177 | } |
178 | ||
d829223a JH |
179 | trace2_data_intmax("midx", the_repository, "load/num_packs", m->num_packs); |
180 | trace2_data_intmax("midx", the_repository, "load/num_objects", m->num_objects); | |
181 | ||
4d80560c DS |
182 | return m; |
183 | ||
184 | cleanup_fail: | |
185 | free(m); | |
186 | free(midx_name); | |
6ab3b8b8 | 187 | free(cf); |
4d80560c DS |
188 | if (midx_map) |
189 | munmap(midx_map, midx_size); | |
190 | if (0 <= fd) | |
191 | close(fd); | |
192 | return NULL; | |
193 | } | |
194 | ||
1dcd9f20 | 195 | void close_midx(struct multi_pack_index *m) |
a40498a1 DS |
196 | { |
197 | uint32_t i; | |
1dcd9f20 DS |
198 | |
199 | if (!m) | |
200 | return; | |
201 | ||
9bb6c2e5 TB |
202 | close_midx(m->next); |
203 | ||
a40498a1 | 204 | munmap((unsigned char *)m->data, m->data_len); |
a40498a1 DS |
205 | |
206 | for (i = 0; i < m->num_packs; i++) { | |
af96fe33 DS |
207 | if (m->packs[i]) |
208 | m->packs[i]->multi_pack_index = 0; | |
a40498a1 DS |
209 | } |
210 | FREE_AND_NULL(m->packs); | |
211 | FREE_AND_NULL(m->pack_names); | |
9bb6c2e5 | 212 | free(m); |
a40498a1 DS |
213 | } |
214 | ||
64404a24 | 215 | int prepare_midx_pack(struct repository *r, struct multi_pack_index *m, uint32_t pack_int_id) |
3715a633 DS |
216 | { |
217 | struct strbuf pack_name = STRBUF_INIT; | |
af96fe33 | 218 | struct packed_git *p; |
3715a633 DS |
219 | |
220 | if (pack_int_id >= m->num_packs) | |
d355e46a | 221 | die(_("bad pack-int-id: %u (%u total packs)"), |
cc6af73c | 222 | pack_int_id, m->num_packs); |
3715a633 DS |
223 | |
224 | if (m->packs[pack_int_id]) | |
225 | return 0; | |
226 | ||
227 | strbuf_addf(&pack_name, "%s/pack/%s", m->object_dir, | |
228 | m->pack_names[pack_int_id]); | |
229 | ||
af96fe33 | 230 | p = add_packed_git(pack_name.buf, pack_name.len, m->local); |
3715a633 | 231 | strbuf_release(&pack_name); |
af96fe33 DS |
232 | |
233 | if (!p) | |
234 | return 1; | |
235 | ||
236 | p->multi_pack_index = 1; | |
237 | m->packs[pack_int_id] = p; | |
238 | install_packed_git(r, p); | |
239 | list_add_tail(&p->mru, &r->objects->packed_git_mru); | |
240 | ||
241 | return 0; | |
3715a633 DS |
242 | } |
243 | ||
244 | int bsearch_midx(const struct object_id *oid, struct multi_pack_index *m, uint32_t *result) | |
245 | { | |
246 | return bsearch_hash(oid->hash, m->chunk_oid_fanout, m->chunk_oid_lookup, | |
aaa95dfa | 247 | the_hash_algo->rawsz, result); |
3715a633 DS |
248 | } |
249 | ||
8aac67a1 DS |
250 | struct object_id *nth_midxed_object_oid(struct object_id *oid, |
251 | struct multi_pack_index *m, | |
252 | uint32_t n) | |
253 | { | |
254 | if (n >= m->num_objects) | |
255 | return NULL; | |
256 | ||
92e2cab9 | 257 | oidread(oid, m->chunk_oid_lookup + m->hash_len * n); |
8aac67a1 DS |
258 | return oid; |
259 | } | |
260 | ||
62f2c1b5 | 261 | off_t nth_midxed_offset(struct multi_pack_index *m, uint32_t pos) |
3715a633 DS |
262 | { |
263 | const unsigned char *offset_data; | |
264 | uint32_t offset32; | |
265 | ||
329fac3a | 266 | offset_data = m->chunk_object_offsets + (off_t)pos * MIDX_CHUNK_OFFSET_WIDTH; |
3715a633 DS |
267 | offset32 = get_be32(offset_data + sizeof(uint32_t)); |
268 | ||
269 | if (m->chunk_large_offsets && offset32 & MIDX_LARGE_OFFSET_NEEDED) { | |
d8ac9ee1 | 270 | if (sizeof(off_t) < sizeof(uint64_t)) |
3715a633 DS |
271 | die(_("multi-pack-index stores a 64-bit offset, but off_t is too small")); |
272 | ||
273 | offset32 ^= MIDX_LARGE_OFFSET_NEEDED; | |
274 | return get_be64(m->chunk_large_offsets + sizeof(uint64_t) * offset32); | |
275 | } | |
276 | ||
277 | return offset32; | |
278 | } | |
279 | ||
62f2c1b5 | 280 | uint32_t nth_midxed_pack_int_id(struct multi_pack_index *m, uint32_t pos) |
3715a633 | 281 | { |
329fac3a DS |
282 | return get_be32(m->chunk_object_offsets + |
283 | (off_t)pos * MIDX_CHUNK_OFFSET_WIDTH); | |
3715a633 DS |
284 | } |
285 | ||
64404a24 DS |
286 | static int nth_midxed_pack_entry(struct repository *r, |
287 | struct multi_pack_index *m, | |
288 | struct pack_entry *e, | |
289 | uint32_t pos) | |
3715a633 DS |
290 | { |
291 | uint32_t pack_int_id; | |
292 | struct packed_git *p; | |
293 | ||
294 | if (pos >= m->num_objects) | |
295 | return 0; | |
296 | ||
297 | pack_int_id = nth_midxed_pack_int_id(m, pos); | |
298 | ||
64404a24 | 299 | if (prepare_midx_pack(r, m, pack_int_id)) |
506ec2fb | 300 | return 0; |
3715a633 DS |
301 | p = m->packs[pack_int_id]; |
302 | ||
303 | /* | |
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 | |
308 | * loaded! | |
309 | */ | |
310 | if (!is_pack_valid(p)) | |
311 | return 0; | |
312 | ||
c39b02ae DS |
313 | if (p->num_bad_objects) { |
314 | uint32_t i; | |
315 | struct object_id oid; | |
316 | nth_midxed_object_oid(&oid, m, pos); | |
317 | for (i = 0; i < p->num_bad_objects; i++) | |
e43d2dcc JK |
318 | if (hasheq(oid.hash, |
319 | p->bad_object_sha1 + the_hash_algo->rawsz * i)) | |
c39b02ae DS |
320 | return 0; |
321 | } | |
322 | ||
3715a633 DS |
323 | e->offset = nth_midxed_offset(m, pos); |
324 | e->p = p; | |
325 | ||
326 | return 1; | |
327 | } | |
328 | ||
64404a24 DS |
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) | |
3715a633 DS |
333 | { |
334 | uint32_t pos; | |
335 | ||
336 | if (!bsearch_midx(oid, m, &pos)) | |
337 | return 0; | |
338 | ||
64404a24 | 339 | return nth_midxed_pack_entry(r, m, e, pos); |
3715a633 DS |
340 | } |
341 | ||
013fd7ad JK |
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) | |
345 | { | |
346 | /* Skip past any initial matching prefix. */ | |
347 | while (*idx_name && *idx_name == *idx_or_pack_name) { | |
348 | idx_name++; | |
349 | idx_or_pack_name++; | |
350 | } | |
351 | ||
352 | /* | |
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. | |
358 | * | |
359 | * Technically this matches "fooidx" and "foopack", but we'd never have | |
360 | * such names in the first place. | |
361 | */ | |
362 | if (!strcmp(idx_name, "idx") && !strcmp(idx_or_pack_name, "pack")) | |
363 | return 0; | |
364 | ||
365 | /* | |
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. | |
370 | */ | |
371 | return strcmp(idx_or_pack_name, idx_name); | |
372 | } | |
373 | ||
374 | int midx_contains_pack(struct multi_pack_index *m, const char *idx_or_pack_name) | |
a40498a1 DS |
375 | { |
376 | uint32_t first = 0, last = m->num_packs; | |
377 | ||
378 | while (first < last) { | |
379 | uint32_t mid = first + (last - first) / 2; | |
380 | const char *current; | |
381 | int cmp; | |
382 | ||
383 | current = m->pack_names[mid]; | |
013fd7ad | 384 | cmp = cmp_idx_or_pack_name(idx_or_pack_name, current); |
a40498a1 DS |
385 | if (!cmp) |
386 | return 1; | |
387 | if (cmp > 0) { | |
388 | first = mid + 1; | |
389 | continue; | |
390 | } | |
391 | last = mid; | |
392 | } | |
393 | ||
394 | return 0; | |
395 | } | |
396 | ||
2cf489a3 | 397 | int prepare_multi_pack_index_one(struct repository *r, const char *object_dir, int local) |
c4d25228 | 398 | { |
29e2016b | 399 | struct multi_pack_index *m; |
c4d25228 | 400 | struct multi_pack_index *m_search; |
c4d25228 | 401 | |
18e449f8 DS |
402 | prepare_repo_settings(r); |
403 | if (!r->settings.core_multi_pack_index) | |
c4d25228 DS |
404 | return 0; |
405 | ||
29e2016b | 406 | for (m_search = r->objects->multi_pack_index; m_search; m_search = m_search->next) |
c4d25228 DS |
407 | if (!strcmp(object_dir, m_search->object_dir)) |
408 | return 1; | |
409 | ||
29e2016b | 410 | m = load_multi_pack_index(object_dir, local); |
c4d25228 | 411 | |
29e2016b | 412 | if (m) { |
59552fb3 TB |
413 | struct multi_pack_index *mp = r->objects->multi_pack_index; |
414 | if (mp) { | |
415 | m->next = mp->next; | |
416 | mp->next = m; | |
417 | } else | |
418 | r->objects->multi_pack_index = m; | |
c4d25228 DS |
419 | return 1; |
420 | } | |
421 | ||
422 | return 0; | |
423 | } | |
424 | ||
fc59e748 DS |
425 | static size_t write_midx_header(struct hashfile *f, |
426 | unsigned char num_chunks, | |
427 | uint32_t num_packs) | |
428 | { | |
fc59e748 | 429 | hashwrite_be32(f, MIDX_SIGNATURE); |
014f1447 RS |
430 | hashwrite_u8(f, MIDX_VERSION); |
431 | hashwrite_u8(f, oid_version()); | |
432 | hashwrite_u8(f, num_chunks); | |
433 | hashwrite_u8(f, 0); /* unused */ | |
fc59e748 DS |
434 | hashwrite_be32(f, num_packs); |
435 | ||
436 | return MIDX_HEADER_SIZE; | |
437 | } | |
438 | ||
d01bf2e6 DS |
439 | struct pack_info { |
440 | uint32_t orig_pack_int_id; | |
441 | char *pack_name; | |
442 | struct packed_git *p; | |
19575c7c | 443 | unsigned expired : 1; |
d01bf2e6 DS |
444 | }; |
445 | ||
446 | static int pack_info_compare(const void *_a, const void *_b) | |
447 | { | |
448 | struct pack_info *a = (struct pack_info *)_a; | |
449 | struct pack_info *b = (struct pack_info *)_b; | |
450 | return strcmp(a->pack_name, b->pack_name); | |
451 | } | |
452 | ||
9218c6a4 TB |
453 | static int idx_or_pack_name_cmp(const void *_va, const void *_vb) |
454 | { | |
455 | const char *pack_name = _va; | |
456 | const struct pack_info *compar = _vb; | |
457 | ||
458 | return cmp_idx_or_pack_name(pack_name, compar->pack_name); | |
459 | } | |
460 | ||
577dc496 | 461 | struct write_midx_context { |
d01bf2e6 | 462 | struct pack_info *info; |
396f2570 | 463 | uint32_t nr; |
d01bf2e6 | 464 | uint32_t alloc; |
a40498a1 | 465 | struct multi_pack_index *m; |
840cef0c WB |
466 | struct progress *progress; |
467 | unsigned pack_paths_checked; | |
31bda9a2 DS |
468 | |
469 | struct pack_midx_entry *entries; | |
470 | uint32_t entries_nr; | |
7a3ada11 DS |
471 | |
472 | uint32_t *pack_perm; | |
38ff7cab | 473 | uint32_t *pack_order; |
7a3ada11 | 474 | unsigned large_offsets_needed:1; |
980f525c | 475 | uint32_t num_large_offsets; |
9218c6a4 TB |
476 | |
477 | int preferred_pack_idx; | |
56d863e9 TB |
478 | |
479 | struct string_list *to_include; | |
396f2570 DS |
480 | }; |
481 | ||
482 | static void add_pack_to_midx(const char *full_path, size_t full_path_len, | |
483 | const char *file_name, void *data) | |
484 | { | |
577dc496 | 485 | struct write_midx_context *ctx = data; |
396f2570 DS |
486 | |
487 | if (ends_with(file_name, ".idx")) { | |
577dc496 | 488 | display_progress(ctx->progress, ++ctx->pack_paths_checked); |
56d863e9 TB |
489 | /* |
490 | * Note that at most one of ctx->m and ctx->to_include are set, | |
491 | * so we are testing midx_contains_pack() and | |
492 | * string_list_has_string() independently (guarded by the | |
493 | * appropriate NULL checks). | |
494 | * | |
495 | * We could support passing to_include while reusing an existing | |
496 | * MIDX, but don't currently since the reuse process drags | |
497 | * forward all packs from an existing MIDX (without checking | |
498 | * whether or not they appear in the to_include list). | |
499 | * | |
500 | * If we added support for that, these next two conditional | |
501 | * should be performed independently (likely checking | |
502 | * to_include before the existing MIDX). | |
503 | */ | |
577dc496 | 504 | if (ctx->m && midx_contains_pack(ctx->m, file_name)) |
a40498a1 | 505 | return; |
56d863e9 TB |
506 | else if (ctx->to_include && |
507 | !string_list_has_string(ctx->to_include, file_name)) | |
508 | return; | |
a40498a1 | 509 | |
577dc496 | 510 | ALLOC_GROW(ctx->info, ctx->nr + 1, ctx->alloc); |
396f2570 | 511 | |
577dc496 DS |
512 | ctx->info[ctx->nr].p = add_packed_git(full_path, |
513 | full_path_len, | |
514 | 0); | |
fe1ed56f | 515 | |
577dc496 | 516 | if (!ctx->info[ctx->nr].p) { |
396f2570 DS |
517 | warning(_("failed to add packfile '%s'"), |
518 | full_path); | |
519 | return; | |
520 | } | |
521 | ||
577dc496 | 522 | if (open_pack_index(ctx->info[ctx->nr].p)) { |
fe1ed56f DS |
523 | warning(_("failed to open pack-index '%s'"), |
524 | full_path); | |
577dc496 DS |
525 | close_pack(ctx->info[ctx->nr].p); |
526 | FREE_AND_NULL(ctx->info[ctx->nr].p); | |
fe1ed56f DS |
527 | return; |
528 | } | |
529 | ||
577dc496 DS |
530 | ctx->info[ctx->nr].pack_name = xstrdup(file_name); |
531 | ctx->info[ctx->nr].orig_pack_int_id = ctx->nr; | |
532 | ctx->info[ctx->nr].expired = 0; | |
533 | ctx->nr++; | |
396f2570 DS |
534 | } |
535 | } | |
536 | ||
fe1ed56f DS |
537 | struct pack_midx_entry { |
538 | struct object_id oid; | |
539 | uint32_t pack_int_id; | |
540 | time_t pack_mtime; | |
541 | uint64_t offset; | |
9218c6a4 | 542 | unsigned preferred : 1; |
fe1ed56f DS |
543 | }; |
544 | ||
545 | static int midx_oid_compare(const void *_a, const void *_b) | |
546 | { | |
547 | const struct pack_midx_entry *a = (const struct pack_midx_entry *)_a; | |
548 | const struct pack_midx_entry *b = (const struct pack_midx_entry *)_b; | |
549 | int cmp = oidcmp(&a->oid, &b->oid); | |
550 | ||
551 | if (cmp) | |
552 | return cmp; | |
553 | ||
9218c6a4 TB |
554 | /* Sort objects in a preferred pack first when multiple copies exist. */ |
555 | if (a->preferred > b->preferred) | |
556 | return -1; | |
557 | if (a->preferred < b->preferred) | |
558 | return 1; | |
559 | ||
fe1ed56f DS |
560 | if (a->pack_mtime > b->pack_mtime) |
561 | return -1; | |
562 | else if (a->pack_mtime < b->pack_mtime) | |
563 | return 1; | |
564 | ||
565 | return a->pack_int_id - b->pack_int_id; | |
566 | } | |
567 | ||
a40498a1 | 568 | static int nth_midxed_pack_midx_entry(struct multi_pack_index *m, |
a40498a1 DS |
569 | struct pack_midx_entry *e, |
570 | uint32_t pos) | |
571 | { | |
572 | if (pos >= m->num_objects) | |
573 | return 1; | |
574 | ||
575 | nth_midxed_object_oid(&e->oid, m, pos); | |
d01bf2e6 | 576 | e->pack_int_id = nth_midxed_pack_int_id(m, pos); |
a40498a1 DS |
577 | e->offset = nth_midxed_offset(m, pos); |
578 | ||
579 | /* consider objects in midx to be from "old" packs */ | |
580 | e->pack_mtime = 0; | |
581 | return 0; | |
582 | } | |
583 | ||
fe1ed56f DS |
584 | static void fill_pack_entry(uint32_t pack_int_id, |
585 | struct packed_git *p, | |
586 | uint32_t cur_object, | |
9218c6a4 TB |
587 | struct pack_midx_entry *entry, |
588 | int preferred) | |
fe1ed56f | 589 | { |
0763671b | 590 | if (nth_packed_object_id(&entry->oid, p, cur_object) < 0) |
fe1ed56f DS |
591 | die(_("failed to locate object %d in packfile"), cur_object); |
592 | ||
593 | entry->pack_int_id = pack_int_id; | |
594 | entry->pack_mtime = p->mtime; | |
595 | ||
596 | entry->offset = nth_packed_object_offset(p, cur_object); | |
9218c6a4 | 597 | entry->preferred = !!preferred; |
fe1ed56f DS |
598 | } |
599 | ||
600 | /* | |
601 | * It is possible to artificially get into a state where there are many | |
602 | * duplicate copies of objects. That can create high memory pressure if | |
603 | * we are to create a list of all objects before de-duplication. To reduce | |
604 | * this memory pressure without a significant performance drop, automatically | |
605 | * group objects by the first byte of their object id. Use the IDX fanout | |
606 | * tables to group the data, copy to a local array, then sort. | |
607 | * | |
608 | * Copy only the de-duplicated entries (selected by most-recent modified time | |
609 | * of a packfile containing the object). | |
610 | */ | |
a40498a1 | 611 | static struct pack_midx_entry *get_sorted_entries(struct multi_pack_index *m, |
d01bf2e6 | 612 | struct pack_info *info, |
fe1ed56f | 613 | uint32_t nr_packs, |
9218c6a4 TB |
614 | uint32_t *nr_objects, |
615 | int preferred_pack) | |
fe1ed56f DS |
616 | { |
617 | uint32_t cur_fanout, cur_pack, cur_object; | |
618 | uint32_t alloc_fanout, alloc_objects, total_objects = 0; | |
619 | struct pack_midx_entry *entries_by_fanout = NULL; | |
620 | struct pack_midx_entry *deduplicated_entries = NULL; | |
a40498a1 | 621 | uint32_t start_pack = m ? m->num_packs : 0; |
fe1ed56f | 622 | |
a40498a1 | 623 | for (cur_pack = start_pack; cur_pack < nr_packs; cur_pack++) |
d01bf2e6 | 624 | total_objects += info[cur_pack].p->num_objects; |
fe1ed56f DS |
625 | |
626 | /* | |
627 | * As we de-duplicate by fanout value, we expect the fanout | |
628 | * slices to be evenly distributed, with some noise. Hence, | |
629 | * allocate slightly more than one 256th. | |
630 | */ | |
631 | alloc_objects = alloc_fanout = total_objects > 3200 ? total_objects / 200 : 16; | |
632 | ||
633 | ALLOC_ARRAY(entries_by_fanout, alloc_fanout); | |
634 | ALLOC_ARRAY(deduplicated_entries, alloc_objects); | |
635 | *nr_objects = 0; | |
636 | ||
637 | for (cur_fanout = 0; cur_fanout < 256; cur_fanout++) { | |
638 | uint32_t nr_fanout = 0; | |
639 | ||
a40498a1 DS |
640 | if (m) { |
641 | uint32_t start = 0, end; | |
642 | ||
643 | if (cur_fanout) | |
644 | start = ntohl(m->chunk_oid_fanout[cur_fanout - 1]); | |
645 | end = ntohl(m->chunk_oid_fanout[cur_fanout]); | |
646 | ||
647 | for (cur_object = start; cur_object < end; cur_object++) { | |
648 | ALLOC_GROW(entries_by_fanout, nr_fanout + 1, alloc_fanout); | |
d01bf2e6 | 649 | nth_midxed_pack_midx_entry(m, |
a40498a1 DS |
650 | &entries_by_fanout[nr_fanout], |
651 | cur_object); | |
9218c6a4 TB |
652 | if (nth_midxed_pack_int_id(m, cur_object) == preferred_pack) |
653 | entries_by_fanout[nr_fanout].preferred = 1; | |
654 | else | |
655 | entries_by_fanout[nr_fanout].preferred = 0; | |
a40498a1 DS |
656 | nr_fanout++; |
657 | } | |
658 | } | |
659 | ||
660 | for (cur_pack = start_pack; cur_pack < nr_packs; cur_pack++) { | |
fe1ed56f | 661 | uint32_t start = 0, end; |
9218c6a4 | 662 | int preferred = cur_pack == preferred_pack; |
fe1ed56f DS |
663 | |
664 | if (cur_fanout) | |
d01bf2e6 DS |
665 | start = get_pack_fanout(info[cur_pack].p, cur_fanout - 1); |
666 | end = get_pack_fanout(info[cur_pack].p, cur_fanout); | |
fe1ed56f DS |
667 | |
668 | for (cur_object = start; cur_object < end; cur_object++) { | |
669 | ALLOC_GROW(entries_by_fanout, nr_fanout + 1, alloc_fanout); | |
9218c6a4 TB |
670 | fill_pack_entry(cur_pack, |
671 | info[cur_pack].p, | |
672 | cur_object, | |
673 | &entries_by_fanout[nr_fanout], | |
674 | preferred); | |
fe1ed56f DS |
675 | nr_fanout++; |
676 | } | |
677 | } | |
678 | ||
679 | QSORT(entries_by_fanout, nr_fanout, midx_oid_compare); | |
680 | ||
681 | /* | |
682 | * The batch is now sorted by OID and then mtime (descending). | |
683 | * Take only the first duplicate. | |
684 | */ | |
685 | for (cur_object = 0; cur_object < nr_fanout; cur_object++) { | |
e43d2dcc JK |
686 | if (cur_object && oideq(&entries_by_fanout[cur_object - 1].oid, |
687 | &entries_by_fanout[cur_object].oid)) | |
fe1ed56f DS |
688 | continue; |
689 | ||
690 | ALLOC_GROW(deduplicated_entries, *nr_objects + 1, alloc_objects); | |
691 | memcpy(&deduplicated_entries[*nr_objects], | |
692 | &entries_by_fanout[cur_object], | |
693 | sizeof(struct pack_midx_entry)); | |
694 | (*nr_objects)++; | |
695 | } | |
696 | } | |
697 | ||
698 | free(entries_by_fanout); | |
699 | return deduplicated_entries; | |
700 | } | |
701 | ||
0ccd713c | 702 | static int write_midx_pack_names(struct hashfile *f, void *data) |
32f3c541 | 703 | { |
b4d94142 | 704 | struct write_midx_context *ctx = data; |
32f3c541 DS |
705 | uint32_t i; |
706 | unsigned char padding[MIDX_CHUNK_ALIGNMENT]; | |
707 | size_t written = 0; | |
708 | ||
b4d94142 | 709 | for (i = 0; i < ctx->nr; i++) { |
19575c7c DS |
710 | size_t writelen; |
711 | ||
b4d94142 | 712 | if (ctx->info[i].expired) |
19575c7c | 713 | continue; |
32f3c541 | 714 | |
b4d94142 | 715 | if (i && strcmp(ctx->info[i].pack_name, ctx->info[i - 1].pack_name) <= 0) |
32f3c541 | 716 | BUG("incorrect pack-file order: %s before %s", |
b4d94142 DS |
717 | ctx->info[i - 1].pack_name, |
718 | ctx->info[i].pack_name); | |
32f3c541 | 719 | |
b4d94142 DS |
720 | writelen = strlen(ctx->info[i].pack_name) + 1; |
721 | hashwrite(f, ctx->info[i].pack_name, writelen); | |
32f3c541 DS |
722 | written += writelen; |
723 | } | |
724 | ||
725 | /* add padding to be aligned */ | |
726 | i = MIDX_CHUNK_ALIGNMENT - (written % MIDX_CHUNK_ALIGNMENT); | |
727 | if (i < MIDX_CHUNK_ALIGNMENT) { | |
728 | memset(padding, 0, sizeof(padding)); | |
729 | hashwrite(f, padding, i); | |
32f3c541 DS |
730 | } |
731 | ||
0ccd713c | 732 | return 0; |
32f3c541 DS |
733 | } |
734 | ||
0ccd713c DS |
735 | static int write_midx_oid_fanout(struct hashfile *f, |
736 | void *data) | |
d7cacf29 | 737 | { |
31bda9a2 DS |
738 | struct write_midx_context *ctx = data; |
739 | struct pack_midx_entry *list = ctx->entries; | |
740 | struct pack_midx_entry *last = ctx->entries + ctx->entries_nr; | |
d7cacf29 DS |
741 | uint32_t count = 0; |
742 | uint32_t i; | |
743 | ||
744 | /* | |
745 | * Write the first-level table (the list is sorted, | |
746 | * but we use a 256-entry lookup to be able to avoid | |
747 | * having to do eight extra binary search iterations). | |
748 | */ | |
749 | for (i = 0; i < 256; i++) { | |
750 | struct pack_midx_entry *next = list; | |
751 | ||
752 | while (next < last && next->oid.hash[0] == i) { | |
753 | count++; | |
754 | next++; | |
755 | } | |
756 | ||
757 | hashwrite_be32(f, count); | |
758 | list = next; | |
759 | } | |
760 | ||
0ccd713c | 761 | return 0; |
d7cacf29 DS |
762 | } |
763 | ||
0ccd713c DS |
764 | static int write_midx_oid_lookup(struct hashfile *f, |
765 | void *data) | |
0d5b3a5e | 766 | { |
31bda9a2 DS |
767 | struct write_midx_context *ctx = data; |
768 | unsigned char hash_len = the_hash_algo->rawsz; | |
769 | struct pack_midx_entry *list = ctx->entries; | |
0d5b3a5e | 770 | uint32_t i; |
0d5b3a5e | 771 | |
31bda9a2 | 772 | for (i = 0; i < ctx->entries_nr; i++) { |
0d5b3a5e DS |
773 | struct pack_midx_entry *obj = list++; |
774 | ||
31bda9a2 | 775 | if (i < ctx->entries_nr - 1) { |
0d5b3a5e DS |
776 | struct pack_midx_entry *next = list; |
777 | if (oidcmp(&obj->oid, &next->oid) >= 0) | |
778 | BUG("OIDs not in order: %s >= %s", | |
779 | oid_to_hex(&obj->oid), | |
780 | oid_to_hex(&next->oid)); | |
781 | } | |
782 | ||
783 | hashwrite(f, obj->oid.hash, (int)hash_len); | |
0d5b3a5e DS |
784 | } |
785 | ||
0ccd713c | 786 | return 0; |
0d5b3a5e DS |
787 | } |
788 | ||
0ccd713c DS |
789 | static int write_midx_object_offsets(struct hashfile *f, |
790 | void *data) | |
662148c4 | 791 | { |
7a3ada11 DS |
792 | struct write_midx_context *ctx = data; |
793 | struct pack_midx_entry *list = ctx->entries; | |
662148c4 | 794 | uint32_t i, nr_large_offset = 0; |
662148c4 | 795 | |
7a3ada11 | 796 | for (i = 0; i < ctx->entries_nr; i++) { |
662148c4 DS |
797 | struct pack_midx_entry *obj = list++; |
798 | ||
7a3ada11 | 799 | if (ctx->pack_perm[obj->pack_int_id] == PACK_EXPIRED) |
19575c7c DS |
800 | BUG("object %s is in an expired pack with int-id %d", |
801 | oid_to_hex(&obj->oid), | |
802 | obj->pack_int_id); | |
803 | ||
7a3ada11 | 804 | hashwrite_be32(f, ctx->pack_perm[obj->pack_int_id]); |
662148c4 | 805 | |
7a3ada11 | 806 | if (ctx->large_offsets_needed && obj->offset >> 31) |
662148c4 | 807 | hashwrite_be32(f, MIDX_LARGE_OFFSET_NEEDED | nr_large_offset++); |
7a3ada11 | 808 | else if (!ctx->large_offsets_needed && obj->offset >> 32) |
662148c4 DS |
809 | BUG("object %s requires a large offset (%"PRIx64") but the MIDX is not writing large offsets!", |
810 | oid_to_hex(&obj->oid), | |
811 | obj->offset); | |
812 | else | |
813 | hashwrite_be32(f, (uint32_t)obj->offset); | |
662148c4 DS |
814 | } |
815 | ||
0ccd713c | 816 | return 0; |
662148c4 DS |
817 | } |
818 | ||
0ccd713c DS |
819 | static int write_midx_large_offsets(struct hashfile *f, |
820 | void *data) | |
662148c4 | 821 | { |
980f525c DS |
822 | struct write_midx_context *ctx = data; |
823 | struct pack_midx_entry *list = ctx->entries; | |
824 | struct pack_midx_entry *end = ctx->entries + ctx->entries_nr; | |
980f525c | 825 | uint32_t nr_large_offset = ctx->num_large_offsets; |
662148c4 DS |
826 | |
827 | while (nr_large_offset) { | |
61b0fcbb JK |
828 | struct pack_midx_entry *obj; |
829 | uint64_t offset; | |
830 | ||
831 | if (list >= end) | |
832 | BUG("too many large-offset objects"); | |
833 | ||
834 | obj = list++; | |
835 | offset = obj->offset; | |
662148c4 DS |
836 | |
837 | if (!(offset >> 31)) | |
838 | continue; | |
839 | ||
0ccd713c | 840 | hashwrite_be64(f, offset); |
662148c4 DS |
841 | |
842 | nr_large_offset--; | |
843 | } | |
844 | ||
0ccd713c | 845 | return 0; |
662148c4 DS |
846 | } |
847 | ||
30077524 JK |
848 | struct midx_pack_order_data { |
849 | uint32_t nr; | |
850 | uint32_t pack; | |
851 | off_t offset; | |
852 | }; | |
38ff7cab | 853 | |
30077524 JK |
854 | static int midx_pack_order_cmp(const void *va, const void *vb) |
855 | { | |
856 | const struct midx_pack_order_data *a = va, *b = vb; | |
857 | if (a->pack < b->pack) | |
38ff7cab | 858 | return -1; |
30077524 | 859 | else if (a->pack > b->pack) |
38ff7cab | 860 | return 1; |
30077524 | 861 | else if (a->offset < b->offset) |
38ff7cab | 862 | return -1; |
30077524 | 863 | else if (a->offset > b->offset) |
38ff7cab | 864 | return 1; |
30077524 JK |
865 | else |
866 | return 0; | |
38ff7cab TB |
867 | } |
868 | ||
869 | static uint32_t *midx_pack_order(struct write_midx_context *ctx) | |
870 | { | |
30077524 | 871 | struct midx_pack_order_data *data; |
38ff7cab TB |
872 | uint32_t *pack_order; |
873 | uint32_t i; | |
874 | ||
30077524 JK |
875 | ALLOC_ARRAY(data, ctx->entries_nr); |
876 | for (i = 0; i < ctx->entries_nr; i++) { | |
877 | struct pack_midx_entry *e = &ctx->entries[i]; | |
878 | data[i].nr = i; | |
879 | data[i].pack = ctx->pack_perm[e->pack_int_id]; | |
880 | if (!e->preferred) | |
881 | data[i].pack |= (1U << 31); | |
882 | data[i].offset = e->offset; | |
883 | } | |
884 | ||
885 | QSORT(data, ctx->entries_nr, midx_pack_order_cmp); | |
886 | ||
38ff7cab TB |
887 | ALLOC_ARRAY(pack_order, ctx->entries_nr); |
888 | for (i = 0; i < ctx->entries_nr; i++) | |
30077524 JK |
889 | pack_order[i] = data[i].nr; |
890 | free(data); | |
38ff7cab TB |
891 | |
892 | return pack_order; | |
893 | } | |
894 | ||
895 | static void write_midx_reverse_index(char *midx_name, unsigned char *midx_hash, | |
896 | struct write_midx_context *ctx) | |
897 | { | |
898 | struct strbuf buf = STRBUF_INIT; | |
899 | const char *tmp_file; | |
900 | ||
901 | strbuf_addf(&buf, "%s-%s.rev", midx_name, hash_to_hex(midx_hash)); | |
902 | ||
903 | tmp_file = write_rev_file_order(NULL, ctx->pack_order, ctx->entries_nr, | |
904 | midx_hash, WRITE_REV); | |
905 | ||
906 | if (finalize_object_file(tmp_file, buf.buf)) | |
907 | die(_("cannot store reverse index file")); | |
908 | ||
909 | strbuf_release(&buf); | |
910 | } | |
911 | ||
426c00e4 | 912 | static void clear_midx_files_ext(const char *object_dir, const char *ext, |
38ff7cab TB |
913 | unsigned char *keep_hash); |
914 | ||
ec1e28ef TB |
915 | static int midx_checksum_valid(struct multi_pack_index *m) |
916 | { | |
917 | return hashfile_checksum_valid(m->data, m->data_len); | |
918 | } | |
919 | ||
c528e179 TB |
920 | static void prepare_midx_packing_data(struct packing_data *pdata, |
921 | struct write_midx_context *ctx) | |
922 | { | |
923 | uint32_t i; | |
924 | ||
925 | memset(pdata, 0, sizeof(struct packing_data)); | |
926 | prepare_packing_data(the_repository, pdata); | |
927 | ||
928 | for (i = 0; i < ctx->entries_nr; i++) { | |
929 | struct pack_midx_entry *from = &ctx->entries[ctx->pack_order[i]]; | |
930 | struct object_entry *to = packlist_alloc(pdata, &from->oid); | |
931 | ||
932 | oe_set_in_pack(pdata, to, | |
933 | ctx->info[ctx->pack_perm[from->pack_int_id]].p); | |
934 | } | |
935 | } | |
936 | ||
937 | static int add_ref_to_pending(const char *refname, | |
938 | const struct object_id *oid, | |
939 | int flag, void *cb_data) | |
940 | { | |
941 | struct rev_info *revs = (struct rev_info*)cb_data; | |
942 | struct object *object; | |
943 | ||
944 | if ((flag & REF_ISSYMREF) && (flag & REF_ISBROKEN)) { | |
945 | warning("symbolic ref is dangling: %s", refname); | |
946 | return 0; | |
947 | } | |
948 | ||
949 | object = parse_object_or_die(oid, refname); | |
950 | if (object->type != OBJ_COMMIT) | |
951 | return 0; | |
952 | ||
953 | add_pending_object(revs, object, ""); | |
954 | if (bitmap_is_preferred_refname(revs->repo, refname)) | |
955 | object->flags |= NEEDS_BITMAP; | |
956 | return 0; | |
957 | } | |
958 | ||
959 | struct bitmap_commit_cb { | |
960 | struct commit **commits; | |
961 | size_t commits_nr, commits_alloc; | |
962 | ||
963 | struct write_midx_context *ctx; | |
964 | }; | |
965 | ||
966 | static const struct object_id *bitmap_oid_access(size_t index, | |
967 | const void *_entries) | |
968 | { | |
969 | const struct pack_midx_entry *entries = _entries; | |
970 | return &entries[index].oid; | |
971 | } | |
972 | ||
973 | static void bitmap_show_commit(struct commit *commit, void *_data) | |
974 | { | |
975 | struct bitmap_commit_cb *data = _data; | |
976 | int pos = oid_pos(&commit->object.oid, data->ctx->entries, | |
977 | data->ctx->entries_nr, | |
978 | bitmap_oid_access); | |
979 | if (pos < 0) | |
980 | return; | |
981 | ||
982 | ALLOC_GROW(data->commits, data->commits_nr + 1, data->commits_alloc); | |
983 | data->commits[data->commits_nr++] = commit; | |
984 | } | |
985 | ||
08944d1c TB |
986 | static int read_refs_snapshot(const char *refs_snapshot, |
987 | struct rev_info *revs) | |
988 | { | |
989 | struct strbuf buf = STRBUF_INIT; | |
990 | struct object_id oid; | |
991 | FILE *f = xfopen(refs_snapshot, "r"); | |
992 | ||
993 | while (strbuf_getline(&buf, f) != EOF) { | |
994 | struct object *object; | |
995 | int preferred = 0; | |
996 | char *hex = buf.buf; | |
997 | const char *end = NULL; | |
998 | ||
999 | if (buf.len && *buf.buf == '+') { | |
1000 | preferred = 1; | |
1001 | hex = &buf.buf[1]; | |
1002 | } | |
1003 | ||
1004 | if (parse_oid_hex(hex, &oid, &end) < 0) | |
1005 | die(_("could not parse line: %s"), buf.buf); | |
1006 | if (*end) | |
1007 | die(_("malformed line: %s"), buf.buf); | |
1008 | ||
1009 | object = parse_object_or_die(&oid, NULL); | |
1010 | if (preferred) | |
1011 | object->flags |= NEEDS_BITMAP; | |
1012 | ||
1013 | add_pending_object(revs, object, ""); | |
1014 | } | |
1015 | ||
1016 | fclose(f); | |
1017 | strbuf_release(&buf); | |
1018 | return 0; | |
1019 | } | |
1020 | ||
c528e179 | 1021 | static struct commit **find_commits_for_midx_bitmap(uint32_t *indexed_commits_nr_p, |
08944d1c | 1022 | const char *refs_snapshot, |
c528e179 TB |
1023 | struct write_midx_context *ctx) |
1024 | { | |
1025 | struct rev_info revs; | |
1026 | struct bitmap_commit_cb cb = {0}; | |
1027 | ||
1028 | cb.ctx = ctx; | |
1029 | ||
1030 | repo_init_revisions(the_repository, &revs, NULL); | |
08944d1c TB |
1031 | if (refs_snapshot) { |
1032 | read_refs_snapshot(refs_snapshot, &revs); | |
1033 | } else { | |
1034 | setup_revisions(0, NULL, &revs, NULL); | |
1035 | for_each_ref(add_ref_to_pending, &revs); | |
1036 | } | |
c528e179 TB |
1037 | |
1038 | /* | |
1039 | * Skipping promisor objects here is intentional, since it only excludes | |
1040 | * them from the list of reachable commits that we want to select from | |
1041 | * when computing the selection of MIDX'd commits to receive bitmaps. | |
1042 | * | |
1043 | * Reachability bitmaps do require that their objects be closed under | |
1044 | * reachability, but fetching any objects missing from promisors at this | |
1045 | * point is too late. But, if one of those objects can be reached from | |
1046 | * an another object that is included in the bitmap, then we will | |
1047 | * complain later that we don't have reachability closure (and fail | |
1048 | * appropriately). | |
1049 | */ | |
1050 | fetch_if_missing = 0; | |
1051 | revs.exclude_promisor_objects = 1; | |
1052 | ||
1053 | if (prepare_revision_walk(&revs)) | |
1054 | die(_("revision walk setup failed")); | |
1055 | ||
1056 | traverse_commit_list(&revs, bitmap_show_commit, NULL, &cb); | |
1057 | if (indexed_commits_nr_p) | |
1058 | *indexed_commits_nr_p = cb.commits_nr; | |
1059 | ||
1060 | return cb.commits; | |
1061 | } | |
1062 | ||
1063 | static int write_midx_bitmap(char *midx_name, unsigned char *midx_hash, | |
1064 | struct write_midx_context *ctx, | |
08944d1c | 1065 | const char *refs_snapshot, |
c528e179 TB |
1066 | unsigned flags) |
1067 | { | |
1068 | struct packing_data pdata; | |
1069 | struct pack_idx_entry **index; | |
1070 | struct commit **commits = NULL; | |
1071 | uint32_t i, commits_nr; | |
1072 | char *bitmap_name = xstrfmt("%s-%s.bitmap", midx_name, hash_to_hex(midx_hash)); | |
1073 | int ret; | |
1074 | ||
1075 | prepare_midx_packing_data(&pdata, ctx); | |
1076 | ||
08944d1c | 1077 | commits = find_commits_for_midx_bitmap(&commits_nr, refs_snapshot, ctx); |
c528e179 TB |
1078 | |
1079 | /* | |
1080 | * Build the MIDX-order index based on pdata.objects (which is already | |
1081 | * in MIDX order; c.f., 'midx_pack_order_cmp()' for the definition of | |
1082 | * this order). | |
1083 | */ | |
1084 | ALLOC_ARRAY(index, pdata.nr_objects); | |
1085 | for (i = 0; i < pdata.nr_objects; i++) | |
1086 | index[i] = &pdata.objects[i].idx; | |
1087 | ||
1088 | bitmap_writer_show_progress(flags & MIDX_PROGRESS); | |
1089 | bitmap_writer_build_type_index(&pdata, index, pdata.nr_objects); | |
1090 | ||
1091 | /* | |
1092 | * bitmap_writer_finish expects objects in lex order, but pack_order | |
1093 | * gives us exactly that. use it directly instead of re-sorting the | |
1094 | * array. | |
1095 | * | |
1096 | * This changes the order of objects in 'index' between | |
1097 | * bitmap_writer_build_type_index and bitmap_writer_finish. | |
1098 | * | |
1099 | * The same re-ordering takes place in the single-pack bitmap code via | |
1100 | * write_idx_file(), which is called by finish_tmp_packfile(), which | |
1101 | * happens between bitmap_writer_build_type_index() and | |
1102 | * bitmap_writer_finish(). | |
1103 | */ | |
1104 | for (i = 0; i < pdata.nr_objects; i++) | |
1105 | index[ctx->pack_order[i]] = &pdata.objects[i].idx; | |
1106 | ||
1107 | bitmap_writer_select_commits(commits, commits_nr, -1); | |
1108 | ret = bitmap_writer_build(&pdata); | |
1109 | if (ret < 0) | |
1110 | goto cleanup; | |
1111 | ||
1112 | bitmap_writer_set_checksum(midx_hash); | |
1113 | bitmap_writer_finish(index, pdata.nr_objects, bitmap_name, 0); | |
1114 | ||
1115 | cleanup: | |
1116 | free(index); | |
1117 | free(bitmap_name); | |
1118 | return ret; | |
1119 | } | |
1120 | ||
f57a7396 | 1121 | static int write_midx_internal(const char *object_dir, |
56d863e9 | 1122 | struct string_list *packs_to_include, |
9218c6a4 TB |
1123 | struct string_list *packs_to_drop, |
1124 | const char *preferred_pack_name, | |
08944d1c | 1125 | const char *refs_snapshot, |
9218c6a4 | 1126 | unsigned flags) |
a3407730 | 1127 | { |
fc59e748 | 1128 | char *midx_name; |
9f191611 | 1129 | unsigned char midx_hash[GIT_MAX_RAWSZ]; |
396f2570 | 1130 | uint32_t i; |
fc59e748 DS |
1131 | struct hashfile *f = NULL; |
1132 | struct lock_file lk; | |
577dc496 | 1133 | struct write_midx_context ctx = { 0 }; |
f57a7396 | 1134 | struct multi_pack_index *cur; |
dba6175c | 1135 | int pack_name_concat_len = 0; |
19575c7c DS |
1136 | int dropped_packs = 0; |
1137 | int result = 0; | |
63a8f0e9 | 1138 | struct chunkfile *cf; |
fc59e748 | 1139 | |
f57a7396 TB |
1140 | /* Ensure the given object_dir is local, or a known alternate. */ |
1141 | find_odb(the_repository, object_dir); | |
1142 | ||
fc59e748 | 1143 | midx_name = get_midx_filename(object_dir); |
d5e1961c | 1144 | if (safe_create_leading_directories(midx_name)) |
fc59e748 DS |
1145 | die_errno(_("unable to create leading directories of %s"), |
1146 | midx_name); | |
fc59e748 | 1147 | |
56d863e9 TB |
1148 | if (!packs_to_include) { |
1149 | /* | |
1150 | * Only reference an existing MIDX when not filtering which | |
1151 | * packs to include, since all packs and objects are copied | |
1152 | * blindly from an existing MIDX if one is present. | |
1153 | */ | |
1154 | for (cur = get_multi_pack_index(the_repository); cur; cur = cur->next) { | |
1155 | if (!strcmp(object_dir, cur->object_dir)) { | |
1156 | ctx.m = cur; | |
1157 | break; | |
1158 | } | |
f57a7396 TB |
1159 | } |
1160 | } | |
577dc496 | 1161 | |
ec1e28ef TB |
1162 | if (ctx.m && !midx_checksum_valid(ctx.m)) { |
1163 | warning(_("ignoring existing multi-pack-index; checksum mismatch")); | |
1164 | ctx.m = NULL; | |
1165 | } | |
1166 | ||
577dc496 DS |
1167 | ctx.nr = 0; |
1168 | ctx.alloc = ctx.m ? ctx.m->num_packs : 16; | |
1169 | ctx.info = NULL; | |
1170 | ALLOC_ARRAY(ctx.info, ctx.alloc); | |
1171 | ||
1172 | if (ctx.m) { | |
1173 | for (i = 0; i < ctx.m->num_packs; i++) { | |
1174 | ALLOC_GROW(ctx.info, ctx.nr + 1, ctx.alloc); | |
1175 | ||
1176 | ctx.info[ctx.nr].orig_pack_int_id = i; | |
1177 | ctx.info[ctx.nr].pack_name = xstrdup(ctx.m->pack_names[i]); | |
c528e179 | 1178 | ctx.info[ctx.nr].p = ctx.m->packs[i]; |
577dc496 | 1179 | ctx.info[ctx.nr].expired = 0; |
5d3cd09a TB |
1180 | |
1181 | if (flags & MIDX_WRITE_REV_INDEX) { | |
1182 | /* | |
1183 | * If generating a reverse index, need to have | |
1184 | * packed_git's loaded to compare their | |
1185 | * mtimes and object count. | |
1186 | */ | |
1187 | if (prepare_midx_pack(the_repository, ctx.m, i)) { | |
1188 | error(_("could not load pack")); | |
1189 | result = 1; | |
1190 | goto cleanup; | |
1191 | } | |
1192 | ||
1193 | if (open_pack_index(ctx.m->packs[i])) | |
1194 | die(_("could not open index for %s"), | |
1195 | ctx.m->packs[i]->pack_name); | |
1196 | ctx.info[ctx.nr].p = ctx.m->packs[i]; | |
1197 | } | |
1198 | ||
577dc496 | 1199 | ctx.nr++; |
a40498a1 DS |
1200 | } |
1201 | } | |
1202 | ||
577dc496 | 1203 | ctx.pack_paths_checked = 0; |
840cef0c | 1204 | if (flags & MIDX_PROGRESS) |
577dc496 | 1205 | ctx.progress = start_delayed_progress(_("Adding packfiles to multi-pack-index"), 0); |
840cef0c | 1206 | else |
577dc496 | 1207 | ctx.progress = NULL; |
840cef0c | 1208 | |
56d863e9 TB |
1209 | ctx.to_include = packs_to_include; |
1210 | ||
577dc496 DS |
1211 | for_each_file_in_pack_dir(object_dir, add_pack_to_midx, &ctx); |
1212 | stop_progress(&ctx.progress); | |
396f2570 | 1213 | |
56d863e9 TB |
1214 | if ((ctx.m && ctx.nr == ctx.m->num_packs) && |
1215 | !(packs_to_include || packs_to_drop)) { | |
c528e179 TB |
1216 | struct bitmap_index *bitmap_git; |
1217 | int bitmap_exists; | |
1218 | int want_bitmap = flags & MIDX_WRITE_BITMAP; | |
1219 | ||
bfbb60d3 | 1220 | bitmap_git = prepare_midx_bitmap_git(ctx.m); |
c528e179 TB |
1221 | bitmap_exists = bitmap_git && bitmap_is_midx(bitmap_git); |
1222 | free_bitmap_index(bitmap_git); | |
1223 | ||
1224 | if (bitmap_exists || !want_bitmap) { | |
1225 | /* | |
1226 | * The correct MIDX already exists, and so does a | |
1227 | * corresponding bitmap (or one wasn't requested). | |
1228 | */ | |
1229 | if (!want_bitmap) | |
1230 | clear_midx_files_ext(object_dir, ".bitmap", | |
1231 | NULL); | |
1232 | goto cleanup; | |
1233 | } | |
1234 | } | |
a40498a1 | 1235 | |
9218c6a4 | 1236 | if (preferred_pack_name) { |
177c0d6e | 1237 | int found = 0; |
9218c6a4 TB |
1238 | for (i = 0; i < ctx.nr; i++) { |
1239 | if (!cmp_idx_or_pack_name(preferred_pack_name, | |
1240 | ctx.info[i].pack_name)) { | |
1241 | ctx.preferred_pack_idx = i; | |
177c0d6e | 1242 | found = 1; |
9218c6a4 TB |
1243 | break; |
1244 | } | |
1245 | } | |
177c0d6e TB |
1246 | |
1247 | if (!found) | |
1248 | warning(_("unknown preferred pack: '%s'"), | |
1249 | preferred_pack_name); | |
c528e179 TB |
1250 | } else if (ctx.nr && |
1251 | (flags & (MIDX_WRITE_REV_INDEX | MIDX_WRITE_BITMAP))) { | |
177c0d6e TB |
1252 | struct packed_git *oldest = ctx.info[ctx.preferred_pack_idx].p; |
1253 | ctx.preferred_pack_idx = 0; | |
1254 | ||
1255 | if (packs_to_drop && packs_to_drop->nr) | |
1256 | BUG("cannot write a MIDX bitmap during expiration"); | |
1257 | ||
1258 | /* | |
1259 | * set a preferred pack when writing a bitmap to ensure that | |
1260 | * the pack from which the first object is selected in pseudo | |
1261 | * pack-order has all of its objects selected from that pack | |
1262 | * (and not another pack containing a duplicate) | |
1263 | */ | |
1264 | for (i = 1; i < ctx.nr; i++) { | |
1265 | struct packed_git *p = ctx.info[i].p; | |
1266 | ||
1267 | if (!oldest->num_objects || p->mtime < oldest->mtime) { | |
1268 | oldest = p; | |
1269 | ctx.preferred_pack_idx = i; | |
1270 | } | |
1271 | } | |
1272 | ||
1273 | if (!oldest->num_objects) { | |
1274 | /* | |
1275 | * If all packs are empty; unset the preferred index. | |
1276 | * This is acceptable since there will be no duplicate | |
1277 | * objects to resolve, so the preferred value doesn't | |
1278 | * matter. | |
1279 | */ | |
1280 | ctx.preferred_pack_idx = -1; | |
1281 | } | |
1282 | } else { | |
1283 | /* | |
1284 | * otherwise don't mark any pack as preferred to avoid | |
1285 | * interfering with expiration logic below | |
1286 | */ | |
1287 | ctx.preferred_pack_idx = -1; | |
9218c6a4 TB |
1288 | } |
1289 | ||
5d3cd09a TB |
1290 | if (ctx.preferred_pack_idx > -1) { |
1291 | struct packed_git *preferred = ctx.info[ctx.preferred_pack_idx].p; | |
1292 | if (!preferred->num_objects) { | |
1293 | error(_("cannot select preferred pack %s with no objects"), | |
1294 | preferred->pack_name); | |
1295 | result = 1; | |
1296 | goto cleanup; | |
1297 | } | |
1298 | } | |
1299 | ||
9218c6a4 TB |
1300 | ctx.entries = get_sorted_entries(ctx.m, ctx.info, ctx.nr, &ctx.entries_nr, |
1301 | ctx.preferred_pack_idx); | |
a40498a1 | 1302 | |
7a3ada11 | 1303 | ctx.large_offsets_needed = 0; |
31bda9a2 DS |
1304 | for (i = 0; i < ctx.entries_nr; i++) { |
1305 | if (ctx.entries[i].offset > 0x7fffffff) | |
980f525c | 1306 | ctx.num_large_offsets++; |
31bda9a2 | 1307 | if (ctx.entries[i].offset > 0xffffffff) |
7a3ada11 | 1308 | ctx.large_offsets_needed = 1; |
662148c4 | 1309 | } |
fe1ed56f | 1310 | |
577dc496 | 1311 | QSORT(ctx.info, ctx.nr, pack_info_compare); |
d01bf2e6 | 1312 | |
19575c7c DS |
1313 | if (packs_to_drop && packs_to_drop->nr) { |
1314 | int drop_index = 0; | |
1315 | int missing_drops = 0; | |
1316 | ||
577dc496 DS |
1317 | for (i = 0; i < ctx.nr && drop_index < packs_to_drop->nr; i++) { |
1318 | int cmp = strcmp(ctx.info[i].pack_name, | |
19575c7c DS |
1319 | packs_to_drop->items[drop_index].string); |
1320 | ||
1321 | if (!cmp) { | |
1322 | drop_index++; | |
577dc496 | 1323 | ctx.info[i].expired = 1; |
19575c7c DS |
1324 | } else if (cmp > 0) { |
1325 | error(_("did not see pack-file %s to drop"), | |
1326 | packs_to_drop->items[drop_index].string); | |
1327 | drop_index++; | |
1328 | missing_drops++; | |
1329 | i--; | |
1330 | } else { | |
577dc496 | 1331 | ctx.info[i].expired = 0; |
19575c7c DS |
1332 | } |
1333 | } | |
1334 | ||
1335 | if (missing_drops) { | |
1336 | result = 1; | |
1337 | goto cleanup; | |
1338 | } | |
1339 | } | |
1340 | ||
d01bf2e6 DS |
1341 | /* |
1342 | * pack_perm stores a permutation between pack-int-ids from the | |
1343 | * previous multi-pack-index to the new one we are writing: | |
1344 | * | |
1345 | * pack_perm[old_id] = new_id | |
1346 | */ | |
7a3ada11 | 1347 | ALLOC_ARRAY(ctx.pack_perm, ctx.nr); |
577dc496 DS |
1348 | for (i = 0; i < ctx.nr; i++) { |
1349 | if (ctx.info[i].expired) { | |
19575c7c | 1350 | dropped_packs++; |
7a3ada11 | 1351 | ctx.pack_perm[ctx.info[i].orig_pack_int_id] = PACK_EXPIRED; |
19575c7c | 1352 | } else { |
7a3ada11 | 1353 | ctx.pack_perm[ctx.info[i].orig_pack_int_id] = i - dropped_packs; |
19575c7c | 1354 | } |
d01bf2e6 DS |
1355 | } |
1356 | ||
577dc496 DS |
1357 | for (i = 0; i < ctx.nr; i++) { |
1358 | if (!ctx.info[i].expired) | |
1359 | pack_name_concat_len += strlen(ctx.info[i].pack_name) + 1; | |
19575c7c | 1360 | } |
dba6175c | 1361 | |
9218c6a4 TB |
1362 | /* Check that the preferred pack wasn't expired (if given). */ |
1363 | if (preferred_pack_name) { | |
1364 | struct pack_info *preferred = bsearch(preferred_pack_name, | |
1365 | ctx.info, ctx.nr, | |
1366 | sizeof(*ctx.info), | |
1367 | idx_or_pack_name_cmp); | |
177c0d6e | 1368 | if (preferred) { |
9218c6a4 TB |
1369 | uint32_t perm = ctx.pack_perm[preferred->orig_pack_int_id]; |
1370 | if (perm == PACK_EXPIRED) | |
1371 | warning(_("preferred pack '%s' is expired"), | |
1372 | preferred_pack_name); | |
1373 | } | |
1374 | } | |
1375 | ||
dba6175c DS |
1376 | if (pack_name_concat_len % MIDX_CHUNK_ALIGNMENT) |
1377 | pack_name_concat_len += MIDX_CHUNK_ALIGNMENT - | |
1378 | (pack_name_concat_len % MIDX_CHUNK_ALIGNMENT); | |
1379 | ||
fc59e748 | 1380 | hold_lock_file_for_update(&lk, midx_name, LOCK_DIE_ON_ERROR); |
acd71602 | 1381 | f = hashfd(get_lock_file_fd(&lk), get_lock_file_path(&lk)); |
fc59e748 | 1382 | |
577dc496 | 1383 | if (ctx.nr - dropped_packs == 0) { |
796d61cd DR |
1384 | error(_("no pack files to index.")); |
1385 | result = 1; | |
1386 | goto cleanup; | |
1387 | } | |
1388 | ||
63a8f0e9 | 1389 | cf = init_chunkfile(f); |
32f3c541 | 1390 | |
63a8f0e9 DS |
1391 | add_chunk(cf, MIDX_CHUNKID_PACKNAMES, pack_name_concat_len, |
1392 | write_midx_pack_names); | |
1393 | add_chunk(cf, MIDX_CHUNKID_OIDFANOUT, MIDX_CHUNK_FANOUT_SIZE, | |
1394 | write_midx_oid_fanout); | |
1395 | add_chunk(cf, MIDX_CHUNKID_OIDLOOKUP, | |
329fac3a | 1396 | (size_t)ctx.entries_nr * the_hash_algo->rawsz, |
63a8f0e9 DS |
1397 | write_midx_oid_lookup); |
1398 | add_chunk(cf, MIDX_CHUNKID_OBJECTOFFSETS, | |
329fac3a | 1399 | (size_t)ctx.entries_nr * MIDX_CHUNK_OFFSET_WIDTH, |
63a8f0e9 | 1400 | write_midx_object_offsets); |
32f3c541 | 1401 | |
63a8f0e9 DS |
1402 | if (ctx.large_offsets_needed) |
1403 | add_chunk(cf, MIDX_CHUNKID_LARGEOFFSETS, | |
329fac3a | 1404 | (size_t)ctx.num_large_offsets * MIDX_CHUNK_LARGE_OFFSET_WIDTH, |
63a8f0e9 | 1405 | write_midx_large_offsets); |
32f3c541 | 1406 | |
63a8f0e9 DS |
1407 | write_midx_header(f, get_num_chunks(cf), ctx.nr - dropped_packs); |
1408 | write_chunkfile(cf, &ctx); | |
fc59e748 | 1409 | |
9f191611 | 1410 | finalize_hashfile(f, midx_hash, CSUM_FSYNC | CSUM_HASH_IN_STREAM); |
63a8f0e9 | 1411 | free_chunkfile(cf); |
38ff7cab | 1412 | |
c528e179 | 1413 | if (flags & (MIDX_WRITE_REV_INDEX | MIDX_WRITE_BITMAP)) |
38ff7cab TB |
1414 | ctx.pack_order = midx_pack_order(&ctx); |
1415 | ||
1416 | if (flags & MIDX_WRITE_REV_INDEX) | |
1417 | write_midx_reverse_index(midx_name, midx_hash, &ctx); | |
c528e179 | 1418 | if (flags & MIDX_WRITE_BITMAP) { |
08944d1c TB |
1419 | if (write_midx_bitmap(midx_name, midx_hash, &ctx, |
1420 | refs_snapshot, flags) < 0) { | |
c528e179 TB |
1421 | error(_("could not write multi-pack bitmap")); |
1422 | result = 1; | |
1423 | goto cleanup; | |
1424 | } | |
1425 | } | |
1426 | ||
1427 | if (ctx.m) | |
1428 | close_object_store(the_repository->objects); | |
38ff7cab | 1429 | |
fc59e748 DS |
1430 | commit_lock_file(&lk); |
1431 | ||
c528e179 | 1432 | clear_midx_files_ext(object_dir, ".bitmap", midx_hash); |
f5909d34 TB |
1433 | clear_midx_files_ext(object_dir, ".rev", midx_hash); |
1434 | ||
a40498a1 | 1435 | cleanup: |
577dc496 DS |
1436 | for (i = 0; i < ctx.nr; i++) { |
1437 | if (ctx.info[i].p) { | |
1438 | close_pack(ctx.info[i].p); | |
1439 | free(ctx.info[i].p); | |
396f2570 | 1440 | } |
577dc496 | 1441 | free(ctx.info[i].pack_name); |
396f2570 DS |
1442 | } |
1443 | ||
577dc496 | 1444 | free(ctx.info); |
31bda9a2 | 1445 | free(ctx.entries); |
7a3ada11 | 1446 | free(ctx.pack_perm); |
38ff7cab | 1447 | free(ctx.pack_order); |
a40498a1 | 1448 | free(midx_name); |
c528e179 | 1449 | |
19575c7c DS |
1450 | return result; |
1451 | } | |
1452 | ||
9218c6a4 TB |
1453 | int write_midx_file(const char *object_dir, |
1454 | const char *preferred_pack_name, | |
08944d1c | 1455 | const char *refs_snapshot, |
9218c6a4 | 1456 | unsigned flags) |
19575c7c | 1457 | { |
56d863e9 | 1458 | return write_midx_internal(object_dir, NULL, NULL, preferred_pack_name, |
08944d1c | 1459 | refs_snapshot, flags); |
56d863e9 TB |
1460 | } |
1461 | ||
1462 | int write_midx_file_only(const char *object_dir, | |
1463 | struct string_list *packs_to_include, | |
1464 | const char *preferred_pack_name, | |
08944d1c | 1465 | const char *refs_snapshot, |
56d863e9 TB |
1466 | unsigned flags) |
1467 | { | |
1468 | return write_midx_internal(object_dir, packs_to_include, NULL, | |
08944d1c | 1469 | preferred_pack_name, refs_snapshot, flags); |
a3407730 | 1470 | } |
525e18c0 | 1471 | |
38ff7cab TB |
1472 | struct clear_midx_data { |
1473 | char *keep; | |
1474 | const char *ext; | |
1475 | }; | |
1476 | ||
1477 | static void clear_midx_file_ext(const char *full_path, size_t full_path_len, | |
1478 | const char *file_name, void *_data) | |
1479 | { | |
1480 | struct clear_midx_data *data = _data; | |
1481 | ||
1482 | if (!(starts_with(file_name, "multi-pack-index-") && | |
1483 | ends_with(file_name, data->ext))) | |
1484 | return; | |
1485 | if (data->keep && !strcmp(data->keep, file_name)) | |
1486 | return; | |
1487 | ||
1488 | if (unlink(full_path)) | |
1489 | die_errno(_("failed to remove %s"), full_path); | |
1490 | } | |
1491 | ||
426c00e4 | 1492 | static void clear_midx_files_ext(const char *object_dir, const char *ext, |
38ff7cab TB |
1493 | unsigned char *keep_hash) |
1494 | { | |
1495 | struct clear_midx_data data; | |
1496 | memset(&data, 0, sizeof(struct clear_midx_data)); | |
1497 | ||
1498 | if (keep_hash) | |
1499 | data.keep = xstrfmt("multi-pack-index-%s%s", | |
1500 | hash_to_hex(keep_hash), ext); | |
1501 | data.ext = ext; | |
1502 | ||
426c00e4 | 1503 | for_each_file_in_pack_dir(object_dir, |
38ff7cab TB |
1504 | clear_midx_file_ext, |
1505 | &data); | |
1506 | ||
1507 | free(data.keep); | |
a3407730 | 1508 | } |
525e18c0 | 1509 | |
1dcd9f20 | 1510 | void clear_midx_file(struct repository *r) |
525e18c0 | 1511 | { |
3b2f8a02 | 1512 | char *midx = get_midx_filename(r->objects->odb->path); |
1dcd9f20 DS |
1513 | |
1514 | if (r->objects && r->objects->multi_pack_index) { | |
1515 | close_midx(r->objects->multi_pack_index); | |
1516 | r->objects->multi_pack_index = NULL; | |
1517 | } | |
525e18c0 | 1518 | |
d5e1961c | 1519 | if (remove_path(midx)) |
525e18c0 | 1520 | die(_("failed to clear multi-pack-index at %s"), midx); |
525e18c0 | 1521 | |
c528e179 | 1522 | clear_midx_files_ext(r->objects->odb->path, ".bitmap", NULL); |
426c00e4 | 1523 | clear_midx_files_ext(r->objects->odb->path, ".rev", NULL); |
38ff7cab | 1524 | |
525e18c0 DS |
1525 | free(midx); |
1526 | } | |
56ee7ff1 DS |
1527 | |
1528 | static int verify_midx_error; | |
1529 | ||
48ca53ca | 1530 | __attribute__((format (printf, 1, 2))) |
d4bf1d88 DS |
1531 | static void midx_report(const char *fmt, ...) |
1532 | { | |
1533 | va_list ap; | |
1534 | verify_midx_error = 1; | |
1535 | va_start(ap, fmt); | |
1536 | vfprintf(stderr, fmt, ap); | |
1537 | fprintf(stderr, "\n"); | |
1538 | va_end(ap); | |
1539 | } | |
1540 | ||
5ae18df9 JH |
1541 | struct pair_pos_vs_id |
1542 | { | |
1543 | uint32_t pos; | |
1544 | uint32_t pack_int_id; | |
1545 | }; | |
1546 | ||
1547 | static int compare_pair_pos_vs_id(const void *_a, const void *_b) | |
1548 | { | |
1549 | struct pair_pos_vs_id *a = (struct pair_pos_vs_id *)_a; | |
1550 | struct pair_pos_vs_id *b = (struct pair_pos_vs_id *)_b; | |
1551 | ||
1552 | return b->pack_int_id - a->pack_int_id; | |
1553 | } | |
1554 | ||
430efb8a JH |
1555 | /* |
1556 | * Limit calls to display_progress() for performance reasons. | |
1557 | * The interval here was arbitrarily chosen. | |
1558 | */ | |
1559 | #define SPARSE_PROGRESS_INTERVAL (1 << 12) | |
1560 | #define midx_display_sparse_progress(progress, n) \ | |
1561 | do { \ | |
1562 | uint64_t _n = (n); \ | |
1563 | if ((_n & (SPARSE_PROGRESS_INTERVAL - 1)) == 0) \ | |
1564 | display_progress(progress, _n); \ | |
1565 | } while (0) | |
1566 | ||
efbc3aee | 1567 | int verify_midx_file(struct repository *r, const char *object_dir, unsigned flags) |
56ee7ff1 | 1568 | { |
5ae18df9 | 1569 | struct pair_pos_vs_id *pairs = NULL; |
d4bf1d88 | 1570 | uint32_t i; |
ad60096d | 1571 | struct progress *progress = NULL; |
56ee7ff1 DS |
1572 | struct multi_pack_index *m = load_multi_pack_index(object_dir, 1); |
1573 | verify_midx_error = 0; | |
1574 | ||
d9607542 DS |
1575 | if (!m) { |
1576 | int result = 0; | |
1577 | struct stat sb; | |
1578 | char *filename = get_midx_filename(object_dir); | |
1579 | if (!stat(filename, &sb)) { | |
1580 | error(_("multi-pack-index file exists, but failed to parse")); | |
1581 | result = 1; | |
1582 | } | |
1583 | free(filename); | |
1584 | return result; | |
1585 | } | |
56ee7ff1 | 1586 | |
f89ecf79 TB |
1587 | if (!midx_checksum_valid(m)) |
1588 | midx_report(_("incorrect checksum")); | |
1589 | ||
ad60096d | 1590 | if (flags & MIDX_PROGRESS) |
efdd2f0d | 1591 | progress = start_delayed_progress(_("Looking for referenced packfiles"), |
ad60096d | 1592 | m->num_packs); |
d4bf1d88 | 1593 | for (i = 0; i < m->num_packs; i++) { |
64404a24 | 1594 | if (prepare_midx_pack(r, m, i)) |
d4bf1d88 | 1595 | midx_report("failed to load pack in position %d", i); |
430efb8a JH |
1596 | |
1597 | display_progress(progress, i + 1); | |
d4bf1d88 | 1598 | } |
430efb8a | 1599 | stop_progress(&progress); |
d4bf1d88 | 1600 | |
2f23d3f3 DS |
1601 | for (i = 0; i < 255; i++) { |
1602 | uint32_t oid_fanout1 = ntohl(m->chunk_oid_fanout[i]); | |
1603 | uint32_t oid_fanout2 = ntohl(m->chunk_oid_fanout[i + 1]); | |
1604 | ||
1605 | if (oid_fanout1 > oid_fanout2) | |
1606 | midx_report(_("oid fanout out of order: fanout[%d] = %"PRIx32" > %"PRIx32" = fanout[%d]"), | |
1607 | i, oid_fanout1, oid_fanout2, i + 1); | |
1608 | } | |
1609 | ||
796d61cd DR |
1610 | if (m->num_objects == 0) { |
1611 | midx_report(_("the midx contains no oid")); | |
1612 | /* | |
1613 | * Remaining tests assume that we have objects, so we can | |
1614 | * return here. | |
1615 | */ | |
1616 | return verify_midx_error; | |
1617 | } | |
1618 | ||
ad60096d WB |
1619 | if (flags & MIDX_PROGRESS) |
1620 | progress = start_sparse_progress(_("Verifying OID order in multi-pack-index"), | |
1621 | m->num_objects - 1); | |
55c5648d DS |
1622 | for (i = 0; i < m->num_objects - 1; i++) { |
1623 | struct object_id oid1, oid2; | |
1624 | ||
1625 | nth_midxed_object_oid(&oid1, m, i); | |
1626 | nth_midxed_object_oid(&oid2, m, i + 1); | |
1627 | ||
1628 | if (oidcmp(&oid1, &oid2) >= 0) | |
1629 | midx_report(_("oid lookup out of order: oid[%d] = %s >= %s = oid[%d]"), | |
1630 | i, oid_to_hex(&oid1), oid_to_hex(&oid2), i + 1); | |
430efb8a JH |
1631 | |
1632 | midx_display_sparse_progress(progress, i + 1); | |
55c5648d | 1633 | } |
430efb8a | 1634 | stop_progress(&progress); |
55c5648d | 1635 | |
5ae18df9 JH |
1636 | /* |
1637 | * Create an array mapping each object to its packfile id. Sort it | |
1638 | * to group the objects by packfile. Use this permutation to visit | |
1639 | * each of the objects and only require 1 packfile to be open at a | |
1640 | * time. | |
1641 | */ | |
1642 | ALLOC_ARRAY(pairs, m->num_objects); | |
1643 | for (i = 0; i < m->num_objects; i++) { | |
1644 | pairs[i].pos = i; | |
1645 | pairs[i].pack_int_id = nth_midxed_pack_int_id(m, i); | |
1646 | } | |
1647 | ||
ad60096d WB |
1648 | if (flags & MIDX_PROGRESS) |
1649 | progress = start_sparse_progress(_("Sorting objects by packfile"), | |
1650 | m->num_objects); | |
5ae18df9 JH |
1651 | display_progress(progress, 0); /* TODO: Measure QSORT() progress */ |
1652 | QSORT(pairs, m->num_objects, compare_pair_pos_vs_id); | |
1653 | stop_progress(&progress); | |
1654 | ||
ad60096d WB |
1655 | if (flags & MIDX_PROGRESS) |
1656 | progress = start_sparse_progress(_("Verifying object offsets"), m->num_objects); | |
cc6af73c DS |
1657 | for (i = 0; i < m->num_objects; i++) { |
1658 | struct object_id oid; | |
1659 | struct pack_entry e; | |
1660 | off_t m_offset, p_offset; | |
1661 | ||
5ae18df9 JH |
1662 | if (i > 0 && pairs[i-1].pack_int_id != pairs[i].pack_int_id && |
1663 | m->packs[pairs[i-1].pack_int_id]) | |
1664 | { | |
1665 | close_pack_fd(m->packs[pairs[i-1].pack_int_id]); | |
1666 | close_pack_index(m->packs[pairs[i-1].pack_int_id]); | |
1667 | } | |
1668 | ||
1669 | nth_midxed_object_oid(&oid, m, pairs[i].pos); | |
1670 | ||
64404a24 | 1671 | if (!fill_midx_entry(r, &oid, &e, m)) { |
cc6af73c | 1672 | midx_report(_("failed to load pack entry for oid[%d] = %s"), |
5ae18df9 | 1673 | pairs[i].pos, oid_to_hex(&oid)); |
cc6af73c DS |
1674 | continue; |
1675 | } | |
1676 | ||
1677 | if (open_pack_index(e.p)) { | |
1678 | midx_report(_("failed to load pack-index for packfile %s"), | |
1679 | e.p->pack_name); | |
1680 | break; | |
1681 | } | |
1682 | ||
1683 | m_offset = e.offset; | |
1684 | p_offset = find_pack_entry_one(oid.hash, e.p); | |
1685 | ||
1686 | if (m_offset != p_offset) | |
1687 | midx_report(_("incorrect object offset for oid[%d] = %s: %"PRIx64" != %"PRIx64), | |
5ae18df9 | 1688 | pairs[i].pos, oid_to_hex(&oid), m_offset, p_offset); |
144d7033 | 1689 | |
430efb8a | 1690 | midx_display_sparse_progress(progress, i + 1); |
cc6af73c | 1691 | } |
144d7033 | 1692 | stop_progress(&progress); |
cc6af73c | 1693 | |
5ae18df9 JH |
1694 | free(pairs); |
1695 | ||
56ee7ff1 DS |
1696 | return verify_midx_error; |
1697 | } | |
cff97116 | 1698 | |
efbc3aee | 1699 | int expire_midx_packs(struct repository *r, const char *object_dir, unsigned flags) |
cff97116 | 1700 | { |
19575c7c DS |
1701 | uint32_t i, *count, result = 0; |
1702 | struct string_list packs_to_drop = STRING_LIST_INIT_DUP; | |
1703 | struct multi_pack_index *m = load_multi_pack_index(object_dir, 1); | |
8dc18f89 | 1704 | struct progress *progress = NULL; |
19575c7c DS |
1705 | |
1706 | if (!m) | |
1707 | return 0; | |
1708 | ||
ca56dadb | 1709 | CALLOC_ARRAY(count, m->num_packs); |
8dc18f89 WB |
1710 | |
1711 | if (flags & MIDX_PROGRESS) | |
efdd2f0d | 1712 | progress = start_delayed_progress(_("Counting referenced objects"), |
8dc18f89 | 1713 | m->num_objects); |
19575c7c DS |
1714 | for (i = 0; i < m->num_objects; i++) { |
1715 | int pack_int_id = nth_midxed_pack_int_id(m, i); | |
1716 | count[pack_int_id]++; | |
8dc18f89 | 1717 | display_progress(progress, i + 1); |
19575c7c | 1718 | } |
8dc18f89 | 1719 | stop_progress(&progress); |
19575c7c | 1720 | |
8dc18f89 | 1721 | if (flags & MIDX_PROGRESS) |
efdd2f0d | 1722 | progress = start_delayed_progress(_("Finding and deleting unreferenced packfiles"), |
8dc18f89 | 1723 | m->num_packs); |
19575c7c DS |
1724 | for (i = 0; i < m->num_packs; i++) { |
1725 | char *pack_name; | |
8dc18f89 | 1726 | display_progress(progress, i + 1); |
19575c7c DS |
1727 | |
1728 | if (count[i]) | |
1729 | continue; | |
1730 | ||
1731 | if (prepare_midx_pack(r, m, i)) | |
1732 | continue; | |
1733 | ||
1734 | if (m->packs[i]->pack_keep) | |
1735 | continue; | |
1736 | ||
1737 | pack_name = xstrdup(m->packs[i]->pack_name); | |
1738 | close_pack(m->packs[i]); | |
1739 | ||
1740 | string_list_insert(&packs_to_drop, m->pack_names[i]); | |
1741 | unlink_pack_path(pack_name, 0); | |
1742 | free(pack_name); | |
1743 | } | |
8dc18f89 | 1744 | stop_progress(&progress); |
19575c7c DS |
1745 | |
1746 | free(count); | |
1747 | ||
f57a7396 | 1748 | if (packs_to_drop.nr) { |
08944d1c | 1749 | result = write_midx_internal(object_dir, NULL, &packs_to_drop, NULL, NULL, flags); |
f57a7396 TB |
1750 | m = NULL; |
1751 | } | |
19575c7c DS |
1752 | |
1753 | string_list_clear(&packs_to_drop, 0); | |
1754 | return result; | |
cff97116 | 1755 | } |
2af890bb | 1756 | |
ce1e4a10 DS |
1757 | struct repack_info { |
1758 | timestamp_t mtime; | |
1759 | uint32_t referenced_objects; | |
1760 | uint32_t pack_int_id; | |
1761 | }; | |
1762 | ||
1763 | static int compare_by_mtime(const void *a_, const void *b_) | |
2af890bb | 1764 | { |
ce1e4a10 DS |
1765 | const struct repack_info *a, *b; |
1766 | ||
1767 | a = (const struct repack_info *)a_; | |
1768 | b = (const struct repack_info *)b_; | |
1769 | ||
1770 | if (a->mtime < b->mtime) | |
1771 | return -1; | |
1772 | if (a->mtime > b->mtime) | |
1773 | return 1; | |
1774 | return 0; | |
1775 | } | |
1776 | ||
3ce4ca0a DS |
1777 | static int fill_included_packs_all(struct repository *r, |
1778 | struct multi_pack_index *m, | |
ce1e4a10 DS |
1779 | unsigned char *include_pack) |
1780 | { | |
3ce4ca0a DS |
1781 | uint32_t i, count = 0; |
1782 | int pack_kept_objects = 0; | |
1783 | ||
1784 | repo_config_get_bool(r, "repack.packkeptobjects", &pack_kept_objects); | |
1785 | ||
1786 | for (i = 0; i < m->num_packs; i++) { | |
1787 | if (prepare_midx_pack(r, m, i)) | |
1788 | continue; | |
1789 | if (!pack_kept_objects && m->packs[i]->pack_keep) | |
1790 | continue; | |
ce1e4a10 | 1791 | |
ce1e4a10 | 1792 | include_pack[i] = 1; |
3ce4ca0a DS |
1793 | count++; |
1794 | } | |
ce1e4a10 | 1795 | |
3ce4ca0a | 1796 | return count < 2; |
ce1e4a10 DS |
1797 | } |
1798 | ||
1799 | static int fill_included_packs_batch(struct repository *r, | |
1800 | struct multi_pack_index *m, | |
1801 | unsigned char *include_pack, | |
1802 | size_t batch_size) | |
1803 | { | |
1804 | uint32_t i, packs_to_repack; | |
1805 | size_t total_size; | |
1806 | struct repack_info *pack_info = xcalloc(m->num_packs, sizeof(struct repack_info)); | |
3ce4ca0a DS |
1807 | int pack_kept_objects = 0; |
1808 | ||
1809 | repo_config_get_bool(r, "repack.packkeptobjects", &pack_kept_objects); | |
ce1e4a10 DS |
1810 | |
1811 | for (i = 0; i < m->num_packs; i++) { | |
1812 | pack_info[i].pack_int_id = i; | |
1813 | ||
1814 | if (prepare_midx_pack(r, m, i)) | |
1815 | continue; | |
1816 | ||
1817 | pack_info[i].mtime = m->packs[i]->mtime; | |
1818 | } | |
1819 | ||
1820 | for (i = 0; batch_size && i < m->num_objects; i++) { | |
1821 | uint32_t pack_int_id = nth_midxed_pack_int_id(m, i); | |
1822 | pack_info[pack_int_id].referenced_objects++; | |
1823 | } | |
1824 | ||
1825 | QSORT(pack_info, m->num_packs, compare_by_mtime); | |
1826 | ||
1827 | total_size = 0; | |
1828 | packs_to_repack = 0; | |
1829 | for (i = 0; total_size < batch_size && i < m->num_packs; i++) { | |
1830 | int pack_int_id = pack_info[i].pack_int_id; | |
1831 | struct packed_git *p = m->packs[pack_int_id]; | |
1832 | size_t expected_size; | |
1833 | ||
1834 | if (!p) | |
1835 | continue; | |
3ce4ca0a DS |
1836 | if (!pack_kept_objects && p->pack_keep) |
1837 | continue; | |
ce1e4a10 DS |
1838 | if (open_pack_index(p) || !p->num_objects) |
1839 | continue; | |
1840 | ||
1841 | expected_size = (size_t)(p->pack_size | |
1842 | * pack_info[i].referenced_objects); | |
1843 | expected_size /= p->num_objects; | |
1844 | ||
1845 | if (expected_size >= batch_size) | |
1846 | continue; | |
1847 | ||
1848 | packs_to_repack++; | |
1849 | total_size += expected_size; | |
1850 | include_pack[pack_int_id] = 1; | |
1851 | } | |
1852 | ||
1853 | free(pack_info); | |
1854 | ||
1eb22c7d | 1855 | if (packs_to_repack < 2) |
ce1e4a10 DS |
1856 | return 1; |
1857 | ||
2af890bb DS |
1858 | return 0; |
1859 | } | |
ce1e4a10 | 1860 | |
efbc3aee | 1861 | int midx_repack(struct repository *r, const char *object_dir, size_t batch_size, unsigned flags) |
ce1e4a10 DS |
1862 | { |
1863 | int result = 0; | |
1864 | uint32_t i; | |
1865 | unsigned char *include_pack; | |
1866 | struct child_process cmd = CHILD_PROCESS_INIT; | |
6af3b00a | 1867 | FILE *cmd_in; |
ce1e4a10 DS |
1868 | struct strbuf base_name = STRBUF_INIT; |
1869 | struct multi_pack_index *m = load_multi_pack_index(object_dir, 1); | |
1870 | ||
e11d86de SLN |
1871 | /* |
1872 | * When updating the default for these configuration | |
1873 | * variables in builtin/repack.c, these must be adjusted | |
1874 | * to match. | |
1875 | */ | |
1876 | int delta_base_offset = 1; | |
1877 | int use_delta_islands = 0; | |
1878 | ||
ce1e4a10 DS |
1879 | if (!m) |
1880 | return 0; | |
1881 | ||
ca56dadb | 1882 | CALLOC_ARRAY(include_pack, m->num_packs); |
ce1e4a10 DS |
1883 | |
1884 | if (batch_size) { | |
1885 | if (fill_included_packs_batch(r, m, include_pack, batch_size)) | |
1886 | goto cleanup; | |
3ce4ca0a | 1887 | } else if (fill_included_packs_all(r, m, include_pack)) |
ce1e4a10 DS |
1888 | goto cleanup; |
1889 | ||
e11d86de SLN |
1890 | repo_config_get_bool(r, "repack.usedeltabaseoffset", &delta_base_offset); |
1891 | repo_config_get_bool(r, "repack.usedeltaislands", &use_delta_islands); | |
1892 | ||
c972bf4c | 1893 | strvec_push(&cmd.args, "pack-objects"); |
ce1e4a10 DS |
1894 | |
1895 | strbuf_addstr(&base_name, object_dir); | |
1896 | strbuf_addstr(&base_name, "/pack/pack"); | |
c972bf4c | 1897 | strvec_push(&cmd.args, base_name.buf); |
64d80e7d | 1898 | |
e11d86de | 1899 | if (delta_base_offset) |
c972bf4c | 1900 | strvec_push(&cmd.args, "--delta-base-offset"); |
e11d86de | 1901 | if (use_delta_islands) |
c972bf4c | 1902 | strvec_push(&cmd.args, "--delta-islands"); |
e11d86de | 1903 | |
64d80e7d | 1904 | if (flags & MIDX_PROGRESS) |
c972bf4c | 1905 | strvec_push(&cmd.args, "--progress"); |
64d80e7d | 1906 | else |
c972bf4c | 1907 | strvec_push(&cmd.args, "-q"); |
64d80e7d | 1908 | |
ce1e4a10 DS |
1909 | strbuf_release(&base_name); |
1910 | ||
1911 | cmd.git_cmd = 1; | |
1912 | cmd.in = cmd.out = -1; | |
1913 | ||
1914 | if (start_command(&cmd)) { | |
1915 | error(_("could not start pack-objects")); | |
1916 | result = 1; | |
1917 | goto cleanup; | |
1918 | } | |
1919 | ||
6af3b00a RS |
1920 | cmd_in = xfdopen(cmd.in, "w"); |
1921 | ||
ce1e4a10 DS |
1922 | for (i = 0; i < m->num_objects; i++) { |
1923 | struct object_id oid; | |
1924 | uint32_t pack_int_id = nth_midxed_pack_int_id(m, i); | |
1925 | ||
1926 | if (!include_pack[pack_int_id]) | |
1927 | continue; | |
1928 | ||
1929 | nth_midxed_object_oid(&oid, m, i); | |
6af3b00a | 1930 | fprintf(cmd_in, "%s\n", oid_to_hex(&oid)); |
ce1e4a10 | 1931 | } |
6af3b00a | 1932 | fclose(cmd_in); |
ce1e4a10 DS |
1933 | |
1934 | if (finish_command(&cmd)) { | |
1935 | error(_("could not finish pack-objects")); | |
1936 | result = 1; | |
1937 | goto cleanup; | |
1938 | } | |
1939 | ||
08944d1c | 1940 | result = write_midx_internal(object_dir, NULL, NULL, NULL, NULL, flags); |
ce1e4a10 DS |
1941 | m = NULL; |
1942 | ||
1943 | cleanup: | |
1944 | if (m) | |
1945 | close_midx(m); | |
1946 | free(include_pack); | |
1947 | return result; | |
1948 | } |