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