make open_loose_object return a file descriptor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
diff --git a/lib/object.c b/lib/object.c
index 23a4cf3..425c26d 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -309,7 +309,7 @@ object_path(char **path, struct got_object_id *id, struct got_repository *repo)
}
static const struct got_error *
-open_loose_object(FILE **f, struct got_object *obj, struct got_repository *repo)
+open_loose_object(int *fd, struct got_object *obj, struct got_repository *repo)
{
const struct got_error *err = NULL;
char *path;
@@ -317,8 +317,8 @@ open_loose_object(FILE **f, struct got_object *obj, struct got_repository *repo)
err = object_path(&path, &obj->id, repo);
if (err)
return err;
- *f = fopen(path, "rb");
- if (*f == NULL) {
+ *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
+ if (*fd == -1) {
err = got_error_from_errno();
goto done;
}
@@ -728,9 +728,16 @@ got_object_commit_open(struct got_commit_object **commit,
free(buf);
} else {
FILE *f;
- err = open_loose_object(&f, obj, repo);
+ int fd;
+ err = open_loose_object(&fd, obj, repo);
if (err)
return err;
+ f = fdopen(fd, "rb");
+ if (f == NULL) {
+ err = got_error_from_errno();
+ close(fd);
+ return err;
+ }
err = read_commit_object(commit, repo, obj, f);
fclose(f);
}
@@ -804,11 +811,18 @@ got_object_tree_open(struct got_tree_object **tree,
free(buf);
} else {
FILE *f;
- err = open_loose_object(&f, obj, repo);
+ int fd;
+ err = open_loose_object(&fd, obj, repo);
if (err)
return err;
+ f = fdopen(fd, "rb");
+ if (f == NULL) {
+ close(fd);
+ return got_error_from_errno();
+ }
err = read_tree_object(tree, repo, obj, f);
fclose(f);
+ close(fd);
}
return err;
}
@@ -859,12 +873,20 @@ got_object_blob_open(struct got_blob_object **blob,
return err;
}
} else {
- err = open_loose_object(&((*blob)->f), obj, repo);
+ int fd;
+ err = open_loose_object(&fd, obj, repo);
if (err) {
free(*blob);
*blob = NULL;
return err;
}
+ (*blob)->f = fdopen(fd, "rb");
+ if ((*blob)->f == NULL) {
+ free(*blob);
+ *blob = NULL;
+ close(fd);
+ return err;
+ }
err = got_inflate_init(&(*blob)->zb, NULL, blocksize);
if (err != NULL) {