Whamcloud - gitweb
b=22977 add echoclient async journal regression test to acc-sm obdfilter-survey
[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 (c) 2002, 2010, Oracle and/or its affiliates. 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         fprintf(stderr, "accessing (1)\n");
95         if (access(fname, F_OK) == 0) {
96                 fprintf(stderr, "%s still exists\n", fname2);
97                 exit(1);
98         }
99
100         fprintf(stderr, "seeking (1)\n");
101         rc = lseek(fd, 0, SEEK_SET);
102         if (rc) {
103                 fprintf(stderr, "seek %s\n", strerror(errno));
104                 exit(1);
105         }
106
107         fprintf(stderr, "accessing (2)\n");
108         if (access(fname, F_OK) == 0) {
109                 fprintf(stderr, "%s still exists\n", fname);
110                 exit(1);
111         }
112
113         fprintf(stderr, "fstat...\n");
114         rc = fstat(fd, &st);
115         if (rc) {
116                 fprintf(stderr, "fstat (unlink) %s\n", strerror(errno));
117                 exit(1);
118         }
119         if (st.st_nlink != 0)
120                 fprintf(stderr, "st_nlink = %d\n", (int)st.st_nlink);
121
122         fprintf(stderr, "reading\n");
123         rc = read(fd, buf, strlen(T1) + 1);
124         if (rc != strlen(T1) + 1) {
125                 fprintf(stderr, "read (unlink) %s (rc %d)\n",
126                         strerror(errno), rc);
127                 exit(1);
128         }
129
130         fprintf(stderr, "comparing data\n");
131         if (memcmp(buf, T1, strlen(T1) + 1) ) {
132                 fprintf(stderr, "FAILURE: read wrong data after unlink\n");
133                 exit(1);
134         }
135
136         fprintf(stderr, "truncating\n");
137         rc = ftruncate(fd, 0);
138         if (rc) {
139                 fprintf(stderr, "truncate (unlink) %s\n", strerror(errno));
140                 exit(1);
141         }
142
143         fprintf(stderr, "seeking (2)\n");
144         rc = lseek(fd, 0, SEEK_SET);
145         if (rc) {
146                 fprintf(stderr, "seek (after unlink trunc) %s\n",
147                         strerror(errno));
148                 exit(1);
149         }
150
151         fprintf(stderr, "writing again\n");
152         rc = write(fd, T2, strlen(T2) + 1);
153         if (rc != strlen(T2) + 1) {
154                 fprintf(stderr, "write (after unlink trunc) %s (rc %d)\n",
155                         strerror(errno), rc);
156                 exit(1);
157         }
158
159         fprintf(stderr, "seeking (3)\n");
160         rc = lseek(fd, 0, SEEK_SET);
161         if (rc) {
162                 fprintf(stderr, "seek (before unlink read) %s\n",
163                         strerror(errno));
164                 exit(1);
165         }
166
167         fprintf(stderr, "reading again\n");
168         rc = read(fd, buf, strlen(T2) + 1);
169         if (rc != strlen(T2) + 1) {
170                 fprintf(stderr, "read (after unlink rewrite) %s (rc %d)\n",
171                         strerror(errno), rc);
172                 exit(1);
173         }
174
175         fprintf(stderr, "comparing data again\n");
176         if (memcmp(buf, T2, strlen(T2) + 1)) {
177                 fprintf(stderr, "FAILURE: read wrong data after rewrite\n");
178                 exit(1);
179         }
180
181         fprintf(stderr, "closing\n");
182         rc = close(fd);
183         if (rc) {
184                 fprintf(stderr, "close (unlink) %s\n", strerror(errno));
185                 exit(1);
186         }
187
188         fprintf(stderr, "SUCCESS - goto beer\n");
189         return 0;
190 }