1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/tests/mkdirdeep.c
39 * cc -I../../lnet/include -o mkdirdeep mkdirdeep.c
40 * -L../../lnet/linux/utils -lptlctl
48 #include <sys/types.h>
52 #include <linux/limits.h>
53 #include <libcfs/lltrace.h>
55 static int opt_verbose = 0;
56 static int opt_trace = 0;
58 void usage(const char *pname)
60 fprintf(stderr, "Usage: %s --depth <d> [--output <outputtracefilename>]"
61 " [--mknod] [--verbose] [--notrace] <basepath>\n", pname);
65 int do_mkdir(char *path)
67 int rc = mkdir(path, 0755);
70 fprintf(stderr, "mkdir(%s) failed: %s\n",
71 path, strerror(errno));
75 printf("mkdir %s\n", path);
81 int do_mknod(char *path)
83 int rc = mknod(path, 0755, S_IFIFO);
86 fprintf(stderr, "mkdir(%s) failed: %s\n",
87 path, strerror(errno));
91 printf("mknod %s\n", path);
96 int do_chdir(char* path)
101 fprintf(stderr, "chdir(%s) failed: %s\n",
102 path, strerror(errno));
106 printf("chdir %s\n", path);
111 int do_stat(char *path)
113 char mark_buf[PATH_MAX + 50];
115 int rc = stat(path, &mystat);
118 fprintf(stderr, "stat(%s) failed: %s\n",
119 path, strerror(errno));
123 printf("stat %s = inode %lu\n", path,
124 (unsigned long)mystat.st_ino);
127 snprintf(mark_buf, PATH_MAX, "stat %s = inode %lu",
128 path, (unsigned long)mystat.st_ino);
129 ltrace_mark(0, mark_buf);
135 int main(int argc, char** argv)
141 static struct option long_opt[] = {
142 {"depth", 1, 0, 'd' },
143 {"help", 0, 0, 'h' },
144 {"mknod", 0, 0, 'm' },
145 {"output", 1, 0, 'o' },
146 {"trace", 1, 0, 't' },
147 {"verbose", 0, 0, 'v' },
151 char *outputfilename = NULL;
153 char pathname[PATH_MAX];
154 char mark_buf[PATH_MAX + 50];
155 char mycwd[PATH_MAX];
156 char *pname = argv[0];
158 while ((c = getopt_long(argc, argv, "d:mhvo:", long_opt, NULL)) != -1) {
161 opt_depth = atoi(optarg);
162 if ((opt_depth == 0) || (opt_depth > 1100))
175 outputfilename = optarg;
186 if (optind != (argc - 1))
189 base_pathname = argv[optind];
192 if (!getcwd(&mycwd[0], sizeof(mycwd))) {
193 fprintf(stderr, "%s: unable to getcwd()\n", pname);
197 printf("%s(pid=%d) depth=%d mknod=%d, basepathname=%s, trace=%d\n",
198 pname, mypid, opt_depth, opt_mknod, base_pathname, opt_trace);
201 printf("outputfilename=%s\n", outputfilename);
206 snprintf(mark_buf, PATH_MAX, "Initialize - mkdir %s; chdir %s",
207 base_pathname, base_pathname);
208 ltrace_mark(2, mark_buf);
211 if (do_mkdir(base_pathname)!=0)
213 if (do_chdir(base_pathname)!=0)
216 /* Create directory tree with depth level of subdirectories */
219 snprintf(mark_buf, PATH_MAX,
220 "Create Directory Tree (depth %d)", opt_depth);
221 ltrace_mark(2, mark_buf);
224 for (i = 0; i < opt_depth; i++) {
225 snprintf(pathname, sizeof(pathname), "%d", i + 1);
227 if (i == (opt_depth - 1)) {
231 snprintf(mark_buf, PATH_MAX,
232 "Tree Leaf (%d) %s/stat", i,
233 (opt_mknod ? "mknod" : "mkdir"));
234 ltrace_mark(3, mark_buf);
247 snprintf(mark_buf, sizeof(mark_buf),
248 "Tree Level (%d) mkdir/stat/chdir", i);
249 ltrace_mark(3, mark_buf);
258 /* Stat through directory tree with fullpaths */
261 snprintf(mark_buf, PATH_MAX, "Walk Directory Tree");
262 ltrace_mark(2, mark_buf);
265 do_chdir(base_pathname);
267 strncpy(pathname, base_pathname, sizeof(pathname));
269 c = strlen(base_pathname);
270 for (i = 0; i < opt_depth; i++) {
271 c += snprintf(pathname + c, sizeof(pathname) - c, "/%d", i+1);
274 snprintf(mark_buf, PATH_MAX, "stat %s", pathname);
275 ltrace_mark(2, mark_buf);
281 if (opt_trace && outputfilename) {
282 ltrace_write_file(outputfilename);
283 ltrace_add_processnames(outputfilename);
287 do_chdir(base_pathname);
289 printf("%s (pid=%d) done.\n", pname, mypid);