diff options
author | Patrick McDermott <patrick.mcdermott@libiquity.com> | 2023-05-07 04:48:47 (EDT) |
---|---|---|
committer | Patrick McDermott <patrick.mcdermott@libiquity.com> | 2023-05-07 07:14:13 (EDT) |
commit | 34b2d80ed29b6d914429d95ed1229cec03307172 (patch) | |
tree | 03134df15fdc39ebb85fbe159d560f14c388db0d | |
parent | e473ff37e0febb0c0dda71d6d277cc833bc9f7a3 (diff) |
gzip: Write compressed output to file directly
-rw-r--r-- | src/gzip.c | 50 | ||||
-rw-r--r-- | src/gzip.h | 9 | ||||
-rw-r--r-- | src/opk/write.c | 17 |
3 files changed, 22 insertions, 54 deletions
@@ -17,6 +17,7 @@ * along with opkg-opkg. If not, see <http://www.gnu.org/licenses/>. */ +#include <stdio.h> #include <stdlib.h> #include <zlib.h> #include "defs.h" @@ -31,14 +32,13 @@ enum _opkg_opk_gzip_dir { }; struct opkg_opk_gzip { - enum _opkg_opk_gzip_dir dir; - char *buffer; - size_t buffer_size; - opkg_opk_gzip_read_func *read_func; - opkg_opk_gzip_write_func *write_func; - void *user_data; - z_stream stream; - gz_header gz_header; + enum _opkg_opk_gzip_dir dir; + opkg_opk_gzip_read_func *read_func; + void *user_data; + FILE *write_fp; + char write_buffer[512]; + z_stream stream; + gz_header gz_header; }; struct opkg_opk_gzip * @@ -70,7 +70,7 @@ opkg_opk_gzip_init_read(opkg_opk_gzip_read_func *read_func, void *user_data) } struct opkg_opk_gzip * -opkg_opk_gzip_init_write(opkg_opk_gzip_write_func *write_func, void *user_data) +opkg_opk_gzip_init_write(FILE *write_fp) { struct opkg_opk_gzip *gzip; @@ -79,9 +79,8 @@ opkg_opk_gzip_init_write(opkg_opk_gzip_write_func *write_func, void *user_data) return NULL; } - gzip->dir = _OPKG_OPK_GZIP_DIR_WRITE; - gzip->write_func = write_func; - gzip->user_data = user_data; + gzip->dir = _OPKG_OPK_GZIP_DIR_WRITE; + gzip->write_fp = write_fp; gzip->stream.zalloc = Z_NULL; gzip->stream.zfree = Z_NULL; @@ -112,15 +111,6 @@ opkg_opk_gzip_init_write(opkg_opk_gzip_write_func *write_func, void *user_data) } int -opkg_opk_gzip_set_write_buffer(struct opkg_opk_gzip *gzip, char *buffer, - size_t size) -{ - gzip->buffer = buffer; - gzip->buffer_size = size; - return OPKG_OPK_OK; -} - -int opkg_opk_gzip_read(struct opkg_opk_gzip *gzip, void *record) { int end; @@ -179,6 +169,8 @@ static int _opkg_opk_gzip_write(struct opkg_opk_gzip *gzip, void *record, size_t size, int last) { + size_t len; + /* Sanity check */ if (gzip->dir != _OPKG_OPK_GZIP_DIR_WRITE) { return OPKG_OPK_ERROR; @@ -188,8 +180,8 @@ _opkg_opk_gzip_write(struct opkg_opk_gzip *gzip, void *record, size_t size, gzip->stream.avail_in = size; do { - gzip->stream.next_out = gzip->buffer; - gzip->stream.avail_out = gzip->buffer_size; + gzip->stream.next_out = gzip->write_buffer; + gzip->stream.avail_out = sizeof(gzip->write_buffer); switch (deflate(&gzip->stream, (last > 0 ? Z_FINISH : Z_NO_FLUSH))) { case Z_OK: @@ -200,15 +192,9 @@ _opkg_opk_gzip_write(struct opkg_opk_gzip *gzip, void *record, size_t size, return OPKG_OPK_ERROR; } /* Process output buffer. */ - switch (gzip->write_func(gzip->user_data, - gzip->buffer_size - - gzip->stream.avail_out)) { - case OPKG_OPK_OK: - break; - case OPKG_OPK_END: - case OPKG_OPK_ERROR: - default: - return OPKG_OPK_ERROR; + len = sizeof(gzip->write_buffer) - gzip->stream.avail_out; + if (fwrite(gzip->write_buffer, 1, len, gzip->write_fp) != len) { + return OPKG_OPK_ERROR; } } while (gzip->stream.avail_out == 0); @@ -20,10 +20,11 @@ #ifndef OPKG_OPK_GZIP_H_ #define OPKG_OPK_GZIP_H_ +#include <stdio.h> + struct opkg_opk_gzip; typedef int (opkg_opk_gzip_read_func)(void *, char **, size_t *); -typedef int (opkg_opk_gzip_write_func)(void *, size_t); /* * Allocates and initializes a decompression structure. @@ -45,11 +46,7 @@ struct opkg_opk_gzip * opkg_opk_gzip_init_read(opkg_opk_gzip_read_func *read_func, void *user_data); struct opkg_opk_gzip * -opkg_opk_gzip_init_write(opkg_opk_gzip_write_func *write_func, void *user_data); - -int -opkg_opk_gzip_set_write_buffer(struct opkg_opk_gzip *gzip, char *buffer, - size_t size); +opkg_opk_gzip_init_write(FILE *write_fp); /* * Reads and decompresses data to output the next record (512 octets). diff --git a/src/opk/write.c b/src/opk/write.c index 5ddd502..2a38f79 100644 --- a/src/opk/write.c +++ b/src/opk/write.c @@ -31,18 +31,6 @@ #include "opk.h" static int -_opkg_opk_opk_write_file(void *user_data, size_t size) -{ - struct opkg_opk_opk *opk = user_data; - - if (fwrite(opk->file_buffer, 1, size, opk->file) == size) { - return OPKG_OPK_OK; - } else { - return OPKG_OPK_ERROR; - } -} - -static int _opkg_opk_opk_source_date_epoch(uint64_t *mtime) { char *env; @@ -87,15 +75,12 @@ opkg_opk_opk_write(struct opkg_opk_opk *opk, const char *file_name) } /* Initialize outer gzip decompressor. */ - opk->outer_gzip = opkg_opk_gzip_init_write(&_opkg_opk_opk_write_file, - opk); + opk->outer_gzip = opkg_opk_gzip_init_write(opk->file); if (opk->outer_gzip == NULL) { fputs(_("Error: Failed to initialize\n"), stderr); ret = OPKG_OPK_ERROR; goto out1; } - opkg_opk_gzip_set_write_buffer(opk->outer_gzip, opk->file_buffer, - sizeof(opk->file_buffer)); /* Initialize outer ustar unarchiver. */ opk->outer_ustar = opkg_opk_ustar_init(opk->outer_gzip); |