Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/refs/packed-backend.c # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / refs / packed-backend.c` 2193 lines ยท 60114 bytes Raw Blame History 1 #define DISABLE_SIGN_COMPARE_WARNINGS 2 3 #include "../git-compat-util.h" 4 #include "../config.h" 5 #include "../dir.h" 6 #include "../fsck.h" 7 #include "../gettext.h" 8 #include "../hash.h" 9 #include "../hex.h" 10 #include "../refs.h" 11 #include "refs-internal.h" 12 #include "packed-backend.h" 13 #include "../iterator.h" 14 #include "../lockfile.h" 15 #include "../chdir-notify.h" 16 #include "../statinfo.h" 17 #include "../worktree.h" 18 #include "../wrapper.h" 19 #include "../write-or-die.h" 20 #include "../trace2.h" 21 22 enum mmap_strategy { 23 /* 24 * Don't use mmap() at all for reading `packed-refs`. 25 */ 26 MMAP_NONE, 27 28 /* 29 * Can use mmap() for reading `packed-refs`, but the file must 30 * not remain mmapped. This is the usual option on Windows, 31 * where you cannot rename a new version of a file onto a file 32 * that is currently mmapped. 33 */ 34 MMAP_TEMPORARY, 35 36 /* 37 * It is OK to leave the `packed-refs` file mmapped while 38 * arbitrary other code is running. 39 */ 40 MMAP_OK 41 }; 42 43 #if defined(NO_MMAP) 44 static enum mmap_strategy mmap_strategy = MMAP_NONE; 45 #elif defined(MMAP_PREVENTS_DELETE) 46 static enum mmap_strategy mmap_strategy = MMAP_TEMPORARY; 47 #else 48 static enum mmap_strategy mmap_strategy = MMAP_OK; 49 #endif 50 51 struct packed_ref_store; 52 53 /* 54 * A `snapshot` represents one snapshot of a `packed-refs` file. 55 * 56 * Normally, this will be a mmapped view of the contents of the 57 * `packed-refs` file at the time the snapshot was created. However, 58 * if the `packed-refs` file was not sorted, this might point at heap 59 * memory holding the contents of the `packed-refs` file with its 60 * records sorted by refname. 61 * 62 * `snapshot` instances are reference counted (via 63 * `acquire_snapshot()` and `release_snapshot()`). This is to prevent 64 * an instance from disappearing while an iterator is still iterating 65 * over it. Instances are garbage collected when their `referrers` 66 * count goes to zero. 67 * 68 * The most recent `snapshot`, if available, is referenced by the 69 * `packed_ref_store`. Its freshness is checked whenever 70 * `get_snapshot()` is called; if the existing snapshot is obsolete, a 71 * new snapshot is taken. 72 */ 73 struct snapshot { 74 /* 75 * A back-pointer to the packed_ref_store with which this 76 * snapshot is associated: 77 */ 78 struct packed_ref_store *refs; 79 80 /* Is the `packed-refs` file currently mmapped? */ 81 int mmapped; 82 83 /* 84 * The contents of the `packed-refs` file: 85 * 86 * - buf -- a pointer to the start of the memory 87 * - start -- a pointer to the first byte of actual references 88 * (i.e., after the header line, if one is present) 89 * - eof -- a pointer just past the end of the reference 90 * contents 91 * 92 * If the `packed-refs` file was already sorted, `buf` points 93 * at the mmapped contents of the file. If not, it points at 94 * heap-allocated memory containing the contents, sorted. If 95 * there were no contents (e.g., because the file didn't 96 * exist), `buf`, `start`, and `eof` are all NULL. 97 */ 98 char *buf, *start, *eof; 99 100 /* 101 * What is the peeled state of the `packed-refs` file that 102 * this snapshot represents? (This is usually determined from 103 * the file's header.) 104 */ 105 enum { PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled; 106 107 /* 108 * Count of references to this instance, including the pointer 109 * from `packed_ref_store::snapshot`, if any. The instance 110 * will not be freed as long as the reference count is 111 * nonzero. 112 */ 113 unsigned int referrers; 114 115 /* 116 * The metadata of the `packed-refs` file from which this 117 * snapshot was created, used to tell if the file has been 118 * replaced since we read it. 119 */ 120 struct stat_validity validity; 121 }; 122 123 /* 124 * A `ref_store` representing references stored in a `packed-refs` 125 * file. It implements the `ref_store` interface, though it has some 126 * limitations: 127 * 128 * - It cannot store symbolic references. 129 * 130 * - It cannot store reflogs. 131 * 132 * - It does not support reference renaming (though it could). 133 * 134 * On the other hand, it can be locked outside of a reference 135 * transaction. In that case, it remains locked even after the 136 * transaction is done and the new `packed-refs` file is activated. 137 */ 138 struct packed_ref_store { 139 struct ref_store base; 140 141 unsigned int store_flags; 142 143 /* The path of the "packed-refs" file: */ 144 char *path; 145 146 /* 147 * A snapshot of the values read from the `packed-refs` file, 148 * if it might still be current; otherwise, NULL. 149 */ 150 struct snapshot *snapshot; 151 152 /* 153 * Lock used for the "packed-refs" file. Note that this (and 154 * thus the enclosing `packed_ref_store`) must not be freed. 155 */ 156 struct lock_file lock; 157 158 /* 159 * Temporary file used when rewriting new contents to the 160 * "packed-refs" file. Note that this (and thus the enclosing 161 * `packed_ref_store`) must not be freed. 162 */ 163 struct tempfile *tempfile; 164 165 /* 166 * Timeout when taking the "packed-refs.lock" file. configurable via 167 * "core.packedRefsTimeout". 168 */ 169 bool timeout_configured; 170 int timeout_value; 171 }; 172 173 /* 174 * Increment the reference count of `*snapshot`. 175 */ 176 static void acquire_snapshot(struct snapshot *snapshot) 177 { 178 snapshot->referrers++; 179 } 180 181 /* 182 * If the buffer in `snapshot` is active, then either munmap the 183 * memory and close the file, or free the memory. Then set the buffer 184 * pointers to NULL. 185 */ 186 static void clear_snapshot_buffer(struct snapshot *snapshot) 187 { 188 if (snapshot->mmapped) { 189 if (munmap(snapshot->buf, snapshot->eof - snapshot->buf)) 190 die_errno("error ummapping packed-refs file %s", 191 snapshot->refs->path); 192 snapshot->mmapped = 0; 193 } else { 194 free(snapshot->buf); 195 } 196 snapshot->buf = snapshot->start = snapshot->eof = NULL; 197 } 198 199 /* 200 * Decrease the reference count of `*snapshot`. If it goes to zero, 201 * free `*snapshot` and return true; otherwise return false. 202 */ 203 static int release_snapshot(struct snapshot *snapshot) 204 { 205 if (!--snapshot->referrers) { 206 stat_validity_clear(&snapshot->validity); 207 clear_snapshot_buffer(snapshot); 208 free(snapshot); 209 return 1; 210 } else { 211 return 0; 212 } 213 } 214 215 static size_t snapshot_hexsz(const struct snapshot *snapshot) 216 { 217 return snapshot->refs->base.repo->hash_algo->hexsz; 218 } 219 220 static void packed_ref_store_reparent(const char *old_cwd, 221 const char *new_cwd, 222 void *payload) 223 { 224 struct packed_ref_store *refs = payload; 225 char *tmp; 226 227 tmp = reparent_relative_path(old_cwd, new_cwd, refs->path); 228 free(refs->path); 229 refs->path = tmp; 230 } 231 232 /* 233 * Since packed-refs is only stored in the common dir, don't parse the 234 * payload and rely on the files-backend to set 'gitdir' correctly. 235 */ 236 struct ref_store *packed_ref_store_init(struct repository *repo, 237 const char *payload UNUSED, 238 const char *gitdir, 239 const struct ref_store_init_options *opts) 240 { 241 struct packed_ref_store *refs = xcalloc(1, sizeof(*refs)); 242 struct ref_store *ref_store = (struct ref_store *)refs; 243 struct strbuf sb = STRBUF_INIT; 244 245 base_ref_store_init(ref_store, repo, gitdir, &refs_be_packed); 246 refs->store_flags = opts->access_flags; 247 248 strbuf_addf(&sb, "%s/packed-refs", gitdir); 249 refs->path = strbuf_detach(&sb, NULL); 250 chdir_notify_register(packed_ref_store_reparent, refs); 251 return ref_store; 252 } 253 254 /* 255 * Downcast `ref_store` to `packed_ref_store`. Die if `ref_store` is 256 * not a `packed_ref_store`. Also die if `packed_ref_store` doesn't 257 * support at least the flags specified in `required_flags`. `caller` 258 * is used in any necessary error messages. 259 */ 260 static struct packed_ref_store *packed_downcast(struct ref_store *ref_store, 261 unsigned int required_flags, 262 const char *caller) 263 { 264 struct packed_ref_store *refs; 265 266 if (ref_store->be != &refs_be_packed) 267 BUG("ref_store is type \"%s\" not \"packed\" in %s", 268 ref_store->be->name, caller); 269 270 refs = (struct packed_ref_store *)ref_store; 271 272 if ((refs->store_flags & required_flags) != required_flags) 273 BUG("unallowed operation (%s), requires %x, has %x\n", 274 caller, required_flags, refs->store_flags); 275 276 return refs; 277 } 278 279 static void clear_snapshot(struct packed_ref_store *refs) 280 { 281 if (refs->snapshot) { 282 struct snapshot *snapshot = refs->snapshot; 283 284 refs->snapshot = NULL; 285 release_snapshot(snapshot); 286 } 287 } 288 289 static void packed_ref_store_release(struct ref_store *ref_store) 290 { 291 struct packed_ref_store *refs = packed_downcast(ref_store, 0, "release"); 292 clear_snapshot(refs); 293 rollback_lock_file(&refs->lock); 294 delete_tempfile(&refs->tempfile); 295 chdir_notify_unregister(packed_ref_store_reparent, refs); 296 free(refs->path); 297 } 298 299 static NORETURN void die_unterminated_line(const char *path, 300 const char *p, size_t len) 301 { 302 if (len < 80) 303 die("unterminated line in %s: %.*s", path, (int)len, p); 304 else 305 die("unterminated line in %s: %.75s...", path, p); 306 } 307 308 static NORETURN void die_invalid_line(const char *path, 309 const char *p, size_t len) 310 { 311 const char *eol = memchr(p, '\n', len); 312 313 if (!eol) 314 die_unterminated_line(path, p, len); 315 else if (eol - p < 80) 316 die("unexpected line in %s: %.*s", path, (int)(eol - p), p); 317 else 318 die("unexpected line in %s: %.75s...", path, p); 319 320 } 321 322 struct snapshot_record { 323 const char *start; 324 size_t len; 325 }; 326 327 328 static int cmp_packed_refname(const char *r1, const char *r2) 329 { 330 while (1) { 331 if (*r1 == '\n') 332 return *r2 == '\n' ? 0 : -1; 333 if (*r1 != *r2) { 334 if (*r2 == '\n') 335 return 1; 336 else 337 return (unsigned char)*r1 < (unsigned char)*r2 ? -1 : +1; 338 } 339 r1++; 340 r2++; 341 } 342 } 343 344 static int cmp_packed_ref_records(const void *v1, const void *v2, 345 void *cb_data) 346 { 347 const struct snapshot *snapshot = cb_data; 348 const struct snapshot_record *e1 = v1, *e2 = v2; 349 const char *r1 = e1->start + snapshot_hexsz(snapshot) + 1; 350 const char *r2 = e2->start + snapshot_hexsz(snapshot) + 1; 351 352 return cmp_packed_refname(r1, r2); 353 } 354 355 /* 356 * Compare a snapshot record at `rec` to the specified NUL-terminated 357 * refname. 358 */ 359 static int cmp_record_to_refname(const char *rec, const char *refname, 360 int start, const struct snapshot *snapshot) 361 { 362 const char *r1 = rec + snapshot_hexsz(snapshot) + 1; 363 const char *r2 = refname; 364 365 while (1) { 366 if (*r1 == '\n') 367 return *r2 ? -1 : 0; 368 if (!*r2) 369 return start ? 1 : -1; 370 if (*r1 != *r2) 371 return (unsigned char)*r1 < (unsigned char)*r2 ? -1 : +1; 372 r1++; 373 r2++; 374 } 375 } 376 377 /* 378 * `snapshot->buf` is not known to be sorted. Check whether it is, and 379 * if not, sort it into new memory and munmap/free the old storage. 380 */ 381 static void sort_snapshot(struct snapshot *snapshot) 382 { 383 struct snapshot_record *records = NULL; 384 size_t alloc = 0, nr = 0; 385 int sorted = 1; 386 const char *pos, *eof, *eol; 387 size_t len, i; 388 char *new_buffer, *dst; 389 390 pos = snapshot->start; 391 eof = snapshot->eof; 392 393 if (pos == eof) 394 return; 395 396 len = eof - pos; 397 398 /* 399 * Initialize records based on a crude estimate of the number 400 * of references in the file (we'll grow it below if needed): 401 */ 402 ALLOC_GROW(records, len / 80 + 20, alloc); 403 404 while (pos < eof) { 405 eol = memchr(pos, '\n', eof - pos); 406 if (!eol) 407 /* The safety check should prevent this. */ 408 BUG("unterminated line found in packed-refs"); 409 if (eol - pos < snapshot_hexsz(snapshot) + 2) 410 die_invalid_line(snapshot->refs->path, 411 pos, eof - pos); 412 eol++; 413 if (eol < eof && *eol == '^') { 414 /* 415 * Keep any peeled line together with its 416 * reference: 417 */ 418 const char *peeled_start = eol; 419 420 eol = memchr(peeled_start, '\n', eof - peeled_start); 421 if (!eol) 422 /* The safety check should prevent this. */ 423 BUG("unterminated peeled line found in packed-refs"); 424 eol++; 425 } 426 427 ALLOC_GROW(records, nr + 1, alloc); 428 records[nr].start = pos; 429 records[nr].len = eol - pos; 430 nr++; 431 432 if (sorted && 433 nr > 1 && 434 cmp_packed_ref_records(&records[nr - 2], 435 &records[nr - 1], snapshot) >= 0) 436 sorted = 0; 437 438 pos = eol; 439 } 440 441 if (sorted) 442 goto cleanup; 443 444 /* We need to sort the memory. First we sort the records array: */ 445 QSORT_S(records, nr, cmp_packed_ref_records, snapshot); 446 447 /* 448 * Allocate a new chunk of memory, and copy the old memory to 449 * the new in the order indicated by `records` (not bothering 450 * with the header line): 451 */ 452 new_buffer = xmalloc(len); 453 for (dst = new_buffer, i = 0; i < nr; i++) { 454 memcpy(dst, records[i].start, records[i].len); 455 dst += records[i].len; 456 } 457 458 /* 459 * Now munmap the old buffer and use the sorted buffer in its 460 * place: 461 */ 462 clear_snapshot_buffer(snapshot); 463 snapshot->buf = snapshot->start = new_buffer; 464 snapshot->eof = new_buffer + len; 465 466 cleanup: 467 free(records); 468 } 469 470 /* 471 * Return a pointer to the start of the record that contains the 472 * character `*p` (which must be within the buffer). If no other 473 * record start is found, return `buf`. 474 */ 475 static const char *find_start_of_record(const char *buf, const char *p) 476 { 477 while (p > buf && (p[-1] != '\n' || p[0] == '^')) 478 p--; 479 return p; 480 } 481 482 /* 483 * Return a pointer to the start of the record following the record 484 * that contains `*p`. If none is found before `end`, return `end`. 485 */ 486 static const char *find_end_of_record(const char *p, const char *end) 487 { 488 while (++p < end && (p[-1] != '\n' || p[0] == '^')) 489 ; 490 return p; 491 } 492 493 /* 494 * We want to be able to compare mmapped reference records quickly, 495 * without totally parsing them. We can do so because the records are 496 * LF-terminated, and the refname should start exactly (GIT_SHA1_HEXSZ 497 * + 1) bytes past the beginning of the record. 498 * 499 * But what if the `packed-refs` file contains garbage? We're willing 500 * to tolerate not detecting the problem, as long as we don't produce 501 * totally garbled output (we can't afford to check the integrity of 502 * the whole file during every Git invocation). But we do want to be 503 * sure that we never read past the end of the buffer in memory and 504 * perform an illegal memory access. 505 * 506 * Guarantee that minimum level of safety by verifying that the last 507 * record in the file is LF-terminated, and that it has at least 508 * (GIT_SHA1_HEXSZ + 1) characters before the LF. Die if either of 509 * these checks fails. 510 */ 511 static void verify_buffer_safe(struct snapshot *snapshot) 512 { 513 const char *start = snapshot->start; 514 const char *eof = snapshot->eof; 515 const char *last_line; 516 517 if (start == eof) 518 return; 519 520 last_line = find_start_of_record(start, eof - 1); 521 if (*(eof - 1) != '\n' || 522 eof - last_line < snapshot_hexsz(snapshot) + 2) 523 die_invalid_line(snapshot->refs->path, 524 last_line, eof - last_line); 525 } 526 527 /* 528 * When parsing the "packed-refs" file, we will parse it line by line. 529 * Because we know the start pointer of the refname and the next 530 * newline pointer, we could calculate the length of the refname by 531 * subtracting the two pointers. However, there is a corner case where 532 * the refname contains corrupted embedded NUL characters. And 533 * `check_refname_format()` will not catch this when the truncated 534 * refname is still a valid refname. To prevent this, we need to check 535 * whether the refname contains the NUL characters. 536 */ 537 static int refname_contains_nul(struct strbuf *refname) 538 { 539 return !!memchr(refname->buf, '\0', refname->len); 540 } 541 542 #define SMALL_FILE_SIZE (32*1024) 543 544 static int allocate_snapshot_buffer(struct snapshot *snapshot, int fd, struct stat *st) 545 { 546 ssize_t bytes_read; 547 size_t size; 548 549 size = xsize_t(st->st_size); 550 if (!size) 551 return 0; 552 553 if (mmap_strategy == MMAP_NONE || size <= SMALL_FILE_SIZE) { 554 snapshot->buf = xmalloc(size); 555 bytes_read = read_in_full(fd, snapshot->buf, size); 556 if (bytes_read < 0 || bytes_read != size) 557 die_errno("couldn't read %s", snapshot->refs->path); 558 snapshot->mmapped = 0; 559 } else { 560 snapshot->buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); 561 snapshot->mmapped = 1; 562 } 563 564 snapshot->start = snapshot->buf; 565 snapshot->eof = snapshot->buf + size; 566 567 return 1; 568 } 569 570 /* 571 * Depending on `mmap_strategy`, either mmap or read the contents of 572 * the `packed-refs` file into the snapshot. Return 1 if the file 573 * existed and was read, or 0 if the file was absent or empty. Die on 574 * errors. 575 */ 576 static int load_contents(struct snapshot *snapshot) 577 { 578 struct stat st; 579 int ret; 580 int fd; 581 582 fd = open(snapshot->refs->path, O_RDONLY); 583 if (fd < 0) { 584 if (errno == ENOENT) { 585 /* 586 * This is OK; it just means that no 587 * "packed-refs" file has been written yet, 588 * which is equivalent to it being empty, 589 * which is its state when initialized with 590 * zeros. 591 */ 592 return 0; 593 } else { 594 die_errno("couldn't read %s", snapshot->refs->path); 595 } 596 } 597 598 stat_validity_update(&snapshot->validity, fd); 599 600 if (fstat(fd, &st) < 0) 601 die_errno("couldn't stat %s", snapshot->refs->path); 602 603 ret = allocate_snapshot_buffer(snapshot, fd, &st); 604 605 close(fd); 606 return ret; 607 } 608 609 static const char *find_reference_location_1(struct snapshot *snapshot, 610 const char *refname, int mustexist, 611 int start) 612 { 613 /* 614 * This is not *quite* a garden-variety binary search, because 615 * the data we're searching is made up of records, and we 616 * always need to find the beginning of a record to do a 617 * comparison. A "record" here is one line for the reference 618 * itself and zero or one peel lines that start with '^'. Our 619 * loop invariant is described in the next two comments. 620 */ 621 622 /* 623 * A pointer to the character at the start of a record whose 624 * preceding records all have reference names that come 625 * *before* `refname`. 626 */ 627 const char *lo = snapshot->start; 628 629 /* 630 * A pointer to a the first character of a record whose 631 * reference name comes *after* `refname`. 632 */ 633 const char *hi = snapshot->eof; 634 635 while (lo != hi) { 636 const char *mid, *rec; 637 int cmp; 638 639 mid = lo + (hi - lo) / 2; 640 rec = find_start_of_record(lo, mid); 641 cmp = cmp_record_to_refname(rec, refname, start, snapshot); 642 if (cmp < 0) { 643 lo = find_end_of_record(mid, hi); 644 } else if (cmp > 0) { 645 hi = rec; 646 } else { 647 return rec; 648 } 649 } 650 651 if (mustexist) 652 return NULL; 653 else 654 return lo; 655 } 656 657 /* 658 * Find the place in `snapshot->buf` where the start of the record for 659 * `refname` starts. If `mustexist` is true and the reference doesn't 660 * exist, then return NULL. If `mustexist` is false and the reference 661 * doesn't exist, then return the point where that reference would be 662 * inserted, or `snapshot->eof` (which might be NULL) if it would be 663 * inserted at the end of the file. In the latter mode, `refname` 664 * doesn't have to be a proper reference name; for example, one could 665 * search for "refs/replace/" to find the start of any replace 666 * references. 667 * 668 * The record is sought using a binary search, so `snapshot->buf` must 669 * be sorted. 670 */ 671 static const char *find_reference_location(struct snapshot *snapshot, 672 const char *refname, int mustexist) 673 { 674 return find_reference_location_1(snapshot, refname, mustexist, 1); 675 } 676 677 /* 678 * Find the place in `snapshot->buf` after the end of the record for 679 * `refname`. In other words, find the location of first thing *after* 680 * `refname`. 681 * 682 * Other semantics are identical to the ones in 683 * `find_reference_location()`. 684 */ 685 static const char *find_reference_location_end(struct snapshot *snapshot, 686 const char *refname, 687 int mustexist) 688 { 689 return find_reference_location_1(snapshot, refname, mustexist, 0); 690 } 691 692 /* 693 * Create a newly-allocated `snapshot` of the `packed-refs` file in 694 * its current state and return it. The return value will already have 695 * its reference count incremented. 696 * 697 * A comment line of the form "# pack-refs with: " may contain zero or 698 * more traits. We interpret the traits as follows: 699 * 700 * Neither `peeled` nor `fully-peeled`: 701 * 702 * Probably no references are peeled. But if the file contains a 703 * peeled value for a reference, we will use it. 704 * 705 * `peeled`: 706 * 707 * References under "refs/tags/", if they *can* be peeled, *are* 708 * peeled in this file. References outside of "refs/tags/" are 709 * probably not peeled even if they could have been, but if we find 710 * a peeled value for such a reference we will use it. 711 * 712 * `fully-peeled`: 713 * 714 * All references in the file that can be peeled are peeled. 715 * Inversely (and this is more important), any references in the 716 * file for which no peeled value is recorded is not peelable. This 717 * trait should typically be written alongside "peeled" for 718 * compatibility with older clients, but we do not require it 719 * (i.e., "peeled" is a no-op if "fully-peeled" is set). 720 * 721 * `sorted`: 722 * 723 * The references in this file are known to be sorted by refname. 724 */ 725 static struct snapshot *create_snapshot(struct packed_ref_store *refs) 726 { 727 struct snapshot *snapshot = xcalloc(1, sizeof(*snapshot)); 728 int sorted = 0; 729 730 snapshot->refs = refs; 731 acquire_snapshot(snapshot); 732 snapshot->peeled = PEELED_NONE; 733 734 if (!load_contents(snapshot)) 735 return snapshot; 736 737 /* If the file has a header line, process it: */ 738 if (snapshot->buf < snapshot->eof && *snapshot->buf == '#') { 739 char *tmp, *p, *eol; 740 struct string_list traits = STRING_LIST_INIT_NODUP; 741 742 eol = memchr(snapshot->buf, '\n', 743 snapshot->eof - snapshot->buf); 744 if (!eol) 745 die_unterminated_line(refs->path, 746 snapshot->buf, 747 snapshot->eof - snapshot->buf); 748 749 tmp = xmemdupz(snapshot->buf, eol - snapshot->buf); 750 751 if (!skip_prefix(tmp, "# pack-refs with: ", (const char **)&p)) 752 die_invalid_line(refs->path, 753 snapshot->buf, 754 snapshot->eof - snapshot->buf); 755 756 string_list_split_in_place(&traits, p, " ", -1); 757 758 if (unsorted_string_list_has_string(&traits, "fully-peeled")) 759 snapshot->peeled = PEELED_FULLY; 760 else if (unsorted_string_list_has_string(&traits, "peeled")) 761 snapshot->peeled = PEELED_TAGS; 762 763 sorted = unsorted_string_list_has_string(&traits, "sorted"); 764 765 /* perhaps other traits later as well */ 766 767 /* The "+ 1" is for the LF character. */ 768 snapshot->start = eol + 1; 769 770 string_list_clear(&traits, 0); 771 free(tmp); 772 } 773 774 verify_buffer_safe(snapshot); 775 776 if (!sorted) { 777 sort_snapshot(snapshot); 778 779 /* 780 * Reordering the records might have moved a short one 781 * to the end of the buffer, so verify the buffer's 782 * safety again: 783 */ 784 verify_buffer_safe(snapshot); 785 } 786 787 if (mmap_strategy != MMAP_OK && snapshot->mmapped) { 788 /* 789 * We don't want to leave the file mmapped, so we are 790 * forced to make a copy now: 791 */ 792 size_t size = snapshot->eof - snapshot->start; 793 char *buf_copy = xmalloc(size); 794 795 memcpy(buf_copy, snapshot->start, size); 796 clear_snapshot_buffer(snapshot); 797 snapshot->buf = snapshot->start = buf_copy; 798 snapshot->eof = buf_copy + size; 799 } 800 801 return snapshot; 802 } 803 804 /* 805 * Check that `refs->snapshot` (if present) still reflects the 806 * contents of the `packed-refs` file. If not, clear the snapshot. 807 */ 808 static void validate_snapshot(struct packed_ref_store *refs) 809 { 810 if (refs->snapshot && 811 !stat_validity_check(&refs->snapshot->validity, refs->path)) 812 clear_snapshot(refs); 813 } 814 815 /* 816 * Get the `snapshot` for the specified packed_ref_store, creating and 817 * populating it if it hasn't been read before or if the file has been 818 * changed (according to its `validity` field) since it was last read. 819 * On the other hand, if we hold the lock, then assume that the file 820 * hasn't been changed out from under us, so skip the extra `stat()` 821 * call in `stat_validity_check()`. This function does *not* increase 822 * the snapshot's reference count on behalf of the caller. 823 */ 824 static struct snapshot *get_snapshot(struct packed_ref_store *refs) 825 { 826 if (!is_lock_file_locked(&refs->lock)) 827 validate_snapshot(refs); 828 829 if (!refs->snapshot) 830 refs->snapshot = create_snapshot(refs); 831 832 return refs->snapshot; 833 } 834 835 static int packed_read_raw_ref(struct ref_store *ref_store, const char *refname, 836 struct object_id *oid, struct strbuf *referent UNUSED, 837 unsigned int *type, int *failure_errno) 838 { 839 struct packed_ref_store *refs = 840 packed_downcast(ref_store, REF_STORE_READ, "read_raw_ref"); 841 struct snapshot *snapshot = get_snapshot(refs); 842 const char *rec; 843 844 *type = 0; 845 846 rec = find_reference_location(snapshot, refname, 1); 847 848 if (!rec) { 849 /* refname is not a packed reference. */ 850 *failure_errno = ENOENT; 851 return -1; 852 } 853 854 if (get_oid_hex_algop(rec, oid, ref_store->repo->hash_algo)) 855 die_invalid_line(refs->path, rec, snapshot->eof - rec); 856 857 *type = REF_ISPACKED; 858 return 0; 859 } 860 861 /* 862 * This value is set in `base.flags` if the peeled value of the 863 * current reference is known. In that case, `peeled` contains the 864 * correct peeled value for the reference, which might be `null_oid` 865 * if the reference is not a tag or if it is broken. 866 */ 867 #define REF_KNOWS_PEELED 0x40 868 869 /* 870 * An iterator over a snapshot of a `packed-refs` file. 871 */ 872 struct packed_ref_iterator { 873 struct ref_iterator base; 874 875 struct snapshot *snapshot; 876 877 char *prefix; 878 879 /* The current position in the snapshot's buffer: */ 880 const char *pos; 881 882 /* The starting position of the current ref record. */ 883 const char *record_start; 884 885 /* The end of the part of the buffer that will be iterated over: */ 886 const char *eof; 887 888 struct jump_list_entry { 889 const char *start; 890 const char *end; 891 } *jump; 892 size_t jump_nr, jump_alloc; 893 size_t jump_cur; 894 895 /* Scratch space for current values: */ 896 struct object_id oid, peeled; 897 struct strbuf refname_buf; 898 899 struct repository *repo; 900 unsigned int flags; 901 }; 902 903 /* 904 * Move the iterator to the next record in the snapshot. Adjust the fields in 905 * `iter` and return `ITER_OK` or `ITER_DONE`. This function does not free the 906 * iterator in the case of `ITER_DONE`. 907 */ 908 static int next_record(struct packed_ref_iterator *iter) 909 { 910 const char *p, *eol; 911 912 memset(&iter->base.ref, 0, sizeof(iter->base.ref)); 913 strbuf_reset(&iter->refname_buf); 914 915 /* 916 * If iter->pos is contained within a skipped region, jump past 917 * it. 918 * 919 * Note that each skipped region is considered at most once, 920 * since they are ordered based on their starting position. 921 */ 922 while (iter->jump_cur < iter->jump_nr) { 923 struct jump_list_entry *curr = &iter->jump[iter->jump_cur]; 924 if (iter->pos < curr->start) 925 break; /* not to the next jump yet */ 926 927 iter->jump_cur++; 928 if (iter->pos < curr->end) { 929 iter->pos = curr->end; 930 trace2_counter_add(TRACE2_COUNTER_ID_PACKED_REFS_JUMPS, 1); 931 /* jumps are coalesced, so only one jump is necessary */ 932 break; 933 } 934 } 935 936 if (iter->pos == iter->eof) 937 return ITER_DONE; 938 939 iter->record_start = iter->pos; 940 iter->base.ref.flags = REF_ISPACKED; 941 p = iter->pos; 942 943 if (iter->eof - p < snapshot_hexsz(iter->snapshot) + 2 || 944 parse_oid_hex_algop(p, &iter->oid, &p, iter->repo->hash_algo) || 945 !isspace(*p++)) 946 die_invalid_line(iter->snapshot->refs->path, 947 iter->pos, iter->eof - iter->pos); 948 iter->base.ref.oid = &iter->oid; 949 950 eol = memchr(p, '\n', iter->eof - p); 951 if (!eol) 952 die_unterminated_line(iter->snapshot->refs->path, 953 iter->pos, iter->eof - iter->pos); 954 955 strbuf_add(&iter->refname_buf, p, eol - p); 956 iter->base.ref.name = iter->refname_buf.buf; 957 958 if (refname_contains_nul(&iter->refname_buf)) 959 die("packed refname contains embedded NULL: %s", iter->base.ref.name); 960 961 if (check_refname_format(iter->base.ref.name, REFNAME_ALLOW_ONELEVEL)) { 962 if (!refname_is_safe(iter->base.ref.name)) 963 die("packed refname is dangerous: %s", 964 iter->base.ref.name); 965 oidclr(&iter->oid, iter->repo->hash_algo); 966 iter->base.ref.flags |= REF_BAD_NAME | REF_ISBROKEN; 967 } 968 if (iter->snapshot->peeled == PEELED_FULLY || 969 (iter->snapshot->peeled == PEELED_TAGS && 970 starts_with(iter->base.ref.name, "refs/tags/"))) 971 iter->base.ref.flags |= REF_KNOWS_PEELED; 972 973 iter->pos = eol + 1; 974 975 if (iter->pos < iter->eof && *iter->pos == '^') { 976 p = iter->pos + 1; 977 if (iter->eof - p < snapshot_hexsz(iter->snapshot) + 1 || 978 parse_oid_hex_algop(p, &iter->peeled, &p, iter->repo->hash_algo) || 979 *p++ != '\n') 980 die_invalid_line(iter->snapshot->refs->path, 981 iter->pos, iter->eof - iter->pos); 982 iter->pos = p; 983 984 /* 985 * Regardless of what the file header said, we 986 * definitely know the value of *this* reference. But 987 * we suppress it if the reference is broken: 988 */ 989 if ((iter->base.ref.flags & REF_ISBROKEN)) { 990 oidclr(&iter->peeled, iter->repo->hash_algo); 991 iter->base.ref.flags &= ~REF_KNOWS_PEELED; 992 } else { 993 iter->base.ref.flags |= REF_KNOWS_PEELED; 994 iter->base.ref.peeled_oid = &iter->peeled; 995 } 996 } else { 997 oidclr(&iter->peeled, iter->repo->hash_algo); 998 } 999 1000 return ITER_OK; 1001 } 1002 1003 static int packed_ref_iterator_advance(struct ref_iterator *ref_iterator) 1004 { 1005 struct packed_ref_iterator *iter = 1006 (struct packed_ref_iterator *)ref_iterator; 1007 int ok; 1008 1009 while ((ok = next_record(iter)) == ITER_OK) { 1010 const char *refname = iter->base.ref.name; 1011 const char *prefix = iter->prefix; 1012 1013 if (iter->flags & REFS_FOR_EACH_PER_WORKTREE_ONLY && 1014 !is_per_worktree_ref(iter->base.ref.name)) 1015 continue; 1016 1017 if (!(iter->flags & REFS_FOR_EACH_INCLUDE_BROKEN) && 1018 !ref_resolves_to_object(iter->base.ref.name, iter->repo, 1019 &iter->oid, iter->flags)) 1020 continue; 1021 1022 while (prefix && *prefix) { 1023 if ((unsigned char)*refname < (unsigned char)*prefix) 1024 BUG("packed-refs backend yielded reference preceding its prefix"); 1025 else if ((unsigned char)*refname > (unsigned char)*prefix) 1026 return ITER_DONE; 1027 prefix++; 1028 refname++; 1029 } 1030 1031 return ITER_OK; 1032 } 1033 1034 return ok; 1035 } 1036 1037 static int packed_ref_iterator_seek(struct ref_iterator *ref_iterator, 1038 const char *refname, unsigned int flags) 1039 { 1040 struct packed_ref_iterator *iter = 1041 (struct packed_ref_iterator *)ref_iterator; 1042 const char *start; 1043 1044 if (refname && *refname) 1045 start = find_reference_location(iter->snapshot, refname, 0); 1046 else 1047 start = iter->snapshot->start; 1048 1049 /* Unset any previously set prefix */ 1050 FREE_AND_NULL(iter->prefix); 1051 1052 if (flags & REF_ITERATOR_SEEK_SET_PREFIX) 1053 iter->prefix = xstrdup_or_null(refname); 1054 1055 iter->pos = start; 1056 iter->eof = iter->snapshot->eof; 1057 1058 return 0; 1059 } 1060 1061 static void packed_ref_iterator_release(struct ref_iterator *ref_iterator) 1062 { 1063 struct packed_ref_iterator *iter = 1064 (struct packed_ref_iterator *)ref_iterator; 1065 strbuf_release(&iter->refname_buf); 1066 free(iter->jump); 1067 free(iter->prefix); 1068 release_snapshot(iter->snapshot); 1069 } 1070 1071 static struct ref_iterator_vtable packed_ref_iterator_vtable = { 1072 .advance = packed_ref_iterator_advance, 1073 .seek = packed_ref_iterator_seek, 1074 .release = packed_ref_iterator_release, 1075 }; 1076 1077 static int jump_list_entry_cmp(const void *va, const void *vb) 1078 { 1079 const struct jump_list_entry *a = va; 1080 const struct jump_list_entry *b = vb; 1081 1082 if (a->start < b->start) 1083 return -1; 1084 if (a->start > b->start) 1085 return 1; 1086 return 0; 1087 } 1088 1089 static int has_glob_special(const char *str) 1090 { 1091 const char *p; 1092 for (p = str; *p; p++) { 1093 if (is_glob_special(*p)) 1094 return 1; 1095 } 1096 return 0; 1097 } 1098 1099 static void populate_excluded_jump_list(struct packed_ref_iterator *iter, 1100 struct snapshot *snapshot, 1101 const char **excluded_patterns) 1102 { 1103 size_t i, j; 1104 const char **pattern; 1105 struct jump_list_entry *last_disjoint; 1106 1107 if (!excluded_patterns) 1108 return; 1109 1110 for (pattern = excluded_patterns; *pattern; pattern++) { 1111 struct jump_list_entry *e; 1112 const char *start, *end; 1113 1114 /* 1115 * We can't feed any excludes with globs in them to the 1116 * refs machinery. It only understands prefix matching. 1117 * We likewise can't even feed the string leading up to 1118 * the first meta-character, as something like "foo[a]" 1119 * should not exclude "foobar" (but the prefix "foo" 1120 * would match that and mark it for exclusion). 1121 */ 1122 if (has_glob_special(*pattern)) 1123 continue; 1124 1125 start = find_reference_location(snapshot, *pattern, 0); 1126 end = find_reference_location_end(snapshot, *pattern, 0); 1127 1128 if (start == end) 1129 continue; /* nothing to jump over */ 1130 1131 ALLOC_GROW(iter->jump, iter->jump_nr + 1, iter->jump_alloc); 1132 1133 e = &iter->jump[iter->jump_nr++]; 1134 e->start = start; 1135 e->end = end; 1136 } 1137 1138 if (!iter->jump_nr) { 1139 /* 1140 * Every entry in exclude_patterns has a meta-character, 1141 * nothing to do here. 1142 */ 1143 return; 1144 } 1145 1146 QSORT(iter->jump, iter->jump_nr, jump_list_entry_cmp); 1147 1148 /* 1149 * As an optimization, merge adjacent entries in the jump list 1150 * to jump forwards as far as possible when entering a skipped 1151 * region. 1152 * 1153 * For example, if we have two skipped regions: 1154 * 1155 * [[A, B], [B, C]] 1156 * 1157 * we want to combine that into a single entry jumping from A to 1158 * C. 1159 */ 1160 last_disjoint = iter->jump; 1161 1162 for (i = 1, j = 1; i < iter->jump_nr; i++) { 1163 struct jump_list_entry *ours = &iter->jump[i]; 1164 if (ours->start <= last_disjoint->end) { 1165 /* overlapping regions extend the previous one */ 1166 last_disjoint->end = last_disjoint->end > ours->end 1167 ? last_disjoint->end : ours->end; 1168 } else { 1169 /* otherwise, insert a new region */ 1170 iter->jump[j++] = *ours; 1171 last_disjoint = ours; 1172 } 1173 } 1174 1175 iter->jump_nr = j; 1176 iter->jump_cur = 0; 1177 } 1178 1179 static struct ref_iterator *packed_ref_iterator_begin( 1180 struct ref_store *ref_store, 1181 const char *prefix, const char **exclude_patterns, 1182 unsigned int flags) 1183 { 1184 struct packed_ref_store *refs; 1185 struct snapshot *snapshot; 1186 struct packed_ref_iterator *iter; 1187 struct ref_iterator *ref_iterator; 1188 unsigned int required_flags = REF_STORE_READ; 1189 1190 if (!(flags & REFS_FOR_EACH_INCLUDE_BROKEN)) 1191 required_flags |= REF_STORE_ODB; 1192 refs = packed_downcast(ref_store, required_flags, "ref_iterator_begin"); 1193 1194 /* 1195 * Note that `get_snapshot()` internally checks whether the 1196 * snapshot is up to date with what is on disk, and re-reads 1197 * it if not. 1198 */ 1199 snapshot = get_snapshot(refs); 1200 1201 CALLOC_ARRAY(iter, 1); 1202 ref_iterator = &iter->base; 1203 base_ref_iterator_init(ref_iterator, &packed_ref_iterator_vtable); 1204 1205 if (exclude_patterns) 1206 populate_excluded_jump_list(iter, snapshot, exclude_patterns); 1207 1208 iter->snapshot = snapshot; 1209 acquire_snapshot(snapshot); 1210 strbuf_init(&iter->refname_buf, 0); 1211 iter->repo = ref_store->repo; 1212 iter->flags = flags; 1213 1214 if (packed_ref_iterator_seek(&iter->base, prefix, 1215 REF_ITERATOR_SEEK_SET_PREFIX) < 0) { 1216 ref_iterator_free(&iter->base); 1217 return NULL; 1218 } 1219 1220 return ref_iterator; 1221 } 1222 1223 /* 1224 * Write an entry to the packed-refs file for the specified refname. 1225 * If peeled is non-NULL, write it as the entry's peeled value. On 1226 * error, return a nonzero value and leave errno set at the value left 1227 * by the failing call to `fprintf()`. 1228 */ 1229 static int write_packed_entry(FILE *fh, const char *refname, 1230 const struct object_id *oid, 1231 const struct object_id *peeled) 1232 { 1233 if (fprintf(fh, "%s %s\n", oid_to_hex(oid), refname) < 0 || 1234 (peeled && fprintf(fh, "^%s\n", oid_to_hex(peeled)) < 0)) 1235 return -1; 1236 1237 return 0; 1238 } 1239 1240 int packed_refs_lock(struct ref_store *ref_store, int flags, struct strbuf *err) 1241 { 1242 struct packed_ref_store *refs = 1243 packed_downcast(ref_store, REF_STORE_WRITE | REF_STORE_MAIN, 1244 "packed_refs_lock"); 1245 1246 if (!refs->timeout_configured) { 1247 if (repo_config_get_int(ref_store->repo, "core.packedrefstimeout", 1248 &refs->timeout_value)) 1249 refs->timeout_value = 1000; 1250 refs->timeout_configured = true; 1251 } 1252 1253 /* 1254 * Note that we close the lockfile immediately because we 1255 * don't write new content to it, but rather to a separate 1256 * tempfile. 1257 */ 1258 if (repo_hold_lock_file_for_update_timeout(ref_store->repo, &refs->lock, 1259 refs->path, flags, 1260 refs->timeout_value) < 0) { 1261 unable_to_lock_message(refs->path, errno, err); 1262 return -1; 1263 } 1264 1265 if (close_lock_file_gently(&refs->lock)) { 1266 strbuf_addf(err, "unable to close %s: %s", refs->path, strerror(errno)); 1267 rollback_lock_file(&refs->lock); 1268 return -1; 1269 } 1270 1271 /* 1272 * There is a stat-validity problem might cause `update-ref -d` 1273 * lost the newly commit of a ref, because a new `packed-refs` 1274 * file might has the same on-disk file attributes such as 1275 * timestamp, file size and inode value, but has a changed 1276 * ref value. 1277 * 1278 * This could happen with a very small chance when 1279 * `update-ref -d` is called and at the same time another 1280 * `pack-refs --all` process is running. 1281 * 1282 * Now that we hold the `packed-refs` lock, it is important 1283 * to make sure we could read the latest version of 1284 * `packed-refs` file no matter we have just mmap it or not. 1285 * So what need to do is clear the snapshot if we hold it 1286 * already. 1287 */ 1288 clear_snapshot(refs); 1289 1290 /* 1291 * Now make sure that the packed-refs file as it exists in the 1292 * locked state is loaded into the snapshot: 1293 */ 1294 get_snapshot(refs); 1295 return 0; 1296 } 1297 1298 void packed_refs_unlock(struct ref_store *ref_store) 1299 { 1300 struct packed_ref_store *refs = packed_downcast( 1301 ref_store, 1302 REF_STORE_READ | REF_STORE_WRITE, 1303 "packed_refs_unlock"); 1304 1305 if (!is_lock_file_locked(&refs->lock)) 1306 BUG("packed_refs_unlock() called when not locked"); 1307 rollback_lock_file(&refs->lock); 1308 } 1309 1310 int packed_refs_is_locked(struct ref_store *ref_store) 1311 { 1312 struct packed_ref_store *refs = packed_downcast( 1313 ref_store, 1314 REF_STORE_READ | REF_STORE_WRITE, 1315 "packed_refs_is_locked"); 1316 1317 return is_lock_file_locked(&refs->lock); 1318 } 1319 1320 int packed_refs_size(struct ref_store *ref_store, 1321 size_t *out) 1322 { 1323 struct packed_ref_store *refs = packed_downcast(ref_store, REF_STORE_READ, 1324 "packed_refs_size"); 1325 struct stat st; 1326 1327 if (stat(refs->path, &st) < 0) { 1328 if (errno != ENOENT) 1329 return -1; 1330 *out = 0; 1331 return 0; 1332 } 1333 1334 *out = st.st_size; 1335 return 0; 1336 } 1337 1338 /* 1339 * The packed-refs header line that we write out. Perhaps other traits 1340 * will be added later. 1341 * 1342 * Note that earlier versions of Git used to parse these traits by 1343 * looking for " trait " in the line. For this reason, the space after 1344 * the colon and the trailing space are required. 1345 */ 1346 static const char PACKED_REFS_HEADER[] = 1347 "# pack-refs with: peeled fully-peeled sorted \n"; 1348 1349 static int packed_ref_store_create_on_disk(struct ref_store *ref_store UNUSED, 1350 int flags UNUSED, 1351 struct strbuf *err UNUSED) 1352 { 1353 /* Nothing to do. */ 1354 return 0; 1355 } 1356 1357 static int packed_ref_store_remove_on_disk(struct ref_store *ref_store, 1358 struct strbuf *err) 1359 { 1360 struct packed_ref_store *refs = packed_downcast(ref_store, 0, "remove"); 1361 1362 if (remove_path(refs->path) < 0) { 1363 strbuf_addstr(err, "could not delete packed-refs"); 1364 return -1; 1365 } 1366 1367 return 0; 1368 } 1369 1370 /* 1371 * Write the packed refs from the current snapshot to the packed-refs 1372 * tempfile, incorporating any changes from `updates`. `updates` must 1373 * be a sorted string list whose keys are the refnames and whose util 1374 * values are `struct ref_update *`. On error, rollback the tempfile, 1375 * write an error message to `err`, and return a nonzero value. 1376 * 1377 * The packfile must be locked before calling this function and will 1378 * remain locked when it is done. 1379 */ 1380 static enum ref_transaction_error write_with_updates(struct packed_ref_store *refs, 1381 struct ref_transaction *transaction, 1382 struct strbuf *err) 1383 { 1384 enum ref_transaction_error ret = REF_TRANSACTION_ERROR_GENERIC; 1385 struct string_list *updates = &transaction->refnames; 1386 struct ref_iterator *iter = NULL; 1387 size_t i; 1388 int ok; 1389 FILE *out; 1390 struct strbuf sb = STRBUF_INIT; 1391 char *packed_refs_path; 1392 1393 if (!is_lock_file_locked(&refs->lock)) 1394 BUG("write_with_updates() called while unlocked"); 1395 1396 /* 1397 * If packed-refs is a symlink, we want to overwrite the 1398 * symlinked-to file, not the symlink itself. Also, put the 1399 * staging file next to it: 1400 */ 1401 packed_refs_path = get_locked_file_path(&refs->lock); 1402 strbuf_addf(&sb, "%s.new", packed_refs_path); 1403 free(packed_refs_path); 1404 refs->tempfile = repo_create_tempfile(refs->base.repo, sb.buf); 1405 if (!refs->tempfile) { 1406 strbuf_addf(err, "unable to create file %s: %s", 1407 sb.buf, strerror(errno)); 1408 strbuf_release(&sb); 1409 return REF_TRANSACTION_ERROR_GENERIC; 1410 } 1411 strbuf_release(&sb); 1412 1413 out = fdopen_tempfile(refs->tempfile, "w"); 1414 if (!out) { 1415 strbuf_addf(err, "unable to fdopen packed-refs tempfile: %s", 1416 strerror(errno)); 1417 goto error; 1418 } 1419 1420 if (fprintf(out, "%s", PACKED_REFS_HEADER) < 0) 1421 goto write_error; 1422 1423 /* 1424 * We iterate in parallel through the current list of refs and 1425 * the list of updates, processing an entry from at least one 1426 * of the lists each time through the loop. When the current 1427 * list of refs is exhausted, set iter to NULL. When the list 1428 * of updates is exhausted, leave i set to updates->nr. 1429 */ 1430 iter = packed_ref_iterator_begin(&refs->base, "", NULL, 1431 REFS_FOR_EACH_INCLUDE_BROKEN); 1432 if ((ok = ref_iterator_advance(iter)) != ITER_OK) { 1433 ref_iterator_free(iter); 1434 iter = NULL; 1435 } 1436 1437 i = 0; 1438 1439 while (iter || i < updates->nr) { 1440 struct ref_update *update = NULL; 1441 int cmp; 1442 1443 if (i >= updates->nr) { 1444 cmp = -1; 1445 } else { 1446 update = updates->items[i].util; 1447 1448 if (!iter) 1449 cmp = +1; 1450 else 1451 cmp = strcmp(iter->ref.name, update->refname); 1452 } 1453 1454 if (!cmp) { 1455 /* 1456 * There is both an old value and an update 1457 * for this reference. Check the old value if 1458 * necessary: 1459 */ 1460 if ((update->flags & REF_HAVE_OLD)) { 1461 if (is_null_oid(&update->old_oid)) { 1462 strbuf_addf(err, "cannot update ref '%s': " 1463 "reference already exists", 1464 update->refname); 1465 ret = REF_TRANSACTION_ERROR_CREATE_EXISTS; 1466 1467 if (ref_transaction_maybe_set_rejected(transaction, i, 1468 ret, err)) { 1469 ret = 0; 1470 continue; 1471 } 1472 1473 goto error; 1474 } else if (!oideq(&update->old_oid, iter->ref.oid)) { 1475 strbuf_addf(err, "cannot update ref '%s': " 1476 "is at %s but expected %s", 1477 update->refname, 1478 oid_to_hex(iter->ref.oid), 1479 oid_to_hex(&update->old_oid)); 1480 ret = REF_TRANSACTION_ERROR_INCORRECT_OLD_VALUE; 1481 1482 if (ref_transaction_maybe_set_rejected(transaction, i, 1483 ret, err)) { 1484 ret = 0; 1485 continue; 1486 } 1487 1488 goto error; 1489 } 1490 } 1491 1492 /* Now figure out what to use for the new value: */ 1493 if ((update->flags & REF_HAVE_NEW)) { 1494 /* 1495 * The update takes precedence. Skip 1496 * the iterator over the unneeded 1497 * value. 1498 */ 1499 if ((ok = ref_iterator_advance(iter)) != ITER_OK) { 1500 ref_iterator_free(iter); 1501 iter = NULL; 1502 } 1503 cmp = +1; 1504 } else { 1505 /* 1506 * The update doesn't actually want to 1507 * change anything. We're done with it. 1508 */ 1509 i++; 1510 cmp = -1; 1511 } 1512 } else if (cmp > 0) { 1513 /* 1514 * There is no old value but there is an 1515 * update for this reference. Make sure that 1516 * the update didn't expect an existing value: 1517 */ 1518 if ((update->flags & REF_HAVE_OLD) && 1519 !is_null_oid(&update->old_oid)) { 1520 strbuf_addf(err, "cannot update ref '%s': " 1521 "reference is missing but expected %s", 1522 update->refname, 1523 oid_to_hex(&update->old_oid)); 1524 ret = REF_TRANSACTION_ERROR_NONEXISTENT_REF; 1525 1526 if (ref_transaction_maybe_set_rejected(transaction, i, 1527 ret, err)) { 1528 ret = 0; 1529 continue; 1530 } 1531 1532 goto error; 1533 } 1534 } 1535 1536 if (cmp < 0) { 1537 const struct packed_ref_iterator *packed_iter = 1538 (const struct packed_ref_iterator *)iter; 1539 size_t len = packed_iter->pos - packed_iter->record_start; 1540 1541 /* 1542 * Skip any formatting and directly write to the packed-refs file 1543 * when deleting references and writing the remaining refs verbatim. 1544 */ 1545 if (fwrite(packed_iter->record_start, len, 1, out) != 1) 1546 goto write_error; 1547 1548 if ((ok = ref_iterator_advance(iter)) != ITER_OK) { 1549 ref_iterator_free(iter); 1550 iter = NULL; 1551 } 1552 } else if (is_null_oid(&update->new_oid)) { 1553 /* 1554 * The update wants to delete the reference, 1555 * and the reference either didn't exist or we 1556 * have already skipped it. So we're done with 1557 * the update (and don't have to write 1558 * anything). 1559 */ 1560 i++; 1561 } else { 1562 bool peeled = update->flags & REF_HAVE_PEELED; 1563 1564 if (write_packed_entry(out, update->refname, 1565 &update->new_oid, 1566 peeled ? &update->peeled : NULL)) 1567 goto write_error; 1568 1569 i++; 1570 } 1571 } 1572 1573 if (ok != ITER_DONE) { 1574 strbuf_addstr(err, "unable to write packed-refs file: " 1575 "error iterating over old contents"); 1576 goto error; 1577 } 1578 1579 if (fflush(out) || 1580 fsync_component(FSYNC_COMPONENT_REFERENCE, get_tempfile_fd(refs->tempfile)) || 1581 close_tempfile_gently(refs->tempfile)) { 1582 strbuf_addf(err, "error closing file %s: %s", 1583 get_tempfile_path(refs->tempfile), 1584 strerror(errno)); 1585 strbuf_release(&sb); 1586 delete_tempfile(&refs->tempfile); 1587 return REF_TRANSACTION_ERROR_GENERIC; 1588 } 1589 1590 return 0; 1591 1592 write_error: 1593 strbuf_addf(err, "error writing to %s: %s", 1594 get_tempfile_path(refs->tempfile), strerror(errno)); 1595 ret = REF_TRANSACTION_ERROR_GENERIC; 1596 1597 error: 1598 ref_iterator_free(iter); 1599 delete_tempfile(&refs->tempfile); 1600 return ret; 1601 } 1602 1603 int is_packed_transaction_needed(struct ref_store *ref_store, 1604 struct ref_transaction *transaction) 1605 { 1606 struct packed_ref_store *refs = packed_downcast( 1607 ref_store, 1608 REF_STORE_READ, 1609 "is_packed_transaction_needed"); 1610 struct strbuf referent = STRBUF_INIT; 1611 size_t i; 1612 int ret; 1613 1614 if (!is_lock_file_locked(&refs->lock)) 1615 BUG("is_packed_transaction_needed() called while unlocked"); 1616 1617 /* 1618 * We're only going to bother returning false for the common, 1619 * trivial case that references are only being deleted, their 1620 * old values are not being checked, and the old `packed-refs` 1621 * file doesn't contain any of those reference(s). This gives 1622 * false positives for some other cases that could 1623 * theoretically be optimized away: 1624 * 1625 * 1. It could be that the old value is being verified without 1626 * setting a new value. In this case, we could verify the 1627 * old value here and skip the update if it agrees. If it 1628 * disagrees, we could either let the update go through 1629 * (the actual commit would re-detect and report the 1630 * problem), or come up with a way of reporting such an 1631 * error to *our* caller. 1632 * 1633 * 2. It could be that a new value is being set, but that it 1634 * is identical to the current packed value of the 1635 * reference. 1636 * 1637 * Neither of these cases will come up in the current code, 1638 * because the only caller of this function passes to it a 1639 * transaction that only includes `delete` updates with no 1640 * `old_id`. Even if that ever changes, false positives only 1641 * cause an optimization to be missed; they do not affect 1642 * correctness. 1643 */ 1644 1645 /* 1646 * Start with the cheap checks that don't require old 1647 * reference values to be read: 1648 */ 1649 for (i = 0; i < transaction->nr; i++) { 1650 struct ref_update *update = transaction->updates[i]; 1651 1652 if (update->flags & REF_HAVE_OLD) 1653 /* Have to check the old value -> needed. */ 1654 return 1; 1655 1656 if ((update->flags & REF_HAVE_NEW) && !is_null_oid(&update->new_oid)) 1657 /* Have to set a new value -> needed. */ 1658 return 1; 1659 } 1660 1661 /* 1662 * The transaction isn't checking any old values nor is it 1663 * setting any nonzero new values, so it still might be able 1664 * to be skipped. Now do the more expensive check: the update 1665 * is needed if any of the updates is a delete, and the old 1666 * `packed-refs` file contains a value for that reference. 1667 */ 1668 ret = 0; 1669 for (i = 0; i < transaction->nr; i++) { 1670 struct ref_update *update = transaction->updates[i]; 1671 int failure_errno; 1672 unsigned int type; 1673 struct object_id oid; 1674 1675 if (!(update->flags & REF_HAVE_NEW)) 1676 /* 1677 * This reference isn't being deleted -> not 1678 * needed. 1679 */ 1680 continue; 1681 1682 if (!refs_read_raw_ref(ref_store, update->refname, &oid, 1683 &referent, &type, &failure_errno) || 1684 failure_errno != ENOENT) { 1685 /* 1686 * We have to actually delete that reference 1687 * -> this transaction is needed. 1688 */ 1689 ret = 1; 1690 break; 1691 } 1692 } 1693 1694 strbuf_release(&referent); 1695 return ret; 1696 } 1697 1698 struct packed_transaction_backend_data { 1699 /* True iff the transaction owns the packed-refs lock. */ 1700 int own_lock; 1701 }; 1702 1703 static void packed_transaction_cleanup(struct packed_ref_store *refs, 1704 struct ref_transaction *transaction) 1705 { 1706 struct packed_transaction_backend_data *data = transaction->backend_data; 1707 1708 if (data) { 1709 if (is_tempfile_active(refs->tempfile)) 1710 delete_tempfile(&refs->tempfile); 1711 1712 if (data->own_lock && is_lock_file_locked(&refs->lock)) { 1713 packed_refs_unlock(&refs->base); 1714 data->own_lock = 0; 1715 } 1716 1717 free(data); 1718 transaction->backend_data = NULL; 1719 } 1720 1721 transaction->state = REF_TRANSACTION_CLOSED; 1722 } 1723 1724 static int packed_transaction_prepare(struct ref_store *ref_store, 1725 struct ref_transaction *transaction, 1726 struct strbuf *err) 1727 { 1728 struct packed_ref_store *refs = packed_downcast( 1729 ref_store, 1730 REF_STORE_READ | REF_STORE_WRITE | REF_STORE_ODB, 1731 "ref_transaction_prepare"); 1732 struct packed_transaction_backend_data *data; 1733 enum ref_transaction_error ret = REF_TRANSACTION_ERROR_GENERIC; 1734 1735 /* 1736 * Note that we *don't* skip transactions with zero updates, 1737 * because such a transaction might be executed for the side 1738 * effect of ensuring that all of the references are peeled or 1739 * ensuring that the `packed-refs` file is sorted. If the 1740 * caller wants to optimize away empty transactions, it should 1741 * do so itself. 1742 */ 1743 1744 CALLOC_ARRAY(data, 1); 1745 1746 transaction->backend_data = data; 1747 1748 if (!is_lock_file_locked(&refs->lock)) { 1749 if (packed_refs_lock(ref_store, 0, err)) 1750 goto failure; 1751 data->own_lock = 1; 1752 } 1753 1754 ret = write_with_updates(refs, transaction, err); 1755 if (ret) 1756 goto failure; 1757 1758 transaction->state = REF_TRANSACTION_PREPARED; 1759 return 0; 1760 1761 failure: 1762 packed_transaction_cleanup(refs, transaction); 1763 return ret; 1764 } 1765 1766 static int packed_transaction_abort(struct ref_store *ref_store, 1767 struct ref_transaction *transaction, 1768 struct strbuf *err UNUSED) 1769 { 1770 struct packed_ref_store *refs = packed_downcast( 1771 ref_store, 1772 REF_STORE_READ | REF_STORE_WRITE | REF_STORE_ODB, 1773 "ref_transaction_abort"); 1774 1775 packed_transaction_cleanup(refs, transaction); 1776 return 0; 1777 } 1778 1779 static int packed_transaction_finish(struct ref_store *ref_store, 1780 struct ref_transaction *transaction, 1781 struct strbuf *err) 1782 { 1783 struct packed_ref_store *refs = packed_downcast( 1784 ref_store, 1785 REF_STORE_READ | REF_STORE_WRITE | REF_STORE_ODB, 1786 "ref_transaction_finish"); 1787 int ret = REF_TRANSACTION_ERROR_GENERIC; 1788 char *packed_refs_path; 1789 1790 clear_snapshot(refs); 1791 1792 packed_refs_path = get_locked_file_path(&refs->lock); 1793 if (rename_tempfile(&refs->tempfile, packed_refs_path)) { 1794 strbuf_addf(err, "error replacing %s: %s", 1795 refs->path, strerror(errno)); 1796 goto cleanup; 1797 } 1798 1799 ret = 0; 1800 1801 cleanup: 1802 free(packed_refs_path); 1803 packed_transaction_cleanup(refs, transaction); 1804 return ret; 1805 } 1806 1807 static int packed_optimize(struct ref_store *ref_store UNUSED, 1808 struct refs_optimize_opts *opts UNUSED) 1809 { 1810 /* 1811 * Packed refs are already packed. It might be that loose refs 1812 * are packed *into* a packed refs store, but that is done by 1813 * updating the packed references via a transaction. 1814 */ 1815 return 0; 1816 } 1817 1818 static int packed_optimize_required(struct ref_store *ref_store UNUSED, 1819 struct refs_optimize_opts *opts UNUSED, 1820 bool *required) 1821 { 1822 /* 1823 * Packed refs are already optimized. 1824 */ 1825 *required = false; 1826 return 0; 1827 } 1828 1829 static struct ref_iterator *packed_reflog_iterator_begin(struct ref_store *ref_store UNUSED) 1830 { 1831 return empty_ref_iterator_begin(); 1832 } 1833 1834 static int packed_fsck_ref_next_line(struct fsck_options *o, 1835 unsigned long line_number, const char *start, 1836 const char *eof, const char **eol) 1837 { 1838 int ret = 0; 1839 1840 *eol = memchr(start, '\n', eof - start); 1841 if (!*eol) { 1842 struct strbuf packed_entry = STRBUF_INIT; 1843 struct fsck_ref_report report = { 0 }; 1844 1845 strbuf_addf(&packed_entry, "packed-refs line %lu", line_number); 1846 report.path = packed_entry.buf; 1847 ret = fsck_report_ref(o, &report, 1848 FSCK_MSG_PACKED_REF_ENTRY_NOT_TERMINATED, 1849 "'%.*s' is not terminated with a newline", 1850 (int)(eof - start), start); 1851 1852 /* 1853 * There is no newline but we still want to parse it to the end of 1854 * the buffer. 1855 */ 1856 *eol = eof; 1857 strbuf_release(&packed_entry); 1858 } 1859 1860 return ret; 1861 } 1862 1863 static int packed_fsck_ref_header(struct fsck_options *o, 1864 const char *start, const char *eol, 1865 unsigned int *sorted) 1866 { 1867 struct string_list traits = STRING_LIST_INIT_NODUP; 1868 char *tmp_line; 1869 int ret = 0; 1870 char *p; 1871 1872 tmp_line = xmemdupz(start, eol - start); 1873 if (!skip_prefix(tmp_line, "# pack-refs with: ", (const char **)&p)) { 1874 struct fsck_ref_report report = { 0 }; 1875 report.path = "packed-refs.header"; 1876 1877 ret = fsck_report_ref(o, &report, 1878 FSCK_MSG_BAD_PACKED_REF_HEADER, 1879 "'%.*s' does not start with '# pack-refs with: '", 1880 (int)(eol - start), start); 1881 goto cleanup; 1882 } 1883 1884 string_list_split_in_place(&traits, p, " ", -1); 1885 *sorted = unsorted_string_list_has_string(&traits, "sorted"); 1886 1887 cleanup: 1888 free(tmp_line); 1889 string_list_clear(&traits, 0); 1890 return ret; 1891 } 1892 1893 static int packed_fsck_ref_peeled_line(struct fsck_options *o, 1894 struct ref_store *ref_store, 1895 unsigned long line_number, 1896 const char *start, const char *eol) 1897 { 1898 struct strbuf packed_entry = STRBUF_INIT; 1899 struct fsck_ref_report report = { 0 }; 1900 struct object_id peeled; 1901 const char *p; 1902 int ret = 0; 1903 1904 /* 1905 * Skip the '^' and parse the peeled oid. 1906 */ 1907 start++; 1908 if (parse_oid_hex_algop(start, &peeled, &p, ref_store->repo->hash_algo)) { 1909 strbuf_addf(&packed_entry, "packed-refs line %lu", line_number); 1910 report.path = packed_entry.buf; 1911 1912 ret = fsck_report_ref(o, &report, 1913 FSCK_MSG_BAD_PACKED_REF_ENTRY, 1914 "'%.*s' has invalid peeled oid", 1915 (int)(eol - start), start); 1916 goto cleanup; 1917 } 1918 1919 if (p != eol) { 1920 strbuf_addf(&packed_entry, "packed-refs line %lu", line_number); 1921 report.path = packed_entry.buf; 1922 1923 ret = fsck_report_ref(o, &report, 1924 FSCK_MSG_BAD_PACKED_REF_ENTRY, 1925 "has trailing garbage after peeled oid '%.*s'", 1926 (int)(eol - p), p); 1927 goto cleanup; 1928 } 1929 1930 cleanup: 1931 strbuf_release(&packed_entry); 1932 return ret; 1933 } 1934 1935 static int packed_fsck_ref_main_line(struct fsck_options *o, 1936 struct ref_store *ref_store, 1937 unsigned long line_number, 1938 struct strbuf *refname, 1939 const char *start, const char *eol) 1940 { 1941 struct strbuf packed_entry = STRBUF_INIT; 1942 struct fsck_ref_report report = { 0 }; 1943 struct object_id oid; 1944 const char *p; 1945 int ret = 0; 1946 1947 if (parse_oid_hex_algop(start, &oid, &p, ref_store->repo->hash_algo)) { 1948 strbuf_addf(&packed_entry, "packed-refs line %lu", line_number); 1949 report.path = packed_entry.buf; 1950 1951 ret = fsck_report_ref(o, &report, 1952 FSCK_MSG_BAD_PACKED_REF_ENTRY, 1953 "'%.*s' has invalid oid", 1954 (int)(eol - start), start); 1955 goto cleanup; 1956 } 1957 1958 if (p == eol || !isspace(*p)) { 1959 strbuf_addf(&packed_entry, "packed-refs line %lu", line_number); 1960 report.path = packed_entry.buf; 1961 1962 ret = fsck_report_ref(o, &report, 1963 FSCK_MSG_BAD_PACKED_REF_ENTRY, 1964 "has no space after oid '%s' but with '%.*s'", 1965 oid_to_hex(&oid), (int)(eol - p), p); 1966 goto cleanup; 1967 } 1968 1969 p++; 1970 strbuf_reset(refname); 1971 strbuf_add(refname, p, eol - p); 1972 if (refname_contains_nul(refname)) { 1973 strbuf_addf(&packed_entry, "packed-refs line %lu", line_number); 1974 report.path = packed_entry.buf; 1975 1976 ret = fsck_report_ref(o, &report, 1977 FSCK_MSG_BAD_PACKED_REF_ENTRY, 1978 "refname '%s' contains NULL binaries", 1979 refname->buf); 1980 } 1981 1982 if (check_refname_format(refname->buf, 0)) { 1983 strbuf_addf(&packed_entry, "packed-refs line %lu", line_number); 1984 report.path = packed_entry.buf; 1985 1986 ret = fsck_report_ref(o, &report, 1987 FSCK_MSG_BAD_REF_NAME, 1988 "has bad refname '%s'", refname->buf); 1989 } 1990 1991 cleanup: 1992 strbuf_release(&packed_entry); 1993 return ret; 1994 } 1995 1996 static int packed_fsck_ref_sorted(struct fsck_options *o, 1997 struct ref_store *ref_store, 1998 const char *start, const char *eof) 1999 { 2000 size_t hexsz = ref_store->repo->hash_algo->hexsz; 2001 struct strbuf packed_entry = STRBUF_INIT; 2002 struct fsck_ref_report report = { 0 }; 2003 struct strbuf refname1 = STRBUF_INIT; 2004 struct strbuf refname2 = STRBUF_INIT; 2005 unsigned long line_number = 1; 2006 const char *former = NULL; 2007 const char *current; 2008 const char *eol; 2009 int ret = 0; 2010 2011 if (*start == '#') { 2012 eol = memchr(start, '\n', eof - start); 2013 start = eol + 1; 2014 line_number++; 2015 } 2016 2017 for (; start < eof; line_number++, start = eol + 1) { 2018 eol = memchr(start, '\n', eof - start); 2019 2020 if (*start == '^') 2021 continue; 2022 2023 if (!former) { 2024 former = start + hexsz + 1; 2025 continue; 2026 } 2027 2028 current = start + hexsz + 1; 2029 if (cmp_packed_refname(former, current) >= 0) { 2030 const char *err_fmt = 2031 "refname '%s' is less than previous refname '%s'"; 2032 2033 eol = memchr(former, '\n', eof - former); 2034 strbuf_add(&refname1, former, eol - former); 2035 eol = memchr(current, '\n', eof - current); 2036 strbuf_add(&refname2, current, eol - current); 2037 2038 strbuf_addf(&packed_entry, "packed-refs line %lu", line_number); 2039 report.path = packed_entry.buf; 2040 ret = fsck_report_ref(o, &report, 2041 FSCK_MSG_PACKED_REF_UNSORTED, 2042 err_fmt, refname2.buf, refname1.buf); 2043 goto cleanup; 2044 } 2045 former = current; 2046 } 2047 2048 cleanup: 2049 strbuf_release(&packed_entry); 2050 strbuf_release(&refname1); 2051 strbuf_release(&refname2); 2052 return ret; 2053 } 2054 2055 static int packed_fsck_ref_content(struct fsck_options *o, 2056 struct ref_store *ref_store, 2057 unsigned int *sorted, 2058 const char *start, const char *eof) 2059 { 2060 struct strbuf refname = STRBUF_INIT; 2061 unsigned long line_number = 1; 2062 const char *eol; 2063 int ret = 0; 2064 2065 ret |= packed_fsck_ref_next_line(o, line_number, start, eof, &eol); 2066 if (*start == '#') { 2067 ret |= packed_fsck_ref_header(o, start, eol, sorted); 2068 2069 start = eol + 1; 2070 line_number++; 2071 } 2072 2073 while (start < eof) { 2074 ret |= packed_fsck_ref_next_line(o, line_number, start, eof, &eol); 2075 ret |= packed_fsck_ref_main_line(o, ref_store, line_number, &refname, start, eol); 2076 start = eol + 1; 2077 line_number++; 2078 if (start < eof && *start == '^') { 2079 ret |= packed_fsck_ref_next_line(o, line_number, start, eof, &eol); 2080 ret |= packed_fsck_ref_peeled_line(o, ref_store, line_number, 2081 start, eol); 2082 start = eol + 1; 2083 line_number++; 2084 } 2085 } 2086 2087 strbuf_release(&refname); 2088 return ret; 2089 } 2090 2091 static int packed_fsck(struct ref_store *ref_store, 2092 struct fsck_options *o, 2093 struct worktree *wt) 2094 { 2095 struct packed_ref_store *refs = packed_downcast(ref_store, 2096 REF_STORE_READ, "fsck"); 2097 struct snapshot snapshot = { 0 }; 2098 unsigned int sorted = 0; 2099 struct stat st; 2100 int ret = 0; 2101 int fd = -1; 2102 2103 if (!is_main_worktree(wt)) 2104 goto cleanup; 2105 2106 if (o->verbose) 2107 fprintf_ln(stderr, "Checking packed-refs file %s", refs->path); 2108 2109 fd = open_nofollow(refs->path, O_RDONLY); 2110 if (fd < 0) { 2111 /* 2112 * If the packed-refs file doesn't exist, there's nothing 2113 * to check. 2114 */ 2115 if (errno == ENOENT) 2116 goto cleanup; 2117 2118 if (errno == ELOOP) { 2119 struct fsck_ref_report report = { 0 }; 2120 report.path = "packed-refs"; 2121 ret = fsck_report_ref(o, &report, 2122 FSCK_MSG_BAD_REF_FILETYPE, 2123 "not a regular file but a symlink"); 2124 goto cleanup; 2125 } 2126 2127 ret = error_errno(_("unable to open '%s'"), refs->path); 2128 goto cleanup; 2129 } else if (fstat(fd, &st) < 0) { 2130 ret = error_errno(_("unable to stat '%s'"), refs->path); 2131 goto cleanup; 2132 } else if (!S_ISREG(st.st_mode)) { 2133 struct fsck_ref_report report = { 0 }; 2134 report.path = "packed-refs"; 2135 ret = fsck_report_ref(o, &report, 2136 FSCK_MSG_BAD_REF_FILETYPE, 2137 "not a regular file"); 2138 goto cleanup; 2139 } 2140 2141 if (!allocate_snapshot_buffer(&snapshot, fd, &st)) { 2142 struct fsck_ref_report report = { 0 }; 2143 report.path = "packed-refs"; 2144 ret = fsck_report_ref(o, &report, 2145 FSCK_MSG_EMPTY_PACKED_REFS_FILE, 2146 "file is empty"); 2147 goto cleanup; 2148 } 2149 2150 ret = packed_fsck_ref_content(o, ref_store, &sorted, snapshot.start, 2151 snapshot.eof); 2152 if (!ret && sorted) 2153 ret = packed_fsck_ref_sorted(o, ref_store, snapshot.start, 2154 snapshot.eof); 2155 2156 cleanup: 2157 if (fd >= 0) 2158 close(fd); 2159 clear_snapshot_buffer(&snapshot); 2160 return ret; 2161 } 2162 2163 struct ref_storage_be refs_be_packed = { 2164 .name = "packed", 2165 .init = packed_ref_store_init, 2166 .release = packed_ref_store_release, 2167 .create_on_disk = packed_ref_store_create_on_disk, 2168 .remove_on_disk = packed_ref_store_remove_on_disk, 2169 2170 .transaction_prepare = packed_transaction_prepare, 2171 .transaction_finish = packed_transaction_finish, 2172 .transaction_abort = packed_transaction_abort, 2173 2174 .optimize = packed_optimize, 2175 .optimize_required = packed_optimize_required, 2176 2177 .rename_ref = NULL, 2178 .copy_ref = NULL, 2179 2180 .iterator_begin = packed_ref_iterator_begin, 2181 .read_raw_ref = packed_read_raw_ref, 2182 .read_symbolic_ref = NULL, 2183 2184 .reflog_iterator_begin = packed_reflog_iterator_begin, 2185 .for_each_reflog_ent = NULL, 2186 .for_each_reflog_ent_reverse = NULL, 2187 .reflog_exists = NULL, 2188 .create_reflog = NULL, 2189 .delete_reflog = NULL, 2190 .reflog_expire = NULL, 2191 2192 .fsck = packed_fsck, 2193 };