Commit Diff


commit - c7254d795765d62beabc23051fba3028bf97533e
commit + 57efb1af5959f9ae3844d2ee45e5e1970c820423
blob - 5ccc4389388e2e198f43ced7311b5c28058150d4
blob + 3d236f40ffaa89bf6170f1a07863ce6d3c07a755
--- lib/object.c
+++ lib/object.c
@@ -1014,7 +1014,35 @@ 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,32 +1070,15 @@ 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);
+		err = open_loose_blob(&(*blob)->f, fd);
+		close(fd);
+		if (err)
 			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)
-			goto done;
 	}
 
 	(*blob)->hdrlen = obj->hdrlen;