12 // Command line options
13 const struct option opts[] = {
14 { "seed", required_argument, 0, 's' },
15 { "verify", no_argument, 0, 'v' },
16 { "create", no_argument, 0, 'c' },
17 { "flush", no_argument, 0, 'f' },
18 { "help", no_argument, 0, 'h' },
19 { "version", no_argument, 0, 'V' },
23 // Display help message
24 static void help(void) {
25 printf("vbig - create or verify a large but pseudo-random file\n"
28 " vbig [--seed SEED] --verify|--create PATH SIZE\n"
31 " --seed, -s Specify random seed\n"
32 " --verify, -v Verify that PATH contains the expected contents\n"
33 " --create, -c Create PATH with psuedo-random contents\n"
34 " --flush, -f Flush cache\n"
35 " --help, -h Display usage message\n"
36 " --version, -V Display version string\n");
39 // Possible modes of operation
46 // Report an error and exit
47 static void fatal(int errno_value, const char *fmt, ...) {
49 fprintf(stderr, "ERROR: ");
51 vfprintf(stderr, fmt, ap);
54 fprintf(stderr, ": %s", strerror(errno_value));
59 // Evict whatever FP points to from RAM
60 static void flushCache(FILE *fp) {
61 // drop_caches only evicts clean pages, so first the target file is
63 if(fsync(fileno(fp)) < 0)
64 fatal(errno, "fsync");
65 #if defined DROP_CACHE_FILE
67 if((fd = open(DROP_CACHE_FILE, O_WRONLY, 0)) < 0)
68 fatal(errno, "%s", DROP_CACHE_FILE);
69 if(write(fd, "3\n", 2) < 0)
70 fatal(errno, "%s", DROP_CACHE_FILE);
72 #elif defined PURGE_COMMAND
74 if((rc = system(PURGE_COMMAND)) < 0)
75 fatal(errno, "executing %s", PURGE_COMMAND);
78 fprintf(stderr, "%s%s\n",
79 strsignal(WTERMSIG(rc)),
80 WCOREDUMP(rc) ? " (core dumped)" : "");
81 exit(WTERMSIG(rc) + 128);
83 exit(WEXITSTATUS(rc));
88 int main(int argc, char **argv) {
89 const char *seed = "hexapodia as the key insight";
90 mode_type mode = NONE;
93 while((n = getopt_long(argc, argv, "+s:vcfhV", opts, 0)) >= 0) {
95 case 's': seed = optarg; break;
96 case 'v': mode = VERIFY; break;
97 case 'c': mode = CREATE; break;
98 case 'f': flush = true; break;
99 case 'h': help(); exit(0);
100 case 'V': puts(VERSION); exit(0);
102 fatal(0, "unknown option");
106 fatal(0, "must specify one of --verify or --create");
109 if(optind + 2 != argc) {
110 fatal(0, "must specify a path and size");
113 const char *path = argv[optind];
116 long long size = strtoll(argv[optind + 1], &end, 10);
118 fatal(errno, "invalid size");
121 if(end == argv[optind + 1]) {
122 fatal(0, "invalid size");
125 if(!strcmp(end, "K"))
127 else if(!strcmp(end, "M"))
129 else if(!strcmp(end, "G"))
130 size *= 1024 * 1024 * 1024;
132 fatal(0, "invalid size");
135 Arcfour rng(seed, strlen(seed));
136 FILE *fp = fopen(path, mode == VERIFY ? "rb" : "wb");
138 fatal(errno, "%s", path);
141 if(mode == VERIFY && flush)
143 char generated[4096], input[4096];
144 long long remain = size;
146 size_t bytesGenerated = (remain > (ssize_t)sizeof generated
149 rng.stream(generated, bytesGenerated);
151 fwrite(generated, 1, bytesGenerated, fp);
153 fatal(errno, "%s", path);
157 size_t bytesRead = fread(input, 1, bytesGenerated, fp);
159 fatal(errno, "%s", path);
162 if(bytesRead < bytesGenerated) {
163 fatal(0, "%s: truncated at %lld/%lld bytes",
164 path, (size - remain + bytesRead), size);
167 if(memcmp(generated, input, bytesGenerated)) {
168 for(size_t n = 0; n < bytesGenerated; ++n)
169 if(generated[n] != input[n]){
170 fatal(0, "%s corrupted at %lld/%lld bytes (expected %d got %d)",
171 path, size - remain + n, size,
172 (unsigned char)generated[n], (unsigned char)input[n]);
177 remain -= bytesGenerated;
179 if(mode == VERIFY && getc(fp) != EOF) {
180 fatal(0, "%s: extended beyond %lld bytes",
184 if(mode == CREATE && flush) {
186 fatal(errno, "%s", path);
190 fatal(errno, "%s", path);