Whamcloud - gitweb
7bdbc7876f0dbc08a7f1687213b9be30e3b91582
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  */
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <fcntl.h>
40 #include <unistd.h>
41 #include <errno.h>
42 #include <string.h>
43
44 #ifndef S_SHIFT
45 #define S_SHIFT 12
46 #endif
47
48 int usage(char *prog)
49 {
50         fprintf(stderr, "usage: %s <basename>\n", prog);
51         exit(1);
52 }
53
54 int main(int argc, char *argv[])
55 {
56         char name[4096];
57         int i;
58
59         if (argc != 2)
60                 usage(argv[0]);
61
62         umask(0);
63         for (i = 0; i <= S_IFMT; i += (1 << S_SHIFT)) {
64                 struct stat st;
65                 int mode = i | 0644;
66                 int rc;
67
68                 sprintf(name, "%s-mknod%07o", argv[1], mode);
69                 rc = mknod(name, mode, 0x1234);
70                 switch (i) {
71                 case 0:
72                         mode |= S_IFREG;
73                 case S_IFREG:
74                 case S_IFCHR: case S_IFBLK:
75                         if (rc < 0 && getuid() != 0)
76                                 continue;
77                 case S_IFSOCK: case S_IFIFO:
78                         if (rc < 0) {
79                                 fprintf(stderr, "%s: ERROR mknod %s: %s\n",
80                                         argv[0], name, strerror(errno));
81                                 exit(10);
82                         }
83                         rc = stat(name, &st);
84                         if (rc < 0) {
85                                 fprintf(stderr, "%s: ERROR stat %s: %s",
86                                         argv[0], name, strerror(errno));
87                                 exit(11);
88                         }
89                         if (st.st_mode != mode) {
90                                 fprintf(stderr, "%s: ERROR mode %s: %o != %o",
91                                         argv[0], name, st.st_mode, mode);
92                                 exit(12);
93                         }
94                         if (i == S_IFCHR || i == S_IFBLK) {
95                                 if (st.st_rdev != 0x1234) {
96                                         fprintf(stderr, "%s: ERROR rdev %s: "
97                                                 "%llu != 0x1234",
98                                                 argv[0], name,
99                                                 (unsigned long long)st.st_rdev);
100                                         exit(13);
101                                 }
102                         }
103                         rc = unlink(name);
104                         if (rc < 0) {
105                                 fprintf(stderr, "%s: ERROR unlink %s: %s",
106                                         argv[0], name, strerror(errno));
107                                 exit(14);
108                         }
109                         break;
110                 default:
111                         if (rc == 0) {
112                                 fprintf(stderr, "%s: ERROR: %s created\n",
113                                         argv[0], name);
114                                 exit(15);
115                         }
116                 }
117         }
118
119         for (i = 0; i <= S_IFMT; i += (1 << S_SHIFT)) {
120                 struct stat st;
121                 int mode;
122                 int fd;
123                 int rc;
124
125                 mode = i | 0644;
126                 sprintf(name, "%s-creat%07o", argv[1], mode);
127                 fd = open(name, O_CREAT|O_RDONLY, mode);
128                 if (fd < 0) {
129                         fprintf(stderr, "%s: ERROR creat %s: %s\n",
130                                 argv[0], name, strerror(errno));
131                         exit(21);
132                 }
133                 close(fd);
134                 rc = stat(name, &st);
135                 if (rc < 0) {
136                         fprintf(stderr, "%s: ERROR stat %s: %s",
137                                 argv[0], name, strerror(errno));
138                         exit(11);
139                 }
140                 if (!S_ISREG(st.st_mode & S_IFMT)) {
141                         fprintf(stderr, "%s: ERROR mode %s: %o != %o",
142                                 argv[0], name, st.st_mode & S_IFMT, S_IFREG);
143                         exit(12);
144                 }
145                 rc = unlink(name);
146                 if (rc < 0) {
147                         fprintf(stderr, "%s: ERROR unlink %s: %s\n",
148                                 argv[0], name, strerror(errno));
149                         exit(20);
150                 }
151         }
152
153         for (i = 0; i <= S_IFMT; i += (1 << S_SHIFT)) {
154                 struct stat st;
155                 int rc;
156
157                 sprintf(name, "%s-mkdir%06o", argv[1], i | 0644);
158                 rc = mkdir(name, i | 0664);
159                 if (rc < 0) {
160                         fprintf(stderr, "%s: ERROR mkdir %s: %s\n",
161                                 argv[0], name, strerror(errno));
162                         exit(30);
163                 }
164                 rc = stat(name, &st);
165                 if (rc < 0) {
166                         fprintf(stderr, "%s: ERROR stat %s: %s",
167                                 argv[0], name, strerror(errno));
168                         exit(11);
169                 }
170                 if (!S_ISDIR(st.st_mode)) {
171                         fprintf(stderr, "%s: ERROR mode %s: %o != %o",
172                                 argv[0], name, st.st_mode & S_IFMT, S_IFDIR);
173                         exit(12);
174                 }
175                 rc = rmdir(name);
176                 if (rc < 0) {
177                         fprintf(stderr, "%s: ERROR rmdir %s: %s\n",
178                                 argv[0], name, strerror(errno));
179                         exit(31);
180                 }
181         }
182
183         printf("%s: SUCCESS\n", argv[0]);
184         return 0;
185 }