chiark / gitweb /
New mode --both (-b), which does create then verify
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Thu, 21 Feb 2013 15:44:49 +0000 (15:44 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Thu, 21 Feb 2013 17:57:52 +0000 (17:57 +0000)
This implies --entire unless a size is specified.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
vbig.1
vbig.cc

diff --git a/vbig.1 b/vbig.1
index a3f963ba76034c9e8fa9af4bd7e1e8febbefd7c2..e6f230d80e8779572917a89c981d9a5141140e0d 100644 (file)
--- a/vbig.1
+++ b/vbig.1
 .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 4bad93029254361c95c03855124b30f8d9eabea4..47f314db82ea55ead29b5c710185ffd645473baa 100644 (file)
--- 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;
 }