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