Check for free space before compression/decompression and abort if -f option is not enabled.

This commit is contained in:
Con Kolivas 2011-02-26 23:10:28 +11:00
parent 3433438a8e
commit 8bdd5688c8
3 changed files with 30 additions and 1 deletions

15
main.c
View file

@ -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);

15
rzip.c
View file

@ -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

1
rzip.h
View file

@ -38,6 +38,7 @@
#include <bzlib.h>
#include <zlib.h>
#include <pthread.h>
#include <sys/statvfs.h>
#include <sys/resource.h>
#include <netinet/in.h>