From 8bdd5688c8971611830b1c680dd68f727f3265c9 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Sat, 26 Feb 2011 23:10:28 +1100 Subject: [PATCH] Check for free space before compression/decompression and abort if -f option is not enabled. --- main.c | 15 ++++++++++++++- rzip.c | 15 +++++++++++++++ rzip.h | 1 + 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/main.c b/main.c index c083c48..cf7e676 100644 --- a/main.c +++ b/main.c @@ -255,7 +255,8 @@ static void decompress_file(void) { char *tmp, *tmpoutfile, *infilecopy = NULL; int fd_in, fd_out = -1, fd_hist = -1; - i64 expected_size; + i64 expected_size, free_space; + struct statvfs fbuf; if (!STDIN) { if ((tmp = strrchr(control.infile, '.')) && strcmp(tmp,control.suffix)) { @@ -320,6 +321,18 @@ static void decompress_file(void) } } + /* Check if there's enough free space on the device chosen to fit the + * decompressed file. */ + if (unlikely(fstatvfs(fd_in, &fbuf))) + fatal("Failed to fstatvfs in decompress_file\n"); + free_space = fbuf.f_bsize * fbuf.f_bavail; + if (free_space < expected_size) { + if (FORCE_REPLACE) + print_err("Warning, inadequate free space detected, but attempting to decompress due to -f option being used.\n"); + else + failure("Inadequate free space to decompress file, use -f to override.\n"); + } + if (!(TEST_ONLY | STDOUT)) { if (FORCE_REPLACE) fd_out = open(control.outfile, O_WRONLY | O_CREAT | O_TRUNC, 0666); diff --git a/rzip.c b/rzip.c index 08c4d99..13f8a72 100644 --- a/rzip.c +++ b/rzip.c @@ -740,6 +740,8 @@ void rzip_fd(int fd_in, int fd_out) elapsed_minutes, elapsed_seconds; double finish_time, elapsed_time, chunkmbs; char md5_resblock[MD5_DIGEST_SIZE]; + struct statvfs fbuf; + i64 free_space; md5_init_ctx (&control.ctx); @@ -761,6 +763,19 @@ void rzip_fd(int fd_in, int fd_out) } else control.st_size = 0; + /* Check if there's enough free space on the device chosen to fit the + * compressed file, based on the compressed file being as large as the + * uncompressed file. */ + if (unlikely(fstatvfs(fd_in, &fbuf))) + fatal("Failed to fstatvfs in decompress_file\n"); + free_space = fbuf.f_bsize * fbuf.f_bavail; + if (free_space < control.st_size) { + if (FORCE_REPLACE) + print_err("Warning, possibly inadequate free space detected, but attempting to compress due to -f option being used.\n"); + else + failure("Possibly inadequate free space to compress file, use -f to override.\n"); + } + /* Optimal use of ram involves using no more than 2/3 of it, so we * allocate 1/3 of it to the main buffer and use a sliding mmap * buffer to work on 2/3 ram size, leaving enough ram for the diff --git a/rzip.h b/rzip.h index 7461869..2de50bd 100644 --- a/rzip.h +++ b/rzip.h @@ -38,6 +38,7 @@ #include #include #include +#include #include #include