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