packed-backend: extract snapshot allocation in `load_contents`
"load_contents" would choose which way to load the content of the "packed-refs". However, we cannot directly use this function when checking the consistency due to we don't want to open the file. And we also need to reuse the logic to avoid causing repetition. Let's create a new helper function "allocate_snapshot_buffer" to extract the snapshot allocation logic in "load_contents" and update the "load_contents" to align with the behavior. Suggested-by: Jeff King <peff@peff.net> Suggested-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: shejialuo <shejialuo@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
shejialuo committed
May 14, 2025 at 23:50 UTC
a0dee3f74b4f42076b7c23ca6d9aca61ed064e82
1 file changed
+31
-22
refs/packed-backend.c
+31
-22
@@ -517,6 +517,32 @@ static int refname_contains_nul(struct strbuf *refname)
517
518
#define SMALL_FILE_SIZE (32*1024)
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
+
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
@@ -525,10 +551,9 @@ static int refname_contains_nul(struct strbuf *refname)
551
*/
552
static int load_contents(struct snapshot *snapshot)
553
{
528
- int fd;
554
struct stat st;
530
- size_t size;
531
- ssize_t bytes_read;
555
+ int ret;
556
+ int fd;
557
558
fd = open(snapshot->refs->path, O_RDONLY);
559
if (fd < 0) {
@@ -550,27 +575,11 @@ static int load_contents(struct snapshot *snapshot)
575
576
if (fstat(fd, &st) < 0)
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);
578
570
- snapshot->start = snapshot->buf;
571
- snapshot->eof = snapshot->buf + size;
579
+ ret = allocate_snapshot_buffer(snapshot, fd, &st);
580
573
- return 1;
581
+ close(fd);
582
+ return ret;
583
}
584
585
static const char *find_reference_location_1(struct snapshot *snapshot,