at commit a5a7e86d

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