From 6ac74aa9f020f60335fadedcfcda9ad04c0016f6 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Sun, 13 Mar 2011 08:34:06 +1100 Subject: [PATCH] Create a flag to know when the temporary output buffer is in use, in preparation for when we use it on decompression. --- liblrzip.h | 1 + lrzip.c | 4 +++- lrzip_private.h | 1 + main.c | 1 + rzip.c | 2 +- stream.c | 8 ++++---- 6 files changed, 11 insertions(+), 6 deletions(-) diff --git a/liblrzip.h b/liblrzip.h index 659ad5d..ec8254b 100644 --- a/liblrzip.h +++ b/liblrzip.h @@ -46,6 +46,7 @@ #define CHECK_FILE (control->flags & FLAG_CHECK) #define KEEP_BROKEN (control->flags & FLAG_KEEP_BROKEN) #define LZO_TEST (control->flags & FLAG_THRESHOLD) +#define TMP_OUTBUF (control->flags & FLAG_TMP_OUTBUF) #define print_output(format, args...) do {\ fprintf(control->msgout, format, ##args); \ diff --git a/lrzip.c b/lrzip.c index eb32d55..f42ae1c 100644 --- a/lrzip.c +++ b/lrzip.c @@ -738,8 +738,8 @@ next_chunk: * a pseudo-temporary file */ static void open_tmpoutbuf(rzip_control *control) { + control->flags |= FLAG_TMP_OUTBUF; control->out_maxlen = control->maxram + control->page_size; - control->tmp_outbuf = malloc(control->out_maxlen); if (unlikely(!control->tmp_outbuf)) fatal("Failed to malloc tmp_outbuf in open_tmpoutbuf\n"); @@ -838,6 +838,8 @@ void compress_file(rzip_control *control) fatal("Failed to close fd_in\n"); if (unlikely(!STDOUT && close(fd_out))) fatal("Failed to close fd_out\n"); + if (TMP_OUTBUF) + free(control->tmp_outbuf); if (!KEEP_FILES) { if (unlikely(unlink(control->infile))) diff --git a/lrzip_private.h b/lrzip_private.h index 2c7ebe7..ae89e90 100644 --- a/lrzip_private.h +++ b/lrzip_private.h @@ -120,6 +120,7 @@ typedef struct md5_ctx md5_ctx; #define FLAG_CHECK (1 << 18) #define FLAG_KEEP_BROKEN (1 << 19) #define FLAG_THRESHOLD (1 << 20) +#define FLAG_TMP_OUTBUF (1 << 21) #define NO_MD5 (!(HASH_CHECK) && !(HAS_MD5)) diff --git a/main.c b/main.c index 9447c9f..9bc5044 100644 --- a/main.c +++ b/main.c @@ -75,6 +75,7 @@ #define CHECK_FILE (control.flags & FLAG_CHECK) #define KEEP_BROKEN (control.flags & FLAG_KEEP_BROKEN) #define LZO_TEST (control.flags & FLAG_THRESHOLD) +#define TMP_OUTBUF (control.flags & FLAG_TMP_OUTBUF) #define print_output(format, args...) do {\ fprintf(control.msgout, format, ##args); \ diff --git a/rzip.c b/rzip.c index e9d6b6b..409c211 100644 --- a/rzip.c +++ b/rzip.c @@ -980,7 +980,7 @@ retry: if (unlikely(write_1g(control, control->fd_out, md5_resblock, MD5_DIGEST_SIZE) != MD5_DIGEST_SIZE)) fatal("Failed to write md5 in rzip_fd\n"); - if (STDOUT) + if (TMP_OUTBUF) flush_stdout(control); gettimeofday(¤t, NULL); diff --git a/stream.c b/stream.c index 7d11133..ba49d9e 100644 --- a/stream.c +++ b/stream.c @@ -637,7 +637,7 @@ const i64 one_g = 1000 * 1024 * 1024; ssize_t put_fdout(rzip_control *control, int fd, void *offset_buf, ssize_t ret) { - if (!STDOUT || DECOMPRESS || TEST_ONLY) + if (!TMP_OUTBUF) return write(fd, offset_buf, (size_t)ret); if (unlikely(control->out_ofs + ret > control->out_maxlen)) @@ -769,7 +769,7 @@ static int seekto(rzip_control *control, struct stream_info *sinfo, i64 pos) { i64 spos = pos + sinfo->initial_pos; - if (!(DECOMPRESS || TEST_ONLY) && STDOUT) { + if (TMP_OUTBUF) { spos -= control->rel_ofs; control->out_ofs = spos; if (unlikely(spos > control->out_len || spos < 0)) { @@ -790,7 +790,7 @@ static i64 get_seek(rzip_control *control, int fd) { i64 ret; - if (!(DECOMPRESS || TEST_ONLY) && STDOUT) + if (TMP_OUTBUF) return control->rel_ofs + control->out_ofs; ret = lseek(fd, 0, SEEK_CUR); if (unlikely(ret == -1)) @@ -1107,7 +1107,7 @@ retry: if (!ctis->chunks++) { int j; - if (STDOUT) { + if (TMP_OUTBUF) { if (!control->magic_written) write_stdout_header(control); flush_stdout(control);