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