at commit 1bbb92d5

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