Path: blob/main/sys/contrib/openzfs/tests/zfs-tests/cmd/mktree.c
48529 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* 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.LICENSE9* or https://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/2122/*23* Copyright 2007 Sun Microsystems, Inc. All rights reserved.24* Use is subject to license terms.25*/2627#include <errno.h>28#include <stdio.h>29#include <stdlib.h>30#include <unistd.h>31#include <string.h>32#include <fcntl.h>33#ifdef __linux__34#include <sys/xattr.h>35#endif36#include <sys/stat.h>37#include <sys/types.h>38#include <sys/param.h>3940#define TYPE_D 'D'41#define TYPE_F 'F'4243static char fdname[MAXPATHLEN] = {0};44static char *pbasedir = NULL;45static int nlevel = 2;46static int ndir = 2;47static int nfile = 2;4849static void usage(char *this);50static void crtfile(char *pname);51static char *getfdname(char *pdir, char type, int level, int dir, int file);52static int mktree(char *pbasedir, int level);5354int55main(int argc, char *argv[])56{57int c, ret;5859while ((c = getopt(argc, argv, "b:l:d:f:")) != -1) {60switch (c) {61case 'b':62pbasedir = optarg;63break;64case 'l':65nlevel = atoi(optarg);66break;67case 'd':68ndir = atoi(optarg);69break;70case 'f':71nfile = atoi(optarg);72break;73case '?':74usage(argv[0]);75}76}77if (nlevel < 0 || ndir < 0 || nfile < 0 || pbasedir == NULL) {78usage(argv[0]);79}8081ret = mktree(pbasedir, 1);8283return (ret);84}8586static void87usage(char *this)88{89(void) fprintf(stderr,90"\tUsage: %s -b <base_dir> -l [nlevel] -d [ndir] -f [nfile]\n",91this);92exit(1);93}9495static int96mktree(char *pdir, int level)97{98int d, f;99char dname[MAXPATHLEN] = {0};100char fname[MAXPATHLEN] = {0};101102if (level > nlevel) {103return (1);104}105106for (d = 0; d < ndir; d++) {107(void) memset(dname, '\0', sizeof (dname));108(void) strcpy(dname, getfdname(pdir, TYPE_D, level, d, 0));109110if (mkdir(dname, 0777) != 0) {111(void) fprintf(stderr, "mkdir(%s) failed."112"\n[%d]: %s.\n",113dname, errno, strerror(errno));114exit(errno);115}116117/*118* No sub-directory need be created, only create files in it.119*/120if (mktree(dname, level+1) != 0) {121for (f = 0; f < nfile; f++) {122(void) memset(fname, '\0', sizeof (fname));123(void) strcpy(fname,124getfdname(dname, TYPE_F, level+1, d, f));125crtfile(fname);126}127}128}129130for (f = 0; f < nfile; f++) {131(void) memset(fname, '\0', sizeof (fname));132(void) strcpy(fname, getfdname(pdir, TYPE_F, level, d, f));133crtfile(fname);134}135136return (0);137}138139static char *140getfdname(char *pdir, char type, int level, int dir, int file)141{142size_t size = sizeof (fdname);143if (snprintf(fdname, size, "%s/%c-l%dd%df%d", pdir, type, level, dir,144file) >= size) {145(void) fprintf(stderr, "fdname truncated\n");146exit(EINVAL);147}148return (fdname);149}150151static void152crtfile(char *pname)153{154int fd;155int i, size;156const char *context = "0123456789ABCDF";157char *pbuf;158159if (pname == NULL) {160exit(1);161}162163size = sizeof (char) * 1024;164pbuf = (char *)valloc(size);165for (i = 0; i < size / strlen(context); i++) {166int offset = i * strlen(context);167(void) snprintf(pbuf+offset, size-offset, "%s", context);168}169170if ((fd = open(pname, O_CREAT|O_RDWR, 0777)) < 0) {171(void) fprintf(stderr, "open(%s, O_CREAT|O_RDWR, 0777) failed."172"\n[%d]: %s.\n", pname, errno, strerror(errno));173exit(errno);174}175if (write(fd, pbuf, 1024) < 1024) {176(void) fprintf(stderr, "write(fd, pbuf, 1024) failed."177"\n[%d]: %s.\n", errno, strerror(errno));178exit(errno);179}180181#ifdef __linux__182if (fsetxattr(fd, "user.xattr", pbuf, 1024, 0) < 0) {183(void) fprintf(stderr, "fsetxattr(fd, \"xattr\", pbuf, "184"1024, 0) failed.\n[%d]: %s.\n", errno, strerror(errno));185exit(errno);186}187#endif188189(void) close(fd);190free(pbuf);191}192193194