tweak the API which inflates data to a file
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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
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 *);