Whamcloud - gitweb
LU-6210 tests: Change positional struct initializers to C99
[fs/lustre-release.git] / lustre / tests / createtest.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) 2003, 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 <stdlib.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36 #include <unistd.h>
37 #include <errno.h>
38 #include <string.h>
39
40 #ifndef S_SHIFT
41 #define S_SHIFT 12
42 #endif
43
44 int usage(char *prog)
45 {
46         fprintf(stderr, "usage: %s <basename>\n", prog);
47         exit(1);
48 }
49
50 int main(int argc, char *argv[])
51 {
52         char name[4096];
53         int i;
54
55         if (argc != 2)
56                 usage(argv[0]);
57
58         umask(0);
59         for (i = 0; i <= S_IFMT; i += (1 << S_SHIFT)) {
60                 struct stat st;
61                 int mode = i | 0644;
62                 int rc;
63
64                 snprintf(name, sizeof(name), "%s-mknod%07o", argv[1], mode);
65                 rc = mknod(name, mode, 0x1234);
66                 switch (i) {
67                 case 0:
68                         mode |= S_IFREG;
69                 case S_IFREG:
70                 case S_IFCHR: case S_IFBLK:
71                         if (rc < 0 && getuid() != 0)
72                                 continue;
73                 case S_IFSOCK: case S_IFIFO:
74                         if (rc < 0) {
75                                 fprintf(stderr, "%s: ERROR mknod %s: %s\n",
76                                         argv[0], name, strerror(errno));
77                                 exit(10);
78                         }
79                         rc = stat(name, &st);
80                         if (rc < 0) {
81                                 fprintf(stderr, "%s: ERROR stat %s: %s",
82                                         argv[0], name, strerror(errno));
83                                 exit(11);
84                         }
85                         if (st.st_mode != mode) {
86                                 fprintf(stderr, "%s: ERROR mode %s: %o != %o",
87                                         argv[0], name, st.st_mode, mode);
88                                 exit(12);
89                         }
90                         if (i == S_IFCHR || i == S_IFBLK) {
91                                 if (st.st_rdev != 0x1234) {
92                                         fprintf(stderr, "%s: ERROR rdev %s: "
93                                                 "%llu != 0x1234",
94                                                 argv[0], name,
95                                                 (unsigned long long)st.st_rdev);
96                                         exit(13);
97                                 }
98                         }
99                         rc = unlink(name);
100                         if (rc < 0) {
101                                 fprintf(stderr, "%s: ERROR unlink %s: %s",
102                                         argv[0], name, strerror(errno));
103                                 exit(14);
104                         }
105                         break;
106                 default:
107                         if (rc == 0) {
108                                 fprintf(stderr, "%s: ERROR: %s created\n",
109                                         argv[0], name);
110                                 exit(15);
111                         }
112                 }
113         }
114
115         for (i = 0; i <= S_IFMT; i += (1 << S_SHIFT)) {
116                 struct stat st;
117                 int mode;
118                 int fd;
119                 int rc;
120
121                 mode = i | 0644;
122                 snprintf(name, sizeof(name), "%s-creat%07o", argv[1], mode);
123                 fd = open(name, O_CREAT|O_RDONLY, mode);
124                 if (fd < 0) {
125                         fprintf(stderr, "%s: ERROR creat %s: %s\n",
126                                 argv[0], name, strerror(errno));
127                         exit(21);
128                 }
129                 close(fd);
130                 rc = stat(name, &st);
131                 if (rc < 0) {
132                         fprintf(stderr, "%s: ERROR stat %s: %s",
133                                 argv[0], name, strerror(errno));
134                         exit(11);
135                 }
136                 if (!S_ISREG(st.st_mode & S_IFMT)) {
137                         fprintf(stderr, "%s: ERROR mode %s: %o != %o",
138                                 argv[0], name, st.st_mode & S_IFMT, S_IFREG);
139                         exit(12);
140                 }
141                 rc = unlink(name);
142                 if (rc < 0) {
143                         fprintf(stderr, "%s: ERROR unlink %s: %s\n",
144                                 argv[0], name, strerror(errno));
145                         exit(20);
146                 }
147         }
148
149         for (i = 0; i <= S_IFMT; i += (1 << S_SHIFT)) {
150                 struct stat st;
151                 int rc;
152
153                 snprintf(name, sizeof(name), "%s-mkdir%06o", argv[1], i | 0644);
154                 rc = mkdir(name, i | 0664);
155                 if (rc < 0) {
156                         fprintf(stderr, "%s: ERROR mkdir %s: %s\n",
157                                 argv[0], name, strerror(errno));
158                         exit(30);
159                 }
160                 rc = stat(name, &st);
161                 if (rc < 0) {
162                         fprintf(stderr, "%s: ERROR stat %s: %s",
163                                 argv[0], name, strerror(errno));
164                         exit(11);
165                 }
166                 if (!S_ISDIR(st.st_mode)) {
167                         fprintf(stderr, "%s: ERROR mode %s: %o != %o",
168                                 argv[0], name, st.st_mode & S_IFMT, S_IFDIR);
169                         exit(12);
170                 }
171                 rc = rmdir(name);
172                 if (rc < 0) {
173                         fprintf(stderr, "%s: ERROR rmdir %s: %s\n",
174                                 argv[0], name, strerror(errno));
175                         exit(31);
176                 }
177         }
178
179         printf("%s: SUCCESS\n", argv[0]);
180         return 0;
181 }