]>
git.proxmox.com Git - mirror_zfs-debian.git/blob - tests/zfs-tests/cmd/mktree/mktree.c
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]
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
33 #include <sys/xattr.h>
35 #include <sys/types.h>
36 #include <sys/param.h>
41 static char fdname
[MAXPATHLEN
] = {0};
42 static char *pbasedir
= NULL
;
43 static int nlevel
= 2;
47 static void usage(char *this);
48 static void crtfile(char *pname
);
49 static char *getfdname(char *pdir
, char type
, int level
, int dir
, int file
);
50 static int mktree(char *pbasedir
, int level
);
53 main(int argc
, char *argv
[])
57 while ((c
= getopt(argc
, argv
, "b:l:d:f:")) != -1) {
63 nlevel
= atoi(optarg
);
75 if (nlevel
< 0 || ndir
< 0 || nfile
< 0 || pbasedir
== NULL
) {
79 ret
= mktree(pbasedir
, 1);
87 (void) fprintf(stderr
,
88 "\tUsage: %s -b <base_dir> -l [nlevel] -d [ndir] -f [nfile]\n",
94 mktree(char *pdir
, int level
)
97 char dname
[MAXPATHLEN
] = {0};
98 char fname
[MAXPATHLEN
] = {0};
100 if (level
> nlevel
) {
104 for (d
= 0; d
< ndir
; d
++) {
105 (void) memset(dname
, '\0', sizeof (dname
));
106 (void) strcpy(dname
, getfdname(pdir
, TYPE_D
, level
, d
, 0));
108 if (mkdir(dname
, 0777) != 0) {
109 (void) fprintf(stderr
, "mkdir(%s) failed."
111 dname
, errno
, strerror(errno
));
116 * No sub-directory need be created, only create files in it.
118 if (mktree(dname
, level
+1) != 0) {
119 for (f
= 0; f
< nfile
; f
++) {
120 (void) memset(fname
, '\0', sizeof (fname
));
122 getfdname(dname
, TYPE_F
, level
+1, d
, f
));
128 for (f
= 0; f
< nfile
; f
++) {
129 (void) memset(fname
, '\0', sizeof (fname
));
130 (void) strcpy(fname
, getfdname(pdir
, TYPE_F
, level
, d
, f
));
138 getfdname(char *pdir
, char type
, int level
, int dir
, int file
)
140 size_t size
= sizeof (fdname
);
141 if (snprintf(fdname
, size
, "%s/%c-l%dd%df%d", pdir
, type
, level
, dir
,
143 (void) fprintf(stderr
, "fdname truncated\n");
154 char *context
= "0123456789ABCDF";
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
);
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
));
173 if (write(fd
, pbuf
, 1024) < 1024) {
174 (void) fprintf(stderr
, "write(fd, pbuf, 1024) failed."
175 "\n[%d]: %s.\n", errno
, strerror(errno
));
179 if (fsetxattr(fd
, "user.xattr", pbuf
, 1024, 0) < 0) {
180 (void) fprintf(stderr
, "fsetxattr(fd, \"xattr\", pbuf, "
181 "1024, 0) failed.\n[%d]: %s.\n", errno
, strerror(errno
));