Whamcloud - gitweb
b=16098
[fs/lustre-release.git] / lustre / tests / createtest.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <fcntl.h>
42 #include <unistd.h>
43 #include <errno.h>
44 #include <string.h>
45
46 #ifndef S_SHIFT
47 #define S_SHIFT 12
48 #endif
49
50 int usage(char *prog)
51 {
52         fprintf(stderr, "usage: %s <basename>\n", prog);
53         exit(1);
54 }
55
56 int main(int argc, char *argv[])
57 {
58         char name[4096];
59         int i;
60
61         if (argc != 2)
62                 usage(argv[0]);
63
64         umask(0);
65         for (i = 0; i <= S_IFMT; i += (1 << S_SHIFT)) {
66                 struct stat st;
67                 int mode = i | 0644;
68                 int rc;
69
70                 sprintf(name, "%s-mknod%06o", argv[1], mode);
71                 rc = mknod(name, mode, 0x1234);
72                 switch (i) {
73                 case 0:
74                         mode |= S_IFREG;
75                 case S_IFREG:
76                 case S_IFCHR: case S_IFBLK:
77                         if (rc < 0 && getuid() != 0)
78                                 continue;
79                 case S_IFSOCK: case S_IFIFO:
80                         if (rc < 0) {
81                                 fprintf(stderr, "%s: ERROR mknod %s: %s\n",
82                                         argv[0], name, strerror(errno));
83                                 exit(10);
84                         }
85                         rc = stat(name, &st);
86                         if (rc < 0) {
87                                 fprintf(stderr, "%s: ERROR stat %s: %s",
88                                         argv[0], name, strerror(errno));
89                                 exit(11);
90                         }
91                         if (st.st_mode != mode) {
92                                 fprintf(stderr, "%s: ERROR mode %s: %o != %o",
93                                         argv[0], name, st.st_mode, mode);
94                                 exit(12);
95                         }
96                         rc = unlink(name);
97                         if (rc < 0) {
98                                 fprintf(stderr, "%s: ERROR unlink %s: %s",
99                                         argv[0], name, strerror(errno));
100                                 exit(13);
101                         }
102                         break;
103                 default:
104                         if (rc == 0) {
105                                 fprintf(stderr, "%s: ERROR: %s created\n",
106                                         argv[0], name);
107                                 exit(14);
108                         }
109                 }
110         }
111
112         for (i = 0; i <= S_IFMT; i += (1 << S_SHIFT)) {
113                 struct stat st;
114                 int mode;
115                 int fd;
116                 int rc;
117
118                 mode = i | 0644;
119                 sprintf(name, "%s-creat%06o", argv[1], mode);
120                 fd = open(name, O_CREAT|O_RDONLY, mode);
121                 if (fd < 0) {
122                         fprintf(stderr, "%s: ERROR creat %s: %s\n",
123                                 argv[0], name, strerror(errno));
124                         exit(21);
125                 }
126                 close(fd);
127                 rc = stat(name, &st);
128                 if (rc < 0) {
129                         fprintf(stderr, "%s: ERROR stat %s: %s",
130                                 argv[0], name, strerror(errno));
131                         exit(11);
132                 }
133                 if (!S_ISREG(st.st_mode & S_IFMT)) {
134                         fprintf(stderr, "%s: ERROR mode %s: %o != %o",
135                                 argv[0], name, st.st_mode & S_IFMT, S_IFREG);
136                         exit(12);
137                 }
138                 rc = unlink(name);
139                 if (rc < 0) {
140                         fprintf(stderr, "%s: ERROR unlink %s: %s\n",
141                                 argv[0], name, strerror(errno));
142                         exit(20);
143                 }
144         }
145
146         for (i = 0; i <= S_IFMT; i += (1 << S_SHIFT)) {
147                 struct stat st;
148                 int rc;
149
150                 sprintf(name, "%s-mkdir%06o", argv[1], i | 0644);
151                 rc = mkdir(name, i | 0664);
152                 if (rc < 0) {
153                         fprintf(stderr, "%s: ERROR mkdir %s: %s\n",
154                                 argv[0], name, strerror(errno));
155                         exit(30);
156                 }
157                 rc = stat(name, &st);
158                 if (rc < 0) {
159                         fprintf(stderr, "%s: ERROR stat %s: %s",
160                                 argv[0], name, strerror(errno));
161                         exit(11);
162                 }
163                 if (!S_ISDIR(st.st_mode)) {
164                         fprintf(stderr, "%s: ERROR mode %s: %o != %o",
165                                 argv[0], name, st.st_mode & S_IFMT, S_IFDIR);
166                         exit(12);
167                 }
168                 rc = rmdir(name);
169                 if (rc < 0) {
170                         fprintf(stderr, "%s: ERROR rmdir %s: %s\n",
171                                 argv[0], name, strerror(errno));
172                         exit(31);
173                 }
174         }
175
176         printf("%s: SUCCESS\n", argv[0]);
177         return 0;
178 }