generic: test invalid swap file activation
[xfstests-dev.git] / src / multi_open_unlink.c
1 /*
2  * Copyright (c) 2006 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <unistd.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <limits.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <attr/attributes.h>
28
29 #define MAX_EA_NAME 30
30
31 /*
32  * multi_open_unlink path_prefix num_files sleep_time
33  * e.g.
34  *   $ multi_open_unlink file 100 60
35  *   Creates 100 files: file.1, file.2, ..., file.100
36  *   unlinks them all but doesn't close them all until after 60 seconds.
37  */
38
39 void
40 usage(char *prog)
41 {
42         fprintf(stderr, "Usage: %s [-e num-eas] [-f path_prefix] [-n num_files] [-s sleep_time] [-v ea-valuesize] \n", prog);
43         exit(1);
44 }
45
46 int
47 main(int argc, char *argv[])
48 {
49         char *given_path = "file";
50         char path[PATH_MAX];
51         char *prog = argv[0];
52         int sleep_time = 60;
53         int num_files = 100;
54         int num_eas = 0;
55         int value_size = ATTR_MAX_VALUELEN;
56         int fd = -1;
57         int i,j,c;
58
59         while ((c = getopt(argc, argv, "e:f:n:s:v:")) != EOF) {
60                 switch (c) {
61                         case 'e':   /* create eas */
62                                 num_eas = atoi(optarg);
63                                 break;
64                         case 'f':   /* file prefix */
65                                 given_path = optarg;
66                                 break;
67                         case 'n':   /* number of files */
68                                 num_files = atoi(optarg);
69                                 break;
70                         case 's':   /* sleep time */
71                                 sleep_time = atoi(optarg);
72                                 break;
73                         case 'v':  /* value size on eas */
74                                 value_size = atoi(optarg);
75                                 break;
76                         case '?':
77                                 usage(prog);
78                 }
79         }
80
81         /* create and unlink a bunch of files */
82         for (i = 0; i < num_files; i++) {
83                 sprintf(path, "%s.%d", given_path, i+1);
84
85                 /* if file already exists then error out */
86                 if (access(path, F_OK) == 0) {
87                         fprintf(stderr, "%s: file \"%s\" already exists\n", prog, path);
88                         return 1;
89                 }
90
91                 fd = open(path, O_RDWR|O_CREAT|O_EXCL, 0666);
92                 if (fd == -1) {
93                         fprintf(stderr, "%s: failed to create \"%s\": %s\n", prog, path, strerror(errno));
94                         return 1;
95                 }
96
97                 /* set the EAs */
98                 for (j = 0; j < num_eas; j++) {
99                         int sts;
100                         char *attrvalue;
101                         char attrname[MAX_EA_NAME];
102                         int flags = 0;
103
104                         snprintf(attrname, MAX_EA_NAME, "user.name.%d", j);
105
106                         attrvalue = calloc(value_size, 1);
107                         if (attrvalue == NULL) {
108                                 fprintf(stderr, "%s: failed to create EA value of size %d on path \"%s\": %s\n",
109                                         prog, value_size, path, strerror(errno));
110                                 return 1;
111                         }
112
113                         sts = attr_set(path, attrname, attrvalue, value_size, flags);
114                         if (sts == -1) {
115                                 fprintf(stderr, "%s: failed to create EA \"%s\" of size %d on path \"%s\": %s\n",
116                                         prog, attrname, value_size, path, strerror(errno));
117                                 return 1;
118                         }
119                 }
120
121                 if (unlink(path) == -1) {
122                         fprintf(stderr, "%s: failed to unlink \"%s\": %s\n",
123                                 prog, path, strerror(errno));
124                         return 1;
125                 }
126         }
127
128         sleep(sleep_time);
129
130         return 0;
131 }