Skip to content

Commit ae285ac

Browse files
jonathantanmygitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent acd6f0d commit ae285ac

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

object-file.c

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,43 +1211,38 @@ static int quick_has_loose(struct repository *r,
12111211
}
12121212

12131213
/*
1214-
* Map the loose object at "path" if it is not NULL, or the path found by
1215-
* searching for a loose object named "oid".
1214+
* Map and close the given loose object fd. The path argument is used for
1215+
* error reporting.
12161216
*/
1217-
static void *map_loose_object_1(struct repository *r, const char *path,
1218-
const struct object_id *oid, unsigned long *size)
1217+
static void *map_fd(int fd, const char *path, unsigned long *size)
12191218
{
1220-
void *map;
1221-
int fd;
1222-
1223-
if (path)
1224-
fd = git_open(path);
1225-
else
1226-
fd = open_loose_object(r, oid, &path);
1227-
map = NULL;
1228-
if (fd >= 0) {
1229-
struct stat st;
1219+
void *map = NULL;
1220+
struct stat st;
12301221

1231-
if (!fstat(fd, &st)) {
1232-
*size = xsize_t(st.st_size);
1233-
if (!*size) {
1234-
/* mmap() is forbidden on empty files */
1235-
error(_("object file %s is empty"), path);
1236-
close(fd);
1237-
return NULL;
1238-
}
1239-
map = xmmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0);
1222+
if (!fstat(fd, &st)) {
1223+
*size = xsize_t(st.st_size);
1224+
if (!*size) {
1225+
/* mmap() is forbidden on empty files */
1226+
error(_("object file %s is empty"), path);
1227+
close(fd);
1228+
return NULL;
12401229
}
1241-
close(fd);
1230+
map = xmmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0);
12421231
}
1232+
close(fd);
12431233
return map;
12441234
}
12451235

12461236
void *map_loose_object(struct repository *r,
12471237
const struct object_id *oid,
12481238
unsigned long *size)
12491239
{
1250-
return map_loose_object_1(r, NULL, oid, size);
1240+
const char *p;
1241+
int fd = open_loose_object(r, oid, &p);
1242+
1243+
if (fd < 0)
1244+
return NULL;
1245+
return map_fd(fd, p, size);
12511246
}
12521247

12531248
enum unpack_loose_header_result unpack_loose_header(git_zstream *stream,
@@ -2789,13 +2784,16 @@ int read_loose_object(const char *path,
27892784
struct object_info *oi)
27902785
{
27912786
int ret = -1;
2787+
int fd;
27922788
void *map = NULL;
27932789
unsigned long mapsize;
27942790
git_zstream stream;
27952791
char hdr[MAX_HEADER_LEN];
27962792
unsigned long *size = oi->sizep;
27972793

2798-
map = map_loose_object_1(the_repository, path, NULL, &mapsize);
2794+
fd = git_open(path);
2795+
if (fd >= 0)
2796+
map = map_fd(fd, path, &mapsize);
27992797
if (!map) {
28002798
error_errno(_("unable to mmap %s"), path);
28012799
goto out;

0 commit comments

Comments
 (0)