4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
34 * lustre/tests/mkdirdeep.c
37 * cc -I../../lnet/include -o mkdirdeep mkdirdeep.c
38 * -L../../lnet/linux/utils -lptlctl
46 #include <sys/types.h>
50 #include <linux/limits.h>
51 #include <libcfs/lltrace.h>
53 static int opt_verbose = 0;
54 static int opt_trace = 0;
56 void usage(const char *pname)
58 fprintf(stderr, "Usage: %s --depth <d> [--output <outputtracefilename>]"
59 " [--mknod] [--verbose] [--notrace] <basepath>\n", pname);
63 int do_mkdir(char *path)
65 int rc = mkdir(path, 0755);
68 fprintf(stderr, "mkdir(%s) failed: %s\n",
69 path, strerror(errno));
73 printf("mkdir %s\n", path);
79 int do_mknod(char *path)
81 int rc = mknod(path, 0755, S_IFIFO);
84 fprintf(stderr, "mkdir(%s) failed: %s\n",
85 path, strerror(errno));
89 printf("mknod %s\n", path);
94 int do_chdir(char* path)
99 fprintf(stderr, "chdir(%s) failed: %s\n",
100 path, strerror(errno));
104 printf("chdir %s\n", path);
109 int do_stat(char *path)
111 char mark_buf[PATH_MAX + 50];
113 int rc = stat(path, &mystat);
116 fprintf(stderr, "stat(%s) failed: %s\n",
117 path, strerror(errno));
121 printf("stat %s = inode %lu\n", path,
122 (unsigned long)mystat.st_ino);
125 snprintf(mark_buf, PATH_MAX, "stat %s = inode %lu",
126 path, (unsigned long)mystat.st_ino);
127 ltrace_mark(0, mark_buf);
133 int main(int argc, char** argv)
139 static struct option long_opt[] = {
140 {"depth", 1, 0, 'd' },
141 {"help", 0, 0, 'h' },
142 {"mknod", 0, 0, 'm' },
143 {"output", 1, 0, 'o' },
144 {"trace", 1, 0, 't' },
145 {"verbose", 0, 0, 'v' },
149 char *outputfilename = NULL;
151 char pathname[PATH_MAX];
152 char mark_buf[PATH_MAX + 50];
153 char mycwd[PATH_MAX];
154 char *pname = argv[0];
156 while ((c = getopt_long(argc, argv, "d:mhvo:", long_opt, NULL)) != -1) {
159 opt_depth = atoi(optarg);
160 if ((opt_depth == 0) || (opt_depth > 1100))
173 outputfilename = optarg;
184 if (optind != (argc - 1))
187 base_pathname = argv[optind];
190 if (!getcwd(&mycwd[0], sizeof(mycwd))) {
191 fprintf(stderr, "%s: unable to getcwd()\n", pname);
195 printf("%s(pid=%d) depth=%d mknod=%d, basepathname=%s, trace=%d\n",
196 pname, mypid, opt_depth, opt_mknod, base_pathname, opt_trace);
199 printf("outputfilename=%s\n", outputfilename);
204 snprintf(mark_buf, PATH_MAX, "Initialize - mkdir %s; chdir %s",
205 base_pathname, base_pathname);
206 ltrace_mark(2, mark_buf);
209 if (do_mkdir(base_pathname)!=0)
211 if (do_chdir(base_pathname)!=0)
214 /* Create directory tree with depth level of subdirectories */
217 snprintf(mark_buf, PATH_MAX,
218 "Create Directory Tree (depth %d)", opt_depth);
219 ltrace_mark(2, mark_buf);
222 for (i = 0; i < opt_depth; i++) {
223 snprintf(pathname, sizeof(pathname), "%d", i + 1);
225 if (i == (opt_depth - 1)) {
229 snprintf(mark_buf, PATH_MAX,
230 "Tree Leaf (%d) %s/stat", i,
231 (opt_mknod ? "mknod" : "mkdir"));
232 ltrace_mark(3, mark_buf);
245 snprintf(mark_buf, sizeof(mark_buf),
246 "Tree Level (%d) mkdir/stat/chdir", i);
247 ltrace_mark(3, mark_buf);
256 /* Stat through directory tree with fullpaths */
259 snprintf(mark_buf, PATH_MAX, "Walk Directory Tree");
260 ltrace_mark(2, mark_buf);
263 do_chdir(base_pathname);
265 strncpy(pathname, base_pathname, sizeof(pathname));
267 c = strlen(base_pathname);
268 for (i = 0; i < opt_depth; i++) {
269 c += snprintf(pathname + c, sizeof(pathname) - c, "/%d", i+1);
272 snprintf(mark_buf, PATH_MAX, "stat %s", pathname);
273 ltrace_mark(2, mark_buf);
279 if (opt_trace && outputfilename) {
280 ltrace_write_file(outputfilename);
281 ltrace_add_processnames(outputfilename);
285 do_chdir(base_pathname);
287 printf("%s (pid=%d) done.\n", pname, mypid);