1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
5 * cc -I../../portals/include -o mkdirdeep mkdirdeep.c
6 * -L../../portals/linux/utils -lptlctl
14 #include <sys/types.h>
18 #include <linux/limits.h>
19 #include <libcfs/lltrace.h>
21 static int opt_verbose = 0;
22 static int opt_trace = 0;
24 void usage(const char *pname)
26 fprintf(stderr, "Usage: %s --depth <d> [--output <outputtracefilename>]"
27 " [--mknod] [--verbose] [--notrace] <basepath>\n", pname);
31 int do_mkdir(char *path)
33 int rc = mkdir(path, 0755);
36 fprintf(stderr, "mkdir(%s) failed: %s\n",
37 path, strerror(errno));
41 printf("mkdir %s\n", path);
47 int do_mknod(char *path)
49 int rc = mknod(path, 0755, S_IFIFO);
52 fprintf(stderr, "mkdir(%s) failed: %s\n",
53 path, strerror(errno));
57 printf("mknod %s\n", path);
62 int do_chdir(char* path)
67 fprintf(stderr, "chdir(%s) failed: %s\n",
68 path, strerror(errno));
72 printf("chdir %s\n", path);
77 int do_stat(char *path)
79 char mark_buf[PATH_MAX + 50];
81 int rc = stat(path, &mystat);
84 fprintf(stderr, "stat(%s) failed: %s\n",
85 path, strerror(errno));
89 printf("stat %s = inode %lu\n", path,
90 (unsigned long)mystat.st_ino);
93 snprintf(mark_buf, PATH_MAX, "stat %s = inode %lu",
94 path, (unsigned long)mystat.st_ino);
95 ltrace_mark(0, mark_buf);
101 int main(int argc, char** argv)
107 static struct option long_opt[] = {
108 {"depth", 1, 0, 'd' },
109 {"help", 0, 0, 'h' },
110 {"mknod", 0, 0, 'm' },
111 {"output", 1, 0, 'o' },
112 {"trace", 1, 0, 't' },
113 {"verbose", 0, 0, 'v' },
117 char *outputfilename = NULL;
119 char pathname[PATH_MAX];
120 char mark_buf[PATH_MAX + 50];
121 char mycwd[PATH_MAX];
122 char *pname = argv[0];
124 while ((c = getopt_long(argc, argv, "d:mhvo:", long_opt, NULL)) != -1) {
127 opt_depth = atoi(optarg);
128 if ((opt_depth == 0) || (opt_depth > 1100))
141 outputfilename = optarg;
152 if (optind != (argc - 1))
155 base_pathname = argv[optind];
158 if (!getcwd(&mycwd[0], sizeof(mycwd))) {
159 fprintf(stderr, "%s: unable to getcwd()\n", pname);
163 printf("%s(pid=%d) depth=%d mknod=%d, basepathname=%s, trace=%d\n",
164 pname, mypid, opt_depth, opt_mknod, base_pathname, opt_trace);
167 printf("outputfilename=%s\n", outputfilename);
172 snprintf(mark_buf, PATH_MAX, "Initialize - mkdir %s; chdir %s",
173 base_pathname, base_pathname);
174 ltrace_mark(2, mark_buf);
177 if (do_mkdir(base_pathname)!=0)
179 if (do_chdir(base_pathname)!=0)
182 /* Create directory tree with depth level of subdirectories */
185 snprintf(mark_buf, PATH_MAX,
186 "Create Directory Tree (depth %d)", opt_depth);
187 ltrace_mark(2, mark_buf);
190 for (i = 0; i < opt_depth; i++) {
191 snprintf(pathname, sizeof(pathname), "%d", i + 1);
193 if (i == (opt_depth - 1)) {
197 snprintf(mark_buf, PATH_MAX,
198 "Tree Leaf (%d) %s/stat", i,
199 (opt_mknod ? "mknod" : "mkdir"));
200 ltrace_mark(3, mark_buf);
213 snprintf(mark_buf, sizeof(mark_buf),
214 "Tree Level (%d) mkdir/stat/chdir", i);
215 ltrace_mark(3, mark_buf);
224 /* Stat through directory tree with fullpaths */
227 snprintf(mark_buf, PATH_MAX, "Walk Directory Tree");
228 ltrace_mark(2, mark_buf);
231 do_chdir(base_pathname);
233 strncpy(pathname, base_pathname, sizeof(pathname));
235 c = strlen(base_pathname);
236 for (i = 0; i < opt_depth; i++) {
237 c += snprintf(pathname + c, sizeof(pathname) - c, "/%d", i+1);
240 snprintf(mark_buf, PATH_MAX, "stat %s", pathname);
241 ltrace_mark(2, mark_buf);
247 if (opt_trace && outputfilename) {
248 ltrace_write_file(outputfilename);
249 ltrace_add_processnames(outputfilename);
253 do_chdir(base_pathname);
255 printf("%s (pid=%d) done.\n", pname, mypid);