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