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