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