From bd4d478f4284deb0069210ad75f168cf48536703 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Thu, 21 Feb 2013 15:44:49 +0000 Subject: [PATCH] New mode --both (-b), which does create then verify This implies --entire unless a size is specified. Signed-off-by: Ian Jackson --- vbig.1 | 14 ++++++++++---- vbig.cc | 19 +++++++++++++++---- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/vbig.1 b/vbig.1 index a3f963b..e6f230d 100644 --- a/vbig.1 +++ b/vbig.1 @@ -19,27 +19,33 @@ .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 \fR[\fISIZE\fR] +\fBvbig \fR[\fB--seed \fRSEED\fR] \fB--both\fR|\fB--create\fR|\fB--verify\fR \fIPATH \fR[\fISIZE\fR] .br \fBvbig --help .br \fBvbig --version .SH DESCRIPTION -\fBvbig\fR either creates a file of specified size full of predictable -psuedo-random data, or verifies that it has the expected contents. +\fBvbig\fR creates a file of specified size full of predictable +psuedo-random data, and/or verifies that it has the expected contents. The intended use is to test whether storage media really do what they 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, unless \-\-entire is specified. +it, unless \-\-entire is specified. If \fBSIZE\fR not specified when +writing-then-verifying, it is as if \fB\-\-entire\fR was specified. .SH OPTIONS .TP .B --seed\fR, \fB-s \fISEED Specifies the random seed to use. Optional. .TP +.B --both\fR, \fB-b +Selects both mode. +\fIPATH\fR will be filled with \fISIZE\fR psuedo-random bytes and +then read to check that it contains the data just written. +.TP .B --create\fR, \fB-c Selects create mode. \fIPATH\fR will be filled with \fISIZE\fR psuedo-random bytes. diff --git a/vbig.cc b/vbig.cc index 4bad930..47f314d 100644 --- a/vbig.cc +++ b/vbig.cc @@ -32,6 +32,7 @@ // Command line options const struct option opts[] = { { "seed", required_argument, 0, 's' }, + { "both", no_argument, 0, 'b' }, { "verify", no_argument, 0, 'v' }, { "create", no_argument, 0, 'c' }, { "flush", no_argument, 0, 'f' }, @@ -62,7 +63,8 @@ static void help(void) { enum mode_type { NONE, VERIFY, - CREATE + CREATE, + BOTH }; // Report an error and exit @@ -107,7 +109,7 @@ static void flushCache(FILE *fp) { #endif } -static void execute(mode_type mode, bool entire, const char *show); +static long long execute(mode_type mode, bool entire, const char *show); static const char *seed = "hexapodia as the key insight"; static const char *path; @@ -121,6 +123,7 @@ int main(int argc, char **argv) { while((n = getopt_long(argc, argv, "+s:vcefhV", opts, 0)) >= 0) { switch(n) { case 's': seed = optarg; break; + case 'b': mode = BOTH; break; case 'v': mode = VERIFY; break; case 'c': mode = CREATE; break; case 'e': entireopt = true; break; @@ -137,6 +140,8 @@ int main(int argc, char **argv) { fatal(0, "must specify one of --verify or --create"); if(argc > 2) fatal(0, "excess arguments"); + if(argc == 1 && mode == BOTH) + entireopt = true; if(entireopt) { if(argc != 1) fatal(0, "with --entire, size should not be specified"); @@ -170,11 +175,16 @@ int main(int argc, char **argv) { size = sb.st_size; } const char *show = entireopt ? (mode == CREATE ? "written" : "verified") : 0; - execute(mode, entireopt, show); + if(mode == BOTH) { + size = execute(CREATE, entireopt, 0); + execute(VERIFY, false, show); + } else { + execute(mode, entireopt, show); + } return 0; } -static void execute(mode_type mode, bool entire, const char *show) { +static long long execute(mode_type mode, bool entire, const char *show) { Arcfour rng(seed, strlen(seed)); FILE *fp = fopen(path, mode == VERIFY ? "rb" : "wb"); if(!fp) @@ -240,4 +250,5 @@ static void execute(mode_type mode, bool entire, const char *show) { if(ferror(stdout) || fflush(stdout)) fatal(errno, "flush stdout"); } + return done; } -- 2.30.2