dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / test / zfs-tests / cmd / mktree / mktree.c
bloba6a5adfb2f5164ea1c0d990157287a06cd819560
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <fcntl.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34 #include <sys/errno.h>
35 #include <sys/param.h>
37 #define TYPE_D 'D'
38 #define TYPE_F 'F'
40 extern int errno;
42 static char fdname[MAXPATHLEN] = {0};
43 static char *pbasedir = NULL;
44 static int nlevel = 2;
45 static int ndir = 2;
46 static int nfile = 2;
48 static void usage(char *this);
49 static void crtfile(char *pname);
50 static char *getfdname(char *pdir, char type, int level, int dir, int file);
51 static int mktree(char *pbasedir, int level);
53 int
54 main(int argc, char *argv[])
56 int c, ret;
58 while ((c = getopt(argc, argv, "b:l:d:f:")) != -1) {
59 switch (c) {
60 case 'b':
61 pbasedir = optarg;
62 break;
63 case 'l':
64 nlevel = atoi(optarg);
65 break;
66 case 'd':
67 ndir = atoi(optarg);
68 break;
69 case 'f':
70 nfile = atoi(optarg);
71 break;
72 case '?':
73 usage(argv[0]);
76 if (nlevel < 0 || ndir < 0 || nfile < 0 || pbasedir == NULL) {
77 usage(argv[0]);
80 ret = mktree(pbasedir, 1);
82 return (ret);
85 static void
86 usage(char *this)
88 (void) fprintf(stderr,
89 "\tUsage: %s -b <base_dir> -l [nlevel] -d [ndir] -f [nfile]\n",
90 this);
91 exit(1);
94 static int
95 mktree(char *pdir, int level)
97 int d, f;
98 char dname[MAXPATHLEN] = {0};
99 char fname[MAXPATHLEN] = {0};
101 if (level > nlevel) {
102 return (1);
105 for (d = 0; d < ndir; d++) {
106 (void) memset(dname, '\0', sizeof (dname));
107 (void) strcpy(dname, getfdname(pdir, TYPE_D, level, d, 0));
109 if (mkdir(dname, 0777) != 0) {
110 (void) fprintf(stderr, "mkdir(%s) failed."
111 "\n[%d]: %s.\n",
112 dname, errno, strerror(errno));
113 exit(errno);
117 * No sub-directory need be created, only create files in it.
119 if (mktree(dname, level+1) != 0) {
120 for (f = 0; f < nfile; f++) {
121 (void) memset(fname, '\0', sizeof (fname));
122 (void) strcpy(fname,
123 getfdname(dname, TYPE_F, level+1, d, f));
124 crtfile(fname);
129 for (f = 0; f < nfile; f++) {
130 (void) memset(fname, '\0', sizeof (fname));
131 (void) strcpy(fname, getfdname(pdir, TYPE_F, level, d, f));
132 crtfile(fname);
135 return (0);
138 static char *
139 getfdname(char *pdir, char type, int level, int dir, int file)
141 (void) snprintf(fdname, sizeof (fdname),
142 "%s/%c-l%dd%df%d", pdir, type, level, dir, file);
143 return (fdname);
146 static void
147 crtfile(char *pname)
149 int fd = -1;
150 int afd = -1;
151 int i, size;
152 char *context = "0123456789ABCDF";
153 char *pbuf;
155 if (pname == NULL) {
156 exit(1);
159 size = sizeof (char) * 1024;
160 pbuf = (char *)valloc(size);
161 for (i = 0; i < size / strlen(context); i++) {
162 int offset = i * strlen(context);
163 (void) snprintf(pbuf+offset, size-offset, "%s", context);
166 if ((fd = open(pname, O_CREAT|O_RDWR, 0777)) < 0) {
167 (void) fprintf(stderr, "open(%s, O_CREAT|O_RDWR, 0777) failed."
168 "\n[%d]: %s.\n", pname, errno, strerror(errno));
169 exit(errno);
171 if (write(fd, pbuf, 1024) < 1024) {
172 (void) fprintf(stderr, "write(fd, pbuf, 1024) failed."
173 "\n[%d]: %s.\n", errno, strerror(errno));
174 exit(errno);
177 if ((afd = openat(fd, "xattr", O_CREAT | O_RDWR | O_XATTR, 0777)) < 0) {
178 (void) fprintf(stderr, "openat failed.\n[%d]: %s.\n",
179 errno, strerror(errno));
180 exit(errno);
182 if (write(afd, pbuf, 1024) < 1024) {
183 (void) fprintf(stderr, "write(afd, pbuf, 1024) failed."
184 "\n[%d]: %s.\n", errno, strerror(errno));
185 exit(errno);
188 (void) close(afd);
189 (void) close(fd);
190 free(pbuf);