Whamcloud - gitweb
b=16098
[fs/lustre-release.git] / lustre / tests / mkdirdeep.c
index cfd1535..853e1e7 100644 (file)
@@ -1,9 +1,43 @@
 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
  * vim:expandtab:shiftwidth=8:tabstop=8:
  *
+ * GPL HEADER START
+ *
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 only,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License version 2 for more details (a copy is included
+ * in the LICENSE file that accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; If not, see
+ * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ *
+ * GPL HEADER END
+ */
+/*
+ * Copyright  2008 Sun Microsystems, Inc. All rights reserved
+ * Use is subject to license terms.
+ */
+/*
+ * This file is part of Lustre, http://www.lustre.org/
+ * Lustre is a trademark of Sun Microsystems, Inc.
+ *
+ * lustre/tests/mkdirdeep.c
+ *
  * Compile with:
- * cc -I../../portals/include -o mkdirdeep mkdirdeep.c 
- *    -L../../portals/linux/utils -lptlctl 
+ * cc -I../../lnet/include -o mkdirdeep mkdirdeep.c
+ *    -L../../lnet/linux/utils -lptlctl
  */
 
 #include <stdio.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <linux/limits.h>
-#include <portals/lltrace.h>
+#include <libcfs/lltrace.h>
 
-static int opt_depth = 1;
-static int opt_mknod = 0; 
 static int opt_verbose = 0;
-static int opt_trace = 1;
-static char* basepathname = 0;
-static char mycwd[PATH_MAX];
-static char* pname = 0;
-static char* outputfilename = 0;
+static int opt_trace = 0;
 
-void usage()
+void usage(const char *pname)
 {
-        fprintf(stderr, "Usage: %s --depth <d> --output <outputtracefilename>"
-                "[--mknod] [--verbose] [--notrace] <basepath>\n", pname);
+        fprintf(stderr, "Usage: %s --depth <d> [--output <outputtracefilename>]"
+                " [--mknod] [--verbose] [--notrace] <basepath>\n", pname);
         exit(1);
 }
 
-int do_mkdir(charpath)
+int do_mkdir(char *path)
 {
         int rc = mkdir(path, 0755);
-        if (rc!=0) 
+
+        if (rc) {
                 fprintf(stderr, "mkdir(%s) failed: %s\n",
                         path, strerror(errno));
+                exit(1);
+        }
         if (opt_verbose)
                 printf("mkdir %s\n", path);
+
         return rc;
 }
 
 
-int do_mknod(charpath)
+int do_mknod(char *path)
 {
         int rc = mknod(path, 0755, S_IFIFO);
-        if (rc!=0) 
+
+        if (rc) {
                 fprintf(stderr, "mkdir(%s) failed: %s\n",
                         path, strerror(errno));
+                exit(1);
+        }
         if (opt_verbose)
                 printf("mknod %s\n", path);
+
         return rc;
 }
 
 int do_chdir(char* path)
 {
         int rc = chdir(path);
-        if (rc!=0) 
+
+        if (rc) {
                 fprintf(stderr, "chdir(%s) failed: %s\n",
                         path, strerror(errno));
+                exit(1);
+        }
         if (opt_verbose)
                 printf("chdir %s\n", path);
 
         return rc;
 }
 
-
-int do_stat(char* path)
+int do_stat(char *path)
 {
-        char mark_buf[PATH_MAX];
+        char mark_buf[PATH_MAX + 50];
         struct stat mystat;
         int rc = stat(path, &mystat);
-        if (rc!=0) 
+
+        if (rc) {
                 fprintf(stderr, "stat(%s) failed: %s\n",
                         path, strerror(errno));
+                exit(1);
+        }
         if (opt_verbose)
-                printf("stat %s = inode %lu\n", path, mystat.st_ino);
+                printf("stat %s = inode %lu\n", path,
+                       (unsigned long)mystat.st_ino);
 
         if (opt_trace) {
-                snprintf(mark_buf, PATH_MAX, "stat %s = inode %lu", 
-                         path, mystat.st_ino);
+                snprintf(mark_buf, PATH_MAX, "stat %s = inode %lu",
+                         path, (unsigned long)mystat.st_ino);
                 ltrace_mark(0, mark_buf);
         }
 
@@ -92,44 +134,40 @@ int do_stat(char* path)
 
 int main(int argc, char** argv)
 {
-        int c, opt_index, i, mypid;
-
-        static struct option long_options[] = {
-                {"depth", 1, 0, 0 },
-                {"help", 0, 0, 0 },
-                {"mknod", 0, 0, 0 },  
-                {"verbose", 0, 0, 0 },  
-                {"notrace", 0, 0, 0 },  
-                {"output", 1, 0, 0 },  
+        int c, i, mypid;
+        int opt_depth = 1;
+        int opt_mknod = 0;
+
+        static struct option long_opt[] = {
+                {"depth", 1, 0, 'd' },
+                {"help", 0, 0, 'h' },
+                {"mknod", 0, 0, 'm' },
+                {"output", 1, 0, 'o' },
+                {"trace", 1, 0, 't' },
+                {"verbose", 0, 0, 'v' },
                 {0,0,0,0}
         };
 
-        char full_pathname[PATH_MAX];
-        char rel_pathname[PATH_MAX];
-        char mark_buf[PATH_MAX];
+        char *outputfilename = NULL;
+        char *base_pathname;
+        char pathname[PATH_MAX];
+        char mark_buf[PATH_MAX + 50];
+        char mycwd[PATH_MAX];
+        char *pname = argv[0];
 
-        pname = strdup(argv[0]);
-        
-        while (1) {
-                c = getopt_long(argc, argv, "d:mhv", long_options, &opt_index);
-                if (c == -1)
-                        break;
-                if (c==0) {
-                        if (!strcmp(long_options[opt_index].name, "notrace")) {
-                                opt_trace = 0;
-                                continue;
-                        }
-                        c = long_options[opt_index].name[0];
-                }
+        while ((c = getopt_long(argc, argv, "d:mhvo:", long_opt, NULL)) != -1) {
                 switch (c) {
-                case 'd': 
+                case 'd':
                         opt_depth = atoi(optarg);
-                        if ((opt_depth == 0) || (opt_depth > 100))
-                                usage();
+                        if ((opt_depth == 0) || (opt_depth > 1100))
+                                usage(pname);
                         break;
                 case 'm':
                         opt_mknod = 1;
                         break;
+                case 't':
+                        opt_trace = 1;
+                        break;
                 case 'v':
                         opt_verbose = 1;
                         break;
@@ -137,92 +175,86 @@ int main(int argc, char** argv)
                         outputfilename = optarg;
                         break;
                 case 'h':
-                case '?': 
-                case ':': 
+                case '?':
+                case ':':
                 default:
-                        usage();
+                        usage(pname);
                         break;
                 }
         }
-                
-        if (optind != (argc-1)) 
-                usage();
 
-        if (outputfilename == NULL)
-                usage();
+        if (optind != (argc - 1))
+                usage(pname);
 
-        basepathname = argv[optind];
+        base_pathname = argv[optind];
         mypid = getpid();
-        
-        printf("%s(pid=%d) depth=%d mknod=%d, basepathname=%s, "
-               "trace=%d, outputfilename=%s\n",
-               pname, mypid, opt_depth, opt_mknod, basepathname, opt_trace, 
-               outputfilename);
 
         if (!getcwd(&mycwd[0], sizeof(mycwd))) {
                 fprintf(stderr, "%s: unable to getcwd()\n", pname);
                 exit(1);
         }
 
+        printf("%s(pid=%d) depth=%d mknod=%d, basepathname=%s, trace=%d\n",
+               pname, mypid, opt_depth, opt_mknod, base_pathname, opt_trace);
+
+        if (outputfilename)
+                printf("outputfilename=%s\n", outputfilename);
+
         if (opt_trace) {
                 ltrace_start();
                 ltrace_clear();
-                snprintf(mark_buf, PATH_MAX, 
-                         "Initialize - mkdir %s; chdir %s",
-                         basepathname, basepathname);
+                snprintf(mark_buf, PATH_MAX, "Initialize - mkdir %s; chdir %s",
+                         base_pathname, base_pathname);
                 ltrace_mark(2, mark_buf);
         }
 
-        if (do_mkdir(basepathname)!=0)
+        if (do_mkdir(base_pathname)!=0)
                 exit(1);
-        if (do_chdir(basepathname)!=0)
+        if (do_chdir(base_pathname)!=0)
                 exit(1);
 
         /* Create directory tree with depth level of subdirectories */
 
         if (opt_trace) {
-                snprintf(mark_buf, PATH_MAX, 
+                snprintf(mark_buf, PATH_MAX,
                          "Create Directory Tree (depth %d)", opt_depth);
                 ltrace_mark(2, mark_buf);
         }
 
-        for (i=0; i<opt_depth; i++) {
-                
-                snprintf(rel_pathname, sizeof(rel_pathname),"%d", i+1);
-                
-                 if (i == (opt_depth-1)) {
-                         /* Last Iteration */
-                         
-                         if (opt_trace) {
-                                 snprintf(mark_buf, PATH_MAX, 
-                                          "Tree Leaf (%d) %s/stat", i,
-                                          (opt_mknod ? "mknod" : "mkdir"));
-                                 ltrace_mark(3, mark_buf);
-                         }
-                         
-                         if (opt_mknod)
-                                 do_mknod(rel_pathname);
-                         else
-                                 do_mkdir(rel_pathname);
-                         /* Now stat it */
-                         do_stat(rel_pathname);
-                 }
-                else {
+        for (i = 0; i < opt_depth; i++) {
+                snprintf(pathname, sizeof(pathname), "%d", i + 1);
+
+                if (i == (opt_depth - 1)) {
+                        /* Last Iteration */
+
+                        if (opt_trace) {
+                                snprintf(mark_buf, PATH_MAX,
+                                         "Tree Leaf (%d) %s/stat", i,
+                                         (opt_mknod ? "mknod" : "mkdir"));
+                                ltrace_mark(3, mark_buf);
+                        }
+
+                        if (opt_mknod)
+                                do_mknod(pathname);
+                        else
+                                do_mkdir(pathname);
+                        /* Now stat it */
+                        do_stat(pathname);
+                } else {
                         /* Not Leaf */
 
                         if (opt_trace) {
-                                snprintf(mark_buf, PATH_MAX, 
-                                         "Tree Level (%d) mkdir/stat/chdir",
-                                         i);
+                                snprintf(mark_buf, sizeof(mark_buf),
+                                         "Tree Level (%d) mkdir/stat/chdir", i);
                                 ltrace_mark(3, mark_buf);
                         }
-                        
-                        do_mkdir(rel_pathname);
-                        do_stat(rel_pathname);
-                        do_chdir(rel_pathname);
+
+                        do_mkdir(pathname);
+                        do_stat(pathname);
+                        do_chdir(pathname);
                 }
         }
-        
+
         /* Stat through directory tree with fullpaths */
 
         if (opt_trace) {
@@ -230,46 +262,31 @@ int main(int argc, char** argv)
                 ltrace_mark(2, mark_buf);
         }
 
-        do_chdir(basepathname);
+        do_chdir(base_pathname);
 
-        strncpy(full_pathname, basepathname, sizeof(full_pathname));
+        strncpy(pathname, base_pathname, sizeof(pathname));
 
-        for (i=0; i<opt_depth; i++) {
-                snprintf(rel_pathname, sizeof(rel_pathname),"%d", i+1);
-                strcat(full_pathname, "/");
-                strcat(full_pathname, rel_pathname);
+        c = strlen(base_pathname);
+        for (i = 0; i < opt_depth; i++) {
+                c += snprintf(pathname + c, sizeof(pathname) - c, "/%d", i+1);
 
                 if (opt_trace) {
-                        snprintf(mark_buf, PATH_MAX, "stat %s", 
-                                 full_pathname);
+                        snprintf(mark_buf, PATH_MAX, "stat %s", pathname);
                         ltrace_mark(2, mark_buf);
                 }
 
-                do_stat(full_pathname);
+                do_stat(pathname);
         }
 
-        /* Cleanup */
-
-        if (opt_trace) {
-                snprintf(mark_buf, PATH_MAX, "Cleanup");
-                ltrace_mark(2, mark_buf);
-        }
-
-        if (opt_trace) {
+        if (opt_trace && outputfilename) {
                     ltrace_write_file(outputfilename);
                     ltrace_add_processnames(outputfilename);
                     ltrace_stop();
         }
 
-        do_chdir(basepathname);        
-        
-        snprintf(full_pathname, sizeof(full_pathname), 
-                 "rm -rf %s\n", basepathname);
-        if (opt_verbose) 
-                printf("Cleanup: %s", full_pathname);
-
-        system(full_pathname);
+        do_chdir(base_pathname);
 
         printf("%s (pid=%d) done.\n", pname, mypid);
+
         return 0;
 }