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