@@ -517,6 +517,32 @@ static int refname_contains_nul(struct strbuf *refname)
517
517
518
518
#define SMALL_FILE_SIZE (32*1024)
519
519
520
+ static int allocate_snapshot_buffer (struct snapshot * snapshot , int fd , struct stat * st )
521
+ {
522
+ ssize_t bytes_read ;
523
+ size_t size ;
524
+
525
+ size = xsize_t (st -> st_size );
526
+ if (!size )
527
+ return 0 ;
528
+
529
+ if (mmap_strategy == MMAP_NONE || size <= SMALL_FILE_SIZE ) {
530
+ snapshot -> buf = xmalloc (size );
531
+ bytes_read = read_in_full (fd , snapshot -> buf , size );
532
+ if (bytes_read < 0 || bytes_read != size )
533
+ die_errno ("couldn't read %s" , snapshot -> refs -> path );
534
+ snapshot -> mmapped = 0 ;
535
+ } else {
536
+ snapshot -> buf = xmmap (NULL , size , PROT_READ , MAP_PRIVATE , fd , 0 );
537
+ snapshot -> mmapped = 1 ;
538
+ }
539
+
540
+ snapshot -> start = snapshot -> buf ;
541
+ snapshot -> eof = snapshot -> buf + size ;
542
+
543
+ return 1 ;
544
+ }
545
+
520
546
/*
521
547
* Depending on `mmap_strategy`, either mmap or read the contents of
522
548
* the `packed-refs` file into the snapshot. Return 1 if the file
@@ -525,10 +551,9 @@ static int refname_contains_nul(struct strbuf *refname)
525
551
*/
526
552
static int load_contents (struct snapshot * snapshot )
527
553
{
528
- int fd ;
529
554
struct stat st ;
530
- size_t size ;
531
- ssize_t bytes_read ;
555
+ int ret ;
556
+ int fd ;
532
557
533
558
fd = open (snapshot -> refs -> path , O_RDONLY );
534
559
if (fd < 0 ) {
@@ -550,27 +575,11 @@ static int load_contents(struct snapshot *snapshot)
550
575
551
576
if (fstat (fd , & st ) < 0 )
552
577
die_errno ("couldn't stat %s" , snapshot -> refs -> path );
553
- size = xsize_t (st .st_size );
554
-
555
- if (!size ) {
556
- close (fd );
557
- return 0 ;
558
- } else if (mmap_strategy == MMAP_NONE || size <= SMALL_FILE_SIZE ) {
559
- snapshot -> buf = xmalloc (size );
560
- bytes_read = read_in_full (fd , snapshot -> buf , size );
561
- if (bytes_read < 0 || bytes_read != size )
562
- die_errno ("couldn't read %s" , snapshot -> refs -> path );
563
- snapshot -> mmapped = 0 ;
564
- } else {
565
- snapshot -> buf = xmmap (NULL , size , PROT_READ , MAP_PRIVATE , fd , 0 );
566
- snapshot -> mmapped = 1 ;
567
- }
568
- close (fd );
569
578
570
- snapshot -> start = snapshot -> buf ;
571
- snapshot -> eof = snapshot -> buf + size ;
579
+ ret = allocate_snapshot_buffer (snapshot , fd , & st );
572
580
573
- return 1 ;
581
+ close (fd );
582
+ return ret ;
574
583
}
575
584
576
585
static const char * find_reference_location_1 (struct snapshot * snapshot ,
@@ -2059,7 +2068,7 @@ static int packed_fsck(struct ref_store *ref_store,
2059
2068
{
2060
2069
struct packed_ref_store * refs = packed_downcast (ref_store ,
2061
2070
REF_STORE_READ , "fsck" );
2062
- struct strbuf packed_ref_content = STRBUF_INIT ;
2071
+ struct snapshot snapshot = { 0 } ;
2063
2072
unsigned int sorted = 0 ;
2064
2073
struct stat st ;
2065
2074
int ret = 0 ;
@@ -2103,21 +2112,25 @@ static int packed_fsck(struct ref_store *ref_store,
2103
2112
goto cleanup ;
2104
2113
}
2105
2114
2106
- if (strbuf_read (& packed_ref_content , fd , 0 ) < 0 ) {
2107
- ret = error_errno (_ ("unable to read '%s'" ), refs -> path );
2115
+ if (!allocate_snapshot_buffer (& snapshot , fd , & st )) {
2116
+ struct fsck_ref_report report = { 0 };
2117
+ report .path = "packed-refs" ;
2118
+ ret = fsck_report_ref (o , & report ,
2119
+ FSCK_MSG_EMPTY_PACKED_REFS_FILE ,
2120
+ "file is empty" );
2108
2121
goto cleanup ;
2109
2122
}
2110
2123
2111
- ret = packed_fsck_ref_content (o , ref_store , & sorted , packed_ref_content . buf ,
2112
- packed_ref_content . buf + packed_ref_content . len );
2124
+ ret = packed_fsck_ref_content (o , ref_store , & sorted , snapshot . start ,
2125
+ snapshot . eof );
2113
2126
if (!ret && sorted )
2114
- ret = packed_fsck_ref_sorted (o , ref_store , packed_ref_content . buf ,
2115
- packed_ref_content . buf + packed_ref_content . len );
2127
+ ret = packed_fsck_ref_sorted (o , ref_store , snapshot . start ,
2128
+ snapshot . eof );
2116
2129
2117
2130
cleanup :
2118
2131
if (fd >= 0 )
2119
2132
close (fd );
2120
- strbuf_release ( & packed_ref_content );
2133
+ clear_snapshot_buffer ( & snapshot );
2121
2134
return ret ;
2122
2135
}
2123
2136
0 commit comments