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