at commit a51b77aa

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