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