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