Whamcloud - gitweb
a87f0fa9ea022f5b340ca31d3a47b0dee924857a
[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 int t_opendir(const char *path)
185 {
186         int fd;
187
188         fd = open(path, O_RDONLY);
189         if (fd < 0) {
190                 printf("opendir(%s) error: %s\n", path, strerror(errno));
191                 EXIT_RET(fd);
192         }
193         return fd;
194 }
195
196 void t_close(int fd)
197 {
198         int rc;
199
200         rc = close(fd);
201         if (rc < 0) {
202                 printf("close(%d) error: %s\n", fd, strerror(errno));
203                 EXIT(1);
204         }
205 }
206
207 int t_check_stat(const char *name, struct stat *buf)
208 {
209         struct stat stat;
210         int rc;
211
212         rc = lstat(name, &stat);
213         if (rc) {
214                 printf("error %d stat %s\n", rc, name);
215                 EXIT_RET(rc);
216         }
217         if (buf)
218                 memcpy(buf, &stat, sizeof(*buf));
219
220         return 0;
221 }
222
223 int t_check_stat_fail(const char *name)
224 {
225         struct stat stat;
226         int rc;
227
228         rc = lstat(name, &stat);
229         if (!rc) {
230                 printf("%s still exists\n", name);
231                 EXIT(-1);
232         }
233
234         return 0;
235 }
236
237 void t_echo_create(const char *path, const char *str)
238 {
239         int fd, rc;
240
241         fd = open(path, O_RDWR|O_CREAT, 0644);
242         if (fd < 0) {
243                 printf("open(%s) error: %s\n", path, strerror(errno));
244                 EXIT(fd);
245         }
246
247         if (write(fd, str, strlen(str)+1) != strlen(str)+1) {
248                 printf("write(%s) error: %s\n", path, strerror(errno));
249                 EXIT(fd);
250         }
251
252         rc = close(fd);
253         if (rc) {
254                 printf("close(%s) error: %s\n", path, strerror(errno));
255                 EXIT(rc);
256         }
257 }
258
259 static void _t_grep(const char *path, char *str, int should_contain)
260 {
261         char buf[1024];
262         int fd;
263         int rc;
264         
265         fd = t_open_readonly(path);
266         if (lseek(fd, 0, SEEK_SET) == -1) {
267                 printf("pread_once: seek to 0 error: %s\n", strerror(errno));
268                 EXIT(fd);
269         }
270
271         rc = read(fd, buf, 1023);
272         if (rc < 0) {
273                 printf("grep: read error: %s\n", strerror(errno));
274                 EXIT(-1);
275         }
276         close(fd);
277         buf[rc] = 0;
278
279         if ((strstr(buf, str) != 0) ^ should_contain) {
280                 printf("grep: can't find string %s\n", str);
281                 EXIT(-1);
282         }
283 }
284
285 void t_grep(const char *path, char *str)
286 {
287         _t_grep(path, str, 1);
288 }
289
290 void t_grep_v(const char *path, char *str)
291 {
292         _t_grep(path, str, 0);
293 }
294
295 void t_ls(int fd, char *buf, int size)
296 {
297         struct dirent64 *ent;
298         int rc, pos;
299         loff_t base = 0;
300
301         printf("dir entries listing...\n");
302         while ((rc = getdirentries64(fd, buf, size, &base)) > 0) {
303                 pos = 0;
304                 while (pos < rc) {
305                         ent = (struct dirent64 *) ((char*) buf + pos);
306                         printf("%s\n", ent->d_name);
307                         pos += ent->d_reclen;
308                 }
309         }
310
311         if (rc < 0) {
312                 printf("getdents error %d\n", rc);
313                 EXIT(-1);
314         }
315 }