diff --git a/main.c b/main.c index d285a92..b3db942 100644 --- a/main.c +++ b/main.c @@ -291,12 +291,13 @@ static void decompress_file(void) if (!NO_SET_PERMS) preserve_perms(fd_in, fd_out); - if (fd_hist == -1) - fatal("Failed to open history file %s\n", control.outfile); } else fd_out = open_tmpoutfile(); fd_hist = open(control.outfile, O_RDONLY); + if (fd_hist == -1) + fatal("Failed to open history file %s\n", control.outfile); + read_magic(fd_in, &expected_size); print_progress("Decompressing..."); diff --git a/rzip.c b/rzip.c index 818845a..7a6a8e1 100644 --- a/rzip.c +++ b/rzip.c @@ -583,8 +583,6 @@ static void rzip_chunk(struct rzip_state *st, int fd_in, int fd_out, i64 offset, continue; } print_maxverbose("Preallocated %lld ram...\n", prealloc_size); - if (!memset(buf, 0, prealloc_size)) - fatal("Failed to memset in rzip_chunk\n"); if (!STDIN) { /* STDIN will use this already allocated ram */ if (munmap(buf, prealloc_size) != 0) diff --git a/stream.c b/stream.c index 2610137..2875369 100644 --- a/stream.c +++ b/stream.c @@ -600,6 +600,7 @@ void *open_stream_out(int f, int n, i64 limit) { unsigned cwindow = control.window; struct stream_info *sinfo; + uchar *testmalloc; int i; sinfo = malloc(sizeof(*sinfo)); @@ -642,6 +643,18 @@ void *open_stream_out(int f, int n, i64 limit) return NULL; } + /* Find the largest we can make the window based on ability to malloc + * ram. We need enough for the 2 streams and for the compression + * backend at most, being conservative. */ +retest_malloc: + testmalloc = malloc(sinfo->bufsize * (n + 1)); + if (!testmalloc) { + sinfo->bufsize = sinfo->bufsize / 10 * 9; + goto retest_malloc; + } + free(testmalloc); + print_maxverbose("Succeeded to malloc for compression bufsize of %lld\n", sinfo->bufsize); + for (i = 0; i < n; i++) { sinfo->s[i].buf = malloc(sinfo->bufsize); if (!sinfo->s[i].buf) @@ -783,6 +796,7 @@ static int flush_buffer(struct stream_info *sinfo, int stream) if (write_buf(sinfo->fd, sinfo->s[stream].buf, c_len) != 0) return -1; + fsync(sinfo->fd); sinfo->cur_pos += c_len; sinfo->s[stream].buflen = 0;