Whamcloud - gitweb
merge b_devel into HEAD, which will become 0.7.3
[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 <sys/stat.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9
10 #define T1 "write data before unlink\n"
11 #define T2 "write data after unlink\n"
12 char buf[128];
13
14 int main(int argc, char **argv)
15 {
16         char *fname, *fname2;
17         struct stat st;
18         int fd, rc;
19
20         if (argc < 2 || argc > 3) {
21                 fprintf(stderr, "usage: %s filename [filename2]\n", argv[0]);
22                 exit(1);
23         }
24
25         fname = argv[1];
26         if (argc == 3)
27                 fname2 = argv[2];
28         else
29                 fname2 = argv[1];
30
31         fprintf(stderr, "opening\n");
32         fd = open(fname, O_RDWR | O_TRUNC | O_CREAT, 0644);
33         if (fd == -1) {
34                 fprintf(stderr, "open (normal) %s\n", strerror(errno));
35                 exit(1);
36         }
37
38         fprintf(stderr, "writing\n");
39         rc = write(fd, T1, strlen(T1) + 1);
40         if (rc != strlen(T1) + 1) {
41                 fprintf(stderr, "write (normal) %s (rc %d)\n",
42                         strerror(errno), rc);
43                 exit(1);
44         }
45
46         if (argc == 3) {
47                 fprintf(stderr, "closing %s\n", fname);
48                 rc = close(fd);
49                 if (rc) {
50                         fprintf(stderr, "close (normal) %s\n", strerror(errno));
51                         exit(1);
52                 }
53
54                 fprintf(stderr, "opening %s\n", fname2);
55                 fd = open(fname2, O_RDWR);
56                 if (fd == -1) {
57                         fprintf(stderr, "open (unlink) %s\n", strerror(errno));
58                         exit(1);
59                 }
60
61                 fprintf (stderr, "unlinking %s\n", fname2);
62                 rc = unlink(fname2);
63                 if (rc) {
64                         fprintf(stderr, "unlink %s\n", strerror(errno));
65                         exit(1);
66                 }
67
68                 if (access(fname2, F_OK) == 0) {
69                         fprintf(stderr, "%s still exists\n", fname2);
70                         exit(1);
71                 }
72         } else {
73                 fprintf(stderr, "resetting fd offset\n");
74                 rc = lseek(fd, 0, SEEK_SET);
75                 if (rc) {
76                         fprintf(stderr, "seek %s\n", strerror(errno));
77                         exit(1);
78                 }
79
80                 printf("unlink %s and press enter\n", fname);
81                 getc(stdin);
82         }
83
84         if (access(fname, F_OK) == 0) {
85                 fprintf(stderr, "%s still exists\n", fname);
86                 exit(1);
87         }
88
89         fprintf(stderr, "fstating\n");
90         rc = fstat(fd, &st);
91         if (rc) {
92                 fprintf(stderr, "fstat (unlink) %s\n", strerror(errno));
93                 exit(1);
94         }
95         if (st.st_nlink != 0)
96                 fprintf(stderr, "st_nlink = %d\n", (int)st.st_nlink);
97
98         fprintf(stderr, "reading\n");
99         rc = read(fd, buf, strlen(T1) + 1);
100         if (rc != strlen(T1) + 1) {
101                 fprintf(stderr, "read (unlink) %s (rc %d)\n",
102                         strerror(errno), rc);
103                 exit(1);
104         }
105
106         fprintf(stderr, "comparing data\n");
107         if (memcmp(buf, T1, strlen(T1) + 1) ) {
108                 fprintf(stderr, "FAILURE: read wrong data after unlink\n");
109                 exit(1);
110         }
111
112         fprintf(stderr, "truncating\n");
113         rc = ftruncate(fd, 0);
114         if (rc) {
115                 fprintf(stderr, "truncate (unlink) %s\n", strerror(errno));
116                 exit(1);
117         }
118
119         fprintf(stderr, "seeking\n");
120         rc = lseek(fd, 0, SEEK_SET);
121         if (rc) {
122                 fprintf(stderr, "seek (after unlink trunc) %s\n",
123                         strerror(errno));
124                 exit(1);
125         }
126
127         fprintf(stderr, "writing again\n");
128         rc = write(fd, T2, strlen(T2) + 1);
129         if (rc != strlen(T2) + 1) {
130                 fprintf(stderr, "write (after unlink trunc) %s (rc %d)\n",
131                         strerror(errno), rc);
132                 exit(1);
133         }
134
135         fprintf(stderr, "seeking\n");
136         rc = lseek(fd, 0, SEEK_SET);
137         if (rc) {
138                 fprintf(stderr, "seek (before unlink read) %s\n",
139                         strerror(errno));
140                 exit(1);
141         }
142
143         fprintf(stderr, "reading again\n");
144         rc = read(fd, buf, strlen(T2) + 1);
145         if (rc != strlen(T2) + 1) {
146                 fprintf(stderr, "read (after unlink rewrite) %s (rc %d)\n",
147                         strerror(errno), rc);
148                 exit(1);
149         }
150
151         fprintf(stderr, "comparing data again\n");
152         if (memcmp(buf, T2, strlen(T2) + 1)) {
153                 fprintf(stderr, "FAILURE: read wrong data after rewrite\n");
154                 exit(1);
155         }
156
157         fprintf(stderr, "closing\n");
158         rc = close(fd);
159         if (rc) {
160                 fprintf(stderr, "close (unlink) %s\n", strerror(errno));
161                 exit(1);
162         }
163
164         fprintf(stderr, "SUCCESS - goto beer\n");
165         return 0;
166 }