Whamcloud - gitweb
Oops.
[fs/lustre-release.git] / lustre / liblustre / tests / test_common.c
1 #include <sys/stat.h>
2 #include <sys/types.h>
3 #include <unistd.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <fcntl.h>
7 #include <string.h>
8 #include <errno.h>
9 #include <dirent.h>
10
11 #include "test_common.h"
12
13 int exit_on_err = 1;
14
15 /******************************************************************
16  * util functions
17  ******************************************************************/
18
19 #define EXIT(err)                                       \
20         do {                                            \
21                 if (exit_on_err)                        \
22                         exit(err);                      \
23         } while (0)
24
25 #define EXIT_RET(err)                                   \
26         do {                                            \
27                 if (exit_on_err)                        \
28                         exit(err);                      \
29                 else                                    \
30                         return (err);                   \
31         } while (0)
32
33
34 void t_touch(const char *path)
35 {
36         int fd, rc;
37
38         fd = open(path, O_RDWR|O_CREAT, 0644);
39         if (fd < 0) {
40                 printf("open(%s) error: %s\n", path, strerror(errno));
41                 EXIT(fd);
42         }
43
44         rc = close(fd);
45         if (rc) {
46                 printf("close(%s) error: %s\n", path, strerror(errno));
47                 EXIT(rc);
48         }
49 }
50
51 /* XXX Now libsysio don't support mcreate */
52 void t_create(const char *path)
53 {
54         return t_touch(path);
55 #if 0
56         int rc;
57
58         rc = mknod(path, S_IFREG | 0644, 0);
59         if (rc) {
60                 printf("mknod(%s) error: %s\n", path, strerror(errno));
61                 exit(-1);
62         }
63 #endif
64 }
65
66 void t_link(const char *src, const char *dst)
67 {
68         int rc;
69
70         rc = link(src, dst);
71         if (rc) {
72                 printf("link(%s -> %s) error: %s\n", src, dst, strerror(errno));
73                 EXIT(1);
74         }
75 }
76
77 void t_unlink(const char *path)
78 {
79         int rc;
80
81         rc = unlink(path);
82         if (rc) {
83                 printf("unlink(%s) error: %s\n", path, strerror(errno));
84                 EXIT(-1);
85         }
86 }
87
88 void t_mkdir(const char *path)
89 {
90         int rc;
91
92         rc = mkdir(path, 00644);
93         if (rc < 0) {
94                 printf("mkdir(%s) error: %s\n", path, strerror(errno));
95                 EXIT(1);
96         }
97 }
98
99 void t_rmdir(const char *path)
100 {
101         int rc;
102
103         rc = rmdir(path);
104         if (rc) {
105                 printf("rmdir(%s) error: %s\n", path, strerror(errno));
106                 EXIT(1);
107         }
108 }
109
110 void t_symlink(const char *src, const char *new)
111 {
112         int rc;
113
114         rc = symlink(src, new);
115         if (rc) {
116                 printf("symlink(%s<-%s) error: %s\n", src, new, strerror(errno));
117                 EXIT(1);
118         }
119 }
120
121 #define MKDEV(a,b) (((a) << 8) | (b))
122 void t_mknod(const char *path, mode_t mode, int major, int minor)
123 {
124         int rc;
125
126         rc = mknod(path, mode, MKDEV(5, 4));
127         if (rc) {
128                 printf("mknod(%s) error: %s\n", path, strerror(errno));
129                 EXIT(1);
130         }
131 }
132
133 void t_chmod_raw(const char *path, mode_t mode)
134 {
135         int rc;
136         
137         rc = chmod(path, mode);
138         if (rc) {
139                 printf("chmod(%s) error: %s\n", path, strerror(errno));
140                 EXIT(1);
141         }
142 }
143
144 void t_chmod(const char *path, const char *format, ...)
145 {
146 }
147
148 void t_rename(const char *oldpath, const char *newpath)
149 {
150         int rc;
151
152         rc = rename(oldpath, newpath);
153         if (rc) {
154                 printf("rename(%s -> %s) error: %s\n",
155                        oldpath, newpath, strerror(errno));
156                 EXIT(1);
157         }
158 }
159
160 int t_open_readonly(const char *path)
161 {
162         int fd;
163
164         fd = open(path, O_RDONLY);
165         if (fd < 0) {
166                 printf("open(%s) error: %s\n", path, strerror(errno));
167                 EXIT_RET(fd);
168         }
169         return fd;
170 }
171
172 int t_open(const char *path)
173 {
174         int fd;
175
176         fd = open(path, O_RDWR);
177         if (fd < 0) {
178                 printf("open(%s) error: %s\n", path, strerror(errno));
179                 EXIT_RET(fd);
180         }
181         return fd;
182 }
183
184 void t_close(int fd)
185 {
186         int rc;
187
188         rc = close(fd);
189         if (rc < 0) {
190                 printf("close(%d) error: %s\n", fd, strerror(errno));
191                 EXIT(1);
192         }
193 }
194
195 int t_check_stat(const char *name, struct stat *buf)
196 {
197         struct stat stat;
198         int rc;
199
200         rc = lstat(name, &stat);
201         if (rc) {
202                 printf("error %d stat %s\n", rc, name);
203                 EXIT_RET(rc);
204         }
205         if (buf)
206                 memcpy(buf, &stat, sizeof(*buf));
207
208         return 0;
209 }
210
211 int t_check_stat_fail(const char *name)
212 {
213         struct stat stat;
214         int rc;
215
216         rc = lstat(name, &stat);
217         if (!rc) {
218                 printf("%s still exists\n", name);
219                 EXIT(-1);
220         }
221
222         return 0;
223 }
224
225 void t_echo_create(const char *path, const char *str)
226 {
227         int fd, rc;
228
229         fd = open(path, O_RDWR|O_CREAT, 0644);
230         if (fd < 0) {
231                 printf("open(%s) error: %s\n", path, strerror(errno));
232                 EXIT(fd);
233         }
234
235         if (write(fd, str, strlen(str)+1) != strlen(str)+1) {
236                 printf("write(%s) error: %s\n", path, strerror(errno));
237                 EXIT(fd);
238         }
239
240         rc = close(fd);
241         if (rc) {
242                 printf("close(%s) error: %s\n", path, strerror(errno));
243                 EXIT(rc);
244         }
245 }
246
247 static void _t_grep(const char *path, char *str, int should_contain)
248 {
249         char buf[1024];
250         int fd;
251         int rc;
252         
253         fd = t_open_readonly(path);
254         if (lseek(fd, 0, SEEK_SET) == -1) {
255                 printf("pread_once: seek to 0 error: %s\n", strerror(errno));
256                 EXIT(fd);
257         }
258
259         rc = read(fd, buf, 1023);
260         if (rc < 0) {
261                 printf("grep: read error: %s\n", strerror(errno));
262                 EXIT(-1);
263         }
264         close(fd);
265         buf[rc] = 0;
266
267         if ((strstr(buf, str) != 0) ^ should_contain) {
268                 printf("grep: can't find string %s\n", str);
269                 EXIT(-1);
270         }
271 }
272
273 void t_grep(const char *path, char *str)
274 {
275         _t_grep(path, str, 1);
276 }
277
278 void t_grep_v(const char *path, char *str)
279 {
280         _t_grep(path, str, 0);
281 }
282
283 void t_ls(int fd, char *buf, int size)
284 {
285         struct dirent64 *ent;
286         int rc, pos;
287         loff_t base = 0;
288
289         printf("dir entries listing...\n");
290         while ((rc = getdirentries64(fd, buf, size, &base)) > 0) {
291                 pos = 0;
292                 while (pos < rc) {
293                         ent = (struct dirent64 *) ((char*) buf + pos);
294                         printf("%s\n", ent->d_name);
295                         pos += ent->d_reclen;
296                 }
297         }
298
299         if (rc < 0) {
300                 printf("getdents error %d\n", rc);
301                 EXIT(-1);
302         }
303 }