chiark / gitweb /
Stricter argument checking.
[vbig.git] / vbig.cc
diff --git a/vbig.cc b/vbig.cc
index d2cc1a2c8ce4b8b28ade90a2fb98321a8a0403e7..6717f2d383b4da9137842dd6db1d1d526759f198 100644 (file)
--- a/vbig.cc
+++ b/vbig.cc
@@ -1,6 +1,7 @@
 /*
  * This file is part of vbig.
- * Copyright (C) 2011 Richard Kettlewell
+ * Copyright (C) 2011, 2013 Richard Kettlewell
+ * Copyright (C) 2013 Ian Jackson
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -41,6 +42,7 @@ const struct option opts[] = {
   { "create", no_argument, 0, 'c' },
   { "flush", no_argument, 0, 'f' },
   { "entire", no_argument, 0, 'e' },
+  { "progress", no_argument, 0, 'p' },
   { "help", no_argument, 0, 'h' },
   { "version", no_argument, 0, 'V' },
   { 0, 0, 0, 0 },
@@ -51,7 +53,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 [OPTIONS] [--both|--verify|--create] PATH] [SIZE]\n"
          "\n"
          "Options:\n"
          "  --seed, -s        Specify random seed as string\n"
@@ -59,8 +61,10 @@ static void help(void) {
          "  --seed-length, -L Set (maximum) seed length to read from file\n"
          "  --verify, -v      Verify that PATH contains the expected contents\n"
          "  --create, -c      Create PATH with psuedo-random contents\n"
+         "  --both, -b        Do both create and verify (default)\n"
          "  --flush, -f       Flush cache\n"
          "  --entire, -e      Write until full; read until EOF\n"
+         "  --progress, -p    Show progress as we go\n"
          "  --help, -h        Display usage message\n"
          "  --version, -V     Display version string\n");
 }
@@ -72,9 +76,12 @@ enum mode_type {
   BOTH
 };
 
+static void clearprogress();
+
 // Report an error and exit
 static void fatal(int errno_value, const char *fmt, ...) {
   va_list ap;
+  clearprogress();
   fprintf(stderr, "ERROR: ");
   va_start(ap, fmt);
   vfprintf(stderr, fmt, ap);
@@ -114,6 +121,18 @@ static void flushCache(FILE *fp) {
 #endif
 }
 
+static void scale(int shift, long long &value) {
+  switch(shift) {
+  case 'K': shift = 10; break;
+  case 'M': shift = 20; break;
+  case 'G': shift = 30; break;
+  default: fatal(0, "invalid scale");
+  }
+  if(value > (LLONG_MAX >> shift))
+    fatal(0, "invalid size");
+  value <<= shift;
+}
+
 static long long execute(mode_type mode, bool entire, const char *show);
 
 static const char default_seed[] = "hexapodia as the key insight";
@@ -123,13 +142,14 @@ static const char *seedpath;
 static const char *path;
 static bool entireopt = false;
 static bool flush = false;
+static bool progress = false;
 static long long size;
 
 int main(int argc, char **argv) {
   mode_type mode = BOTH;
   int n;
   char *ep;
-  while((n = getopt_long(argc, argv, "+s:S:L:vcefhV", opts, 0)) >= 0) {
+  while((n = getopt_long(argc, argv, "+s:S:L:vcepfhV", opts, 0)) >= 0) {
     switch(n) {
     case 's': seed = optarg; seedlen = strlen(optarg); break;
     case 'S': seedpath = optarg; break;
@@ -141,6 +161,7 @@ int main(int argc, char **argv) {
     case 'v': mode = VERIFY; break;
     case 'c': mode = CREATE; break;
     case 'e': entireopt = true; break;
+    case 'p': progress = true; break;
     case 'f': flush = true; break;
     case 'h': help(); exit(0);
     case 'V': puts(VERSION); exit(0);
@@ -150,20 +171,25 @@ int main(int argc, char **argv) {
   }
   argc -= optind;
   argv += optind;
+  /* expect PATH [SIZE] */
   if(argc > 2)
     fatal(0, "excess arguments");
+  /* If --both but no SIZE, assume a block device, which is to be filled */
   if(argc == 1 && mode == BOTH)
     entireopt = true;
   if(entireopt) {
     if(argc != 1)
       fatal(0, "with --entire, size should not be specified");
   } else {
+    /* --create without --entire requires PATH SIZE
+     * --verify just requires PATH, SIZE is optional */
     if(argc < (mode == VERIFY ? 1 : 2))
       fatal(0, "insufficient arguments");
   }
   if(seed && seedpath)
     fatal(0, "both --seed and --seed-file specified");
   if(mode == BOTH && !seed && !seedpath) {
+    /* --both and no seed specified; pick a random one */
 #ifdef HAVE_RANDOM_DEVICE
     seedpath = RANDOM_DEVICE;
 #else
@@ -186,11 +212,13 @@ int main(int argc, char **argv) {
     fclose(seedfile);
   }
   if (!seed) {
+    /* No seed specified, use a constant */
     seed = (void*)default_seed;
     seedlen = sizeof(default_seed)-1;
   }
   path = argv[0];
   if(argc > 1) {
+    /* Explicit size specified */
     errno = 0;
     char *end;
     size = strtoll(argv[1], &end, 10);
@@ -198,17 +226,16 @@ int main(int argc, char **argv) {
       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");
+    if(*end) {
+      if(end[1])
+        fatal(0, "invalid scale");
+      scale(*end, size);
+    }
   } else if(entireopt) {
-    size = LONG_LONG_MAX;
+    /* Use stupidly large size as a proxy for 'infinite' */
+    size = LLONG_MAX;
   } else {
+    /* Retrieve size from target (which must exist) */
     struct stat sb;
     if(stat(path, &sb) < 0)
       fatal(errno, "stat %s", path);
@@ -224,8 +251,43 @@ int main(int argc, char **argv) {
   return 0;
 }
 
+// flush stdout, fatal on error
+static void flushstdout() {
+  if(ferror(stdout) || fflush(stdout))
+    fatal(errno, "flush stdout");
+}
+
+// clear the progress indicator
+static void clearprogress() {
+  if (!progress) return;
+  printf(" %-10s %*s   \r", "", (int)sizeof(long long)*4, "");
+  flushstdout();
+}
+
+// update progress indicator
+static void showprogress(long long amount, const char *show) {
+  if (!progress) return;
+
+  static int counter;
+  if (counter++ < 1000) return;
+  counter = 0;
+
+  int triples = sizeof(amount);
+  char rawbuf[triples*3 + 1];
+  char outbuf[triples*4 + 1];
+  snprintf(rawbuf, sizeof(rawbuf), "% *lld", (int)sizeof(rawbuf)-1, amount);
+  for (int i=0; i<triples; i++) {
+    outbuf[i*4] = ' ';
+    memcpy(outbuf + i*4 + 1, rawbuf + i*3, 3);
+  }
+  outbuf[triples*4] = 0;
+  printf(" %-10s %s...\r", outbuf, show);
+  flushstdout();
+}
+
+// write/verify the target file
 static long long execute(mode_type mode, bool entire, const char *show) {
-  Arcfour rng((const char*)seed, seedlen);
+  Arcfour rng((const uint8_t *)seed, seedlen);
   FILE *fp = fopen(path, mode == VERIFY ? "rb" : "wb");
   if(!fp)
     fatal(errno, "%s", path);
@@ -233,7 +295,7 @@ static long long execute(mode_type mode, bool entire, const char *show) {
     flushCache(fp);
   if(mode == CREATE && entire)
     setvbuf(fp, 0, _IONBF, 0);
-  char generated[4096], input[4096];
+  uint8_t generated[4096], input[4096];
   long long remain = size;
   static const size_t rc4drop = 3072; // en.wikipedia.org/wiki/RC4#Security
   assert(rc4drop <= sizeof(generated));
@@ -259,10 +321,11 @@ static long long execute(mode_type mode, bool entire, const char *show) {
       if(memcmp(generated, input, bytesRead)) {
         for(size_t n = 0; n < bytesRead; ++n)
           if(generated[n] != input[n])
-            fatal(0, "%s corrupted at %lld/%lld bytes (expected %d got %d)",
+            fatal(0, "%s: corrupted at %lld/%lld bytes (expected %d got %d)",
                     path, size - remain + n, size,
                     (unsigned char)generated[n], (unsigned char)input[n]);
       }
+      /* Truncated */
       if(bytesRead < bytesGenerated) {
        if(entire) {
          assert(feof(fp));
@@ -274,7 +337,9 @@ static long long execute(mode_type mode, bool entire, const char *show) {
       }
     }
     remain -= bytesGenerated;
+    showprogress(size - remain, mode == VERIFY ? "verifying" : "writing");
   }
+  clearprogress();
   if(mode == VERIFY && !entire && getc(fp) != EOF)
     fatal(0, "%s: extended beyond %lld bytes",
             path, size);
@@ -285,13 +350,13 @@ static long long execute(mode_type mode, bool entire, const char *show) {
   }
   if(fclose(fp) < 0)
     fatal(errno, "%s", path);
+  /* Actual size written/verified */
   long long done = size - remain;
   if(show) {
     printf("%lld bytes (%lldM, %lldG) %s\n",
           done, done >> 20, done >> 30,
           show);
-    if(ferror(stdout) || fflush(stdout))
-      fatal(errno, "flush stdout");
+    flushstdout();
   }
   return done;
 }