2 * Copyright (c) 2008 Peter Holm <pho@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/types.h>
29 #include <sys/sysctl.h>
35 #include <sys/param.h>
36 #include <sys/errno.h>
47 error(char *op, char* arg, char* file, int line) {
48 fprintf(stderr,"%s. %s. %s (%s:%d)\n",
49 op, arg, sys_errlist[errno], file, line);
53 mkDir(char *path, int level) {
55 char newPath[MAXPATHLEN + 1];
58 if (mkdir(path, 0770) == -1) {
59 error("mkdir", path, __FILE__, __LINE__);
60 fprintf(stderr, "length(path) = %d\n", strlen(path));
61 fprintf(stderr, ") level = %d\n", level);
66 for (j = 0; j < files; j++) {
67 sprintf(file,"f%05d", j);
68 if ((fd = creat(file, 0660)) == -1) {
70 err(1, "%d: creat(%s)", level, file);
74 if (write(fd, buffer, fs) != fs)
75 err(1, "%d: write(%s), %s:%d", level, file, __FILE__, __LINE__);
77 if (fd != -1 && close(fd) == -1)
78 err(2, "%d: close(%d)", level, j);
83 sprintf(newPath,"d%d", level+1);
84 mkDir(newPath, level+1);
94 for (j = 0; j < files; j++) {
95 sprintf(file,"f%05d", j);
101 rmDir(char *path, int level) {
106 sprintf(newPath,"d%d", level+1);
107 rmDir(newPath, level+1);
111 if (rmdir(path) == -1) {
112 error("rmdir", path, __FILE__, __LINE__);
118 rmDir2(char *path, int level) {
124 sprintf(newPath,"d%d", level+1);
125 if (access(newPath, R_OK) == 0)
126 rmDir2(newPath, level+1);
128 if (rmdir(path) == -1) {
129 error("rmdir", path, __FILE__, __LINE__);
130 sprintf(help, "rm -rf ./%s", path);
136 main(int argc, char **argv)
138 int c, levels = 1, leave = 0;
139 char path[128], rpath[128] = "";
144 while ((c = getopt(argc, argv, "ln:r:f:s:")) != -1)
150 strcpy(rpath, optarg);
153 levels = atoi(optarg);
156 files = atoi(optarg);
159 sscanf(optarg, "%d%c", &fs, &ch);
160 if (ch == 'k' || ch == 'K')
162 if (ch == 'm' || ch == 'M')
163 fs = fs * 1024 * 1024;
167 "Usage: %s {-l} | {-n <num>} | -r <dir> | -s <file size> "
170 printf(" -l: Leave the files.\n");
171 printf(" -r: Remove an old tree.\n");
172 printf(" -n: Tree depth.\n");
173 printf(" -f: Number of files.\n");
174 printf(" -s: Size of each file.\n");
181 if ((buffer = calloc(1, fs)) == NULL)
182 err(1, "calloc(%d)", fs);
184 if (strlen(rpath) > 0) {
188 sprintf(path,"p%05d.d%d", pid, 1);
190 if (leave == 0) rmDir(path, 1);