@@ -68,17 +68,21 @@ struct snapshot {
68
68
int mmapped ;
69
69
70
70
/*
71
- * The contents of the `packed-refs` file. If the file was
72
- * already sorted, this points at the mmapped contents of the
73
- * file. If not, this points at heap-allocated memory
74
- * containing the contents, sorted. If there were no contents
75
- * (e.g., because the file didn't exist), `buf` and `eof` are
76
- * both NULL.
71
+ * The contents of the `packed-refs` file:
72
+ *
73
+ * - buf -- a pointer to the start of the memory
74
+ * - start -- a pointer to the first byte of actual references
75
+ * (i.e., after the header line, if one is present)
76
+ * - eof -- a pointer just past the end of the reference
77
+ * contents
78
+ *
79
+ * If the `packed-refs` file was already sorted, `buf` points
80
+ * at the mmapped contents of the file. If not, it points at
81
+ * heap-allocated memory containing the contents, sorted. If
82
+ * there were no contents (e.g., because the file didn't
83
+ * exist), `buf`, `start`, and `eof` are all NULL.
77
84
*/
78
- char * buf , * eof ;
79
-
80
- /* The size of the header line, if any; otherwise, 0: */
81
- size_t header_len ;
85
+ char * buf , * start , * eof ;
82
86
83
87
/*
84
88
* What is the peeled state of the `packed-refs` file that
@@ -169,8 +173,7 @@ static void clear_snapshot_buffer(struct snapshot *snapshot)
169
173
} else {
170
174
free (snapshot -> buf );
171
175
}
172
- snapshot -> buf = snapshot -> eof = NULL ;
173
- snapshot -> header_len = 0 ;
176
+ snapshot -> buf = snapshot -> start = snapshot -> eof = NULL ;
174
177
}
175
178
176
179
/*
@@ -319,13 +322,14 @@ static void sort_snapshot(struct snapshot *snapshot)
319
322
size_t len , i ;
320
323
char * new_buffer , * dst ;
321
324
322
- pos = snapshot -> buf + snapshot -> header_len ;
325
+ pos = snapshot -> start ;
323
326
eof = snapshot -> eof ;
324
- len = eof - pos ;
325
327
326
- if (! len )
328
+ if (pos == eof )
327
329
return ;
328
330
331
+ len = eof - pos ;
332
+
329
333
/*
330
334
* Initialize records based on a crude estimate of the number
331
335
* of references in the file (we'll grow it below if needed):
@@ -391,9 +395,8 @@ static void sort_snapshot(struct snapshot *snapshot)
391
395
* place:
392
396
*/
393
397
clear_snapshot_buffer (snapshot );
394
- snapshot -> buf = new_buffer ;
398
+ snapshot -> buf = snapshot -> start = new_buffer ;
395
399
snapshot -> eof = new_buffer + len ;
396
- snapshot -> header_len = 0 ;
397
400
398
401
cleanup :
399
402
free (records );
@@ -442,14 +445,14 @@ static const char *find_end_of_record(const char *p, const char *end)
442
445
*/
443
446
static void verify_buffer_safe (struct snapshot * snapshot )
444
447
{
445
- const char * buf = snapshot -> buf + snapshot -> header_len ;
448
+ const char * start = snapshot -> start ;
446
449
const char * eof = snapshot -> eof ;
447
450
const char * last_line ;
448
451
449
- if (buf == eof )
452
+ if (start == eof )
450
453
return ;
451
454
452
- last_line = find_start_of_record (buf , eof - 1 );
455
+ last_line = find_start_of_record (start , eof - 1 );
453
456
if (* (eof - 1 ) != '\n' || eof - last_line < GIT_SHA1_HEXSZ + 2 )
454
457
die_invalid_line (snapshot -> refs -> path ,
455
458
last_line , eof - last_line );
@@ -495,18 +498,19 @@ static int load_contents(struct snapshot *snapshot)
495
498
bytes_read = read_in_full (fd , snapshot -> buf , size );
496
499
if (bytes_read < 0 || bytes_read != size )
497
500
die_errno ("couldn't read %s" , snapshot -> refs -> path );
498
- snapshot -> eof = snapshot -> buf + size ;
499
501
snapshot -> mmapped = 0 ;
500
502
break ;
501
503
case MMAP_TEMPORARY :
502
504
case MMAP_OK :
503
505
snapshot -> buf = xmmap (NULL , size , PROT_READ , MAP_PRIVATE , fd , 0 );
504
- snapshot -> eof = snapshot -> buf + size ;
505
506
snapshot -> mmapped = 1 ;
506
507
break ;
507
508
}
508
509
close (fd );
509
510
511
+ snapshot -> start = snapshot -> buf ;
512
+ snapshot -> eof = snapshot -> buf + size ;
513
+
510
514
return 1 ;
511
515
}
512
516
@@ -539,7 +543,7 @@ static const char *find_reference_location(struct snapshot *snapshot,
539
543
* preceding records all have reference names that come
540
544
* *before* `refname`.
541
545
*/
542
- const char * lo = snapshot -> buf + snapshot -> header_len ;
546
+ const char * lo = snapshot -> start ;
543
547
544
548
/*
545
549
* A pointer to a the first character of a record whose
@@ -617,8 +621,7 @@ static struct snapshot *create_snapshot(struct packed_ref_store *refs)
617
621
/* If the file has a header line, process it: */
618
622
if (snapshot -> buf < snapshot -> eof && * snapshot -> buf == '#' ) {
619
623
struct strbuf tmp = STRBUF_INIT ;
620
- char * p ;
621
- const char * eol ;
624
+ char * p , * eol ;
622
625
struct string_list traits = STRING_LIST_INIT_NODUP ;
623
626
624
627
eol = memchr (snapshot -> buf , '\n' ,
@@ -647,7 +650,7 @@ static struct snapshot *create_snapshot(struct packed_ref_store *refs)
647
650
/* perhaps other traits later as well */
648
651
649
652
/* The "+ 1" is for the LF character. */
650
- snapshot -> header_len = eol + 1 - snapshot -> buf ;
653
+ snapshot -> start = eol + 1 ;
651
654
652
655
string_list_clear (& traits , 0 );
653
656
strbuf_release (& tmp );
@@ -671,13 +674,12 @@ static struct snapshot *create_snapshot(struct packed_ref_store *refs)
671
674
* We don't want to leave the file mmapped, so we are
672
675
* forced to make a copy now:
673
676
*/
674
- size_t size = snapshot -> eof -
675
- (snapshot -> buf + snapshot -> header_len );
677
+ size_t size = snapshot -> eof - snapshot -> start ;
676
678
char * buf_copy = xmalloc (size );
677
679
678
- memcpy (buf_copy , snapshot -> buf + snapshot -> header_len , size );
680
+ memcpy (buf_copy , snapshot -> start , size );
679
681
clear_snapshot_buffer (snapshot );
680
- snapshot -> buf = buf_copy ;
682
+ snapshot -> buf = snapshot -> start = buf_copy ;
681
683
snapshot -> eof = buf_copy + size ;
682
684
}
683
685
@@ -937,7 +939,7 @@ static struct ref_iterator *packed_ref_iterator_begin(
937
939
if (prefix && * prefix )
938
940
start = find_reference_location (snapshot , prefix , 0 );
939
941
else
940
- start = snapshot -> buf + snapshot -> header_len ;
942
+ start = snapshot -> start ;
941
943
942
944
iter -> pos = start ;
943
945
iter -> eof = snapshot -> eof ;
0 commit comments