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