Whamcloud - gitweb
8b7ba3bb85edca92a00f9f95c866967d16f70436
[fs/lustre-release.git] / lustre / liblustre / tests / test_common.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  */
4
5 #include <sys/stat.h>
6 #include <sys/types.h>
7 #include <unistd.h>
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <fcntl.h>
11 #include <string.h>
12 #include <errno.h>
13 #include <dirent.h>
14 #include <utime.h>
15 #include <stdarg.h>
16
17 #include <liblustre.h>
18
19 #include "test_common.h"
20
21 int exit_on_err = 1;
22
23 /******************************************************************
24  * util functions
25  ******************************************************************/
26
27 #define EXIT(err)                                       \
28         do {                                            \
29                 if (exit_on_err)                        \
30                         exit(err);                      \
31         } while (0)
32
33 #define EXIT_RET(err)                                   \
34         do {                                            \
35                 if (exit_on_err)                        \
36                         exit(err);                      \
37                 else                                    \
38                         return (err);                   \
39         } while (0)
40
41
42 void t_touch(const char *path)
43 {
44         int fd, rc;
45
46         fd = open(path, O_RDWR|O_CREAT, 0644);
47         if (fd < 0) {
48                 printf("open(%s) error: %s\n", path, strerror(errno));
49                 EXIT(fd);
50         }
51
52         rc = close(fd);
53         if (rc) {
54                 printf("close(%s) error: %s\n", path, strerror(errno));
55                 EXIT(rc);
56         }
57 }
58
59 /* XXX Now libsysio don't support mcreate */
60 void t_create(const char *path)
61 {
62         return t_touch(path);
63 #if 0
64         int rc;
65
66         rc = mknod(path, S_IFREG | 0644, 0);
67         if (rc) {
68                 printf("mknod(%s) error: %s\n", path, strerror(errno));
69                 exit(-1);
70         }
71 #endif
72 }
73
74 void t_link(const char *src, const char *dst)
75 {
76         int rc;
77
78         rc = link(src, dst);
79         if (rc) {
80                 printf("link(%s -> %s) error: %s\n", src, dst, strerror(errno));
81                 EXIT(1);
82         }
83 }
84
85 void t_unlink(const char *path)
86 {
87         int rc;
88
89         rc = unlink(path);
90         if (rc) {
91                 printf("unlink(%s) error: %s\n", path, strerror(errno));
92                 EXIT(-1);
93         }
94 }
95
96 void t_mkdir(const char *path)
97 {
98         int rc;
99
100         rc = mkdir(path, 00755);
101         if (rc < 0) {
102                 printf("mkdir(%s) error: %s\n", path, strerror(errno));
103                 EXIT(1);
104         }
105 }
106
107 void t_rmdir(const char *path)
108 {
109         int rc;
110
111         rc = rmdir(path);
112         if (rc) {
113                 printf("rmdir(%s) error: %s\n", path, strerror(errno));
114                 EXIT(1);
115         }
116 }
117
118 void t_symlink(const char *src, const char *new)
119 {
120         int rc;
121
122         rc = symlink(src, new);
123         if (rc) {
124                 printf("symlink(%s<-%s) error: %s\n", src, new, strerror(errno));
125                 EXIT(1);
126         }
127 }
128
129 #define MKDEV(a,b) (((a) << 8) | (b))
130 void t_mknod(const char *path, mode_t mode, int major, int minor)
131 {
132         int rc;
133
134         rc = mknod(path, mode, MKDEV(5, 4));
135         if (rc) {
136                 printf("mknod(%s) error: %s\n", path, strerror(errno));
137                 EXIT(1);
138         }
139 }
140
141 void t_chmod_raw(const char *path, mode_t mode)
142 {
143         int rc;
144         
145         rc = chmod(path, mode);
146         if (rc) {
147                 printf("chmod(%s) error: %s\n", path, strerror(errno));
148                 EXIT(1);
149         }
150 }
151
152 void t_chmod(const char *path, const char *format, ...)
153 {
154 }
155
156 void t_rename(const char *oldpath, const char *newpath)
157 {
158         int rc;
159
160         rc = rename(oldpath, newpath);
161         if (rc) {
162                 printf("rename(%s -> %s) error: %s\n",
163                        oldpath, newpath, strerror(errno));
164                 EXIT(1);
165         }
166 }
167
168 int t_open_readonly(const char *path)
169 {
170         int fd;
171
172         fd = open(path, O_RDONLY);
173         if (fd < 0) {
174                 printf("open(%s) error: %s\n", path, strerror(errno));
175                 EXIT_RET(fd);
176         }
177         return fd;
178 }
179
180 int t_open(const char *path)
181 {
182         int fd;
183
184         fd = open(path, O_RDWR | O_LARGEFILE);
185         if (fd < 0) {
186                 printf("open(%s) error: %s\n", path, strerror(errno));
187                 EXIT_RET(fd);
188         }
189         return fd;
190 }
191
192 int t_chdir(const char *path)
193 {
194         int rc = chdir(path);
195         if (rc < 0) {
196                 printf("chdir(%s) error: %s\n", path, strerror(errno));
197                 EXIT_RET(rc);
198         }
199         return rc;
200 }
201
202 int t_utime(const char *path, const struct utimbuf *buf)
203 {
204         int rc = utime(path, buf);
205         if (rc < 0) {
206                 printf("utime(%s, %p) error: %s\n", path, buf,
207                        strerror(errno));
208                 EXIT_RET(rc);
209         }
210         return rc;
211 }
212
213 int t_opendir(const char *path)
214 {
215         int fd;
216
217         fd = open(path, O_RDONLY);
218         if (fd < 0) {
219                 printf("opendir(%s) error: %s\n", path, strerror(errno));
220                 EXIT_RET(fd);
221         }
222         return fd;
223 }
224
225 void t_close(int fd)
226 {
227         int rc;
228
229         rc = close(fd);
230         if (rc < 0) {
231                 printf("close(%d) error: %s\n", fd, strerror(errno));
232                 EXIT(1);
233         }
234 }
235
236 int t_check_stat(const char *name, struct stat *buf)
237 {
238         struct stat stat;
239         int rc;
240
241         memset(&stat, 0, sizeof(stat));
242
243         rc = lstat(name, &stat);
244         if (rc) {
245                 printf("error %d stat %s\n", rc, name);
246                 EXIT_RET(rc);
247         }
248         if (buf)
249                 memcpy(buf, &stat, sizeof(*buf));
250         if (stat.st_blksize == 0) {
251                 printf("error: blksize is 0\n");
252                 EXIT_RET(-EINVAL);
253         }
254
255         return 0;
256 }
257
258 int t_check_stat_fail(const char *name)
259 {
260         struct stat stat;
261         int rc;
262
263         rc = lstat(name, &stat);
264         if (!rc) {
265                 printf("%s still exists\n", name);
266                 EXIT(-1);
267         }
268
269         return 0;
270 }
271
272 void t_echo_create(const char *path, const char *str)
273 {
274         int fd, rc;
275
276         fd = open(path, O_RDWR|O_CREAT, 0644);
277         if (fd < 0) {
278                 printf("open(%s) error: %s\n", path, strerror(errno));
279                 EXIT(fd);
280         }
281
282         if (write(fd, str, strlen(str)+1) != strlen(str)+1) {
283                 printf("write(%s) error: %s\n", path, strerror(errno));
284                 EXIT(fd);
285         }
286
287         rc = close(fd);
288         if (rc) {
289                 printf("close(%s) error: %s\n", path, strerror(errno));
290                 EXIT(rc);
291         }
292 }
293
294 static void _t_grep(const char *path, char *str, int should_contain)
295 {
296         char buf[1024];
297         int fd;
298         int rc;
299         
300         fd = t_open_readonly(path);
301         if (lseek(fd, 0, SEEK_SET) == -1) {
302                 printf("pread_once: seek to 0 error: %s\n", strerror(errno));
303                 EXIT(fd);
304         }
305
306         rc = read(fd, buf, 1023);
307         if (rc < 0) {
308                 printf("grep: read error: %s\n", strerror(errno));
309                 EXIT(-1);
310         }
311         close(fd);
312         buf[rc] = 0;
313
314         if ((strstr(buf, str) != 0) ^ should_contain) {
315                 printf("grep: can't find string %s\n", str);
316                 EXIT(-1);
317         }
318 }
319
320 void t_grep(const char *path, char *str)
321 {
322         _t_grep(path, str, 1);
323 }
324
325 void t_grep_v(const char *path, char *str)
326 {
327         _t_grep(path, str, 0);
328 }
329
330 void t_ls(int fd, char *buf, int size)
331 {
332         cfs_dirent_t *ent;
333         int rc, pos;
334         loff_t base = 0;
335
336         printf("dir entries listing...\n");
337         while ((rc = getdirentries64(fd, buf, size, &base)) > 0) {
338                 pos = 0;
339                 while (pos < rc) {
340                         ent = (cfs_dirent_t *) ((char*) buf + pos);
341                         printf("%s\n", ent->d_name);
342                         pos += ent->d_reclen;
343                 }
344         }
345
346         if (rc < 0) {
347                 printf("getdents error %d\n", rc);
348                 EXIT(-1);
349         }
350 }
351
352 int t_fcntl(int fd, int cmd, ...)
353 {
354         va_list ap;
355         long arg;
356         struct flock *lock;
357         int rc = -1;
358
359         va_start(ap, cmd);
360         switch (cmd) {
361         case F_GETFL:
362                 va_end(ap);
363                 rc = fcntl(fd, cmd);
364                 if (rc == -1) {
365                         printf("fcntl GETFL failed: %s\n",
366                                  strerror(errno));
367                         EXIT(1);
368                 }
369                 break;
370         case F_SETFL:
371                 arg = va_arg(ap, long);
372                 va_end(ap);
373                 rc = fcntl(fd, cmd, arg);
374                 if (rc == -1) {
375                         printf("fcntl SETFL %ld failed: %s\n",
376                                  arg, strerror(errno));
377                         EXIT(1);
378                 }
379                 break;
380         case F_GETLK:
381 #ifdef F_GETLK64
382 #if F_GETLK64 != F_GETLK
383         case F_GETLK64:
384 #endif
385 #endif
386         case F_SETLK:
387 #ifdef F_SETLK64
388 #if F_SETLK64 != F_SETLK
389         case F_SETLK64:
390 #endif
391 #endif
392         case F_SETLKW:
393 #ifdef F_SETLKW64
394 #if F_SETLKW64 != F_SETLKW
395         case F_SETLKW64:
396 #endif
397 #endif
398                 lock = va_arg(ap, struct flock *);
399                 va_end(ap);
400                 rc = fcntl(fd, cmd, lock);
401                 if (rc == -1) {
402                         printf("fcntl cmd %d failed: %s\n",
403                                  cmd, strerror(errno));
404                         EXIT(1);
405                 }
406                 break;
407         case F_DUPFD:
408                 arg = va_arg(ap, long);
409                 va_end(ap);
410                 rc = fcntl(fd, cmd, arg);
411                 if (rc == -1) {
412                         printf("fcntl F_DUPFD %d failed: %s\n",
413                                  (int)arg, strerror(errno));
414                         EXIT(1);
415                 }
416                 break;
417         default:
418                 va_end(ap);
419                 printf("fcntl cmd %d not supported\n", cmd);
420                 EXIT(1);
421         }
422         return rc;
423 }
424
425 char *safe_strncpy(char *dst, char *src, int max_size)
426 {
427        int src_size;
428        src_size=strlen(src);
429        if (src_size >= max_size) {
430          src_size=max_size-1;
431        }
432        memcpy(dst, src, src_size);
433        dst[src_size]=0;
434
435        return(dst);
436 }