1 /*- 2 * Copyright (c) 2008 Peter Holm <pho@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 */ 27 28 #include <sys/types.h> 29 #include <sys/errno.h> 30 #include <sys/param.h> 31 #include <sys/stat.h> 32 #include <sys/sysctl.h> 33 34 #include <err.h> 35 #include <fcntl.h> 36 #include <stdio.h> 37 #include <stdlib.h> 38 #include <string.h> 39 #include <unistd.h> 40 41 extern char *optarg; 42 static pid_t pid; 43 static int edirs, dirs, files, fs; 44 static int max, rnd; 45 static char *buffer; 46 static char here[MAXPATHLEN + 1]; 47 48 static void 49 mkDir2(char *path) { 50 int fd, i, j, len; 51 char newPath[MAXPATHLEN + 1]; 52 char file[128]; 53 54 if (mkdir(path, 0700) == -1) 55 err(1, "mkdir(%s)", path); 56 if (chdir(path) == -1) 57 err(1, "chdir(%s) @ %d", path, __LINE__); 58 59 for (i = 0; i < max; i++) { 60 sprintf(newPath,"d%d", i); 61 if (mkdir(newPath, 0700) == -1) 62 err(1, "mkdir(%s) @ %d", newPath, __LINE__); 63 if (chdir(newPath) == -1) 64 err(1, "chdir(%s) @ %d", newPath, __LINE__); 65 66 len = fs; 67 for (j = 0; j < files; j++) { 68 if (rnd) 69 len = arc4random() % fs + 1; 70 sprintf(file,"f%05d", j); 71 if ((fd = creat(file, 0660)) == -1) { 72 if (errno != EINTR) { 73 err(1, "%d: creat(%s)", j, file); 74 break; 75 } 76 } 77 if (write(fd, buffer, len) != len) 78 err(1, "%d: write(%s), %s:%d", j, file, __FILE__, __LINE__); 79 80 if (fd != -1 && close(fd) == -1) 81 err(2, "%d: close(%d)", j, j); 82 83 } 84 for (j = 0; j < edirs; j++) { 85 sprintf(newPath,"e%d", j); 86 if (mkdir(newPath, 0700) == -1) 87 err(1, "mkdir(%s) @ %d", newPath, __LINE__); 88 } 89 } 90 chdir(here); 91 } 92 93 static void 94 mkDir(void) { 95 int i; 96 char path[MAXPATHLEN + 1]; 97 98 for (i = 0; i < dirs; i++) { 99 sprintf(path,"fstool.%06d.%d", pid, i); 100 mkDir2(path); 101 } 102 } 103 104 static void 105 rmFile(void) 106 { 107 int j; 108 char file[128], newPath[128]; 109 110 for (j = 0; j < files; j++) { 111 sprintf(file,"f%05d", j); 112 (void) unlink(file); 113 } 114 for (j = 0; j < edirs; j++) { 115 sprintf(newPath,"e%d", j); 116 if (rmdir(newPath) == -1) 117 err(1, "rmdir(%s) @ %d", newPath, __LINE__); 118 } 119 } 120 121 static void 122 rmDir2(char *path) { 123 int i, j; 124 char newPath[10]; 125 126 if (chdir(path) == -1) 127 err(1, "chdir(%s)", path); 128 129 for (i = 0; i < max; i++) { 130 sprintf(newPath,"d%d", i); 131 if (chdir(newPath) == -1) 132 err(1, "chdir(%s)", newPath); 133 } 134 for (j = 0; j < max; j++) { 135 rmFile(); 136 if (chdir ("..") == -1) 137 err(1, "chdir(\"..\")"); 138 sprintf(newPath,"d%d", max - j - 1); 139 if (rmdir(newPath) == -1) 140 err(1, "rmdir(%s) @ %d", newPath, __LINE__); 141 } 142 if (chdir ("..") == -1) 143 err(1, "chdir(\"..\")"); 144 if (rmdir(path) == -1) 145 err(1, "rmdir(%s) @ %d", path, __LINE__); 146 } 147 148 static void 149 rmDir(void) { 150 int i; 151 char path[MAXPATHLEN + 1]; 152 153 for (i = 0; i < dirs; i++) { 154 sprintf(path,"fstool.%06d.%d", pid, i); 155 rmDir2(path); 156 } 157 } 158 159 int 160 main(int argc, char **argv) 161 { 162 int c, i, levels, leave, times; 163 char ch = 0; 164 165 edirs = 0; 166 dirs = 1; 167 files = 5; 168 fs = 1024; 169 leave = 0; 170 levels = 1; 171 times = 1; 172 173 while ((c = getopt(argc, argv, "ad:e:ln:r:f:s:t:")) != -1) 174 switch (c) { 175 case 'a': 176 rnd = 1; 177 break; 178 case 'd': 179 dirs = atoi(optarg); 180 break; 181 case 'e': 182 edirs = atoi(optarg); 183 break; 184 case 'f': 185 files = atoi(optarg); 186 break; 187 case 'l': 188 leave = 1; 189 break; 190 case 'n': 191 levels = atoi(optarg); 192 break; 193 case 's': 194 sscanf(optarg, "%d%c", &fs, &ch); 195 if (ch == 'k' || ch == 'K') 196 fs = fs * 1024; 197 if (ch == 'm' || ch == 'M') 198 fs = fs * 1024 * 1024; 199 break; 200 case 't': 201 times = atoi(optarg); 202 break; 203 default: 204 fprintf(stderr, 205 "Usage: %s [ -a ] " 206 "[ -d <parallel dirs> ] [ -e <dirs> ] " 207 "[ -f <num> ][ -l] [ -n <depth> ] " 208 " [ -s <file size> ]\n", argv[0]); 209 printf(" -a: random file size 1-s.\n"); 210 printf(" -d: Tree width (defaults to 1).\n"); 211 printf(" -e: Directories at each level.\n"); 212 printf(" -f: Number of files.\n"); 213 printf(" -l: Leave the files and dirs.\n"); 214 printf(" -n: Tree depth.\n"); 215 printf(" -s: Size of each file.\n"); 216 printf(" -t: Number of times to repeat\n"); 217 exit(1); 218 } 219 220 if (times != 1) 221 leave = 0; 222 max = levels; 223 pid = getpid(); 224 if ((buffer = calloc(1, fs)) == NULL) 225 err(1, "calloc(%d)", fs); 226 if (getcwd(here, sizeof(here)) == NULL) 227 err(1, "getcwd()"); 228 229 for (i = 0; i < times; i++) { 230 mkDir(); 231 if (leave == 0) 232 rmDir(); 233 } 234 235 return (0); 236 } 237