Coverage Report

Created: 2026-01-09 07:10

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