chiark / gitweb /
Rearrange argc/argv for more idiomatic access to positional arguments.
[vbig.git] / vbig.cc
1 /*
2  * This file is part of vbig.
3  * Copyright (C) 2011 Richard Kettlewell
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 #include <config.h>
19 #include <cstdio>
20 #include <cstring>
21 #include <cstdlib>
22 #include <cerrno>
23 #include <cstdarg>
24 #include <getopt.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include "Arcfour.h"
28
29 // Command line options
30 const struct option opts[] = {
31   { "seed", required_argument, 0, 's' },
32   { "verify", no_argument, 0, 'v' },
33   { "create", no_argument, 0, 'c' },
34   { "flush", no_argument, 0, 'f' },
35   { "help", no_argument, 0, 'h' },
36   { "version", no_argument, 0, 'V' },
37   { 0, 0, 0, 0 },
38 };
39
40 // Display help message
41 static void help(void) {
42   printf("vbig - create or verify a large but pseudo-random file\n"
43          "\n"
44          "Usage:\n"
45          "  vbig [--seed SEED] --verify|--create PATH SIZE\n"
46          "\n"
47          "Options:\n"
48          "  --seed, -s     Specify random seed\n"
49          "  --verify, -v   Verify that PATH contains the expected contents\n"
50          "  --create, -c   Create PATH with psuedo-random contents\n"
51          "  --flush, -f    Flush cache\n"
52          "  --help, -h     Display usage message\n"
53          "  --version, -V  Display version string\n");
54 }
55
56 // Possible modes of operation
57 enum mode_type {
58   NONE,
59   VERIFY,
60   CREATE
61 };
62
63 // Report an error and exit
64 static void fatal(int errno_value, const char *fmt, ...) {
65   va_list ap;
66   fprintf(stderr, "ERROR: ");
67   va_start(ap, fmt);
68   vfprintf(stderr, fmt, ap);
69   va_end(ap);
70   if(errno_value)
71     fprintf(stderr, ": %s", strerror(errno_value));
72   fputc('\n', stderr);
73   exit(1);
74 }
75
76 // Evict whatever FP points to from RAM
77 static void flushCache(FILE *fp) {
78   // drop_caches only evicts clean pages, so first the target file is
79   // synced.
80   if(fsync(fileno(fp)) < 0)
81     fatal(errno, "fsync");
82 #if defined DROP_CACHE_FILE
83   int fd;
84   if((fd = open(DROP_CACHE_FILE, O_WRONLY, 0)) < 0)
85     fatal(errno, "%s", DROP_CACHE_FILE);
86   if(write(fd, "3\n", 2) < 0)
87     fatal(errno, "%s", DROP_CACHE_FILE);
88   close(fd);
89 #elif defined PURGE_COMMAND
90   int rc;
91   if((rc = system(PURGE_COMMAND)) < 0)
92     fatal(errno, "executing %s", PURGE_COMMAND);
93   else if(rc) {
94     if(WIFSIGNALED(rc)) {
95       fprintf(stderr, "%s%s\n", 
96               strsignal(WTERMSIG(rc)),
97               WCOREDUMP(rc) ? " (core dumped)" : "");
98       exit(WTERMSIG(rc) + 128);
99     } else
100       exit(WEXITSTATUS(rc));
101   }
102 #endif
103 }
104
105 int main(int argc, char **argv) {
106   const char *seed = "hexapodia as the key insight";
107   mode_type mode = NONE;
108   bool flush = false;
109   int n;
110   while((n = getopt_long(argc, argv, "+s:vcfhV", opts, 0)) >= 0) {
111     switch(n) {
112     case 's': seed = optarg; break;
113     case 'v': mode = VERIFY; break;
114     case 'c': mode = CREATE; break;
115     case 'f': flush = true; break;
116     case 'h': help(); exit(0);
117     case 'V': puts(VERSION); exit(0);
118     default:
119       fatal(0, "unknown option");
120     }
121   }
122   argc -= optind;
123   argv += optind;
124   if(mode == NONE) {
125     fatal(0, "must specify one of --verify or --create");
126     exit(1);
127   }
128   if(argc != 2) {
129     fatal(0, "must specify a path and size");
130     exit(1);
131   }
132   const char *path = argv[0];
133   errno = 0;
134   char *end;
135   long long size = strtoll(argv[1], &end, 10);
136   if(errno) {
137     fatal(errno, "invalid size");
138     exit(1);
139   }
140   if(end == argv[1]) {
141     fatal(0, "invalid size");
142     exit(1);
143   }
144   if(!strcmp(end, "K"))
145     size *= 1024;
146   else if(!strcmp(end, "M"))
147     size *= 1024 * 1024;
148   else if(!strcmp(end, "G"))
149     size *= 1024 * 1024 * 1024;
150   else if(*end) {
151     fatal(0, "invalid size");
152     exit(1);
153   } 
154   Arcfour rng(seed, strlen(seed));
155   FILE *fp = fopen(path, mode == VERIFY ? "rb" : "wb");
156   if(!fp) {
157     fatal(errno, "%s", path);
158     exit(1);
159   }
160   if(mode == VERIFY && flush)
161     flushCache(fp);
162   char generated[4096], input[4096];
163   long long remain = size;
164   while(remain > 0) {
165     size_t bytesGenerated = (remain > (ssize_t)sizeof generated
166                              ? sizeof generated
167                              : remain);
168     rng.stream(generated, bytesGenerated);
169     if(mode == CREATE) {
170       fwrite(generated, 1, bytesGenerated, fp);
171       if(ferror(fp)) {
172         fatal(errno, "%s", path);
173         exit(1);
174       }
175     } else {
176       size_t bytesRead = fread(input, 1, bytesGenerated, fp);
177       if(ferror(fp)) {
178         fatal(errno, "%s", path);
179         exit(1);
180       }
181       if(bytesRead < bytesGenerated) {
182         fatal(0, "%s: truncated at %lld/%lld bytes",
183                 path, (size - remain + bytesRead), size);
184         exit(1);
185       }
186       if(memcmp(generated, input, bytesGenerated)) {
187         for(size_t n = 0; n < bytesGenerated; ++n)
188           if(generated[n] != input[n]){
189             fatal(0, "%s corrupted at %lld/%lld bytes (expected %d got %d)",
190                     path, size - remain + n, size,
191                     (unsigned char)generated[n], (unsigned char)input[n]);
192             exit(1);
193           }
194       }
195     }
196     remain -= bytesGenerated;
197   }
198   if(mode == VERIFY && getc(fp) != EOF) {
199     fatal(0, "%s: extended beyond %lld bytes",
200             path, size);
201     exit(1);
202   }
203   if(mode == CREATE && flush) {
204     if(fflush(fp) < 0)
205       fatal(errno, "%s", path);
206     flushCache(fp);
207   }
208   if(fclose(fp) < 0) {
209     fatal(errno, "%s", path);
210     exit(1);
211   }
212   return 0;
213 }