4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
39 #include <sys/types.h>
44 #define T1 "write data before unlink\n"
45 #define T2 "write data after unlink\n"
48 int main(int argc, char **argv)
54 if (argc < 2 || argc > 3) {
55 fprintf(stderr, "usage: %s filename [filename2]\n", argv[0]);
65 fprintf(stderr, "opening\n");
66 fd = open(fname, O_RDWR | O_TRUNC | O_CREAT, 0644);
68 fprintf(stderr, "open (normal) %s\n", strerror(errno));
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",
81 fprintf (stderr, "unlinking %s\n", fname2);
84 fprintf(stderr, "unlink %s\n", strerror(errno));
88 printf("unlink %s and press enter\n", fname);
92 fprintf(stderr, "accessing (1)\n");
93 if (access(fname, F_OK) == 0) {
94 fprintf(stderr, "%s still exists\n", fname);
98 fprintf(stderr, "seeking (1)\n");
99 rc = lseek(fd, 0, SEEK_SET);
101 fprintf(stderr, "seek %s\n", strerror(errno));
105 fprintf(stderr, "accessing (2)\n");
106 if (access(fname, F_OK) == 0) {
107 fprintf(stderr, "%s still exists\n", fname);
111 fprintf(stderr, "fstat...\n");
114 fprintf(stderr, "fstat (unlink) %s\n", strerror(errno));
117 if (st.st_nlink != 0)
118 fprintf(stderr, "st_nlink = %d\n", (int)st.st_nlink);
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);
128 fprintf(stderr, "comparing data\n");
129 if (memcmp(buf, T1, strlen(T1) + 1) ) {
130 fprintf(stderr, "FAILURE: read wrong data after unlink\n");
134 fprintf(stderr, "truncating\n");
135 rc = ftruncate(fd, 0);
137 fprintf(stderr, "truncate (unlink) %s\n", strerror(errno));
141 fprintf(stderr, "seeking (2)\n");
142 rc = lseek(fd, 0, SEEK_SET);
144 fprintf(stderr, "seek (after unlink trunc) %s\n",
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);
157 fprintf(stderr, "seeking (3)\n");
158 rc = lseek(fd, 0, SEEK_SET);
160 fprintf(stderr, "seek (before unlink read) %s\n",
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);
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");
179 fprintf(stderr, "closing\n");
182 fprintf(stderr, "close (unlink) %s\n", strerror(errno));
186 fprintf(stderr, "SUCCESS - goto beer\n");