xref: /freebsd/sys/contrib/openzfs/tests/zfs-tests/cmd/mkfiles.c (revision 716fd348e01c5f2ba125f878a634a753436c2994)
1*716fd348SMartin Matuska /*
2*716fd348SMartin Matuska  * This file and its contents are supplied under the terms of the
3*716fd348SMartin Matuska  * Common Development and Distribution License ("CDDL"), version 1.0.
4*716fd348SMartin Matuska  * You may only use this file in accordance with the terms of version
5*716fd348SMartin Matuska  * 1.0 of the CDDL.
6*716fd348SMartin Matuska  *
7*716fd348SMartin Matuska  * A full copy of the text of the CDDL should have accompanied this
8*716fd348SMartin Matuska  * source.  A copy of the CDDL is also available via the Internet at
9*716fd348SMartin Matuska  * http://www.illumos.org/license/CDDL.
10*716fd348SMartin Matuska  */
11*716fd348SMartin Matuska 
12*716fd348SMartin Matuska /*
13*716fd348SMartin Matuska  * Copyright (c) 2016 by Delphix. All rights reserved.
14*716fd348SMartin Matuska  */
15*716fd348SMartin Matuska 
16*716fd348SMartin Matuska #include <stdio.h>
17*716fd348SMartin Matuska #include <stdlib.h>
18*716fd348SMartin Matuska #include <fcntl.h>
19*716fd348SMartin Matuska #include <string.h>
20*716fd348SMartin Matuska #include <errno.h>
21*716fd348SMartin Matuska #include <unistd.h>
22*716fd348SMartin Matuska #include <sys/param.h>
23*716fd348SMartin Matuska 
24*716fd348SMartin Matuska #define	MAX_INT_LENGTH 10
25*716fd348SMartin Matuska 
26*716fd348SMartin Matuska static void
27*716fd348SMartin Matuska usage(char *msg, int exit_value)
28*716fd348SMartin Matuska {
29*716fd348SMartin Matuska 	(void) fprintf(stderr, "mkfiles basename max_file [min_file]\n");
30*716fd348SMartin Matuska 	(void) fprintf(stderr, "%s\n", msg);
31*716fd348SMartin Matuska 	exit(exit_value);
32*716fd348SMartin Matuska }
33*716fd348SMartin Matuska 
34*716fd348SMartin Matuska int
35*716fd348SMartin Matuska main(int argc, char **argv)
36*716fd348SMartin Matuska {
37*716fd348SMartin Matuska 	unsigned int numfiles = 0;
38*716fd348SMartin Matuska 	unsigned int first_file = 0;
39*716fd348SMartin Matuska 	unsigned int i;
40*716fd348SMartin Matuska 	char buf[MAXPATHLEN];
41*716fd348SMartin Matuska 
42*716fd348SMartin Matuska 	if (argc < 3 || argc > 4)
43*716fd348SMartin Matuska 		usage("Invalid number of arguments", -1);
44*716fd348SMartin Matuska 
45*716fd348SMartin Matuska 	if (sscanf(argv[2], "%u", &numfiles) != 1)
46*716fd348SMartin Matuska 		usage("Invalid maximum file", -2);
47*716fd348SMartin Matuska 
48*716fd348SMartin Matuska 	if (argc == 4 && sscanf(argv[3], "%u", &first_file) != 1)
49*716fd348SMartin Matuska 		usage("Invalid first file", -3);
50*716fd348SMartin Matuska 
51*716fd348SMartin Matuska 	for (i = first_file; i < first_file + numfiles; i++) {
52*716fd348SMartin Matuska 		int fd;
53*716fd348SMartin Matuska 		(void) snprintf(buf, MAXPATHLEN, "%s%u", argv[1], i);
54*716fd348SMartin Matuska 		if ((fd = open(buf, O_CREAT | O_EXCL, O_RDWR)) == -1) {
55*716fd348SMartin Matuska 			(void) fprintf(stderr, "Failed to create %s %s\n", buf,
56*716fd348SMartin Matuska 			    strerror(errno));
57*716fd348SMartin Matuska 			return (-4);
58*716fd348SMartin Matuska 		} else if (fchown(fd, getuid(), getgid()) < 0) {
59*716fd348SMartin Matuska 			(void) fprintf(stderr, "Failed to chown %s %s\n", buf,
60*716fd348SMartin Matuska 			    strerror(errno));
61*716fd348SMartin Matuska 			return (-5);
62*716fd348SMartin Matuska 		}
63*716fd348SMartin Matuska 		(void) close(fd);
64*716fd348SMartin Matuska 	}
65*716fd348SMartin Matuska 	return (0);
66*716fd348SMartin Matuska }
67