chiark / gitweb /
Break out function "execute"
[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 <sys/stat.h>
28 #include "Arcfour.h"
29
30 // Command line options
31 const struct option opts[] = {
32   { "seed", required_argument, 0, 's' },
33   { "verify", no_argument, 0, 'v' },
34   { "create", no_argument, 0, 'c' },
35   { "flush", no_argument, 0, 'f' },
36   { "help", no_argument, 0, 'h' },
37   { "version", no_argument, 0, 'V' },
38   { 0, 0, 0, 0 },
39 };
40
41 // Display help message
42 static void help(void) {
43   printf("vbig - create or verify a large but pseudo-random file\n"
44          "\n"
45          "Usage:\n"
46          "  vbig [--seed SEED] --verify|--create PATH [SIZE]\n"
47          "\n"
48          "Options:\n"
49          "  --seed, -s     Specify random seed\n"
50          "  --verify, -v   Verify that PATH contains the expected contents\n"
51          "  --create, -c   Create PATH with psuedo-random contents\n"
52          "  --flush, -f    Flush cache\n"
53          "  --help, -h     Display usage message\n"
54          "  --version, -V  Display version string\n");
55 }
56
57 // Possible modes of operation
58 enum mode_type {
59   NONE,
60   VERIFY,
61   CREATE
62 };
63
64 // Report an error and exit
65 static void fatal(int errno_value, const char *fmt, ...) {
66   va_list ap;
67   fprintf(stderr, "ERROR: ");
68   va_start(ap, fmt);
69   vfprintf(stderr, fmt, ap);
70   va_end(ap);
71   if(errno_value)
72     fprintf(stderr, ": %s", strerror(errno_value));
73   fputc('\n', stderr);
74   exit(1);
75 }
76
77 // Evict whatever FP points to from RAM
78 static void flushCache(FILE *fp) {
79   // drop_caches only evicts clean pages, so first the target file is
80   // synced.
81   if(fsync(fileno(fp)) < 0)
82     fatal(errno, "fsync");
83 #if defined DROP_CACHE_FILE
84   int fd;
85   if((fd = open(DROP_CACHE_FILE, O_WRONLY, 0)) < 0)
86     fatal(errno, "%s", DROP_CACHE_FILE);
87   if(write(fd, "3\n", 2) < 0)
88     fatal(errno, "%s", DROP_CACHE_FILE);
89   close(fd);
90 #elif defined PURGE_COMMAND
91   int rc;
92   if((rc = system(PURGE_COMMAND)) < 0)
93     fatal(errno, "executing %s", PURGE_COMMAND);
94   else if(rc) {
95     if(WIFSIGNALED(rc)) {
96       fprintf(stderr, "%s%s\n", 
97               strsignal(WTERMSIG(rc)),
98               WCOREDUMP(rc) ? " (core dumped)" : "");
99       exit(WTERMSIG(rc) + 128);
100     } else
101       exit(WEXITSTATUS(rc));
102   }
103 #endif
104 }
105
106 static void execute(mode_type mode);
107
108 static const char *seed = "hexapodia as the key insight";
109 static const char *path;
110 static bool flush = false;
111 static long long size;
112
113 int main(int argc, char **argv) {
114   mode_type mode = NONE;
115   int n;
116   while((n = getopt_long(argc, argv, "+s:vcfhV", opts, 0)) >= 0) {
117     switch(n) {
118     case 's': seed = optarg; break;
119     case 'v': mode = VERIFY; break;
120     case 'c': mode = CREATE; break;
121     case 'f': flush = true; break;
122     case 'h': help(); exit(0);
123     case 'V': puts(VERSION); exit(0);
124     default:
125       fatal(0, "unknown option");
126     }
127   }
128   argc -= optind;
129   argv += optind;
130   if(mode == NONE)
131     fatal(0, "must specify one of --verify or --create");
132   if(argc > 2)
133     fatal(0, "excess arguments");
134   if(argc < (mode == VERIFY ? 1 : 2))
135     fatal(0, "insufficient arguments");
136   path = argv[0];
137   if(argc > 1) {
138     errno = 0;
139     char *end;
140     size = strtoll(argv[1], &end, 10);
141     if(errno)
142       fatal(errno, "invalid size");
143     if(end == argv[1])
144       fatal(0, "invalid size");
145     if(!strcmp(end, "K"))
146       size *= 1024;
147     else if(!strcmp(end, "M"))
148       size *= 1024 * 1024;
149     else if(!strcmp(end, "G"))
150       size *= 1024 * 1024 * 1024;
151     else if(*end)
152       fatal(0, "invalid size");
153   } else {
154     struct stat sb;
155     if(stat(path, &sb) < 0)
156       fatal(errno, "stat %s", path);
157     size = sb.st_size;
158   }
159   execute(mode);
160   return 0;
161 }
162
163 static void execute(mode_type mode) {
164   Arcfour rng(seed, strlen(seed));
165   FILE *fp = fopen(path, mode == VERIFY ? "rb" : "wb");
166   if(!fp)
167     fatal(errno, "%s", path);
168   if(mode == VERIFY && flush)
169     flushCache(fp);
170   char generated[4096], input[4096];
171   long long remain = size;
172   while(remain > 0) {
173     size_t bytesGenerated = (remain > (ssize_t)sizeof generated
174                              ? sizeof generated
175                              : remain);
176     rng.stream(generated, bytesGenerated);
177     if(mode == CREATE) {
178       fwrite(generated, 1, bytesGenerated, fp);
179       if(ferror(fp))
180         fatal(errno, "%s", path);
181     } else {
182       size_t bytesRead = fread(input, 1, bytesGenerated, fp);
183       if(ferror(fp))
184         fatal(errno, "%s", path);
185       if(bytesRead < bytesGenerated)
186         fatal(0, "%s: truncated at %lld/%lld bytes",
187                 path, (size - remain + bytesRead), size);
188       if(memcmp(generated, input, bytesGenerated)) {
189         for(size_t n = 0; n < bytesGenerated; ++n)
190           if(generated[n] != input[n])
191             fatal(0, "%s corrupted at %lld/%lld bytes (expected %d got %d)",
192                     path, size - remain + n, size,
193                     (unsigned char)generated[n], (unsigned char)input[n]);
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   if(mode == CREATE && flush) {
202     if(fflush(fp) < 0)
203       fatal(errno, "%s", path);
204     flushCache(fp);
205   }
206   if(fclose(fp) < 0)
207     fatal(errno, "%s", path);
208 }