Skip to content

Commit a0dee3f

Browse files
shejialuogitster
authored andcommitted
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 <[email protected]> Suggested-by: Patrick Steinhardt <[email protected]> Signed-off-by: shejialuo <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 784cecc commit a0dee3f

File tree

1 file changed

+31
-22
lines changed

1 file changed

+31
-22
lines changed

refs/packed-backend.c

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,32 @@ static int refname_contains_nul(struct strbuf *refname)
517517

518518
#define SMALL_FILE_SIZE (32*1024)
519519

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+
520546
/*
521547
* Depending on `mmap_strategy`, either mmap or read the contents of
522548
* the `packed-refs` file into the snapshot. Return 1 if the file
@@ -525,10 +551,9 @@ static int refname_contains_nul(struct strbuf *refname)
525551
*/
526552
static int load_contents(struct snapshot *snapshot)
527553
{
528-
int fd;
529554
struct stat st;
530-
size_t size;
531-
ssize_t bytes_read;
555+
int ret;
556+
int fd;
532557

533558
fd = open(snapshot->refs->path, O_RDONLY);
534559
if (fd < 0) {
@@ -550,27 +575,11 @@ static int load_contents(struct snapshot *snapshot)
550575

551576
if (fstat(fd, &st) < 0)
552577
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);
569578

570-
snapshot->start = snapshot->buf;
571-
snapshot->eof = snapshot->buf + size;
579+
ret = allocate_snapshot_buffer(snapshot, fd, &st);
572580

573-
return 1;
581+
close(fd);
582+
return ret;
574583
}
575584

576585
static const char *find_reference_location_1(struct snapshot *snapshot,

0 commit comments

Comments
 (0)