]>
Commit | Line | Data |
---|---|---|
1 | #include "cache.h" | |
2 | #include "config.h" | |
3 | #include "csum-file.h" | |
4 | #include "dir.h" | |
5 | #include "lockfile.h" | |
6 | #include "packfile.h" | |
7 | #include "object-store.h" | |
8 | #include "sha1-lookup.h" | |
9 | #include "midx.h" | |
10 | #include "progress.h" | |
11 | #include "trace2.h" | |
12 | #include "run-command.h" | |
13 | #include "repository.h" | |
14 | ||
15 | #define MIDX_SIGNATURE 0x4d494458 /* "MIDX" */ | |
16 | #define MIDX_VERSION 1 | |
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 | |
21 | #define MIDX_HASH_VERSION 1 | |
22 | #define MIDX_HEADER_SIZE 12 | |
23 | #define MIDX_MIN_SIZE (MIDX_HEADER_SIZE + the_hash_algo->rawsz) | |
24 | ||
25 | #define MIDX_MAX_CHUNKS 5 | |
26 | #define MIDX_CHUNK_ALIGNMENT 4 | |
27 | #define MIDX_CHUNKID_PACKNAMES 0x504e414d /* "PNAM" */ | |
28 | #define MIDX_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */ | |
29 | #define MIDX_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */ | |
30 | #define MIDX_CHUNKID_OBJECTOFFSETS 0x4f4f4646 /* "OOFF" */ | |
31 | #define MIDX_CHUNKID_LARGEOFFSETS 0x4c4f4646 /* "LOFF" */ | |
32 | #define MIDX_CHUNKLOOKUP_WIDTH (sizeof(uint32_t) + sizeof(uint64_t)) | |
33 | #define MIDX_CHUNK_FANOUT_SIZE (sizeof(uint32_t) * 256) | |
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 | |
37 | ||
38 | #define PACK_EXPIRED UINT_MAX | |
39 | ||
40 | static char *get_midx_filename(const char *object_dir) | |
41 | { | |
42 | return xstrfmt("%s/pack/multi-pack-index", object_dir); | |
43 | } | |
44 | ||
45 | struct multi_pack_index *load_multi_pack_index(const char *object_dir, int local) | |
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); | |
54 | uint32_t i; | |
55 | const char *cur_pack_name; | |
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); | |
76 | close(fd); | |
77 | ||
78 | FLEX_ALLOC_STR(m, object_dir, object_dir); | |
79 | m->data = midx_map; | |
80 | m->data_len = midx_size; | |
81 | m->local = local; | |
82 | ||
83 | m->signature = get_be32(m->data); | |
84 | if (m->signature != MIDX_SIGNATURE) | |
85 | die(_("multi-pack-index signature 0x%08x does not match signature 0x%08x"), | |
86 | m->signature, MIDX_SIGNATURE); | |
87 | ||
88 | m->version = m->data[MIDX_BYTE_FILE_VERSION]; | |
89 | if (m->version != MIDX_VERSION) | |
90 | die(_("multi-pack-index version %d not recognized"), | |
91 | m->version); | |
92 | ||
93 | hash_version = m->data[MIDX_BYTE_HASH_VERSION]; | |
94 | if (hash_version != MIDX_HASH_VERSION) | |
95 | die(_("hash version %u does not match"), hash_version); | |
96 | m->hash_len = the_hash_algo->rawsz; | |
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 | ||
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 | ||
108 | if (chunk_offset >= m->data_len) | |
109 | die(_("invalid chunk offset (too large)")); | |
110 | ||
111 | switch (chunk_id) { | |
112 | case MIDX_CHUNKID_PACKNAMES: | |
113 | m->chunk_pack_names = m->data + chunk_offset; | |
114 | break; | |
115 | ||
116 | case MIDX_CHUNKID_OIDFANOUT: | |
117 | m->chunk_oid_fanout = (uint32_t *)(m->data + chunk_offset); | |
118 | break; | |
119 | ||
120 | case MIDX_CHUNKID_OIDLOOKUP: | |
121 | m->chunk_oid_lookup = m->data + chunk_offset; | |
122 | break; | |
123 | ||
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 | ||
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")); | |
147 | if (!m->chunk_oid_fanout) | |
148 | die(_("multi-pack-index missing required OID fanout chunk")); | |
149 | if (!m->chunk_oid_lookup) | |
150 | die(_("multi-pack-index missing required OID lookup chunk")); | |
151 | if (!m->chunk_object_offsets) | |
152 | die(_("multi-pack-index missing required object offsets chunk")); | |
153 | ||
154 | m->num_objects = ntohl(m->chunk_oid_fanout[255]); | |
155 | ||
156 | m->pack_names = xcalloc(m->num_packs, sizeof(*m->pack_names)); | |
157 | m->packs = xcalloc(m->num_packs, sizeof(*m->packs)); | |
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 | ||
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'"), | |
167 | m->pack_names[i - 1], | |
168 | m->pack_names[i]); | |
169 | } | |
170 | ||
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 | ||
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 | ||
186 | void close_midx(struct multi_pack_index *m) | |
187 | { | |
188 | uint32_t i; | |
189 | ||
190 | if (!m) | |
191 | return; | |
192 | ||
193 | munmap((unsigned char *)m->data, m->data_len); | |
194 | ||
195 | for (i = 0; i < m->num_packs; i++) { | |
196 | if (m->packs[i]) | |
197 | m->packs[i]->multi_pack_index = 0; | |
198 | } | |
199 | FREE_AND_NULL(m->packs); | |
200 | FREE_AND_NULL(m->pack_names); | |
201 | } | |
202 | ||
203 | int prepare_midx_pack(struct repository *r, struct multi_pack_index *m, uint32_t pack_int_id) | |
204 | { | |
205 | struct strbuf pack_name = STRBUF_INIT; | |
206 | struct packed_git *p; | |
207 | ||
208 | if (pack_int_id >= m->num_packs) | |
209 | die(_("bad pack-int-id: %u (%u total packs)"), | |
210 | pack_int_id, m->num_packs); | |
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 | ||
218 | p = add_packed_git(pack_name.buf, pack_name.len, m->local); | |
219 | strbuf_release(&pack_name); | |
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; | |
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, | |
235 | the_hash_algo->rawsz, result); | |
236 | } | |
237 | ||
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 | ||
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) { | |
258 | if (sizeof(off_t) < sizeof(uint64_t)) | |
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 | ||
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) | |
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 | ||
286 | if (prepare_midx_pack(r, m, pack_int_id)) | |
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 | ||
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++) | |
305 | if (hasheq(oid.hash, | |
306 | p->bad_object_sha1 + the_hash_algo->rawsz * i)) | |
307 | return 0; | |
308 | } | |
309 | ||
310 | e->offset = nth_midxed_offset(m, pos); | |
311 | e->p = p; | |
312 | ||
313 | return 1; | |
314 | } | |
315 | ||
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) | |
320 | { | |
321 | uint32_t pos; | |
322 | ||
323 | if (!bsearch_midx(oid, m, &pos)) | |
324 | return 0; | |
325 | ||
326 | return nth_midxed_pack_entry(r, m, e, pos); | |
327 | } | |
328 | ||
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) | |
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]; | |
371 | cmp = cmp_idx_or_pack_name(idx_or_pack_name, current); | |
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 | ||
384 | int prepare_multi_pack_index_one(struct repository *r, const char *object_dir, int local) | |
385 | { | |
386 | struct multi_pack_index *m; | |
387 | struct multi_pack_index *m_search; | |
388 | ||
389 | prepare_repo_settings(r); | |
390 | if (!r->settings.core_multi_pack_index) | |
391 | return 0; | |
392 | ||
393 | for (m_search = r->objects->multi_pack_index; m_search; m_search = m_search->next) | |
394 | if (!strcmp(object_dir, m_search->object_dir)) | |
395 | return 1; | |
396 | ||
397 | m = load_multi_pack_index(object_dir, local); | |
398 | ||
399 | if (m) { | |
400 | m->next = r->objects->multi_pack_index; | |
401 | r->objects->multi_pack_index = m; | |
402 | return 1; | |
403 | } | |
404 | ||
405 | return 0; | |
406 | } | |
407 | ||
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 | ||
425 | struct pack_info { | |
426 | uint32_t orig_pack_int_id; | |
427 | char *pack_name; | |
428 | struct packed_git *p; | |
429 | unsigned expired : 1; | |
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 | ||
439 | struct pack_list { | |
440 | struct pack_info *info; | |
441 | uint32_t nr; | |
442 | uint32_t alloc; | |
443 | struct multi_pack_index *m; | |
444 | struct progress *progress; | |
445 | unsigned pack_paths_checked; | |
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")) { | |
454 | display_progress(packs->progress, ++packs->pack_paths_checked); | |
455 | if (packs->m && midx_contains_pack(packs->m, file_name)) | |
456 | return; | |
457 | ||
458 | ALLOC_GROW(packs->info, packs->nr + 1, packs->alloc); | |
459 | ||
460 | packs->info[packs->nr].p = add_packed_git(full_path, | |
461 | full_path_len, | |
462 | 0); | |
463 | ||
464 | if (!packs->info[packs->nr].p) { | |
465 | warning(_("failed to add packfile '%s'"), | |
466 | full_path); | |
467 | return; | |
468 | } | |
469 | ||
470 | if (open_pack_index(packs->info[packs->nr].p)) { | |
471 | warning(_("failed to open pack-index '%s'"), | |
472 | full_path); | |
473 | close_pack(packs->info[packs->nr].p); | |
474 | FREE_AND_NULL(packs->info[packs->nr].p); | |
475 | return; | |
476 | } | |
477 | ||
478 | packs->info[packs->nr].pack_name = xstrdup(file_name); | |
479 | packs->info[packs->nr].orig_pack_int_id = packs->nr; | |
480 | packs->info[packs->nr].expired = 0; | |
481 | packs->nr++; | |
482 | } | |
483 | } | |
484 | ||
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 | ||
509 | static int nth_midxed_pack_midx_entry(struct multi_pack_index *m, | |
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); | |
517 | e->pack_int_id = nth_midxed_pack_int_id(m, pos); | |
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 | ||
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 | { | |
530 | if (nth_packed_object_id(&entry->oid, p, cur_object) < 0) | |
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 | */ | |
550 | static struct pack_midx_entry *get_sorted_entries(struct multi_pack_index *m, | |
551 | struct pack_info *info, | |
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; | |
559 | uint32_t start_pack = m ? m->num_packs : 0; | |
560 | ||
561 | for (cur_pack = start_pack; cur_pack < nr_packs; cur_pack++) | |
562 | total_objects += info[cur_pack].p->num_objects; | |
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 | ||
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); | |
587 | nth_midxed_pack_midx_entry(m, | |
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++) { | |
595 | uint32_t start = 0, end; | |
596 | ||
597 | if (cur_fanout) | |
598 | start = get_pack_fanout(info[cur_pack].p, cur_fanout - 1); | |
599 | end = get_pack_fanout(info[cur_pack].p, cur_fanout); | |
600 | ||
601 | for (cur_object = start; cur_object < end; cur_object++) { | |
602 | ALLOC_GROW(entries_by_fanout, nr_fanout + 1, alloc_fanout); | |
603 | fill_pack_entry(cur_pack, info[cur_pack].p, cur_object, &entries_by_fanout[nr_fanout]); | |
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++) { | |
615 | if (cur_object && oideq(&entries_by_fanout[cur_object - 1].oid, | |
616 | &entries_by_fanout[cur_object].oid)) | |
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 | ||
631 | static size_t write_midx_pack_names(struct hashfile *f, | |
632 | struct pack_info *info, | |
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++) { | |
640 | size_t writelen; | |
641 | ||
642 | if (info[i].expired) | |
643 | continue; | |
644 | ||
645 | if (i && strcmp(info[i].pack_name, info[i - 1].pack_name) <= 0) | |
646 | BUG("incorrect pack-file order: %s before %s", | |
647 | info[i - 1].pack_name, | |
648 | info[i].pack_name); | |
649 | ||
650 | writelen = strlen(info[i].pack_name) + 1; | |
651 | hashwrite(f, info[i].pack_name, writelen); | |
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 | ||
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 | ||
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 | ||
721 | static size_t write_midx_object_offsets(struct hashfile *f, int large_offset_needed, | |
722 | uint32_t *perm, | |
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 | ||
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 | ||
737 | hashwrite_be32(f, perm[obj->pack_int_id]); | |
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 | { | |
757 | struct pack_midx_entry *list = objects, *end = objects + nr_objects; | |
758 | size_t written = 0; | |
759 | ||
760 | while (nr_large_offset) { | |
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; | |
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 | ||
783 | static int write_midx_internal(const char *object_dir, struct multi_pack_index *m, | |
784 | struct string_list *packs_to_drop, unsigned flags) | |
785 | { | |
786 | unsigned char cur_chunk, num_chunks = 0; | |
787 | char *midx_name; | |
788 | uint32_t i; | |
789 | struct hashfile *f = NULL; | |
790 | struct lock_file lk; | |
791 | struct pack_list packs; | |
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]; | |
796 | uint32_t nr_entries, num_large_offsets = 0; | |
797 | struct pack_midx_entry *entries = NULL; | |
798 | struct progress *progress = NULL; | |
799 | int large_offsets_needed = 0; | |
800 | int pack_name_concat_len = 0; | |
801 | int dropped_packs = 0; | |
802 | int result = 0; | |
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 | ||
811 | if (m) | |
812 | packs.m = m; | |
813 | else | |
814 | packs.m = load_multi_pack_index(object_dir, 1); | |
815 | ||
816 | packs.nr = 0; | |
817 | packs.alloc = packs.m ? packs.m->num_packs : 16; | |
818 | packs.info = NULL; | |
819 | ALLOC_ARRAY(packs.info, packs.alloc); | |
820 | ||
821 | if (packs.m) { | |
822 | for (i = 0; i < packs.m->num_packs; i++) { | |
823 | ALLOC_GROW(packs.info, packs.nr + 1, packs.alloc); | |
824 | ||
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; | |
828 | packs.info[packs.nr].expired = 0; | |
829 | packs.nr++; | |
830 | } | |
831 | } | |
832 | ||
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 | ||
839 | for_each_file_in_pack_dir(object_dir, add_pack_to_midx, &packs); | |
840 | stop_progress(&packs.progress); | |
841 | ||
842 | if (packs.m && packs.nr == packs.m->num_packs && !packs_to_drop) | |
843 | goto cleanup; | |
844 | ||
845 | entries = get_sorted_entries(packs.m, packs.info, packs.nr, &nr_entries); | |
846 | ||
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 | } | |
853 | ||
854 | QSORT(packs.info, packs.nr, pack_info_compare); | |
855 | ||
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 | ||
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++) { | |
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 | } | |
898 | } | |
899 | ||
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 | } | |
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 | ||
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 | ||
913 | if (packs.m) | |
914 | close_midx(packs.m); | |
915 | ||
916 | cur_chunk = 0; | |
917 | num_chunks = large_offsets_needed ? 5 : 4; | |
918 | ||
919 | if (packs.nr - dropped_packs == 0) { | |
920 | error(_("no pack files to index.")); | |
921 | result = 1; | |
922 | goto cleanup; | |
923 | } | |
924 | ||
925 | written = write_midx_header(f, num_chunks, packs.nr - dropped_packs); | |
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++; | |
931 | chunk_ids[cur_chunk] = MIDX_CHUNKID_OIDFANOUT; | |
932 | chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + pack_name_concat_len; | |
933 | ||
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 | ||
938 | cur_chunk++; | |
939 | chunk_ids[cur_chunk] = MIDX_CHUNKID_OBJECTOFFSETS; | |
940 | chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + nr_entries * the_hash_algo->rawsz; | |
941 | ||
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 | ||
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 | ||
971 | if (flags & MIDX_PROGRESS) | |
972 | progress = start_progress(_("Writing chunks to multi-pack-index"), | |
973 | num_chunks); | |
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: | |
983 | written += write_midx_pack_names(f, packs.info, packs.nr); | |
984 | break; | |
985 | ||
986 | case MIDX_CHUNKID_OIDFANOUT: | |
987 | written += write_midx_oid_fanout(f, entries, nr_entries); | |
988 | break; | |
989 | ||
990 | case MIDX_CHUNKID_OIDLOOKUP: | |
991 | written += write_midx_oid_lookup(f, the_hash_algo->rawsz, entries, nr_entries); | |
992 | break; | |
993 | ||
994 | case MIDX_CHUNKID_OBJECTOFFSETS: | |
995 | written += write_midx_object_offsets(f, large_offsets_needed, pack_perm, entries, nr_entries); | |
996 | break; | |
997 | ||
998 | case MIDX_CHUNKID_LARGEOFFSETS: | |
999 | written += write_midx_large_offsets(f, num_large_offsets, entries, nr_entries); | |
1000 | break; | |
1001 | ||
1002 | default: | |
1003 | BUG("trying to write unknown chunk id %"PRIx32, | |
1004 | chunk_ids[i]); | |
1005 | } | |
1006 | ||
1007 | display_progress(progress, i + 1); | |
1008 | } | |
1009 | stop_progress(&progress); | |
1010 | ||
1011 | if (written != chunk_offsets[num_chunks]) | |
1012 | BUG("incorrect final offset %"PRIu64" != %"PRIu64, | |
1013 | written, | |
1014 | chunk_offsets[num_chunks]); | |
1015 | ||
1016 | finalize_hashfile(f, NULL, CSUM_FSYNC | CSUM_HASH_IN_STREAM); | |
1017 | commit_lock_file(&lk); | |
1018 | ||
1019 | cleanup: | |
1020 | for (i = 0; i < packs.nr; i++) { | |
1021 | if (packs.info[i].p) { | |
1022 | close_pack(packs.info[i].p); | |
1023 | free(packs.info[i].p); | |
1024 | } | |
1025 | free(packs.info[i].pack_name); | |
1026 | } | |
1027 | ||
1028 | free(packs.info); | |
1029 | free(entries); | |
1030 | free(pack_perm); | |
1031 | free(midx_name); | |
1032 | return result; | |
1033 | } | |
1034 | ||
1035 | int write_midx_file(const char *object_dir, unsigned flags) | |
1036 | { | |
1037 | return write_midx_internal(object_dir, NULL, NULL, flags); | |
1038 | } | |
1039 | ||
1040 | void clear_midx_file(struct repository *r) | |
1041 | { | |
1042 | char *midx = get_midx_filename(r->objects->odb->path); | |
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 | } | |
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 | } | |
1056 | ||
1057 | static int verify_midx_error; | |
1058 | ||
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 | ||
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 | ||
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 | ||
1095 | int verify_midx_file(struct repository *r, const char *object_dir, unsigned flags) | |
1096 | { | |
1097 | struct pair_pos_vs_id *pairs = NULL; | |
1098 | uint32_t i; | |
1099 | struct progress *progress = NULL; | |
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 | ||
1106 | if (flags & MIDX_PROGRESS) | |
1107 | progress = start_progress(_("Looking for referenced packfiles"), | |
1108 | m->num_packs); | |
1109 | for (i = 0; i < m->num_packs; i++) { | |
1110 | if (prepare_midx_pack(r, m, i)) | |
1111 | midx_report("failed to load pack in position %d", i); | |
1112 | ||
1113 | display_progress(progress, i + 1); | |
1114 | } | |
1115 | stop_progress(&progress); | |
1116 | ||
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 | ||
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 | ||
1135 | if (flags & MIDX_PROGRESS) | |
1136 | progress = start_sparse_progress(_("Verifying OID order in multi-pack-index"), | |
1137 | m->num_objects - 1); | |
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); | |
1147 | ||
1148 | midx_display_sparse_progress(progress, i + 1); | |
1149 | } | |
1150 | stop_progress(&progress); | |
1151 | ||
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 | ||
1164 | if (flags & MIDX_PROGRESS) | |
1165 | progress = start_sparse_progress(_("Sorting objects by packfile"), | |
1166 | m->num_objects); | |
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 | ||
1171 | if (flags & MIDX_PROGRESS) | |
1172 | progress = start_sparse_progress(_("Verifying object offsets"), m->num_objects); | |
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 | ||
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 | ||
1187 | if (!fill_midx_entry(r, &oid, &e, m)) { | |
1188 | midx_report(_("failed to load pack entry for oid[%d] = %s"), | |
1189 | pairs[i].pos, oid_to_hex(&oid)); | |
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), | |
1204 | pairs[i].pos, oid_to_hex(&oid), m_offset, p_offset); | |
1205 | ||
1206 | midx_display_sparse_progress(progress, i + 1); | |
1207 | } | |
1208 | stop_progress(&progress); | |
1209 | ||
1210 | free(pairs); | |
1211 | ||
1212 | return verify_midx_error; | |
1213 | } | |
1214 | ||
1215 | int expire_midx_packs(struct repository *r, const char *object_dir, unsigned flags) | |
1216 | { | |
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); | |
1220 | struct progress *progress = NULL; | |
1221 | ||
1222 | if (!m) | |
1223 | return 0; | |
1224 | ||
1225 | count = xcalloc(m->num_packs, sizeof(uint32_t)); | |
1226 | ||
1227 | if (flags & MIDX_PROGRESS) | |
1228 | progress = start_progress(_("Counting referenced objects"), | |
1229 | m->num_objects); | |
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]++; | |
1233 | display_progress(progress, i + 1); | |
1234 | } | |
1235 | stop_progress(&progress); | |
1236 | ||
1237 | if (flags & MIDX_PROGRESS) | |
1238 | progress = start_progress(_("Finding and deleting unreferenced packfiles"), | |
1239 | m->num_packs); | |
1240 | for (i = 0; i < m->num_packs; i++) { | |
1241 | char *pack_name; | |
1242 | display_progress(progress, i + 1); | |
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 | } | |
1260 | stop_progress(&progress); | |
1261 | ||
1262 | free(count); | |
1263 | ||
1264 | if (packs_to_drop.nr) | |
1265 | result = write_midx_internal(object_dir, m, &packs_to_drop, flags); | |
1266 | ||
1267 | string_list_clear(&packs_to_drop, 0); | |
1268 | return result; | |
1269 | } | |
1270 | ||
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_) | |
1278 | { | |
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 | ||
1291 | static int fill_included_packs_all(struct repository *r, | |
1292 | struct multi_pack_index *m, | |
1293 | unsigned char *include_pack) | |
1294 | { | |
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; | |
1305 | ||
1306 | include_pack[i] = 1; | |
1307 | count++; | |
1308 | } | |
1309 | ||
1310 | return count < 2; | |
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)); | |
1321 | int pack_kept_objects = 0; | |
1322 | ||
1323 | repo_config_get_bool(r, "repack.packkeptobjects", &pack_kept_objects); | |
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; | |
1350 | if (!pack_kept_objects && p->pack_keep) | |
1351 | continue; | |
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 | ||
1372 | return 0; | |
1373 | } | |
1374 | ||
1375 | int midx_repack(struct repository *r, const char *object_dir, size_t batch_size, unsigned flags) | |
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 | ||
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 | ||
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; | |
1400 | } else if (fill_included_packs_all(r, m, include_pack)) | |
1401 | goto cleanup; | |
1402 | ||
1403 | repo_config_get_bool(r, "repack.usedeltabaseoffset", &delta_base_offset); | |
1404 | repo_config_get_bool(r, "repack.usedeltaislands", &use_delta_islands); | |
1405 | ||
1406 | strvec_push(&cmd.args, "pack-objects"); | |
1407 | ||
1408 | strbuf_addstr(&base_name, object_dir); | |
1409 | strbuf_addstr(&base_name, "/pack/pack"); | |
1410 | strvec_push(&cmd.args, base_name.buf); | |
1411 | ||
1412 | if (delta_base_offset) | |
1413 | strvec_push(&cmd.args, "--delta-base-offset"); | |
1414 | if (use_delta_islands) | |
1415 | strvec_push(&cmd.args, "--delta-islands"); | |
1416 | ||
1417 | if (flags & MIDX_PROGRESS) | |
1418 | strvec_push(&cmd.args, "--progress"); | |
1419 | else | |
1420 | strvec_push(&cmd.args, "-q"); | |
1421 | ||
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 | ||
1452 | result = write_midx_internal(object_dir, m, NULL, flags); | |
1453 | m = NULL; | |
1454 | ||
1455 | cleanup: | |
1456 | if (m) | |
1457 | close_midx(m); | |
1458 | free(include_pack); | |
1459 | return result; | |
1460 | } |