Path: blob/main/sys/contrib/openzfs/tests/zfs-tests/cmd/mkfiles.c
48529 views
// SPDX-License-Identifier: CDDL-1.01/*2* This file and its contents are supplied under the terms of the3* Common Development and Distribution License ("CDDL"), version 1.0.4* You may only use this file in accordance with the terms of version5* 1.0 of the CDDL.6*7* A full copy of the text of the CDDL should have accompanied this8* source. A copy of the CDDL is also available via the Internet at9* http://www.illumos.org/license/CDDL.10*/1112/*13* Copyright (c) 2016 by Delphix. All rights reserved.14*/1516#include <stdio.h>17#include <stdlib.h>18#include <fcntl.h>19#include <string.h>20#include <errno.h>21#include <unistd.h>22#include <sys/param.h>2324static void25usage(const char *msg, int exit_value)26{27(void) fprintf(stderr, "usage: mkfiles basename max_file [min_file]\n"28"%s\n", msg);29exit(exit_value);30}3132int33main(int argc, char **argv)34{35unsigned int numfiles = 0;36unsigned int first_file = 0;37unsigned int i;38char buf[MAXPATHLEN];3940if (argc < 3 || argc > 4)41usage("Invalid number of arguments", 1);4243if (sscanf(argv[2], "%u", &numfiles) != 1)44usage("Invalid maximum file", 2);4546if (argc == 4 && sscanf(argv[3], "%u", &first_file) != 1)47usage("Invalid first file", 3);4849for (i = first_file; i < first_file + numfiles; i++) {50int fd;51(void) snprintf(buf, MAXPATHLEN, "%s%u", argv[1], i);52if ((fd = open(buf, O_CREAT | O_EXCL, O_RDWR)) == -1) {53(void) fprintf(stderr, "Failed to create %s %s\n", buf,54strerror(errno));55return (4);56} else if (fchown(fd, getuid(), getgid()) < 0) {57(void) fprintf(stderr, "Failed to chown %s %s\n", buf,58strerror(errno));59return (5);60}61(void) close(fd);62}63return (0);64}656667