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