at commit ae7a0ffc

1 #include "git-compat-util.h"
2 #include "abspath.h"
3 #include "chdir-notify.h"
4 #include "dir.h"
5 #include "fsck.h"
6 #include "git-zlib.h"
7 #include "list-objects-filter-options.h"
8 #include "mergesort.h"
9 #include "midx.h"
10 #include "odb/source-packed.h"
11 #include "odb/streaming.h"
12 #include "pack.h"
13 #include "pack-revindex.h"
14 #include "packfile.h"
15 #include "pack-bitmap.h"
16 #include "progress.h"
17 #include "run-command.h"
18
19 static int find_pack_entry(struct odb_source_packed *store,
20 const struct object_id *oid,
21 struct pack_entry *e)
22 {
23 struct packfile_list_entry *l;
24
25 odb_source_prepare(&store->base, 0);
26 if (store->midx && fill_midx_entry(store->midx, oid, e))
27 return 1;
28
29 for (l = store->packs.head; l; l = l->next) {
30 struct packed_git *p = l->pack;
31
32 if (!p->multi_pack_index && packfile_fill_entry(p, oid, e)) {
33 if (!store->skip_mru_updates)
34 packfile_list_prepend(&store->packs, p);
35 return 1;
36 }
37 }
38
39 return 0;
40 }
41
42 static int odb_source_packed_read_object_info(struct odb_source *source,
43 const struct object_id *oid,
44 struct object_info *oi,
45 enum object_info_flags flags)
46 {
47 struct odb_source_packed *packed = odb_source_packed_downcast(source);
48 struct pack_entry e;
49 int ret;
50
51 /*
52 * In case the first read didn't surface the object, we have to reload
53 * packfiles. This may cause us to discover new packfiles that have
54 * been added since the last time we have prepared the packfile store.
55 */
56 if (flags & OBJECT_INFO_SECOND_READ)
57 odb_source_prepare(source, ODB_PREPARE_FLUSH_CACHES);
58
59 if (!find_pack_entry(packed, oid, &e))
60 return 1;
61
62 /*
63 * We know that the caller doesn't actually need the
64 * information below, so return early.
65 */
66 if (!oi)
67 return 0;
68
69 ret = packed_object_info(packed, e.p, e.offset, oi);
70 if (ret < 0) {
71 mark_bad_packed_object(e.p, oid);
72 return -1;
73 }
74
75 return 0;
76 }
77
78 static int odb_source_packed_read_object_stream(struct odb_stream **out,
79 struct odb_source *source,
80 const struct object_id *oid)
81 {
82 struct odb_source_packed *packed = odb_source_packed_downcast(source);
83 struct pack_entry e;
84
85 if (!find_pack_entry(packed, oid, &e))
86 return -1;
87
88 return packfile_read_object_stream(out, oid, e.p, e.offset);
89 }
90
91 struct odb_source_packed_for_each_object_wrapper_data {
92 struct odb_source_packed *store;
93 const struct object_info *request;
94 odb_for_each_object_cb cb;
95 void *cb_data;
96 };
97
98 static int odb_source_packed_for_each_object_wrapper(const struct object_id *oid,
99 struct packed_git *pack,
100 uint32_t index_pos,
101 void *cb_data)
102 {
103 struct odb_source_packed_for_each_object_wrapper_data *data = cb_data;
104
105 if (data->request) {
106 off_t offset = nth_packed_object_offset(pack, index_pos);
107 struct object_info oi = *data->request;
108
109 if (packed_object_info_with_index_pos(data->store, pack, offset,
110 &index_pos, &oi) < 0) {
111 mark_bad_packed_object(pack, oid);
112 return -1;
113 }
114
115 return data->cb(oid, &oi, data->cb_data);
116 } else {
117 return data->cb(oid, NULL, data->cb_data);
118 }
119 }
120
121 static int match_hash(unsigned len, const unsigned char *a, const unsigned char *b)
122 {
123 do {
124 if (*a != *b)
125 return 0;
126 a++;
127 b++;
128 len -= 2;
129 } while (len > 1);
130 if (len)
131 if ((*a ^ *b) & 0xf0)
132 return 0;
133 return 1;
134 }
135
136 static bool should_exclude_pack(struct packed_git *p, enum odb_for_each_object_flags flags)
137 {
138 if ((flags & ODB_FOR_EACH_OBJECT_LOCAL_ONLY) && !p->pack_local)
139 return true;
140 if ((flags & ODB_FOR_EACH_OBJECT_PROMISOR_ONLY) &&
141 !p->pack_promisor)
142 return true;
143 if ((flags & ODB_FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS) &&
144 p->pack_keep_in_core)
145 return true;
146 if ((flags & ODB_FOR_EACH_OBJECT_SKIP_ON_DISK_KEPT_PACKS) &&
147 p->pack_keep)
148 return true;
149 return false;
150 }
151
152 static int for_each_prefixed_object_in_midx(
153 struct odb_source_packed *source,
154 struct multi_pack_index *m,
155 const struct odb_for_each_object_options *opts,
156 struct odb_source_packed_for_each_object_wrapper_data *data)
157 {
158 bool pack_errors = false;
159 int ret;
160
161 for (; m; m = m->base_midx) {
162 uint32_t num, i, first = 0;
163 int len = opts->prefix_hex_len > m->source->base.odb->repo->hash_algo->hexsz ?
164 m->source->base.odb->repo->hash_algo->hexsz : opts->prefix_hex_len;
165
166 if (!m->num_objects)
167 continue;
168
169 num = m->num_objects + m->num_objects_in_base;
170
171 bsearch_one_midx(opts->prefix, m, &first);
172
173 /*
174 * At this point, "first" is the location of the lowest
175 * object with an object name that could match "opts->prefix".
176 * See if we have 0, 1 or more objects that actually match(es).
177 */
178 for (i = first; i < num; i++) {
179 const struct object_id *current = NULL;
180 struct packed_git *pack;
181 struct object_id oid;
182
183 current = nth_midxed_object_oid(&oid, m, i);
184
185 if (!match_hash(len, opts->prefix->hash, current->hash))
186 break;
187
188 if (opts->flags || data->request) {
189 uint32_t pack_id = nth_midxed_pack_int_id(m, i);
190
191 if (prepare_midx_pack(m, pack_id)) {
192 pack_errors = true;
193 continue;
194 }
195
196 pack = nth_midxed_pack(m, pack_id);
197 if (should_exclude_pack(pack, opts->flags))
198 continue;
199 }
200
201 if (data->request) {
202 struct object_info oi = *data->request;
203 off_t offset = nth_midxed_offset(m, i);
204
205 ret = packed_object_info(source, pack, offset, &oi);
206 if (ret)
207 goto out;
208
209 ret = data->cb(&oid, &oi, data->cb_data);
210 if (ret)
211 goto out;
212 } else {
213 ret = data->cb(&oid, NULL, data->cb_data);
214 if (ret)
215 goto out;
216 }
217 }
218 }
219
220 ret = 0;
221
222 out:
223 if (!ret && pack_errors)
224 ret = -1;
225 return ret;
226 }
227
228 static int for_each_prefixed_object_in_pack(
229 struct odb_source_packed *source,
230 struct packed_git *p,
231 const struct odb_for_each_object_options *opts,
232 struct odb_source_packed_for_each_object_wrapper_data *data)
233 {
234 uint32_t num, i, first = 0;
235 int len = opts->prefix_hex_len > p->repo->hash_algo->hexsz ?
236 p->repo->hash_algo->hexsz : opts->prefix_hex_len;
237 int ret;
238
239 num = p->num_objects;
240 bsearch_pack(opts->prefix, p, &first);
241
242 /*
243 * At this point, "first" is the location of the lowest object
244 * with an object name that could match "bin_pfx". See if we have
245 * 0, 1 or more objects that actually match(es).
246 */
247 for (i = first; i < num; i++) {
248 struct object_id oid;
249
250 nth_packed_object_id(&oid, p, i);
251 if (!match_hash(len, opts->prefix->hash, oid.hash))
252 break;
253
254 if (data->request) {
255 struct object_info oi = *data->request;
256 off_t offset = nth_packed_object_offset(p, i);
257
258 ret = packed_object_info(source, p, offset, &oi);
259 if (ret)
260 goto out;
261
262 ret = data->cb(&oid, &oi, data->cb_data);
263 if (ret)
264 goto out;
265 } else {
266 ret = data->cb(&oid, NULL, data->cb_data);
267 if (ret)
268 goto out;
269 }
270 }
271
272 ret = 0;
273
274 out:
275 return ret;
276 }
277
278 static int odb_source_packed_for_each_prefixed_object(
279 struct odb_source_packed *store,
280 const struct odb_for_each_object_options *opts,
281 struct odb_source_packed_for_each_object_wrapper_data *data)
282 {
283 struct packfile_list_entry *e;
284 struct multi_pack_index *m;
285 bool pack_errors = false;
286 int ret;
287
288 store->skip_mru_updates = true;
289
290 m = get_multi_pack_index(store);
291 if (m) {
292 ret = for_each_prefixed_object_in_midx(store, m, opts, data);
293 if (ret)
294 goto out;
295 }
296
297 for (e = packfile_store_get_packs(store); e; e = e->next) {
298 if (e->pack->multi_pack_index)
299 continue;
300 if (should_exclude_pack(e->pack, opts->flags))
301 continue;
302
303 if (open_pack_index(e->pack)) {
304 pack_errors = true;
305 continue;
306 }
307
308 if (!e->pack->num_objects)
309 continue;
310
311 ret = for_each_prefixed_object_in_pack(store, e->pack, opts, data);
312 if (ret)
313 goto out;
314 }
315
316 ret = 0;
317
318 out:
319 store->skip_mru_updates = false;
320 if (!ret && pack_errors)
321 ret = -1;
322 return ret;
323 }
324
325 struct bitmapped_for_each_object_data {
326 struct odb_source_packed *packed;
327 const struct object_info *request;
328 const struct odb_for_each_object_options *opts;
329 odb_for_each_object_cb cb;
330 void *cb_data;
331 };
332
333 static int bitmapped_for_each_object(const struct object_id *oid,
334 enum object_type type UNUSED,
335 int flags UNUSED,
336 uint32_t hash UNUSED,
337 struct packed_git *pack,
338 off_t offset,
339 void *cb_data)
340 {
341 struct bitmapped_for_each_object_data *data = cb_data;
342
343 if (should_exclude_pack(pack, data->opts->flags))
344 return 0;
345
346 if (data->request) {
347 struct object_info oi = *data->request;
348 if (packed_object_info(data->packed, pack, offset, &oi) < 0)
349 return -1;
350 return data->cb(oid, &oi, data->cb_data);
351 }
352
353 return data->cb(oid, NULL, data->cb_data);
354 }
355
356 static int odb_source_packed_for_each_object(struct odb_source *source,
357 const struct object_info *request,
358 odb_for_each_object_cb cb,
359 void *cb_data,
360 const struct odb_for_each_object_options *opts)
361 {
362 struct odb_source_packed *packed = odb_source_packed_downcast(source);
363 struct odb_source_packed_for_each_object_wrapper_data data = {
364 .store = packed,
365 .request = request,
366 .cb = cb,
367 .cb_data = cb_data,
368 };
369 struct bitmap_index *bitmap = NULL;
370 struct packfile_list_entry *e;
371 int pack_errors = 0, ret;
372
373 if (opts->prefix)
374 return odb_source_packed_for_each_prefixed_object(packed, opts, &data);
375
376 if (opts->filter &&
377 opts->filter->choice != LOFC_DISABLED &&
378 can_filter_bitmap(opts->filter))
379 bitmap = prepare_bitmap_git_for_source(packed);
380 if (bitmap) {
381 struct bitmapped_for_each_object_data bitmap_data = {
382 .packed = packed,
383 .request = request,
384 .opts = opts,
385 .cb = cb,
386 .cb_data = cb_data,
387 };
388
389 ret = for_each_bitmapped_object(bitmap, opts->filter,
390 bitmapped_for_each_object,
391 &bitmap_data);
392 if (ret)
393 goto out;
394 }
395
396 packed->skip_mru_updates = true;
397
398 for (e = packfile_store_get_packs(packed); e; e = e->next) {
399 struct packed_git *p = e->pack;
400
401 if (should_exclude_pack(p, opts->flags))
402 continue;
403
404 /*
405 * Objects covered by the bitmap have already been yielded
406 * above; skip them here to avoid duplicates.
407 */
408 if (bitmap && bitmap_index_contains_pack(bitmap, p))
409 continue;
410
411 if (open_pack_index(p)) {
412 pack_errors = 1;
413 continue;
414 }
415
416 ret = for_each_object_in_pack(p, odb_source_packed_for_each_object_wrapper,
417 &data, opts->flags);
418 if (ret)
419 goto out;
420 }
421
422 ret = 0;
423
424 out:
425 packed->skip_mru_updates = false;
426 free_bitmap_index(bitmap);
427
428 if (!ret && pack_errors)
429 ret = -1;
430 return ret;
431 }
432
433 static int odb_source_packed_count_objects(struct odb_source *source,
434 enum odb_count_objects_flags flags UNUSED,
435 unsigned long *out)
436 {
437 struct odb_source_packed *packed = odb_source_packed_downcast(source);
438 struct packfile_list_entry *e;
439 struct multi_pack_index *m;
440 unsigned long count = 0;
441 int ret;
442
443 m = get_multi_pack_index(packed);
444 if (m)
445 count += m->num_objects + m->num_objects_in_base;
446
447 for (e = packfile_store_get_packs(packed); e; e = e->next) {
448 if (e->pack->multi_pack_index)
449 continue;
450 if (open_pack_index(e->pack)) {
451 ret = -1;
452 goto out;
453 }
454
455 count += e->pack->num_objects;
456 }
457
458 *out = count;
459 ret = 0;
460
461 out:
462 return ret;
463 }
464
465 static int extend_abbrev_len(const struct object_id *a,
466 const struct object_id *b,
467 unsigned *out)
468 {
469 unsigned len = oid_common_prefix_hexlen(a, b);
470 if (len != hash_algos[a->algo].hexsz && len >= *out)
471 *out = len + 1;
472 return 0;
473 }
474
475 static void find_abbrev_len_for_midx(struct multi_pack_index *m,
476 const struct object_id *oid,
477 unsigned min_len,
478 unsigned *out)
479 {
480 unsigned len = min_len;
481
482 for (; m; m = m->base_midx) {
483 int match = 0;
484 uint32_t num, first = 0;
485 struct object_id found_oid;
486
487 if (!m->num_objects)
488 continue;
489
490 num = m->num_objects + m->num_objects_in_base;
491 match = bsearch_one_midx(oid, m, &first);
492
493 /*
494 * first is now the position in the packfile where we
495 * would insert the object ID if it does not exist (or the
496 * position of the object ID if it does exist). Hence, we
497 * consider a maximum of two objects nearby for the
498 * abbreviation length.
499 */
500
501 if (!match) {
502 if (nth_midxed_object_oid(&found_oid, m, first))
503 extend_abbrev_len(&found_oid, oid, &len);
504 } else if (first < num - 1) {
505 if (nth_midxed_object_oid(&found_oid, m, first + 1))
506 extend_abbrev_len(&found_oid, oid, &len);
507 }
508 if (first > 0) {
509 if (nth_midxed_object_oid(&found_oid, m, first - 1))
510 extend_abbrev_len(&found_oid, oid, &len);
511 }
512 }
513
514 *out = len;
515 }
516
517 static void find_abbrev_len_for_pack(struct packed_git *p,
518 const struct object_id *oid,
519 unsigned min_len,
520 unsigned *out)
521 {
522 int match;
523 uint32_t num, first = 0;
524 struct object_id found_oid;
525 unsigned len = min_len;
526
527 num = p->num_objects;
528 match = bsearch_pack(oid, p, &first);
529
530 /*
531 * first is now the position in the packfile where we would insert
532 * the object ID if it does not exist (or the position of mad->hash if
533 * it does exist). Hence, we consider a maximum of two objects
534 * nearby for the abbreviation length.
535 */
536 if (!match) {
537 if (!nth_packed_object_id(&found_oid, p, first))
538 extend_abbrev_len(&found_oid, oid, &len);
539 } else if (first < num - 1) {
540 if (!nth_packed_object_id(&found_oid, p, first + 1))
541 extend_abbrev_len(&found_oid, oid, &len);
542 }
543 if (first > 0) {
544 if (!nth_packed_object_id(&found_oid, p, first - 1))
545 extend_abbrev_len(&found_oid, oid, &len);
546 }
547
548 *out = len;
549 }
550
551 static int odb_source_packed_find_abbrev_len(struct odb_source *source,
552 const struct object_id *oid,
553 unsigned min_len,
554 unsigned *out)
555 {
556 struct odb_source_packed *packed = odb_source_packed_downcast(source);
557 struct packfile_list_entry *e;
558 struct multi_pack_index *m;
559
560 m = get_multi_pack_index(packed);
561 if (m)
562 find_abbrev_len_for_midx(m, oid, min_len, &min_len);
563
564 for (e = packfile_store_get_packs(packed); e; e = e->next) {
565 if (e->pack->multi_pack_index)
566 continue;
567 if (open_pack_index(e->pack) || !e->pack->num_objects)
568 continue;
569
570 find_abbrev_len_for_pack(e->pack, oid, min_len, &min_len);
571 }
572
573 *out = min_len;
574 return 0;
575 }
576
577 static int odb_source_packed_freshen_object(struct odb_source *source,
578 const struct object_id *oid,
579 const time_t *mtime)
580 {
581 struct odb_source_packed *packed = odb_source_packed_downcast(source);
582 struct utimbuf times, *timesp = NULL;
583 struct pack_entry e;
584
585 if (mtime) {
586 times.actime = *mtime;
587 times.modtime = *mtime;
588 timesp = &times;
589 }
590
591 if (!find_pack_entry(packed, oid, &e))
592 return 0;
593 if (e.p->is_cruft)
594 return 0;
595 if (e.p->freshened)
596 return 1;
597 if (utime(e.p->pack_name, timesp))
598 return 0;
599 e.p->freshened = 1;
600
601 return 1;
602 }
603
604 static int odb_source_packed_write_object(struct odb_source *source UNUSED,
605 const void *buf UNUSED,
606 size_t len UNUSED,
607 enum object_type type UNUSED,
608 const struct object_id *oid UNUSED,
609 const struct object_id *compat_oid UNUSED,
610 const time_t *mtime UNUSED,
611 unsigned flags UNUSED)
612 {
613 return error("packed backend cannot write objects");
614 }
615
616 static int odb_source_packed_write_object_stream(struct odb_source *source UNUSED,
617 struct odb_stream *stream UNUSED,
618 struct object_id *oid UNUSED)
619 {
620 return error("packed backend cannot write object streams");
621 }
622
623 static int odb_source_packed_begin_transaction(struct odb_source *source UNUSED,
624 struct odb_transaction **out UNUSED,
625 enum odb_transaction_flags flags UNUSED)
626 {
627 return error("packed backend cannot begin transactions");
628 }
629
630 static int odb_source_packed_read_alternates(struct odb_source *source UNUSED,
631 struct strvec *out UNUSED)
632 {
633 return 0;
634 }
635
636 static int odb_source_packed_write_alternate(struct odb_source *source UNUSED,
637 const char *alternate UNUSED)
638 {
639 return error("packed backend cannot write alternates");
640 }
641
642 void (*report_garbage)(unsigned seen_bits, const char *path);
643
644 static void report_helper(const struct string_list *list,
645 int seen_bits, int first, int last)
646 {
647 if (seen_bits == (PACKDIR_FILE_PACK|PACKDIR_FILE_IDX))
648 return;
649
650 for (; first < last; first++)
651 report_garbage(seen_bits, list->items[first].string);
652 }
653
654 static void report_pack_garbage(struct string_list *list)
655 {
656 int baselen = -1, first = 0, seen_bits = 0;
657
658 if (!report_garbage)
659 return;
660
661 string_list_sort(list);
662
663 for (size_t i = 0; i < list->nr; i++) {
664 const char *path = list->items[i].string;
665 if (baselen != -1 &&
666 strncmp(path, list->items[first].string, baselen)) {
667 report_helper(list, seen_bits, first, i);
668 baselen = -1;
669 seen_bits = 0;
670 }
671 if (baselen == -1) {
672 const char *dot = strrchr(path, '.');
673 if (!dot) {
674 report_garbage(PACKDIR_FILE_GARBAGE, path);
675 continue;
676 }
677 baselen = dot - path + 1;
678 first = i;
679 }
680 if (!strcmp(path + baselen, "pack"))
681 seen_bits |= 1;
682 else if (!strcmp(path + baselen, "idx"))
683 seen_bits |= 2;
684 }
685 report_helper(list, seen_bits, first, list->nr);
686 }
687
688 struct prepare_pack_data {
689 struct odb_source_packed *source;
690 struct string_list *garbage;
691 };
692
693 static void prepare_pack(const char *full_name, size_t full_name_len,
694 const char *file_name, void *_data)
695 {
696 struct prepare_pack_data *data = (struct prepare_pack_data *)_data;
697 size_t base_len = full_name_len;
698
699 if (strip_suffix_mem(full_name, &base_len, ".idx") &&
700 !(data->source->midx &&
701 midx_contains_pack(data->source->midx, file_name))) {
702 char *trimmed_path = xstrndup(full_name, full_name_len);
703 packfile_store_load_pack(data->source,
704 trimmed_path, data->source->base.local);
705 free(trimmed_path);
706 }
707
708 if (!report_garbage)
709 return;
710
711 if (!strcmp(file_name, "multi-pack-index") ||
712 !strcmp(file_name, "multi-pack-index.d"))
713 return;
714 if (starts_with(file_name, "multi-pack-index") &&
715 (ends_with(file_name, ".bitmap") || ends_with(file_name, ".rev")))
716 return;
717 if (ends_with(file_name, ".idx") ||
718 ends_with(file_name, ".rev") ||
719 ends_with(file_name, ".pack") ||
720 ends_with(file_name, ".bitmap") ||
721 ends_with(file_name, ".keep") ||
722 ends_with(file_name, ".promisor") ||
723 ends_with(file_name, ".mtimes"))
724 string_list_append(data->garbage, full_name);
725 else
726 report_garbage(PACKDIR_FILE_GARBAGE, full_name);
727 }
728
729 static void prepare_packed_git_one(struct odb_source_packed *source)
730 {
731 struct string_list garbage = STRING_LIST_INIT_DUP;
732 struct prepare_pack_data data = {
733 .source = source,
734 .garbage = &garbage,
735 };
736
737 for_each_file_in_pack_dir(source->base.path, prepare_pack, &data);
738
739 report_pack_garbage(data.garbage);
740 string_list_clear(data.garbage, 0);
741 }
742
743 DEFINE_LIST_SORT(static, sort_packs, struct packfile_list_entry, next);
744
745 static int sort_pack(const struct packfile_list_entry *a,
746 const struct packfile_list_entry *b)
747 {
748 int st;
749
750 /*
751 * Local packs tend to contain objects specific to our
752 * variant of the project than remote ones. In addition,
753 * remote ones could be on a network mounted filesystem.
754 * Favor local ones for these reasons.
755 */
756 st = a->pack->pack_local - b->pack->pack_local;
757 if (st)
758 return -st;
759
760 /*
761 * Younger packs tend to contain more recent objects,
762 * and more recent objects tend to get accessed more
763 * often.
764 */
765 if (a->pack->mtime < b->pack->mtime)
766 return 1;
767 else if (a->pack->mtime == b->pack->mtime)
768 return 0;
769 return -1;
770 }
771
772 static void odb_source_packed_prepare(struct odb_source *source,
773 enum odb_prepare_flags flags)
774 {
775 struct odb_source_packed *packed = odb_source_packed_downcast(source);
776
777 if (flags & ODB_PREPARE_FLUSH_CACHES)
778 packed->initialized = false;
779 if (packed->initialized)
780 return;
781
782 prepare_multi_pack_index_one(packed);
783 prepare_packed_git_one(packed);
784
785 sort_packs(&packed->packs.head, sort_pack);
786 for (struct packfile_list_entry *e = packed->packs.head; e; e = e->next)
787 if (!e->next)
788 packed->packs.tail = e;
789
790 packed->initialized = true;
791 }
792
793 static void odb_source_packed_reparent(const char *name UNUSED,
794 const char *old_cwd,
795 const char *new_cwd,
796 void *cb_data)
797 {
798 struct odb_source_packed *packed = cb_data;
799 char *path = reparent_relative_path(old_cwd, new_cwd,
800 packed->base.path);
801 free(packed->base.path);
802 packed->base.path = path;
803 }
804
805 static void odb_source_packed_close(struct odb_source *source)
806 {
807 struct odb_source_packed *packed = odb_source_packed_downcast(source);
808
809 for (struct packfile_list_entry *e = packed->packs.head; e; e = e->next) {
810 if (e->pack->do_not_close)
811 BUG("want to close pack marked 'do-not-close'");
812 close_pack(e->pack);
813 }
814 if (packed->midx)
815 close_midx(packed->midx);
816 packed->midx = NULL;
817 }
818
819 static void odb_source_packed_free(struct odb_source *source)
820 {
821 struct odb_source_packed *packed = odb_source_packed_downcast(source);
822
823 chdir_notify_unregister(NULL, odb_source_packed_reparent, packed);
824
825 for (struct packfile_list_entry *e = packed->packs.head; e; e = e->next)
826 free(e->pack);
827 packfile_list_clear(&packed->packs);
828
829 strmap_clear(&packed->packs_by_path, 0);
830 odb_source_release(&packed->base);
831 free(packed);
832 }
833
834 static int verify_packs(struct odb_source_packed *source,
835 struct odb_fsck_options *opts)
836 {
837 struct progress *progress = NULL;
838 struct packfile_list_entry *e;
839 uint32_t total = 0, count = 0;
840 int ret = 0;
841
842 if (opts->flags & ODB_FSCK_PROGRESS) {
843 for (e = packfile_store_get_packs(source); e; e = e->next) {
844 if (open_pack_index(e->pack))
845 continue;
846 total += e->pack->num_objects;
847 }
848
849 progress = start_progress(source->base.odb->repo,
850 _("Checking objects"), total);
851 }
852
853 for (e = packfile_store_get_packs(source); e; e = e->next) {
854 /* verify gives error messages itself */
855 if (verify_pack(source->base.odb->repo, e->pack,
856 opts->object_cb, opts->object_payload,
857 progress, count))
858 ret = -1;
859 count += e->pack->num_objects;
860 }
861 stop_progress(&progress);
862
863 return ret;
864 }
865
866 static int verify_reverse_indices(struct odb_source_packed *source,
867 struct odb_fsck_options *opts)
868 {
869 struct progress *progress = NULL;
870 struct packfile_list_entry *e;
871 uint32_t pack_count = 0;
872 int res = 0;
873
874 if (opts->flags & ODB_FSCK_PROGRESS) {
875 for (e = packfile_store_get_packs(source); e; e = e->next)
876 pack_count++;
877 progress = start_delayed_progress(source->base.odb->repo,
878 "Verifying reverse pack-indexes", pack_count);
879 pack_count = 0;
880 }
881
882 for (e = packfile_store_get_packs(source); e; e = e->next) {
883 int load_error = load_pack_revindex_from_disk(e->pack);
884
885 if (load_error < 0) {
886 error(_("unable to load rev-index for pack '%s'"), e->pack->pack_name);
887 res = -1;
888 } else if (!load_error &&
889 !load_pack_revindex(source->base.odb->repo, e->pack) &&
890 verify_pack_revindex(e->pack)) {
891 error(_("invalid rev-index for pack '%s'"), e->pack->pack_name);
892 res = -1;
893 }
894 display_progress(progress, ++pack_count);
895 }
896 stop_progress(&progress);
897
898 return res;
899 }
900
901 static int verify_midx(struct odb_source_packed *source,
902 struct odb_fsck_options *opts)
903 {
904 struct child_process midx_verify = CHILD_PROCESS_INIT;
905
906 prepare_repo_settings(source->base.odb->repo);
907 if (!source->base.odb->repo->settings.core_multi_pack_index)
908 return 0;
909
910 child_process_init(&midx_verify);
911 midx_verify.git_cmd = 1;
912 strvec_pushl(&midx_verify.args, "multi-pack-index",
913 "verify", "--object-dir", source->base.path, NULL);
914 if (opts->flags & ODB_FSCK_PROGRESS)
915 strvec_push(&midx_verify.args, "--progress");
916 else
917 strvec_push(&midx_verify.args, "--no-progress");
918 if (run_command(&midx_verify))
919 return -1;
920
921 return 0;
922 }
923
924 static int odb_source_packed_fsck(struct odb_source *source,
925 struct odb_fsck_options *opts)
926 {
927 struct odb_source_packed *packed = odb_source_packed_downcast(source);
928 int ret = 0;
929
930 if ((opts->flags & ODB_FSCK_FULL) && verify_packs(packed, opts) < 0)
931 ret = -1;
932
933 if (verify_reverse_indices(packed, opts) < 0)
934 ret = -1;
935
936 if (verify_bitmap_files(packed))
937 ret = -1;
938
939 if (verify_midx(packed, opts) < 0)
940 ret = -1;
941
942 return ret;
943 }
944
945 struct odb_source_packed *odb_source_packed_new(struct object_database *odb,
946 const char *path,
947 bool local)
948 {
949 struct odb_source_packed *packed;
950
951 CALLOC_ARRAY(packed, 1);
952 odb_source_init(&packed->base, odb, ODB_SOURCE_PACKED, path, local);
953 strmap_init(&packed->packs_by_path);
954
955 packed->base.free = odb_source_packed_free;
956 packed->base.close = odb_source_packed_close;
957 packed->base.prepare = odb_source_packed_prepare;
958 packed->base.fsck = odb_source_packed_fsck;
959 packed->base.read_object_info = odb_source_packed_read_object_info;
960 packed->base.read_object_stream = odb_source_packed_read_object_stream;
961 packed->base.for_each_object = odb_source_packed_for_each_object;
962 packed->base.count_objects = odb_source_packed_count_objects;
963 packed->base.find_abbrev_len = odb_source_packed_find_abbrev_len;
964 packed->base.freshen_object = odb_source_packed_freshen_object;
965 packed->base.write_object = odb_source_packed_write_object;
966 packed->base.write_object_stream = odb_source_packed_write_object_stream;
967 packed->base.begin_transaction = odb_source_packed_begin_transaction;
968 packed->base.read_alternates = odb_source_packed_read_alternates;
969 packed->base.write_alternate = odb_source_packed_write_alternate;
970
971 if (!is_absolute_path(path))
972 chdir_notify_register(NULL, odb_source_packed_reparent, packed);
973
974 return packed;
975 }