chiark / gitweb /
Make SIZE argument optional when verifying. It is inferred from the
authorRichard Kettlewell <rjk@greenend.org.uk>
Fri, 17 Jun 2011 19:54:19 +0000 (20:54 +0100)
committerRichard Kettlewell <rjk@greenend.org.uk>
Fri, 17 Jun 2011 19:54:19 +0000 (20:54 +0100)
file to be verified.  This is harmless when verifying storage media
but should be avoided if the filesystem implementation is under
suspicion.

vbig.1
vbig.cc

diff --git a/vbig.1 b/vbig.1
index ccd98f247ba5efc724b8785ef91987384a3d5651..603a442fa2504a84a021a2e3dd5bef9f3ed18c8d 100644 (file)
--- 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 73e6b9f0c940954483c0b8c0f7c869d1c21f64c4..3a35728aa82d115f2707b120ec1d2c5a63fa0301 100644 (file)
--- a/vbig.cc
+++ b/vbig.cc
@@ -24,6 +24,7 @@
 #include <getopt.h>
 #include <unistd.h>
 #include <fcntl.h>
+#include <sys/stat.h>
 #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)