xref: /freebsd/sys/contrib/openzfs/tests/zfs-tests/cmd/mktree.c (revision f1c4c3daccbaf3820f0e2224de53df12fc952fcc)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #include <errno.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <fcntl.h>
34 #ifdef __linux__
35 #include <sys/xattr.h>
36 #endif
37 #include <sys/stat.h>
38 #include <sys/types.h>
39 #include <sys/param.h>
40 
41 #define	TYPE_D 'D'
42 #define	TYPE_F 'F'
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 	size_t size = sizeof (fdname);
144 	if (snprintf(fdname, size, "%s/%c-l%dd%df%d", pdir, type, level, dir,
145 	    file) >= size) {
146 		(void) fprintf(stderr, "fdname truncated\n");
147 		exit(EINVAL);
148 	}
149 	return (fdname);
150 }
151 
152 static void
153 crtfile(char *pname)
154 {
155 	int fd;
156 	int i, size;
157 	const char *context = "0123456789ABCDF";
158 	char *pbuf;
159 
160 	if (pname == NULL) {
161 		exit(1);
162 	}
163 
164 	size = sizeof (char) * 1024;
165 	pbuf = (char *)valloc(size);
166 	for (i = 0; i < size / strlen(context); i++) {
167 		int offset = i * strlen(context);
168 		(void) snprintf(pbuf+offset, size-offset, "%s", context);
169 	}
170 
171 	if ((fd = open(pname, O_CREAT|O_RDWR, 0777)) < 0) {
172 		(void) fprintf(stderr, "open(%s, O_CREAT|O_RDWR, 0777) failed."
173 		    "\n[%d]: %s.\n", pname, errno, strerror(errno));
174 		exit(errno);
175 	}
176 	if (write(fd, pbuf, 1024) < 1024) {
177 		(void) fprintf(stderr, "write(fd, pbuf, 1024) failed."
178 		    "\n[%d]: %s.\n", errno, strerror(errno));
179 		exit(errno);
180 	}
181 
182 #ifdef __linux__
183 	if (fsetxattr(fd, "user.xattr", pbuf, 1024, 0) < 0) {
184 		(void) fprintf(stderr, "fsetxattr(fd, \"xattr\", pbuf, "
185 		    "1024, 0) failed.\n[%d]: %s.\n", errno, strerror(errno));
186 		exit(errno);
187 	}
188 #endif
189 
190 	(void) close(fd);
191 	free(pbuf);
192 }
193