Whamcloud - gitweb
LU-1187 tests: add create remote directory to racer
[fs/lustre-release.git] / lustre / tests / openunlink.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  */
34
35 #include <stdio.h>
36 #include <fcntl.h>
37 #include <string.h>
38 #include <errno.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43
44 #define T1 "write data before unlink\n"
45 #define T2 "write data after unlink\n"
46 char buf[128];
47
48 int main(int argc, char **argv)
49 {
50         char *fname, *fname2;
51         struct stat st;
52         int fd, rc;
53
54         if (argc < 2 || argc > 3) {
55                 fprintf(stderr, "usage: %s filename [filename2]\n", argv[0]);
56                 exit(1);
57         }
58
59         fname = argv[1];
60         if (argc == 3)
61                 fname2 = argv[2];
62         else
63                 fname2 = argv[1];
64
65         fprintf(stderr, "opening\n");
66         fd = open(fname, O_RDWR | O_TRUNC | O_CREAT, 0644);
67         if (fd == -1) {
68                 fprintf(stderr, "open (normal) %s\n", strerror(errno));
69                 exit(1);
70         }
71
72         fprintf(stderr, "writing\n");
73         rc = write(fd, T1, strlen(T1) + 1);
74         if (rc != strlen(T1) + 1) {
75                 fprintf(stderr, "write (normal) %s (rc %d)\n",
76                         strerror(errno), rc);
77                 exit(1);
78         }
79
80         if (argc == 3) {
81                 fprintf (stderr, "unlinking %s\n", fname2);
82                 rc = unlink(fname2);
83                 if (rc) {
84                         fprintf(stderr, "unlink %s\n", strerror(errno));
85                         exit(1);
86                 }
87         } else {
88                 printf("unlink %s and press enter\n", fname);
89                 getc(stdin);
90         }
91
92         fprintf(stderr, "accessing (1)\n");
93         if (access(fname, F_OK) == 0) {
94                 fprintf(stderr, "%s still exists\n", fname2);
95                 exit(1);
96         }
97
98         fprintf(stderr, "seeking (1)\n");
99         rc = lseek(fd, 0, SEEK_SET);
100         if (rc) {
101                 fprintf(stderr, "seek %s\n", strerror(errno));
102                 exit(1);
103         }
104
105         fprintf(stderr, "accessing (2)\n");
106         if (access(fname, F_OK) == 0) {
107                 fprintf(stderr, "%s still exists\n", fname);
108                 exit(1);
109         }
110
111         fprintf(stderr, "fstat...\n");
112         rc = fstat(fd, &st);
113         if (rc) {
114                 fprintf(stderr, "fstat (unlink) %s\n", strerror(errno));
115                 exit(1);
116         }
117         if (st.st_nlink != 0)
118                 fprintf(stderr, "st_nlink = %d\n", (int)st.st_nlink);
119
120         fprintf(stderr, "reading\n");
121         rc = read(fd, buf, strlen(T1) + 1);
122         if (rc != strlen(T1) + 1) {
123                 fprintf(stderr, "read (unlink) %s (rc %d)\n",
124                         strerror(errno), rc);
125                 exit(1);
126         }
127
128         fprintf(stderr, "comparing data\n");
129         if (memcmp(buf, T1, strlen(T1) + 1) ) {
130                 fprintf(stderr, "FAILURE: read wrong data after unlink\n");
131                 exit(1);
132         }
133
134         fprintf(stderr, "truncating\n");
135         rc = ftruncate(fd, 0);
136         if (rc) {
137                 fprintf(stderr, "truncate (unlink) %s\n", strerror(errno));
138                 exit(1);
139         }
140
141         fprintf(stderr, "seeking (2)\n");
142         rc = lseek(fd, 0, SEEK_SET);
143         if (rc) {
144                 fprintf(stderr, "seek (after unlink trunc) %s\n",
145                         strerror(errno));
146                 exit(1);
147         }
148
149         fprintf(stderr, "writing again\n");
150         rc = write(fd, T2, strlen(T2) + 1);
151         if (rc != strlen(T2) + 1) {
152                 fprintf(stderr, "write (after unlink trunc) %s (rc %d)\n",
153                         strerror(errno), rc);
154                 exit(1);
155         }
156
157         fprintf(stderr, "seeking (3)\n");
158         rc = lseek(fd, 0, SEEK_SET);
159         if (rc) {
160                 fprintf(stderr, "seek (before unlink read) %s\n",
161                         strerror(errno));
162                 exit(1);
163         }
164
165         fprintf(stderr, "reading again\n");
166         rc = read(fd, buf, strlen(T2) + 1);
167         if (rc != strlen(T2) + 1) {
168                 fprintf(stderr, "read (after unlink rewrite) %s (rc %d)\n",
169                         strerror(errno), rc);
170                 exit(1);
171         }
172
173         fprintf(stderr, "comparing data again\n");
174         if (memcmp(buf, T2, strlen(T2) + 1)) {
175                 fprintf(stderr, "FAILURE: read wrong data after rewrite\n");
176                 exit(1);
177         }
178
179         fprintf(stderr, "closing\n");
180         rc = close(fd);
181         if (rc) {
182                 fprintf(stderr, "close (unlink) %s\n", strerror(errno));
183                 exit(1);
184         }
185
186         fprintf(stderr, "SUCCESS - goto beer\n");
187         return 0;
188 }