idmapped-mounts: add missing newline to print_r()
[xfstests-dev.git] / src / multi_open_unlink.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2006 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <string.h>
10 #include <errno.h>
11 #include <limits.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <fcntl.h>
15 #include <sys/xattr.h>
16
17 #define MAX_EA_NAME 30
18 #define MAX_VALUELEN    (64*1024)
19
20 /*
21  * multi_open_unlink path_prefix num_files sleep_time
22  * e.g.
23  *   $ multi_open_unlink file 100 60
24  *   Creates 100 files: file.1, file.2, ..., file.100
25  *   unlinks them all but doesn't close them all until after 60 seconds.
26  */
27
28 void
29 usage(char *prog)
30 {
31         fprintf(stderr, "Usage: %s [-e num-eas] [-f path_prefix] [-n num_files] [-s sleep_time] [-v ea-valuesize] \n", prog);
32         exit(1);
33 }
34
35 int
36 main(int argc, char *argv[])
37 {
38         char *given_path = "file";
39         char path[PATH_MAX];
40         char *prog = argv[0];
41         int sleep_time = 60;
42         int num_files = 100;
43         int num_eas = 0;
44         int value_size = MAX_VALUELEN;
45         int fd = -1;
46         int i,j,c;
47
48         while ((c = getopt(argc, argv, "e:f:n:s:v:")) != EOF) {
49                 switch (c) {
50                         case 'e':   /* create eas */
51                                 num_eas = atoi(optarg);
52                                 break;
53                         case 'f':   /* file prefix */
54                                 given_path = optarg;
55                                 break;
56                         case 'n':   /* number of files */
57                                 num_files = atoi(optarg);
58                                 break;
59                         case 's':   /* sleep time */
60                                 sleep_time = atoi(optarg);
61                                 break;
62                         case 'v':  /* value size on eas */
63                                 value_size = atoi(optarg);
64                                 break;
65                         case '?':
66                                 usage(prog);
67                 }
68         }
69
70         /* create and unlink a bunch of files */
71         for (i = 0; i < num_files; i++) {
72                 sprintf(path, "%s.%d", given_path, i+1);
73
74                 /* if file already exists then error out */
75                 if (access(path, F_OK) == 0) {
76                         fprintf(stderr, "%s: file \"%s\" already exists\n", prog, path);
77                         return 1;
78                 }
79
80                 fd = open(path, O_RDWR|O_CREAT|O_EXCL, 0666);
81                 if (fd == -1) {
82                         fprintf(stderr, "%s: failed to create \"%s\": %s\n", prog, path, strerror(errno));
83                         return 1;
84                 }
85
86                 /* set the EAs */
87                 for (j = 0; j < num_eas; j++) {
88                         int sts;
89                         char *attrvalue;
90                         char attrname[MAX_EA_NAME];
91
92                         snprintf(attrname, MAX_EA_NAME, "user.name.%d", j);
93
94                         attrvalue = calloc(value_size, 1);
95                         if (attrvalue == NULL) {
96                                 fprintf(stderr, "%s: failed to create EA value of size %d on path \"%s\": %s\n",
97                                         prog, value_size, path, strerror(errno));
98                                 return 1;
99                         }
100
101                         sts = setxattr(path, attrname, attrvalue, value_size, 0);
102                         if (sts == -1) {
103                                 fprintf(stderr, "%s: failed to create EA \"%s\" of size %d on path \"%s\": %s\n",
104                                         prog, attrname, value_size, path, strerror(errno));
105                                 return 1;
106                         }
107                 }
108
109                 if (unlink(path) == -1) {
110                         fprintf(stderr, "%s: failed to unlink \"%s\": %s\n",
111                                 prog, path, strerror(errno));
112                         return 1;
113                 }
114         }
115
116         sleep(sleep_time);
117
118         return 0;
119 }