12 // Path to magic file to drop filesystem caches
13 static const char dropCaches[] = "/proc/sys/vm/drop_caches";
15 // Command line options
16 const struct option opts[] = {
17 { "seed", required_argument, 0, 's' },
18 { "verify", no_argument, 0, 'v' },
19 { "create", no_argument, 0, 'c' },
20 { "flush", no_argument, 0, 'f' },
21 { "help", no_argument, 0, 'h' },
22 { "version", no_argument, 0, 'V' },
26 // Display help message
27 static void help(void) {
28 printf("vbig - create or verify a large but pseudo-random file\n"
31 " vbig [--seed SEED] --verify|--create PATH SIZE\n"
34 " --seed, -s Specify random seed\n"
35 " --verify, -v Verify that PATH contains the expected contents\n"
36 " --create, -c Create PATH with psuedo-random contents\n"
37 " --flush, -f Flush cache\n"
38 " --help, -h Display usage message\n"
39 " --version, -V Display version string\n");
42 // Possible modes of operation
49 // Report an error and exit
50 static void fatal(int errno_value, const char *fmt, ...) {
52 fprintf(stderr, "ERROR: ");
54 vfprintf(stderr, fmt, ap);
57 fprintf(stderr, ": %s", strerror(errno_value));
62 // Evict whatever FP points to from RAM
63 static void flushCache(FILE *fp) {
64 // drop_caches only evicts clean pages, so first the target file is
66 if(fsync(fileno(fp)) < 0)
67 fatal(errno, "fsync");
69 if((fd = open(dropCaches, O_WRONLY, 0)) < 0)
70 fatal(errno, "%s", dropCaches);
71 if(write(fd, "3\n", 2) < 0)
72 fatal(errno, "%s", dropCaches);
76 int main(int argc, char **argv) {
77 const char *seed = "hexapodia as the key insight";
78 mode_type mode = NONE;
81 while((n = getopt_long(argc, argv, "+s:vcfhV", opts, 0)) >= 0) {
83 case 's': seed = optarg; break;
84 case 'v': mode = VERIFY; break;
85 case 'c': mode = CREATE; break;
86 case 'f': flush = true; break;
87 case 'h': help(); exit(0);
88 case 'V': puts(VERSION); exit(0);
90 fatal(0, "unknown option");
94 fatal(0, "must specify one of --verify or --create");
97 if(optind + 2 != argc) {
98 fatal(0, "must specify a path and size");
101 const char *path = argv[optind];
104 long long size = strtoll(argv[optind + 1], &end, 10);
106 fatal(errno, "invalid size");
109 if(end == argv[optind + 1]) {
110 fatal(0, "invalid size");
113 if(!strcmp(end, "K"))
115 else if(!strcmp(end, "M"))
117 else if(!strcmp(end, "G"))
118 size *= 1024 * 1024 * 1024;
120 fatal(0, "invalid size");
123 Arcfour rng(seed, strlen(seed));
124 FILE *fp = fopen(path, mode == VERIFY ? "rb" : "wb");
126 fatal(errno, "%s", path);
129 if(mode == VERIFY && flush)
131 char generated[4096], input[4096];
132 long long remain = size;
134 size_t bytesGenerated = (remain > (ssize_t)sizeof generated
137 rng.stream(generated, bytesGenerated);
139 fwrite(generated, 1, bytesGenerated, fp);
141 fatal(errno, "%s", path);
145 size_t bytesRead = fread(input, 1, bytesGenerated, fp);
147 fatal(errno, "%s", path);
150 if(bytesRead < bytesGenerated) {
151 fatal(0, "%s: truncated at %lld/%lld bytes",
152 path, (size - remain + bytesRead), size);
155 if(memcmp(generated, input, bytesGenerated)) {
156 for(size_t n = 0; n < bytesGenerated; ++n)
157 if(generated[n] != input[n]){
158 fatal(0, "%s corrupted at %lld/%lld bytes (expected %d got %d)",
159 path, size - remain + n, size,
160 (unsigned char)generated[n], (unsigned char)input[n]);
165 remain -= bytesGenerated;
167 if(mode == VERIFY && getc(fp) != EOF) {
168 fatal(0, "%s: extended beyond %lld bytes",
172 if(mode == CREATE && flush) {
174 fatal(errno, "%s", path);
178 fatal(errno, "%s", path);