Whamcloud - gitweb
b=15967
[fs/lustre-release.git] / lustre / tests / unlinkmany.c
1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include <time.h>
5 #include <errno.h>
6 #include <string.h>
7 #include <fcntl.h>
8 #include <unistd.h>
9 #include <stdlib.h>
10
11 void usage(char *prog)
12 {
13         printf("usage: %s filenamefmt count\n", prog);
14         printf("       %s filenamefmt start count\n", prog);
15 }
16
17 int main(int argc, char ** argv)
18 {
19         int i, rc = 0, do_rmdir = 0;
20         char format[4096], *fmt;
21         char filename[4096];
22         long start, last;
23         long begin = 0, count;
24
25         if (argc < 3 || argc > 5) {
26                 usage(argv[0]);
27                 return 1;
28         }
29
30         if (strcmp(argv[1], "-d") == 0) {
31                 do_rmdir = 1;
32                 argv++;
33                 argc--;
34         }
35
36         if (strlen(argv[1]) > 4080) {
37                 printf("name too long\n");
38                 return 1;
39         }
40
41         start = last = time(0);
42
43         if (argc == 3) {
44                 count = strtol(argv[2], NULL, 0);
45                 if (count < 1) {
46                         printf("count must be at least one\n");
47                         return 1;
48                 }
49         } else {
50                 begin = strtol(argv[2], NULL, 0);
51                 count = strtol(argv[3], NULL, 0);
52         }
53
54         if (strchr(argv[1], '%')) {
55                 fmt = argv[1];
56         } else {
57                 sprintf(format, "%s%%d", argv[1]);
58                 fmt = format;
59         }
60         for (i = 0; i < count; i++, begin++) {
61                 sprintf(filename, fmt, begin);
62                 if (do_rmdir)
63                         rc = rmdir(filename);
64                 else
65                         rc = unlink(filename);
66                 if (rc) {
67                         printf("%s(%s) error: %s\n",
68                                do_rmdir ? "rmdir" : "unlink",
69                                filename, strerror(errno));
70                         rc = errno;
71                         break;
72                 }
73                 if ((i % 10000) == 0) {
74                         printf(" - unlinked %d (time %ld ; total %ld ; last "
75                                "%ld)\n", i, time(0), time(0) - start,
76                                time(0) - last);
77                         last = time(0);
78                 }
79         }
80         printf("total: %d unlinks in %ld seconds: %f unlinks/second\n", i,
81                time(0) - start, ((float)i / (time(0) - start)));
82
83         return rc;
84 }