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