object-file: refactor map_loose_object_1()

This function can do 3 things:
 1. Gets an fd given a path
 2. Simultaneously gets a path and fd given an OID
 3. Memory maps an fd

Keep 3 (renaming the function accordingly) and inline 1 and 2 into their
respective callers.

Signed-off-by: Jonathan Tan <jonathantanmy@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Jonathan Tan
2022-12-14 11:17:41 -08:00
committed by Junio C Hamano
parent acd6f0d973
commit ae285ac449

View File

@ -1211,35 +1211,25 @@ static int quick_has_loose(struct repository *r,
} }
/* /*
* Map the loose object at "path" if it is not NULL, or the path found by * Map and close the given loose object fd. The path argument is used for
* searching for a loose object named "oid". * error reporting.
*/ */
static void *map_loose_object_1(struct repository *r, const char *path, static void *map_fd(int fd, const char *path, unsigned long *size)
const struct object_id *oid, unsigned long *size)
{ {
void *map; void *map = NULL;
int fd; struct stat st;
if (path) if (!fstat(fd, &st)) {
fd = git_open(path); *size = xsize_t(st.st_size);
else if (!*size) {
fd = open_loose_object(r, oid, &path); /* mmap() is forbidden on empty files */
map = NULL; error(_("object file %s is empty"), path);
if (fd >= 0) { close(fd);
struct stat st; return NULL;
if (!fstat(fd, &st)) {
*size = xsize_t(st.st_size);
if (!*size) {
/* mmap() is forbidden on empty files */
error(_("object file %s is empty"), path);
close(fd);
return NULL;
}
map = xmmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0);
} }
close(fd); map = xmmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0);
} }
close(fd);
return map; return map;
} }
@ -1247,7 +1237,12 @@ void *map_loose_object(struct repository *r,
const struct object_id *oid, const struct object_id *oid,
unsigned long *size) unsigned long *size)
{ {
return map_loose_object_1(r, NULL, oid, size); const char *p;
int fd = open_loose_object(r, oid, &p);
if (fd < 0)
return NULL;
return map_fd(fd, p, size);
} }
enum unpack_loose_header_result unpack_loose_header(git_zstream *stream, enum unpack_loose_header_result unpack_loose_header(git_zstream *stream,
@ -2789,13 +2784,16 @@ int read_loose_object(const char *path,
struct object_info *oi) struct object_info *oi)
{ {
int ret = -1; int ret = -1;
int fd;
void *map = NULL; void *map = NULL;
unsigned long mapsize; unsigned long mapsize;
git_zstream stream; git_zstream stream;
char hdr[MAX_HEADER_LEN]; char hdr[MAX_HEADER_LEN];
unsigned long *size = oi->sizep; unsigned long *size = oi->sizep;
map = map_loose_object_1(the_repository, path, NULL, &mapsize); fd = git_open(path);
if (fd >= 0)
map = map_fd(fd, path, &mapsize);
if (!map) { if (!map) {
error_errno(_("unable to mmap %s"), path); error_errno(_("unable to mmap %s"), path);
goto out; goto out;