Whamcloud - gitweb
LU-12616 obclass: fix MDS start/stop race
[fs/lustre-release.git] / lustre / tests / chownmany.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  */
26 /*
27  * This file is part of Lustre, http://www.lustre.org/
28  * Lustre is a trademark of Sun Microsystems, Inc.
29  */
30
31 #include <stdio.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <time.h>
35 #include <errno.h>
36 #include <string.h>
37 #include <fcntl.h>
38 #include <unistd.h>
39 #include <stdlib.h>
40
41 void usage(char *prog)
42 {
43         printf("usage: %s owner filenamefmt count\n", prog);
44         printf("       %s owner filenamefmt start count\n", prog);
45 }
46
47 int main(int argc, char ** argv)
48 {
49         int i, rc = 0, mask = 0;
50         char format[4096], *fmt;
51         char filename[4096];
52         long start, last;
53         long begin = 0, count;
54
55         if (argc < 4 || argc > 5) {
56                 usage(argv[0]);
57                 return 1;
58         }
59
60         mask = strtol(argv[1], NULL, 0);
61
62         if (strlen(argv[2]) > 4080) {
63                 printf("name too long\n");
64                 return 1;
65         }
66
67         start = last = time(0);
68
69         if (argc == 4) {
70                 count = strtol(argv[3], NULL, 0);
71                 if (count < 1) {
72                         printf("count must be at least one\n");
73                         return 1;
74                 }
75         } else {
76                 begin = strtol(argv[3], NULL, 0);
77                 count = strtol(argv[4], NULL, 0);
78         }
79
80         if (strchr(argv[2], '%')) {
81                 fmt = argv[2];
82         } else {
83                 sprintf(format, "%s%%d", argv[2]);
84                 fmt = format;
85         }
86         for (i = 0; i < count; i++, begin++) {
87                 sprintf(filename, fmt, begin);
88                 rc = chown(filename, mask, -1);
89                 if (rc) {
90                         printf("chown (%s) error: %s\n",
91                                filename, strerror(errno));
92                         rc = errno;
93                         break;
94                 }
95                 if ((i % 10000) == 0) {
96                         printf(" - chowned %d (time %ld ; total %ld ; last "
97                                "%ld)\n", i, time(0), time(0) - start,
98                                time(0) - last);
99                         last = time(0);
100                 }
101         }
102         printf("total: %d chowns in %ld seconds: %f chowns/second\n", i,
103                time(0) - start, ((float)i / (time(0) - start)));
104
105         return rc;
106 }