1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 28 #include <stdio.h> 29 #include <stdlib.h> 30 #include <unistd.h> 31 #include <string.h> 32 #include <fcntl.h> 33 #include <sys/stat.h> 34 #include <sys/types.h> 35 #include <sys/errno.h> 36 #include <sys/param.h> 37 38 #define TYPE_D 'D' 39 #define TYPE_F 'F' 40 41 extern int errno; 42 43 static char fdname[MAXPATHLEN] = {0}; 44 static char *pbasedir = NULL; 45 static int nlevel = 2; 46 static int ndir = 2; 47 static int nfile = 2; 48 49 static void usage(char *this); 50 static void crtfile(char *pname); 51 static char *getfdname(char *pdir, char type, int level, int dir, int file); 52 static int mktree(char *pbasedir, int level); 53 54 int 55 main(int argc, char *argv[]) 56 { 57 int c, ret; 58 59 while ((c = getopt(argc, argv, "b:l:d:f:")) != -1) { 60 switch (c) { 61 case 'b': 62 pbasedir = optarg; 63 break; 64 case 'l': 65 nlevel = atoi(optarg); 66 break; 67 case 'd': 68 ndir = atoi(optarg); 69 break; 70 case 'f': 71 nfile = atoi(optarg); 72 break; 73 case '?': 74 usage(argv[0]); 75 } 76 } 77 if (nlevel < 0 || ndir < 0 || nfile < 0 || pbasedir == NULL) { 78 usage(argv[0]); 79 } 80 81 ret = mktree(pbasedir, 1); 82 83 return (ret); 84 } 85 86 static void 87 usage(char *this) 88 { 89 (void) fprintf(stderr, 90 "\tUsage: %s -b <base_dir> -l [nlevel] -d [ndir] -f [nfile]\n", 91 this); 92 exit(1); 93 } 94 95 static int 96 mktree(char *pdir, int level) 97 { 98 int d, f; 99 char dname[MAXPATHLEN] = {0}; 100 char fname[MAXPATHLEN] = {0}; 101 102 if (level > nlevel) { 103 return (1); 104 } 105 106 for (d = 0; d < ndir; d++) { 107 (void) memset(dname, '\0', sizeof (dname)); 108 (void) strcpy(dname, getfdname(pdir, TYPE_D, level, d, 0)); 109 110 if (mkdir(dname, 0777) != 0) { 111 (void) fprintf(stderr, "mkdir(%s) failed." 112 "\n[%d]: %s.\n", 113 dname, errno, strerror(errno)); 114 exit(errno); 115 } 116 117 /* 118 * No sub-directory need be created, only create files in it. 119 */ 120 if (mktree(dname, level+1) != 0) { 121 for (f = 0; f < nfile; f++) { 122 (void) memset(fname, '\0', sizeof (fname)); 123 (void) strcpy(fname, 124 getfdname(dname, TYPE_F, level+1, d, f)); 125 crtfile(fname); 126 } 127 } 128 } 129 130 for (f = 0; f < nfile; f++) { 131 (void) memset(fname, '\0', sizeof (fname)); 132 (void) strcpy(fname, getfdname(pdir, TYPE_F, level, d, f)); 133 crtfile(fname); 134 } 135 136 return (0); 137 } 138 139 static char * 140 getfdname(char *pdir, char type, int level, int dir, int file) 141 { 142 (void) snprintf(fdname, sizeof (fdname), 143 "%s/%c-l%dd%df%d", pdir, type, level, dir, file); 144 return (fdname); 145 } 146 147 static void 148 crtfile(char *pname) 149 { 150 int fd = -1; 151 int afd = -1; 152 int i, size; 153 char *context = "0123456789ABCDF"; 154 char *pbuf; 155 156 if (pname == NULL) { 157 exit(1); 158 } 159 160 size = sizeof (char) * 1024; 161 pbuf = (char *)valloc(size); 162 for (i = 0; i < size / strlen(context); i++) { 163 int offset = i * strlen(context); 164 (void) snprintf(pbuf+offset, size-offset, "%s", context); 165 } 166 167 if ((fd = open(pname, O_CREAT|O_RDWR, 0777)) < 0) { 168 (void) fprintf(stderr, "open(%s, O_CREAT|O_RDWR, 0777) failed." 169 "\n[%d]: %s.\n", pname, errno, strerror(errno)); 170 exit(errno); 171 } 172 if (write(fd, pbuf, 1024) < 1024) { 173 (void) fprintf(stderr, "write(fd, pbuf, 1024) failed." 174 "\n[%d]: %s.\n", errno, strerror(errno)); 175 exit(errno); 176 } 177 178 #if UNSUPPORTED 179 if ((afd = openat(fd, "xattr", O_CREAT | O_RDWR | O_XATTR, 0777)) < 0) { 180 (void) fprintf(stderr, "openat failed.\n[%d]: %s.\n", 181 errno, strerror(errno)); 182 exit(errno); 183 } 184 if (write(afd, pbuf, 1024) < 1024) { 185 (void) fprintf(stderr, "write(afd, pbuf, 1024) failed." 186 "\n[%d]: %s.\n", errno, strerror(errno)); 187 exit(errno); 188 } 189 190 (void) close(afd); 191 #endif 192 (void) close(fd); 193 free(pbuf); 194 } 195