2 * This file is part of vbig.
3 * Copyright (C) 2011 Richard Kettlewell
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.
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.
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/>.
32 #define DEFAULT_SEED_LENGTH 2048;
34 // Command line options
35 const struct option opts[] = {
36 { "seed", required_argument, 0, 's' },
37 { "seed-file", required_argument, 0, 'S' },
38 { "seed-length", required_argument, 0, 'L' },
39 { "both", no_argument, 0, 'b' },
40 { "verify", no_argument, 0, 'v' },
41 { "create", no_argument, 0, 'c' },
42 { "flush", no_argument, 0, 'f' },
43 { "entire", no_argument, 0, 'e' },
44 { "help", no_argument, 0, 'h' },
45 { "version", no_argument, 0, 'V' },
49 // Display help message
50 static void help(void) {
51 printf("vbig - create or verify a large but pseudo-random file\n"
54 " vbig [--seed SEED] --verify|--create PATH [SIZE]\n"
57 " --seed, -s Specify random seed as string\n"
58 " --seed-file, -S Read random seed from (start of) this file\n"
59 " --seed-length, -L Set (maximum) seed length to read from file\n"
60 " --verify, -v Verify that PATH contains the expected contents\n"
61 " --create, -c Create PATH with psuedo-random contents\n"
62 " --flush, -f Flush cache\n"
63 " --entire, -e Write until full; read until EOF\n"
64 " --help, -h Display usage message\n"
65 " --version, -V Display version string\n");
68 // Possible modes of operation
75 // Report an error and exit
76 static void fatal(int errno_value, const char *fmt, ...) {
78 fprintf(stderr, "ERROR: ");
80 vfprintf(stderr, fmt, ap);
83 fprintf(stderr, ": %s", strerror(errno_value));
88 // Evict whatever FP points to from RAM
89 static void flushCache(FILE *fp) {
90 // drop_caches only evicts clean pages, so first the target file is
92 if(fsync(fileno(fp)) < 0)
93 fatal(errno, "fsync");
94 #if defined DROP_CACHE_FILE
96 if((fd = open(DROP_CACHE_FILE, O_WRONLY, 0)) < 0)
97 fatal(errno, "%s", DROP_CACHE_FILE);
98 if(write(fd, "3\n", 2) < 0)
99 fatal(errno, "%s", DROP_CACHE_FILE);
101 #elif defined PURGE_COMMAND
103 if((rc = system(PURGE_COMMAND)) < 0)
104 fatal(errno, "executing %s", PURGE_COMMAND);
106 if(WIFSIGNALED(rc)) {
107 fprintf(stderr, "%s%s\n",
108 strsignal(WTERMSIG(rc)),
109 WCOREDUMP(rc) ? " (core dumped)" : "");
110 exit(WTERMSIG(rc) + 128);
112 exit(WEXITSTATUS(rc));
117 static long long execute(mode_type mode, bool entire, const char *show);
119 static const char default_seed[] = "hexapodia as the key insight";
121 static size_t seedlen;
122 static const char *seedpath;
123 static const char *path;
124 static bool entireopt = false;
125 static bool flush = false;
126 static long long size;
128 int main(int argc, char **argv) {
129 mode_type mode = BOTH;
132 while((n = getopt_long(argc, argv, "+s:S:L:vcefhV", opts, 0)) >= 0) {
134 case 's': seed = optarg; seedlen = strlen(optarg); break;
135 case 'S': seedpath = optarg; break;
137 seedlen = strtoul(optarg,&ep,0);
138 if(ep==optarg || *ep) fatal(0, "bad number for -S");
140 case 'b': mode = BOTH; break;
141 case 'v': mode = VERIFY; break;
142 case 'c': mode = CREATE; break;
143 case 'e': entireopt = true; break;
144 case 'f': flush = true; break;
145 case 'h': help(); exit(0);
146 case 'V': puts(VERSION); exit(0);
148 fatal(0, "unknown option");
154 fatal(0, "excess arguments");
155 if(argc == 1 && mode == BOTH)
159 fatal(0, "with --entire, size should not be specified");
161 if(argc < (mode == VERIFY ? 1 : 2))
162 fatal(0, "insufficient arguments");
165 fatal(0, "both --seed and --seed-file specified");
166 if(mode == BOTH && !seed && !seedpath) {
167 #ifdef HAVE_RANDOM_DEVICE
168 seedpath = RANDOM_DEVICE;
170 fatal(0, "no --seed or --seed-file specified in --both mode"
171 " and random device not supported on this system");
176 seedlen = DEFAULT_SEED_LENGTH;
177 FILE *seedfile = fopen(seedpath, "rb");
179 fatal(errno, "%s", seedpath);
180 seed = malloc(seedlen);
182 fatal(errno, "allocate seed");
183 seedlen = fread(seed, 1, seedlen, seedfile);
185 fatal(errno, "read %s", seedpath);
189 seed = (void*)default_seed;
190 seedlen = sizeof(default_seed)-1;
196 size = strtoll(argv[1], &end, 10);
198 fatal(errno, "invalid size");
200 fatal(0, "invalid size");
201 if(!strcmp(end, "K"))
203 else if(!strcmp(end, "M"))
205 else if(!strcmp(end, "G"))
206 size *= 1024 * 1024 * 1024;
208 fatal(0, "invalid size");
209 } else if(entireopt) {
210 size = LONG_LONG_MAX;
213 if(stat(path, &sb) < 0)
214 fatal(errno, "stat %s", path);
217 const char *show = entireopt ? (mode == CREATE ? "written" : "verified") : 0;
219 size = execute(CREATE, entireopt, 0);
220 execute(VERIFY, false, show);
222 execute(mode, entireopt, show);
227 static long long execute(mode_type mode, bool entire, const char *show) {
228 Arcfour rng((const char*)seed, seedlen);
229 FILE *fp = fopen(path, mode == VERIFY ? "rb" : "wb");
231 fatal(errno, "%s", path);
232 if(mode == VERIFY && flush)
234 if(mode == CREATE && entire)
235 setvbuf(fp, 0, _IONBF, 0);
236 char generated[4096], input[4096];
237 long long remain = size;
238 static const size_t rc4drop = 3072; // en.wikipedia.org/wiki/RC4#Security
239 assert(rc4drop <= sizeof(generated));
240 rng.stream(generated, rc4drop);
242 size_t bytesGenerated = (remain > (ssize_t)sizeof generated
245 rng.stream(generated, bytesGenerated);
247 size_t bytesWritten = fwrite(generated, 1, bytesGenerated, fp);
249 if(!entire || errno != ENOSPC)
250 fatal(errno, "%s", path);
251 remain -= bytesWritten;
254 assert(bytesWritten == bytesGenerated);
256 size_t bytesRead = fread(input, 1, bytesGenerated, fp);
258 fatal(errno, "%s", path);
259 if(memcmp(generated, input, bytesRead)) {
260 for(size_t n = 0; n < bytesRead; ++n)
261 if(generated[n] != input[n])
262 fatal(0, "%s corrupted at %lld/%lld bytes (expected %d got %d)",
263 path, size - remain + n, size,
264 (unsigned char)generated[n], (unsigned char)input[n]);
266 if(bytesRead < bytesGenerated) {
272 fatal(0, "%s: truncated at %lld/%lld bytes",
273 path, (size - remain + bytesRead), size);
276 remain -= bytesGenerated;
278 if(mode == VERIFY && !entire && getc(fp) != EOF)
279 fatal(0, "%s: extended beyond %lld bytes",
281 if(mode == CREATE && flush) {
283 fatal(errno, "%s", path);
287 fatal(errno, "%s", path);
288 long long done = size - remain;
290 printf("%lld bytes (%lldM, %lldG) %s\n",
291 done, done >> 20, done >> 30,
293 if(ferror(stdout) || fflush(stdout))
294 fatal(errno, "flush stdout");