1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2006 Silicon Graphics, Inc.
12 #include <sys/types.h>
15 #include <sys/xattr.h>
17 #define MAX_EA_NAME 30
18 #define MAX_VALUELEN (64*1024)
21 * multi_open_unlink path_prefix num_files sleep_time
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.
31 fprintf(stderr, "Usage: %s [-e num-eas] [-f path_prefix] [-n num_files] [-s sleep_time] [-v ea-valuesize] \n", prog);
36 main(int argc, char *argv[])
38 char *given_path = "file";
44 int value_size = MAX_VALUELEN;
48 while ((c = getopt(argc, argv, "e:f:n:s:v:")) != EOF) {
50 case 'e': /* create eas */
51 num_eas = atoi(optarg);
53 case 'f': /* file prefix */
56 case 'n': /* number of files */
57 num_files = atoi(optarg);
59 case 's': /* sleep time */
60 sleep_time = atoi(optarg);
62 case 'v': /* value size on eas */
63 value_size = atoi(optarg);
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);
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);
80 fd = open(path, O_RDWR|O_CREAT|O_EXCL, 0666);
82 fprintf(stderr, "%s: failed to create \"%s\": %s\n", prog, path, strerror(errno));
87 for (j = 0; j < num_eas; j++) {
90 char attrname[MAX_EA_NAME];
92 snprintf(attrname, MAX_EA_NAME, "user.name.%d", j);
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));
101 sts = setxattr(path, attrname, attrvalue, value_size, 0);
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));
109 if (unlink(path) == -1) {
110 fprintf(stderr, "%s: failed to unlink \"%s\": %s\n",
111 prog, path, strerror(errno));