Whamcloud - gitweb
Allow unlink to be done on another node, to test multi-client openunlink
[fs/lustre-release.git] / lustre / tests / openunlink.c
1 #include <stdio.h>
2 #include <fcntl.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <sys/types.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8
9 #define T1 "write before unlink\n"
10 #define T2 "write after unlink\n"
11 char buf[128];
12
13 int main(int argc, char **argv)
14 {
15         int fd, rc;
16
17         if (argc != 2) {
18                 fprintf(stderr, "usage: %s filename\n", argv[0]);
19                 exit(1);
20         } else {
21                 fprintf(stderr, "congratulations - program starting\n");
22         }
23
24         fprintf(stderr, "opening\n");
25         fd = open(argv[1], O_RDWR | O_TRUNC | O_CREAT, 0644);
26         if (fd == -1) {
27                 fprintf(stderr, "open (normal) %s\n", strerror(errno));
28                 exit(1);
29         }
30
31         fprintf(stderr, "writing\n");
32         rc = write(fd, T1, strlen(T1) + 1);
33         if (rc != strlen(T1) + 1) {
34                 fprintf(stderr, "write (normal) %s\n", strerror(errno));
35                 exit(1);
36         }
37
38         fprintf(stderr, "closing\n");
39         rc = close(fd);
40         if (rc) {
41                 fprintf(stderr, "close (normal) %s\n", strerror(errno));
42                 exit(1);
43         }
44
45         fprintf(stderr, "opening again\n");
46         fd = open(argv[1], O_RDWR);
47         if (fd == -1) {
48                 fprintf(stderr, "open (unlink) %s\n", strerror(errno));
49                 exit(1);
50         }
51
52 #if 0
53         fprintf(stderr, "unlinking\n");
54         rc = unlink(argv[1]);
55         if (rc) {
56                 fprintf(stderr, "unlink %s\n", strerror(errno));
57                 exit(1);
58         }
59 #else
60         printf("unlink %s and press enter\n", argv[1]);
61         getc(stdin);
62 #endif
63
64         fprintf(stderr, "reading\n");
65         rc = read(fd, buf, strlen(T1) + 1);
66         if (rc != strlen(T1) + 1) {
67                 fprintf(stderr, "read (unlink) %s rc %d\n",
68                         strerror(errno), rc);
69                 exit(1);
70         }
71
72         fprintf(stderr, "comparing data\n");
73         if (memcmp(buf, T1, strlen(T1) + 1) ) {
74                 fprintf(stderr, "FAILURE: read wrong data after unlink\n");
75                 exit(1);
76         }
77
78         fprintf(stderr, "truncating\n");
79         rc = ftruncate(fd, 0);
80         if (rc ) {
81                 fprintf(stderr, "truncate (unlink) %s\n", strerror(errno));
82                 exit(1);
83         }
84
85         fprintf(stderr, "seeking\n");
86         rc = lseek(fd, 0, SEEK_SET);
87         if (rc) {
88                 fprintf(stderr, "seek (after unlink trunc) %s\n",
89                         strerror(errno));
90                 exit(1);
91         }
92
93         fprintf(stderr, "writing again\n");
94         rc = write(fd, T2, strlen(T2) + 1);
95         if (rc != strlen(T2) + 1) {
96                 fprintf(stderr, "write (after unlink trunc) %s (rc %d)\n",
97                         strerror(errno), rc);
98                 exit(1);
99         }
100
101         fprintf(stderr, "seeking\n");
102         rc = lseek(fd, 0, SEEK_SET);
103         if (rc) {
104                 fprintf(stderr, "seek (before unlink read) %s\n",
105                         strerror(errno));
106                 exit(1);
107         }
108
109         fprintf(stderr, "reading again\n");
110         rc = read(fd, buf, strlen(T2) + 1);
111         if (rc != strlen(T2) + 1) {
112                 fprintf(stderr, "read (after unlink rewrite) %s\n",
113                         strerror(errno));
114                 exit(1);
115         }
116
117         fprintf(stderr, "comparing data again\n");
118         if (memcmp(buf, T2, strlen(T2) + 1)) {
119                 fprintf(stderr, "FAILURE: read wrong data after rewrite\n");
120                 exit(1);
121         }
122
123         fprintf(stderr, "closing again\n");
124         rc = close(fd);
125         if (rc) {
126                 fprintf(stderr, "close (unlink) %s\n", strerror(errno));
127                 exit(1);
128         }
129
130         fprintf(stderr, "SUCCESS - goto beer\n");
131         return 0;
132 }