2 * This file is part of vbig.
3 * Copyright (C) 2011, 2013 Richard Kettlewell
4 * Copyright (C) 2013 Ian Jackson
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 #define DEFAULT_SEED_LENGTH 2048;
35 // Command line options
36 const struct option opts[] = {
37 { "seed", required_argument, 0, 's' },
38 { "seed-file", required_argument, 0, 'S' },
39 { "seed-length", required_argument, 0, 'L' },
40 { "both", no_argument, 0, 'b' },
41 { "verify", no_argument, 0, 'v' },
42 { "create", no_argument, 0, 'c' },
43 { "flush", no_argument, 0, 'f' },
44 { "entire", no_argument, 0, 'e' },
45 { "progress", no_argument, 0, 'p' },
46 { "help", no_argument, 0, 'h' },
47 { "version", no_argument, 0, 'V' },
51 // Display help message
52 static void help(void) {
53 printf("vbig - create or verify a large but pseudo-random file\n"
56 " vbig [OPTIONS] [--both|--verify|--create] PATH] [SIZE]\n"
59 " --seed, -s Specify random seed as string\n"
60 " --seed-file, -S Read random seed from (start of) this file\n"
61 " --seed-length, -L Set (maximum) seed length to read from file\n"
62 " --verify, -v Verify that PATH contains the expected contents\n"
63 " --create, -c Create PATH with psuedo-random contents\n"
64 " --both, -b Do both create and verify (default)\n"
65 " --flush, -f Flush cache\n"
66 " --entire, -e Write until full; read until EOF\n"
67 " --progress, -p Show progress as we go\n"
68 " --help, -h Display usage message\n"
69 " --version, -V Display version string\n");
72 // Possible modes of operation
79 static void clearprogress();
81 // Report an error and exit
82 static void fatal(int errno_value, const char *fmt, ...) {
85 fprintf(stderr, "ERROR: ");
87 vfprintf(stderr, fmt, ap);
90 fprintf(stderr, ": %s", strerror(errno_value));
95 // Evict whatever FP points to from RAM
96 static void flushCache(FILE *fp) {
97 // drop_caches only evicts clean pages, so first the target file is
99 if(fsync(fileno(fp)) < 0)
100 fatal(errno, "fsync");
101 #if defined DROP_CACHE_FILE
103 if((fd = open(DROP_CACHE_FILE, O_WRONLY, 0)) < 0)
104 fatal(errno, "%s", DROP_CACHE_FILE);
105 if(write(fd, "3\n", 2) < 0)
106 fatal(errno, "%s", DROP_CACHE_FILE);
108 #elif defined PURGE_COMMAND
110 if((rc = system(PURGE_COMMAND)) < 0)
111 fatal(errno, "executing %s", PURGE_COMMAND);
113 if(WIFSIGNALED(rc)) {
114 fprintf(stderr, "%s%s\n",
115 strsignal(WTERMSIG(rc)),
116 WCOREDUMP(rc) ? " (core dumped)" : "");
117 exit(WTERMSIG(rc) + 128);
119 exit(WEXITSTATUS(rc));
124 static long long execute(mode_type mode, bool entire, const char *show);
126 static const char default_seed[] = "hexapodia as the key insight";
128 static size_t seedlen;
129 static const char *seedpath;
130 static const char *path;
131 static bool entireopt = false;
132 static bool flush = false;
133 static bool progress = false;
134 static long long size;
136 int main(int argc, char **argv) {
137 mode_type mode = BOTH;
140 while((n = getopt_long(argc, argv, "+s:S:L:vcepfhV", opts, 0)) >= 0) {
142 case 's': seed = optarg; seedlen = strlen(optarg); break;
143 case 'S': seedpath = optarg; break;
145 seedlen = strtoul(optarg,&ep,0);
146 if(ep==optarg || *ep) fatal(0, "bad number for -S");
148 case 'b': mode = BOTH; break;
149 case 'v': mode = VERIFY; break;
150 case 'c': mode = CREATE; break;
151 case 'e': entireopt = true; break;
152 case 'p': progress = true; break;
153 case 'f': flush = true; break;
154 case 'h': help(); exit(0);
155 case 'V': puts(VERSION); exit(0);
157 fatal(0, "unknown option");
163 fatal(0, "excess arguments");
164 if(argc == 1 && mode == BOTH)
168 fatal(0, "with --entire, size should not be specified");
170 if(argc < (mode == VERIFY ? 1 : 2))
171 fatal(0, "insufficient arguments");
174 fatal(0, "both --seed and --seed-file specified");
175 if(mode == BOTH && !seed && !seedpath) {
176 #ifdef HAVE_RANDOM_DEVICE
177 seedpath = RANDOM_DEVICE;
179 fatal(0, "no --seed or --seed-file specified in --both mode"
180 " and random device not supported on this system");
185 seedlen = DEFAULT_SEED_LENGTH;
186 FILE *seedfile = fopen(seedpath, "rb");
188 fatal(errno, "%s", seedpath);
189 seed = malloc(seedlen);
191 fatal(errno, "allocate seed");
192 seedlen = fread(seed, 1, seedlen, seedfile);
194 fatal(errno, "read %s", seedpath);
198 seed = (void*)default_seed;
199 seedlen = sizeof(default_seed)-1;
205 size = strtoll(argv[1], &end, 10);
207 fatal(errno, "invalid size");
209 fatal(0, "invalid size");
210 if(!strcmp(end, "K"))
212 else if(!strcmp(end, "M"))
214 else if(!strcmp(end, "G"))
215 size *= 1024 * 1024 * 1024;
217 fatal(0, "invalid size");
218 } else if(entireopt) {
222 if(stat(path, &sb) < 0)
223 fatal(errno, "stat %s", path);
226 const char *show = entireopt ? (mode == CREATE ? "written" : "verified") : 0;
228 size = execute(CREATE, entireopt, 0);
229 execute(VERIFY, false, show);
231 execute(mode, entireopt, show);
236 static void flushstdout() {
237 if(ferror(stdout) || fflush(stdout))
238 fatal(errno, "flush stdout");
241 static void clearprogress() {
242 if (!progress) return;
243 printf(" %-10s %*s \r", "", (int)sizeof(long long)*4, "");
247 static void showprogress(long long amount, const char *show) {
248 if (!progress) return;
251 if (counter++ < 1000) return;
254 int triples = sizeof(amount);
255 char rawbuf[triples*3 + 1];
256 char outbuf[triples*4 + 1];
257 snprintf(rawbuf, sizeof(rawbuf), "% *lld", (int)sizeof(rawbuf)-1, amount);
258 for (int i=0; i<triples; i++) {
260 memcpy(outbuf + i*4 + 1, rawbuf + i*3, 3);
262 outbuf[triples*4] = 0;
263 printf(" %-10s %s...\r", outbuf, show);
267 static long long execute(mode_type mode, bool entire, const char *show) {
268 Arcfour rng((const char*)seed, seedlen);
269 FILE *fp = fopen(path, mode == VERIFY ? "rb" : "wb");
271 fatal(errno, "%s", path);
272 if(mode == VERIFY && flush)
274 if(mode == CREATE && entire)
275 setvbuf(fp, 0, _IONBF, 0);
276 char generated[4096], input[4096];
277 long long remain = size;
278 static const size_t rc4drop = 3072; // en.wikipedia.org/wiki/RC4#Security
279 assert(rc4drop <= sizeof(generated));
280 rng.stream(generated, rc4drop);
282 size_t bytesGenerated = (remain > (ssize_t)sizeof generated
285 rng.stream(generated, bytesGenerated);
287 size_t bytesWritten = fwrite(generated, 1, bytesGenerated, fp);
289 if(!entire || errno != ENOSPC)
290 fatal(errno, "%s", path);
291 remain -= bytesWritten;
294 assert(bytesWritten == bytesGenerated);
296 size_t bytesRead = fread(input, 1, bytesGenerated, fp);
298 fatal(errno, "%s", path);
299 if(memcmp(generated, input, bytesRead)) {
300 for(size_t n = 0; n < bytesRead; ++n)
301 if(generated[n] != input[n])
302 fatal(0, "%s corrupted at %lld/%lld bytes (expected %d got %d)",
303 path, size - remain + n, size,
304 (unsigned char)generated[n], (unsigned char)input[n]);
306 if(bytesRead < bytesGenerated) {
312 fatal(0, "%s: truncated at %lld/%lld bytes",
313 path, (size - remain + bytesRead), size);
316 remain -= bytesGenerated;
317 showprogress(size - remain, mode == VERIFY ? "verifying" : "writing");
320 if(mode == VERIFY && !entire && getc(fp) != EOF)
321 fatal(0, "%s: extended beyond %lld bytes",
323 if(mode == CREATE && flush) {
325 fatal(errno, "%s", path);
329 fatal(errno, "%s", path);
330 long long done = size - remain;
332 printf("%lld bytes (%lldM, %lldG) %s\n",
333 done, done >> 20, done >> 30,