1716fd348SMartin Matuska /*
2716fd348SMartin Matuska * CDDL HEADER START
3716fd348SMartin Matuska *
4716fd348SMartin Matuska * The contents of this file are subject to the terms of the
5716fd348SMartin Matuska * Common Development and Distribution License (the "License").
6716fd348SMartin Matuska * You may not use this file except in compliance with the License.
7716fd348SMartin Matuska *
8716fd348SMartin Matuska * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*271171e0SMartin Matuska * or https://opensource.org/licenses/CDDL-1.0.
10716fd348SMartin Matuska * See the License for the specific language governing permissions
11716fd348SMartin Matuska * and limitations under the License.
12716fd348SMartin Matuska *
13716fd348SMartin Matuska * When distributing Covered Code, include this CDDL HEADER in each
14716fd348SMartin Matuska * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15716fd348SMartin Matuska * If applicable, add the following below this CDDL HEADER, with the
16716fd348SMartin Matuska * fields enclosed by brackets "[]" replaced with your own identifying
17716fd348SMartin Matuska * information: Portions Copyright [yyyy] [name of copyright owner]
18716fd348SMartin Matuska *
19716fd348SMartin Matuska * CDDL HEADER END
20716fd348SMartin Matuska */
21716fd348SMartin Matuska
22716fd348SMartin Matuska /*
23716fd348SMartin Matuska * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24716fd348SMartin Matuska * Use is subject to license terms.
25716fd348SMartin Matuska */
26716fd348SMartin Matuska
27716fd348SMartin Matuska #include <errno.h>
28716fd348SMartin Matuska #include <stdio.h>
29716fd348SMartin Matuska #include <stdlib.h>
30716fd348SMartin Matuska #include <unistd.h>
31716fd348SMartin Matuska #include <string.h>
32716fd348SMartin Matuska #include <fcntl.h>
33716fd348SMartin Matuska #ifdef __linux__
34716fd348SMartin Matuska #include <sys/xattr.h>
35716fd348SMartin Matuska #endif
36716fd348SMartin Matuska #include <sys/stat.h>
37716fd348SMartin Matuska #include <sys/types.h>
38716fd348SMartin Matuska #include <sys/param.h>
39716fd348SMartin Matuska
40716fd348SMartin Matuska #define TYPE_D 'D'
41716fd348SMartin Matuska #define TYPE_F 'F'
42716fd348SMartin Matuska
43716fd348SMartin Matuska static char fdname[MAXPATHLEN] = {0};
44716fd348SMartin Matuska static char *pbasedir = NULL;
45716fd348SMartin Matuska static int nlevel = 2;
46716fd348SMartin Matuska static int ndir = 2;
47716fd348SMartin Matuska static int nfile = 2;
48716fd348SMartin Matuska
49716fd348SMartin Matuska static void usage(char *this);
50716fd348SMartin Matuska static void crtfile(char *pname);
51716fd348SMartin Matuska static char *getfdname(char *pdir, char type, int level, int dir, int file);
52716fd348SMartin Matuska static int mktree(char *pbasedir, int level);
53716fd348SMartin Matuska
54716fd348SMartin Matuska int
main(int argc,char * argv[])55716fd348SMartin Matuska main(int argc, char *argv[])
56716fd348SMartin Matuska {
57716fd348SMartin Matuska int c, ret;
58716fd348SMartin Matuska
59716fd348SMartin Matuska while ((c = getopt(argc, argv, "b:l:d:f:")) != -1) {
60716fd348SMartin Matuska switch (c) {
61716fd348SMartin Matuska case 'b':
62716fd348SMartin Matuska pbasedir = optarg;
63716fd348SMartin Matuska break;
64716fd348SMartin Matuska case 'l':
65716fd348SMartin Matuska nlevel = atoi(optarg);
66716fd348SMartin Matuska break;
67716fd348SMartin Matuska case 'd':
68716fd348SMartin Matuska ndir = atoi(optarg);
69716fd348SMartin Matuska break;
70716fd348SMartin Matuska case 'f':
71716fd348SMartin Matuska nfile = atoi(optarg);
72716fd348SMartin Matuska break;
73716fd348SMartin Matuska case '?':
74716fd348SMartin Matuska usage(argv[0]);
75716fd348SMartin Matuska }
76716fd348SMartin Matuska }
77716fd348SMartin Matuska if (nlevel < 0 || ndir < 0 || nfile < 0 || pbasedir == NULL) {
78716fd348SMartin Matuska usage(argv[0]);
79716fd348SMartin Matuska }
80716fd348SMartin Matuska
81716fd348SMartin Matuska ret = mktree(pbasedir, 1);
82716fd348SMartin Matuska
83716fd348SMartin Matuska return (ret);
84716fd348SMartin Matuska }
85716fd348SMartin Matuska
86716fd348SMartin Matuska static void
usage(char * this)87716fd348SMartin Matuska usage(char *this)
88716fd348SMartin Matuska {
89716fd348SMartin Matuska (void) fprintf(stderr,
90716fd348SMartin Matuska "\tUsage: %s -b <base_dir> -l [nlevel] -d [ndir] -f [nfile]\n",
91716fd348SMartin Matuska this);
92716fd348SMartin Matuska exit(1);
93716fd348SMartin Matuska }
94716fd348SMartin Matuska
95716fd348SMartin Matuska static int
mktree(char * pdir,int level)96716fd348SMartin Matuska mktree(char *pdir, int level)
97716fd348SMartin Matuska {
98716fd348SMartin Matuska int d, f;
99716fd348SMartin Matuska char dname[MAXPATHLEN] = {0};
100716fd348SMartin Matuska char fname[MAXPATHLEN] = {0};
101716fd348SMartin Matuska
102716fd348SMartin Matuska if (level > nlevel) {
103716fd348SMartin Matuska return (1);
104716fd348SMartin Matuska }
105716fd348SMartin Matuska
106716fd348SMartin Matuska for (d = 0; d < ndir; d++) {
107716fd348SMartin Matuska (void) memset(dname, '\0', sizeof (dname));
108716fd348SMartin Matuska (void) strcpy(dname, getfdname(pdir, TYPE_D, level, d, 0));
109716fd348SMartin Matuska
110716fd348SMartin Matuska if (mkdir(dname, 0777) != 0) {
111716fd348SMartin Matuska (void) fprintf(stderr, "mkdir(%s) failed."
112716fd348SMartin Matuska "\n[%d]: %s.\n",
113716fd348SMartin Matuska dname, errno, strerror(errno));
114716fd348SMartin Matuska exit(errno);
115716fd348SMartin Matuska }
116716fd348SMartin Matuska
117716fd348SMartin Matuska /*
118716fd348SMartin Matuska * No sub-directory need be created, only create files in it.
119716fd348SMartin Matuska */
120716fd348SMartin Matuska if (mktree(dname, level+1) != 0) {
121716fd348SMartin Matuska for (f = 0; f < nfile; f++) {
122716fd348SMartin Matuska (void) memset(fname, '\0', sizeof (fname));
123716fd348SMartin Matuska (void) strcpy(fname,
124716fd348SMartin Matuska getfdname(dname, TYPE_F, level+1, d, f));
125716fd348SMartin Matuska crtfile(fname);
126716fd348SMartin Matuska }
127716fd348SMartin Matuska }
128716fd348SMartin Matuska }
129716fd348SMartin Matuska
130716fd348SMartin Matuska for (f = 0; f < nfile; f++) {
131716fd348SMartin Matuska (void) memset(fname, '\0', sizeof (fname));
132716fd348SMartin Matuska (void) strcpy(fname, getfdname(pdir, TYPE_F, level, d, f));
133716fd348SMartin Matuska crtfile(fname);
134716fd348SMartin Matuska }
135716fd348SMartin Matuska
136716fd348SMartin Matuska return (0);
137716fd348SMartin Matuska }
138716fd348SMartin Matuska
139716fd348SMartin Matuska static char *
getfdname(char * pdir,char type,int level,int dir,int file)140716fd348SMartin Matuska getfdname(char *pdir, char type, int level, int dir, int file)
141716fd348SMartin Matuska {
142716fd348SMartin Matuska size_t size = sizeof (fdname);
143716fd348SMartin Matuska if (snprintf(fdname, size, "%s/%c-l%dd%df%d", pdir, type, level, dir,
144716fd348SMartin Matuska file) >= size) {
145716fd348SMartin Matuska (void) fprintf(stderr, "fdname truncated\n");
146716fd348SMartin Matuska exit(EINVAL);
147716fd348SMartin Matuska }
148716fd348SMartin Matuska return (fdname);
149716fd348SMartin Matuska }
150716fd348SMartin Matuska
151716fd348SMartin Matuska static void
crtfile(char * pname)152716fd348SMartin Matuska crtfile(char *pname)
153716fd348SMartin Matuska {
154716fd348SMartin Matuska int fd = -1;
155716fd348SMartin Matuska int i, size;
156a0b956f5SMartin Matuska const char *context = "0123456789ABCDF";
157716fd348SMartin Matuska char *pbuf;
158716fd348SMartin Matuska
159716fd348SMartin Matuska if (pname == NULL) {
160716fd348SMartin Matuska exit(1);
161716fd348SMartin Matuska }
162716fd348SMartin Matuska
163716fd348SMartin Matuska size = sizeof (char) * 1024;
164716fd348SMartin Matuska pbuf = (char *)valloc(size);
165716fd348SMartin Matuska for (i = 0; i < size / strlen(context); i++) {
166716fd348SMartin Matuska int offset = i * strlen(context);
167716fd348SMartin Matuska (void) snprintf(pbuf+offset, size-offset, "%s", context);
168716fd348SMartin Matuska }
169716fd348SMartin Matuska
170716fd348SMartin Matuska if ((fd = open(pname, O_CREAT|O_RDWR, 0777)) < 0) {
171716fd348SMartin Matuska (void) fprintf(stderr, "open(%s, O_CREAT|O_RDWR, 0777) failed."
172716fd348SMartin Matuska "\n[%d]: %s.\n", pname, errno, strerror(errno));
173716fd348SMartin Matuska exit(errno);
174716fd348SMartin Matuska }
175716fd348SMartin Matuska if (write(fd, pbuf, 1024) < 1024) {
176716fd348SMartin Matuska (void) fprintf(stderr, "write(fd, pbuf, 1024) failed."
177716fd348SMartin Matuska "\n[%d]: %s.\n", errno, strerror(errno));
178716fd348SMartin Matuska exit(errno);
179716fd348SMartin Matuska }
180716fd348SMartin Matuska
181716fd348SMartin Matuska #ifdef __linux__
182716fd348SMartin Matuska if (fsetxattr(fd, "user.xattr", pbuf, 1024, 0) < 0) {
183716fd348SMartin Matuska (void) fprintf(stderr, "fsetxattr(fd, \"xattr\", pbuf, "
184716fd348SMartin Matuska "1024, 0) failed.\n[%d]: %s.\n", errno, strerror(errno));
185716fd348SMartin Matuska exit(errno);
186716fd348SMartin Matuska }
187716fd348SMartin Matuska #endif
188716fd348SMartin Matuska
189716fd348SMartin Matuska (void) close(fd);
190716fd348SMartin Matuska free(pbuf);
191716fd348SMartin Matuska }
192