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 <portals/lltrace.h>
21 static int opt_depth = 1;
22 static int opt_mknod = 0;
23 static int opt_verbose = 0;
24 static int opt_trace = 1;
25 static char* basepathname = 0;
26 static char mycwd[PATH_MAX];
27 static char* pname = 0;
28 static char* outputfilename = 0;
32 fprintf(stderr, "Usage: %s --depth <d> --output <outputtracefilename>"
33 "[--mknod] [--verbose] [--notrace] <basepath>\n", pname);
37 int do_mkdir(char* path)
39 int rc = mkdir(path, 0755);
41 fprintf(stderr, "mkdir(%s) failed: %s\n",
42 path, strerror(errno));
44 printf("mkdir %s\n", path);
49 int do_mknod(char* path)
51 int rc = mknod(path, 0755, S_IFIFO);
53 fprintf(stderr, "mkdir(%s) failed: %s\n",
54 path, strerror(errno));
56 printf("mknod %s\n", path);
60 int do_chdir(char* path)
64 fprintf(stderr, "chdir(%s) failed: %s\n",
65 path, strerror(errno));
67 printf("chdir %s\n", path);
73 int do_stat(char* path)
75 char mark_buf[PATH_MAX];
77 int rc = stat(path, &mystat);
79 fprintf(stderr, "stat(%s) failed: %s\n",
80 path, strerror(errno));
82 printf("stat %s = inode %lu\n", path, mystat.st_ino);
85 snprintf(mark_buf, PATH_MAX, "stat %s = inode %lu",
87 ltrace_mark(0, mark_buf);
93 int main(int argc, char** argv)
95 int c, opt_index, i, mypid;
97 static struct option long_options[] = {
101 {"verbose", 0, 0, 0 },
102 {"notrace", 0, 0, 0 },
103 {"output", 1, 0, 0 },
107 char full_pathname[PATH_MAX];
108 char rel_pathname[PATH_MAX];
109 char mark_buf[PATH_MAX];
111 pname = strdup(argv[0]);
114 c = getopt_long(argc, argv, "d:mhv", long_options, &opt_index);
118 if (!strcmp(long_options[opt_index].name, "notrace")) {
122 c = long_options[opt_index].name[0];
126 opt_depth = atoi(optarg);
127 if ((opt_depth == 0) || (opt_depth > 100))
137 outputfilename = optarg;
148 if (optind != (argc-1))
151 if (outputfilename == NULL)
154 basepathname = argv[optind];
157 printf("%s(pid=%d) depth=%d mknod=%d, basepathname=%s, "
158 "trace=%d, outputfilename=%s\n",
159 pname, mypid, opt_depth, opt_mknod, basepathname, opt_trace,
162 if (!getcwd(&mycwd[0], sizeof(mycwd))) {
163 fprintf(stderr, "%s: unable to getcwd()\n", pname);
170 snprintf(mark_buf, PATH_MAX,
171 "Initialize - mkdir %s; chdir %s",
172 basepathname, basepathname);
173 ltrace_mark(2, mark_buf);
176 if (do_mkdir(basepathname)!=0)
178 if (do_chdir(basepathname)!=0)
181 /* Create directory tree with depth level of subdirectories */
184 snprintf(mark_buf, PATH_MAX,
185 "Create Directory Tree (depth %d)", opt_depth);
186 ltrace_mark(2, mark_buf);
189 for (i=0; i<opt_depth; i++) {
191 snprintf(rel_pathname, sizeof(rel_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);
204 do_mknod(rel_pathname);
206 do_mkdir(rel_pathname);
208 do_stat(rel_pathname);
214 snprintf(mark_buf, PATH_MAX,
215 "Tree Level (%d) mkdir/stat/chdir",
217 ltrace_mark(3, mark_buf);
220 do_mkdir(rel_pathname);
221 do_stat(rel_pathname);
222 do_chdir(rel_pathname);
226 /* Stat through directory tree with fullpaths */
229 snprintf(mark_buf, PATH_MAX, "Walk Directory Tree");
230 ltrace_mark(2, mark_buf);
233 do_chdir(basepathname);
235 strncpy(full_pathname, basepathname, sizeof(full_pathname));
237 for (i=0; i<opt_depth; i++) {
238 snprintf(rel_pathname, sizeof(rel_pathname),"%d", i+1);
239 strcat(full_pathname, "/");
240 strcat(full_pathname, rel_pathname);
243 snprintf(mark_buf, PATH_MAX, "stat %s",
245 ltrace_mark(2, mark_buf);
248 do_stat(full_pathname);
254 snprintf(mark_buf, PATH_MAX, "Cleanup");
255 ltrace_mark(2, mark_buf);
259 ltrace_write_file(outputfilename);
260 ltrace_add_processnames(outputfilename);
264 do_chdir(basepathname);
266 snprintf(full_pathname, sizeof(full_pathname),
267 "rm -rf %s\n", basepathname);
269 printf("Cleanup: %s", full_pathname);
271 system(full_pathname);
273 printf("%s (pid=%d) done.\n", pname, mypid);