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:
parent
acd6f0d973
commit
ae285ac449
@ -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
|
||||
* searching for a loose object named "oid".
|
||||
* Map and close the given loose object fd. The path argument is used for
|
||||
* error reporting.
|
||||
*/
|
||||
static void *map_loose_object_1(struct repository *r, const char *path,
|
||||
const struct object_id *oid, unsigned long *size)
|
||||
static void *map_fd(int fd, const char *path, unsigned long *size)
|
||||
{
|
||||
void *map;
|
||||
int fd;
|
||||
void *map = NULL;
|
||||
struct stat st;
|
||||
|
||||
if (path)
|
||||
fd = git_open(path);
|
||||
else
|
||||
fd = open_loose_object(r, oid, &path);
|
||||
map = NULL;
|
||||
if (fd >= 0) {
|
||||
struct stat st;
|
||||
|
||||
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);
|
||||
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;
|
||||
}
|
||||
close(fd);
|
||||
map = xmmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0);
|
||||
}
|
||||
close(fd);
|
||||
return map;
|
||||
}
|
||||
|
||||
@ -1247,7 +1237,12 @@ void *map_loose_object(struct repository *r,
|
||||
const struct object_id *oid,
|
||||
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,
|
||||
@ -2789,13 +2784,16 @@ int read_loose_object(const char *path,
|
||||
struct object_info *oi)
|
||||
{
|
||||
int ret = -1;
|
||||
int fd;
|
||||
void *map = NULL;
|
||||
unsigned long mapsize;
|
||||
git_zstream stream;
|
||||
char hdr[MAX_HEADER_LEN];
|
||||
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) {
|
||||
error_errno(_("unable to mmap %s"), path);
|
||||
goto out;
|
||||
|
Loading…
Reference in New Issue
Block a user