From 13a6fb5b430998075eb63246a1a9debb8a330db4 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Mon, 7 Mar 2011 17:14:07 +1100 Subject: [PATCH] Dump the temporary file generated to emulate stdout at the end of each chunk on decompression and then truncate the file instead of writing the whole file before dumping it. --- main.c | 17 +++++++++++------ runzip.c | 5 ++++- rzip.h | 1 + 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/main.c b/main.c index 158ccaa..00aef35 100644 --- a/main.c +++ b/main.c @@ -186,12 +186,12 @@ static int open_tmpoutfile(void) } /* Dump temporary outputfile to perform stdout */ -static void dump_tmpoutfile(int fd_out) +void dump_tmpoutfile(int fd_out) { FILE *tmpoutfp; int tmpchar; - print_progress("Dumping to stdout.\n"); + print_verbose("Dumping temporary file to stdout.\n"); /* flush anything not yet in the temporary file */ fsync(fd_out); tmpoutfp = fdopen(fd_out, "r"); @@ -199,10 +199,15 @@ static void dump_tmpoutfile(int fd_out) fatal("Failed to fdopen out tmpfile: %s\n", strerror(errno)); rewind(tmpoutfp); - while ((tmpchar = fgetc(tmpoutfp)) != EOF) - putchar(tmpchar); + if (!TEST_ONLY) { + while ((tmpchar = fgetc(tmpoutfp)) != EOF) + putchar(tmpchar); + } + fflush(stdout); - fflush(control.msgout); + rewind(tmpoutfp); + if (unlikely(ftruncate(fd_out, 0))) + fatal("Failed to ftruncate fd_out in dump_tmpoutfile\n"); } /* Open a temporary inputfile to perform stdin decompression */ @@ -378,7 +383,7 @@ static void decompress_file(void) runzip_fd(fd_in, fd_out, fd_hist, expected_size); - if (STDOUT && !TEST_ONLY) + if (STDOUT) dump_tmpoutfile(fd_out); /* if we get here, no fatal errors during decompression */ diff --git a/runzip.c b/runzip.c index 49f9c59..9922523 100644 --- a/runzip.c +++ b/runzip.c @@ -242,8 +242,11 @@ i64 runzip_fd(int fd_in, int fd_out, int fd_hist, i64 expected_size) md5_init_ctx (&control.ctx); gettimeofday(&start,NULL); - while (total < expected_size) + while (total < expected_size) { total += runzip_chunk(fd_in, fd_out, fd_hist, expected_size, total); + if (STDOUT) + dump_tmpoutfile(fd_out); + } gettimeofday(&end,NULL); print_progress("\nAverage DeCompression Speed: %6.3fMB/s\n", diff --git a/rzip.h b/rzip.h index 5895752..f2c5ceb 100644 --- a/rzip.h +++ b/rzip.h @@ -334,6 +334,7 @@ const i64 two_gig; void prepare_streamout_threads(void); void close_streamout_threads(void); void round_to_page(i64 *size); +void dump_tmpoutfile(int fd_out); #define print_err(format, args...) do {\ fprintf(stderr, format, ##args); \