xref: /freebsd/sys/contrib/openzfs/tests/zfs-tests/cmd/mkfiles.c (revision a0b956f5ac5e0941f9e74e24c1c53e05ad061a38)
1716fd348SMartin Matuska /*
2716fd348SMartin Matuska  * This file and its contents are supplied under the terms of the
3716fd348SMartin Matuska  * Common Development and Distribution License ("CDDL"), version 1.0.
4716fd348SMartin Matuska  * You may only use this file in accordance with the terms of version
5716fd348SMartin Matuska  * 1.0 of the CDDL.
6716fd348SMartin Matuska  *
7716fd348SMartin Matuska  * A full copy of the text of the CDDL should have accompanied this
8716fd348SMartin Matuska  * source.  A copy of the CDDL is also available via the Internet at
9716fd348SMartin Matuska  * http://www.illumos.org/license/CDDL.
10716fd348SMartin Matuska  */
11716fd348SMartin Matuska 
12716fd348SMartin Matuska /*
13716fd348SMartin Matuska  * Copyright (c) 2016 by Delphix. All rights reserved.
14716fd348SMartin Matuska  */
15716fd348SMartin Matuska 
16716fd348SMartin Matuska #include <stdio.h>
17716fd348SMartin Matuska #include <stdlib.h>
18716fd348SMartin Matuska #include <fcntl.h>
19716fd348SMartin Matuska #include <string.h>
20716fd348SMartin Matuska #include <errno.h>
21716fd348SMartin Matuska #include <unistd.h>
22716fd348SMartin Matuska #include <sys/param.h>
23716fd348SMartin Matuska 
24716fd348SMartin Matuska static void
usage(const char * msg,int exit_value)25*a0b956f5SMartin Matuska usage(const char *msg, int exit_value)
26716fd348SMartin Matuska {
27*a0b956f5SMartin Matuska 	(void) fprintf(stderr, "usage: mkfiles basename max_file [min_file]\n"
28*a0b956f5SMartin Matuska 	    "%s\n", msg);
29716fd348SMartin Matuska 	exit(exit_value);
30716fd348SMartin Matuska }
31716fd348SMartin Matuska 
32716fd348SMartin Matuska int
main(int argc,char ** argv)33716fd348SMartin Matuska main(int argc, char **argv)
34716fd348SMartin Matuska {
35716fd348SMartin Matuska 	unsigned int numfiles = 0;
36716fd348SMartin Matuska 	unsigned int first_file = 0;
37716fd348SMartin Matuska 	unsigned int i;
38716fd348SMartin Matuska 	char buf[MAXPATHLEN];
39716fd348SMartin Matuska 
40716fd348SMartin Matuska 	if (argc < 3 || argc > 4)
41*a0b956f5SMartin Matuska 		usage("Invalid number of arguments", 1);
42716fd348SMartin Matuska 
43716fd348SMartin Matuska 	if (sscanf(argv[2], "%u", &numfiles) != 1)
44*a0b956f5SMartin Matuska 		usage("Invalid maximum file", 2);
45716fd348SMartin Matuska 
46716fd348SMartin Matuska 	if (argc == 4 && sscanf(argv[3], "%u", &first_file) != 1)
47*a0b956f5SMartin Matuska 		usage("Invalid first file", 3);
48716fd348SMartin Matuska 
49716fd348SMartin Matuska 	for (i = first_file; i < first_file + numfiles; i++) {
50716fd348SMartin Matuska 		int fd;
51716fd348SMartin Matuska 		(void) snprintf(buf, MAXPATHLEN, "%s%u", argv[1], i);
52716fd348SMartin Matuska 		if ((fd = open(buf, O_CREAT | O_EXCL, O_RDWR)) == -1) {
53716fd348SMartin Matuska 			(void) fprintf(stderr, "Failed to create %s %s\n", buf,
54716fd348SMartin Matuska 			    strerror(errno));
55*a0b956f5SMartin Matuska 			return (4);
56716fd348SMartin Matuska 		} else if (fchown(fd, getuid(), getgid()) < 0) {
57716fd348SMartin Matuska 			(void) fprintf(stderr, "Failed to chown %s %s\n", buf,
58716fd348SMartin Matuska 			    strerror(errno));
59*a0b956f5SMartin Matuska 			return (5);
60716fd348SMartin Matuska 		}
61716fd348SMartin Matuska 		(void) close(fd);
62716fd348SMartin Matuska 	}
63716fd348SMartin Matuska 	return (0);
64716fd348SMartin Matuska }
65