Whamcloud - gitweb
edb1ff98faa861547a12371835797a08e79d650d
[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         fprintf(stderr, "unlinking\n");
53         rc = unlink(argv[1]);
54         if (rc) {
55                 fprintf(stderr, "unlink %s\n", strerror(errno));
56                 exit(1);
57         }
58
59         fprintf(stderr, "reading\n");
60         rc = read(fd, buf, strlen(T1) + 1);
61         if (rc != strlen(T1) + 1) {
62                 fprintf(stderr, "read (unlink) %s rc %d\n",
63                         strerror(errno), rc);
64                 exit(1);
65         }
66
67         fprintf(stderr, "comparing data\n");
68         if (memcmp(buf, T1, strlen(T1) + 1) ) {
69                 fprintf(stderr, "FAILURE: read wrong data after unlink\n");
70                 exit(1);
71         }
72
73         fprintf(stderr, "truncating\n");
74         rc = ftruncate(fd, 0);
75         if (rc ) {
76                 fprintf(stderr, "truncate (unlink) %s\n", strerror(errno));
77                 exit(1);
78         }
79
80         fprintf(stderr, "seeking\n");
81         rc = lseek(fd, 0, SEEK_SET);
82         if (rc) {
83                 fprintf(stderr, "seek (after unlink trunc) %s\n",
84                         strerror(errno));
85                 exit(1);
86         }
87
88         fprintf(stderr, "writing again\n");
89         rc = write(fd, T2, strlen(T2) + 1);
90         if (rc != strlen(T2) + 1) {
91                 fprintf(stderr, "write (after unlink trunc) %s (rc %d)\n",
92                         strerror(errno), rc);
93                 exit(1);
94         }
95
96         fprintf(stderr, "seeking\n");
97         rc = lseek(fd, 0, SEEK_SET);
98         if (rc) {
99                 fprintf(stderr, "seek (before unlink read) %s\n",
100                         strerror(errno));
101                 exit(1);
102         }
103
104         fprintf(stderr, "reading again\n");
105         rc = read(fd, buf, strlen(T2) + 1);
106         if (rc != strlen(T2) + 1) {
107                 fprintf(stderr, "read (after unlink rewrite) %s\n",
108                         strerror(errno));
109                 exit(1);
110         }
111
112         fprintf(stderr, "comparing data again\n");
113         if (memcmp(buf, T2, strlen(T2) + 1)) {
114                 fprintf(stderr, "FAILURE: read wrong data after rewrite\n");
115                 exit(1);
116         }
117
118         fprintf(stderr, "closing again\n");
119         rc = close(fd);
120         if (rc) {
121                 fprintf(stderr, "close (unlink) %s\n", strerror(errno));
122                 exit(1);
123         }
124
125         fprintf(stderr, "SUCCESS - goto beer\n");
126         return 0;
127 }