xref: /freebsd/tests/sys/cddl/zfs/bin/mktree.c (revision 2ba1d4970a06a1660b46f6fd99351d154b295683)
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 #pragma ident	"@(#)mktree.c	1.3	07/05/25 SMI"
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <fcntl.h>
34 #include <sys/stat.h>
35 #include <sys/types.h>
36 #include <sys/errno.h>
37 #include <sys/param.h>
38 
39 #define	TYPE_D 'D'
40 #define	TYPE_F 'F'
41 
42 extern int errno;
43 
44 static char fdname[MAXPATHLEN] = {0};
45 static char *pbasedir = NULL;
46 static int nlevel = 2;
47 static int ndir = 2;
48 static int nfile = 2;
49 
50 static void  usage(char *this);
51 static void  crtfile(char *pname);
52 static char *getfdname(char *pdir, char type, int level, int dir, int file);
53 static int   mktree(char *pbasedir, int level);
54 
55 int
56 main(int argc, char *argv[])
57 {
58 	int c, ret;
59 
60 	while ((c = getopt(argc, argv, "b:l:d:f:")) != -1) {
61 		switch (c) {
62 		case 'b':
63 			pbasedir = optarg;
64 			break;
65 		case 'l':
66 			nlevel = atoi(optarg);
67 			break;
68 		case 'd':
69 			ndir = atoi(optarg);
70 			break;
71 		case 'f':
72 			nfile = atoi(optarg);
73 			break;
74 		case '?':
75 			usage(argv[0]);
76 		}
77 	}
78 	if (nlevel < 0 || ndir < 0 || nfile < 0 || pbasedir == NULL) {
79 		usage(argv[0]);
80 	}
81 
82 	ret = mktree(pbasedir, 1);
83 
84 	return (ret);
85 }
86 
87 static void
88 usage(char *this)
89 {
90 	(void) fprintf(stderr,
91 	    "\tUsage: %s -b <base_dir> -l [nlevel] -d [ndir] -f [nfile]\n",
92 	    this);
93 	exit(1);
94 }
95 
96 static int
97 mktree(char *pdir, int level)
98 {
99 	int d, f;
100 	char dname[MAXPATHLEN] = {0};
101 	char fname[MAXPATHLEN] = {0};
102 
103 	if (level > nlevel) {
104 		return (1);
105 	}
106 
107 	for (d = 0; d < ndir; d++) {
108 		(void) memset(dname, '\0', sizeof (dname));
109 		(void) strcpy(dname, getfdname(pdir, TYPE_D, level, d, 0));
110 
111 		if (mkdir(dname, 0777) != 0) {
112 			(void) fprintf(stderr, "mkdir(%s) failed."
113 			    "\n[%d]: %s.\n",
114 			    dname, errno, strerror(errno));
115 			exit(errno);
116 		}
117 
118 		/*
119 		 * No sub-directory need be created, only create files in it.
120 		 */
121 		if (mktree(dname, level+1) != 0) {
122 			for (f = 0; f < nfile; f++) {
123 				(void) memset(fname, '\0', sizeof (fname));
124 				(void) strcpy(fname,
125 				    getfdname(dname, TYPE_F, level+1, d, f));
126 				crtfile(fname);
127 			}
128 		}
129 	}
130 
131 	for (f = 0; f < nfile; f++) {
132 		(void) memset(fname, '\0', sizeof (fname));
133 		(void) strcpy(fname, getfdname(pdir, TYPE_F, level, d, f));
134 		crtfile(fname);
135 	}
136 
137 	return (0);
138 }
139 
140 static char *
141 getfdname(char *pdir, char type, int level, int dir, int file)
142 {
143 	(void) snprintf(fdname, sizeof (fdname),
144 	    "%s/%c-l%dd%df%d", pdir, type, level, dir, file);
145 	return (fdname);
146 }
147 
148 static void
149 crtfile(char *pname)
150 {
151 	int fd = -1;
152 	int afd = -1;
153 	int i, size;
154 	char *context = "0123456789ABCDF";
155 	char *pbuf;
156 
157 	if (pname == NULL) {
158 		exit(1);
159 	}
160 
161 	size = sizeof (char) * 1024;
162 	pbuf = (char *)valloc(size);
163 	for (i = 0; i < size / strlen(context); i++) {
164 		int offset = i * strlen(context);
165 		(void) snprintf(pbuf+offset, size-offset, "%s", context);
166 	}
167 
168 	if ((fd = open(pname, O_CREAT|O_RDWR, 0777)) < 0) {
169 		(void) fprintf(stderr, "open(%s, O_CREAT|O_RDWR, 0777) failed."
170 		    "\n[%d]: %s.\n", pname, errno, strerror(errno));
171 		exit(errno);
172 	}
173 	if (write(fd, pbuf, 1024) < 1024) {
174 		(void) fprintf(stderr, "write(fd, pbuf, 1024) failed."
175 		    "\n[%d]: %s.\n", errno, strerror(errno));
176 		exit(errno);
177 	}
178 
179 #if UNSUPPORTED
180 	if ((afd = openat(fd, "xattr", O_CREAT | O_RDWR | O_XATTR, 0777)) < 0) {
181 		(void) fprintf(stderr, "openat failed.\n[%d]: %s.\n",
182 		    errno, strerror(errno));
183 		exit(errno);
184 	}
185 	if (write(afd, pbuf, 1024) < 1024) {
186 		(void) fprintf(stderr, "write(afd, pbuf, 1024) failed."
187 		    "\n[%d]: %s.\n", errno, strerror(errno));
188 		exit(errno);
189 	}
190 
191 	(void) close(afd);
192 #endif
193 	(void) close(fd);
194 	free(pbuf);
195 }
196