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