From: Richard Kettlewell Date: Fri, 17 Jun 2011 19:54:19 +0000 (+0100) Subject: Make SIZE argument optional when verifying. It is inferred from the X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=1f1867e7bd404685a2e651425e7cf27fd3fceb2d;p=vbig.git Make SIZE argument optional when verifying. It is inferred from the file to be verified. This is harmless when verifying storage media but should be avoided if the filesystem implementation is under suspicion. --- diff --git a/vbig.1 b/vbig.1 index ccd98f2..603a442 100644 --- a/vbig.1 +++ b/vbig.1 @@ -19,7 +19,7 @@ .SH NAME vbig \- create or verify a large but pseudo-random file .SH SYNOPSIS -\fBvbig \fR[\fB--seed \fRSEED\fR] \fB--create\fR|\fB--verify \fIPATH SIZE +\fBvbig \fR[\fB--seed \fRSEED\fR] \fB--create\fR|\fB--verify \fIPATH \fR[\fISIZE\fR] .br \fBvbig --help .br @@ -32,6 +32,8 @@ say on the tin. .PP \fISIZE\fR may end with \fBK\fR, \fBM\fR or \fBG\fR to select (binary) kilobytes, megabytes or gigabytes. +The size is mandatory when creating a file but optional when verifying +it. .SH OPTIONS .TP .B --seed\fR, \fB-s \fISEED diff --git a/vbig.cc b/vbig.cc index 73e6b9f..3a35728 100644 --- a/vbig.cc +++ b/vbig.cc @@ -24,6 +24,7 @@ #include #include #include +#include #include "Arcfour.h" // Command line options @@ -42,7 +43,7 @@ static void help(void) { printf("vbig - create or verify a large but pseudo-random file\n" "\n" "Usage:\n" - " vbig [--seed SEED] --verify|--create PATH SIZE\n" + " vbig [--seed SEED] --verify|--create PATH [SIZE]\n" "\n" "Options:\n" " --seed, -s Specify random seed\n" @@ -123,24 +124,34 @@ int main(int argc, char **argv) { argv += optind; if(mode == NONE) fatal(0, "must specify one of --verify or --create"); - if(argc != 2) - fatal(0, "must specify a path and size"); + if(argc > 2) + fatal(0, "excess arguments"); + if(argc < (mode == VERIFY ? 1 : 2)) + fatal(0, "insufficient arguments"); const char *path = argv[0]; - errno = 0; - char *end; - long long size = strtoll(argv[1], &end, 10); - if(errno) - fatal(errno, "invalid size"); - if(end == argv[1]) - fatal(0, "invalid size"); - if(!strcmp(end, "K")) - size *= 1024; - else if(!strcmp(end, "M")) - size *= 1024 * 1024; - else if(!strcmp(end, "G")) - size *= 1024 * 1024 * 1024; - else if(*end) - fatal(0, "invalid size"); + long long size; + if(argc > 1) { + errno = 0; + char *end; + size = strtoll(argv[1], &end, 10); + if(errno) + fatal(errno, "invalid size"); + if(end == argv[1]) + fatal(0, "invalid size"); + if(!strcmp(end, "K")) + size *= 1024; + else if(!strcmp(end, "M")) + size *= 1024 * 1024; + else if(!strcmp(end, "G")) + size *= 1024 * 1024 * 1024; + else if(*end) + fatal(0, "invalid size"); + } else { + struct stat sb; + if(stat(path, &sb) < 0) + fatal(errno, "stat %s", path); + size = sb.st_size; + } Arcfour rng(seed, strlen(seed)); FILE *fp = fopen(path, mode == VERIFY ? "rb" : "wb"); if(!fp)