From 7fbec0a7835aface91bfe13d03847ddbb4515e77 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Sat, 12 Mar 2011 19:56:08 +1100 Subject: [PATCH] Prepare to write compressed output by flushing stdout after each chunk is compressed. --- lrzip.c | 29 +++++++++++++++++++++++------ lrzip.h | 3 ++- lrzip_private.h | 1 + rzip.c | 5 +++++ stream.c | 4 +++- 5 files changed, 34 insertions(+), 8 deletions(-) diff --git a/lrzip.c b/lrzip.c index 2c59c94..f6f4300 100644 --- a/lrzip.c +++ b/lrzip.c @@ -42,13 +42,15 @@ #include "util.h" #include "liblrzip.h" /* flag defines */ -char *make_magic(rzip_control *control, int fd_in) +#define MAGIC_LEN (39) + +static char *make_magic(rzip_control *control, int fd_in) { struct timeval tv; struct stat st; char *magic; - magic = calloc(39, 1); + magic = calloc(MAGIC_LEN, 1); if (unlikely(!magic)) fatal("Failed to calloc magic in make_magic\n"); strcpy(magic, "LRZI"); @@ -89,6 +91,15 @@ char *make_magic(rzip_control *control, int fd_in) return magic; } +void write_stdout_header(rzip_control *control, int fd_in) +{ + char *magic = make_magic(control, fd_in); + + memcpy(control->tmp_outbuf, magic, MAGIC_LEN); + + free(magic); +} + static void write_magic(rzip_control *control, int fd_in, int fd_out) { char *magic = make_magic(control, fd_in); @@ -96,13 +107,15 @@ static void write_magic(rzip_control *control, int fd_in, int fd_out) if (unlikely(lseek(fd_out, 0, SEEK_SET))) fatal("Failed to seek to BOF to write Magic Header\n"); - if (unlikely(write(fd_out, magic, 39) != 39)) + if (unlikely(write(fd_out, magic, MAGIC_LEN) != MAGIC_LEN)) fatal("Failed to write magic header\n"); + + free(magic); } void read_magic(rzip_control *control, int fd_in, i64 *expected_size) { - char magic[39]; + char magic[MAGIC_LEN]; uint32_t v; int md5, i; @@ -198,6 +211,10 @@ int open_tmpoutfile(rzip_control *control) return fd_out; } +void flush_stdout(rzip_control *control) +{ +} + /* Dump temporary outputfile to perform stdout */ void dump_tmpoutfile(rzip_control *control, int fd_out) { @@ -666,7 +683,7 @@ static void open_tmpoutbuf(rzip_control *control) control->tmp_outbuf = malloc(control->maxram); if (unlikely(!control->tmp_outbuf)) fatal("Failed to malloc tmp_outbuf in open_tmpoutbuf\n"); - control->out_ofs = 39; + control->out_ofs = MAGIC_LEN; } /* @@ -678,7 +695,7 @@ void compress_file(rzip_control *control) * Spares a compiler warning */ int fd_in, fd_out; - char header[39]; + char header[MAGIC_LEN]; memset(header, 0, sizeof(header)); diff --git a/lrzip.h b/lrzip.h index e98a9ef..3721034 100644 --- a/lrzip.h +++ b/lrzip.h @@ -32,5 +32,6 @@ void decompress_file(rzip_control *control); void get_header_info(rzip_control *control, int fd_in, uchar *ctype, i64 *c_len, i64 *u_len, i64 *last_head); void get_fileinfo(rzip_control *control); void compress_file(rzip_control *control); -char *make_magic(rzip_control *control, int fd_in); +void write_stdout_header(rzip_control *control, int fd_in); +void flush_stdout(rzip_control *control); #endif diff --git a/lrzip_private.h b/lrzip_private.h index 83d06cc..a9c10cb 100644 --- a/lrzip_private.h +++ b/lrzip_private.h @@ -161,6 +161,7 @@ struct rzip_control { char *tmpdir; // when stdin, stdout, or test used char *tmp_outbuf; // Temporary file storage for stdout i64 out_ofs; // Output offset when tmp_outbuf in use + i64 out_len; // Total length of tmp_outbuf FILE *msgout; //stream for output messages const char *suffix; int compression_level; diff --git a/rzip.c b/rzip.c index 15bc752..cd5e430 100644 --- a/rzip.c +++ b/rzip.c @@ -958,6 +958,11 @@ retry: last.tv_usec = current.tv_usec; rzip_chunk(control, st, fd_in, fd_out, offset, pct_base, pct_multiple); + if (STDOUT) { + if (len == 0) // No header written yet + write_stdout_header(control, fd_in); + flush_stdout(control); + } /* st->chunk_size may be shrunk in rzip_chunk */ last_chunk = st->chunk_size; diff --git a/stream.c b/stream.c index b373f2e..0f64fe7 100644 --- a/stream.c +++ b/stream.c @@ -637,10 +637,12 @@ const i64 one_g = 1000 * 1024 * 1024; ssize_t put_fdout(rzip_control *control, int fd, void *offset_buf, ssize_t ret) { - if (!STDOUT) + if (!STDOUT || DECOMPRESS) return write(fd, offset_buf, (size_t)ret); memcpy(control->tmp_outbuf, offset_buf, ret); control->out_ofs += ret; + if (likely(control->out_ofs > control->out_len)) + control->out_len = control->out_ofs; return ret; }