Commit 3606d7fc8ecd88d9779619bbe6cb06f5309258cc

Stefan Sperling 2018-02-11T16:17:37

tweak the API which inflates data to a file

diff --git a/lib/delta.c b/lib/delta.c
index d999e8a..47d76d2 100644
--- a/lib/delta.c
+++ b/lib/delta.c
@@ -27,6 +27,7 @@
 #include "got_object.h"
 
 #include "delta.h"
+#include "path.h"
 #include "zb.h"
 
 #ifndef MIN
@@ -260,8 +261,13 @@ got_delta_apply(FILE *base_compressed, const uint8_t *delta_buf,
 				break;
 			if (base_file == NULL) {
 				size_t inflated_size;
-				err = got_inflate_to_tempfile(&base_file,
-				    &inflated_size, base_compressed);
+				base_file = got_opentemp();
+				if (base_file == NULL) {
+					err = got_error_from_errno();
+					break;
+				}
+				err = got_inflate_to_file(&inflated_size,
+				    base_compressed, base_file);
 				if (err)
 					break;
 				if (inflated_size != base_size) {
diff --git a/lib/pack.c b/lib/pack.c
index e169c21..9e010e0 100644
--- a/lib/pack.c
+++ b/lib/pack.c
@@ -799,30 +799,6 @@ got_packfile_open_object(struct got_object **obj, struct got_object_id *id,
 }
 
 static const struct got_error *
-dump_plain_object(FILE *infile, uint8_t type, size_t size, FILE *outfile)
-{
-	size_t n;
-
-	while (size > 0) {
-		uint8_t data[2048];
-		size_t len = MIN(size, sizeof(data));
-
-		n = fread(data, len, 1, infile);
-		if (n != 1)
-			return got_ferror(infile, GOT_ERR_BAD_PACKFILE);
-
-		n = fwrite(data, len, 1, outfile);
-		if (n != 1)
-			return got_ferror(outfile, GOT_ERR_IO);
-
-		size -= len;
-	}
-
-	rewind(outfile);
-	return NULL;
-}
-
-static const struct got_error *
 dump_delta_chain(struct got_delta_chain *deltas, FILE *outfile)
 {
 	const struct got_error *err = NULL;
@@ -922,7 +898,7 @@ got_packfile_extract_object(FILE **f, struct got_object *obj,
 			goto done;
 		}
 
-		err = dump_plain_object(packfile, obj->type, obj->size, *f);
+		err = got_inflate_to_file(&obj->size, packfile, *f);
 	} else
 		err = dump_delta_chain(&obj->deltas, *f);
 done:
diff --git a/lib/zb.c b/lib/zb.c
index afccbd6..16623c1 100644
--- a/lib/zb.c
+++ b/lib/zb.c
@@ -155,17 +155,13 @@ done:
 }
 
 const struct got_error *
-got_inflate_to_tempfile(FILE **outfile, size_t *outlen, FILE *f)
+got_inflate_to_file(size_t *outlen, FILE *infile, FILE *outfile)
 {
 	const struct got_error *err;
 	size_t avail;
 	struct got_zstream_buf zb;
 	void *newbuf;
 
-	*outfile = got_opentemp();
-	if (*outfile == NULL)
-		return got_error_from_errno();
-
 	err = got_inflate_init(&zb, 8192);
 	if (err)
 		goto done;
@@ -173,14 +169,14 @@ got_inflate_to_tempfile(FILE **outfile, size_t *outlen, FILE *f)
 	*outlen = 0;
 
 	do {
-		err = got_inflate_read(&zb, f, NULL, &avail);
+		err = got_inflate_read(&zb, infile, NULL, &avail);
 		if (err)
 			return err;
 		if (avail > 0) {
 			size_t n;
-			n = fwrite(zb.outbuf, avail, 1, *outfile);
+			n = fwrite(zb.outbuf, avail, 1, outfile);
 			if (n != 1) {
-				err = got_ferror(*outfile, GOT_ERR_IO);
+				err = got_ferror(outfile, GOT_ERR_IO);
 				goto done;
 			}
 			*outlen += avail;
@@ -188,11 +184,8 @@ got_inflate_to_tempfile(FILE **outfile, size_t *outlen, FILE *f)
 	} while (avail > 0);
 
 done:
-	if (err) {
-		fclose(*outfile);
-		*outfile = NULL;
-	} else
-		rewind(*outfile);
+	if (err == NULL)
+		rewind(outfile);
 	got_inflate_end(&zb);
 	return err;
 }
diff --git a/lib/zb.h b/lib/zb.h
index 0fc27e5..f71dcda 100644
--- a/lib/zb.h
+++ b/lib/zb.h
@@ -20,4 +20,4 @@ const struct got_error *got_inflate_read(struct got_zstream_buf *, FILE *,
 void got_inflate_end(struct got_zstream_buf *);
 const struct got_error *got_inflate_to_mem(uint8_t **, size_t *, FILE *,
     size_t);
-const struct got_error *got_inflate_to_tempfile(FILE **, size_t *, FILE *);
+const struct got_error *got_inflate_to_file(size_t *, FILE *, FILE *);