add open_loose_blob() helper
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
diff --git a/lib/object.c b/lib/object.c
index 5ccc438..3d236f4 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -1015,6 +1015,34 @@ got_object_tree_close(struct got_tree_object *tree)
free(tree);
}
+static const struct got_error *
+open_loose_blob(FILE **outfile, int fd)
+{
+ const struct got_error *err = NULL;
+ FILE *infile = NULL;
+ size_t size;
+
+ infile = fdopen(fd, "rb");
+ if (infile == NULL) {
+ err = got_error_from_errno();
+ /* fd will be closed by caller */
+ goto done;
+ }
+
+ *outfile = got_opentemp();
+ if (*outfile == NULL) {
+ err = got_error_from_errno();
+ fclose(infile);
+ goto done;
+ }
+
+ err = got_inflate_to_file(&size, infile, *outfile);
+done:
+ if (infile)
+ fclose(infile);
+ return err;
+}
+
const struct got_error *
got_object_blob_open(struct got_blob_object **blob,
struct got_repository *repo, struct got_object *obj, size_t blocksize)
@@ -1042,31 +1070,14 @@ got_object_blob_open(struct got_blob_object **blob,
goto done;
} else {
int fd;
- FILE *f = NULL;
- size_t size;
err = open_loose_object(&fd, obj, repo);
if (err)
goto done;
- f = fdopen(fd, "rb");
- if (f == NULL) {
- err = got_error_from_errno();
- close(fd);
- goto done;
- }
-
- (*blob)->f = got_opentemp();
- if ((*blob)->f == NULL) {
- err = got_error_from_errno();
- close(fd);
- fclose(f);
- goto done;
- }
-
- err = got_inflate_to_file(&size, f, (*blob)->f);
- fclose(f);
- if (err != NULL)
+ err = open_loose_blob(&(*blob)->f, fd);
+ close(fd);
+ if (err)
goto done;
}