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.
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.
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]
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
34 #include <sys/xattr.h>
37 #include <sys/types.h>
38 #include <sys/param.h>
43 static char fdname[MAXPATHLEN] = {0};
44 static char *pbasedir = NULL;
45 static int nlevel = 2;
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);
55 main(int argc, char *argv[])
59 while ((c = getopt(argc, argv, "b:l:d:f:")) != -1) {
65 nlevel = atoi(optarg);
77 if (nlevel < 0 || ndir < 0 || nfile < 0 || pbasedir == NULL) {
81 ret = mktree(pbasedir, 1);
89 (void) fprintf(stderr,
90 "\tUsage: %s -b <base_dir> -l [nlevel] -d [ndir] -f [nfile]\n",
96 mktree(char *pdir, int level)
99 char dname[MAXPATHLEN] = {0};
100 char fname[MAXPATHLEN] = {0};
102 if (level > nlevel) {
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));
110 if (mkdir(dname, 0777) != 0) {
111 (void) fprintf(stderr, "mkdir(%s) failed."
113 dname, errno, strerror(errno));
118 * No sub-directory need be created, only create files in it.
120 if (mktree(dname, level+1) != 0) {
121 for (f = 0; f < nfile; f++) {
122 (void) memset(fname, '\0', sizeof (fname));
124 getfdname(dname, TYPE_F, level+1, d, f));
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));
140 getfdname(char *pdir, char type, int level, int dir, int file)
142 size_t size = sizeof (fdname);
143 if (snprintf(fdname, size, "%s/%c-l%dd%df%d", pdir, type, level, dir,
145 (void) fprintf(stderr, "fdname truncated\n");
156 char *context = "0123456789ABCDF";
163 size = sizeof (char) * 1024;
164 pbuf = (char *)valloc(size);
165 for (i = 0; i < size / strlen(context); i++) {
166 int offset = i * strlen(context);
167 (void) snprintf(pbuf+offset, size-offset, "%s", context);
170 if ((fd = open(pname, O_CREAT|O_RDWR, 0777)) < 0) {
171 (void) fprintf(stderr, "open(%s, O_CREAT|O_RDWR, 0777) failed."
172 "\n[%d]: %s.\n", pname, errno, strerror(errno));
175 if (write(fd, pbuf, 1024) < 1024) {
176 (void) fprintf(stderr, "write(fd, pbuf, 1024) failed."
177 "\n[%d]: %s.\n", errno, strerror(errno));
182 if (fsetxattr(fd, "user.xattr", pbuf, 1024, 0) < 0) {
183 (void) fprintf(stderr, "fsetxattr(fd, \"xattr\", pbuf, "
184 "1024, 0) failed.\n[%d]: %s.\n", errno, strerror(errno));