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