Whamcloud - gitweb
d69dff3eb325787ba4918b4ac44cd81941ea420b
[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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #include <stdio.h>
38 #include <fcntl.h>
39 #include <string.h>
40 #include <errno.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <stdlib.h>
44 #include <unistd.h>
45
46 #define T1 "write data before unlink\n"
47 #define T2 "write data after unlink\n"
48 char buf[128];
49
50 int main(int argc, char **argv)
51 {
52         char *fname, *fname2;
53         struct stat st;
54         int fd, rc;
55
56         if (argc < 2 || argc > 3) {
57                 fprintf(stderr, "usage: %s filename [filename2]\n", argv[0]);
58                 exit(1);
59         }
60
61         fname = argv[1];
62         if (argc == 3)
63                 fname2 = argv[2];
64         else
65                 fname2 = argv[1];
66
67         fprintf(stderr, "opening\n");
68         fd = open(fname, O_RDWR | O_TRUNC | O_CREAT, 0644);
69         if (fd == -1) {
70                 fprintf(stderr, "open (normal) %s\n", strerror(errno));
71                 exit(1);
72         }
73
74         fprintf(stderr, "writing\n");
75         rc = write(fd, T1, strlen(T1) + 1);
76         if (rc != strlen(T1) + 1) {
77                 fprintf(stderr, "write (normal) %s (rc %d)\n",
78                         strerror(errno), rc);
79                 exit(1);
80         }
81
82         if (argc == 3) {
83                 fprintf (stderr, "unlinking %s\n", fname2);
84                 rc = unlink(fname2);
85                 if (rc) {
86                         fprintf(stderr, "unlink %s\n", strerror(errno));
87                         exit(1);
88                 }
89         } else {
90                 printf("unlink %s and press enter\n", fname);
91                 getc(stdin);
92         }
93
94         if (access(fname, F_OK) == 0) {
95                 fprintf(stderr, "%s still exists\n", fname2);
96                 exit(1);
97         }
98
99         fprintf(stderr, "resetting fd offset\n");
100         rc = lseek(fd, 0, SEEK_SET);
101         if (rc) {
102                 fprintf(stderr, "seek %s\n", strerror(errno));
103                 exit(1);
104         }
105
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\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\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 }