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