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