1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2006 Silicon Graphics, Inc.
12 #include <sys/types.h>
15 #include <attr/attributes.h>
17 #define MAX_EA_NAME 30
20 * multi_open_unlink path_prefix num_files sleep_time
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.
30 fprintf(stderr, "Usage: %s [-e num-eas] [-f path_prefix] [-n num_files] [-s sleep_time] [-v ea-valuesize] \n", prog);
35 main(int argc, char *argv[])
37 char *given_path = "file";
43 int value_size = ATTR_MAX_VALUELEN;
47 while ((c = getopt(argc, argv, "e:f:n:s:v:")) != EOF) {
49 case 'e': /* create eas */
50 num_eas = atoi(optarg);
52 case 'f': /* file prefix */
55 case 'n': /* number of files */
56 num_files = atoi(optarg);
58 case 's': /* sleep time */
59 sleep_time = atoi(optarg);
61 case 'v': /* value size on eas */
62 value_size = atoi(optarg);
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);
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);
79 fd = open(path, O_RDWR|O_CREAT|O_EXCL, 0666);
81 fprintf(stderr, "%s: failed to create \"%s\": %s\n", prog, path, strerror(errno));
86 for (j = 0; j < num_eas; j++) {
89 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 = attr_set(path, attrname, attrvalue, value_size, flags);
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));