generic/60[01]: fix test failure when setting new grace limit
[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 <attr/attributes.h>
16
17 #define MAX_EA_NAME 30
18
19 /*
20  * multi_open_unlink path_prefix num_files sleep_time
21  * e.g.
22  *   $ multi_open_unlink file 100 60
23  *   Creates 100 files: file.1, file.2, ..., file.100
24  *   unlinks them all but doesn't close them all until after 60 seconds.
25  */
26
27 void
28 usage(char *prog)
29 {
30         fprintf(stderr, "Usage: %s [-e num-eas] [-f path_prefix] [-n num_files] [-s sleep_time] [-v ea-valuesize] \n", prog);
31         exit(1);
32 }
33
34 int
35 main(int argc, char *argv[])
36 {
37         char *given_path = "file";
38         char path[PATH_MAX];
39         char *prog = argv[0];
40         int sleep_time = 60;
41         int num_files = 100;
42         int num_eas = 0;
43         int value_size = ATTR_MAX_VALUELEN;
44         int fd = -1;
45         int i,j,c;
46
47         while ((c = getopt(argc, argv, "e:f:n:s:v:")) != EOF) {
48                 switch (c) {
49                         case 'e':   /* create eas */
50                                 num_eas = atoi(optarg);
51                                 break;
52                         case 'f':   /* file prefix */
53                                 given_path = optarg;
54                                 break;
55                         case 'n':   /* number of files */
56                                 num_files = atoi(optarg);
57                                 break;
58                         case 's':   /* sleep time */
59                                 sleep_time = atoi(optarg);
60                                 break;
61                         case 'v':  /* value size on eas */
62                                 value_size = atoi(optarg);
63                                 break;
64                         case '?':
65                                 usage(prog);
66                 }
67         }
68
69         /* create and unlink a bunch of files */
70         for (i = 0; i < num_files; i++) {
71                 sprintf(path, "%s.%d", given_path, i+1);
72
73                 /* if file already exists then error out */
74                 if (access(path, F_OK) == 0) {
75                         fprintf(stderr, "%s: file \"%s\" already exists\n", prog, path);
76                         return 1;
77                 }
78
79                 fd = open(path, O_RDWR|O_CREAT|O_EXCL, 0666);
80                 if (fd == -1) {
81                         fprintf(stderr, "%s: failed to create \"%s\": %s\n", prog, path, strerror(errno));
82                         return 1;
83                 }
84
85                 /* set the EAs */
86                 for (j = 0; j < num_eas; j++) {
87                         int sts;
88                         char *attrvalue;
89                         char attrname[MAX_EA_NAME];
90                         int flags = 0;
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 = attr_set(path, attrname, attrvalue, value_size, flags);
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 }