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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
27 * This file is part of Lustre, http://www.lustre.org/
28 * Lustre is a trademark of Sun Microsystems, Inc.
35 #include <sys/types.h>
40 #define T1 "write data before unlink\n"
41 #define T2 "write data after unlink\n"
44 int main(int argc, char **argv)
50 if (argc < 2 || argc > 3) {
51 fprintf(stderr, "usage: %s filename [filename2]\n", argv[0]);
61 fprintf(stderr, "opening\n");
62 fd = open(fname, O_RDWR | O_TRUNC | O_CREAT, 0644);
64 fprintf(stderr, "open (normal) %s\n", strerror(errno));
68 fprintf(stderr, "writing\n");
69 rc = write(fd, T1, strlen(T1) + 1);
70 if (rc != strlen(T1) + 1) {
71 fprintf(stderr, "write (normal) %s (rc %d)\n",
77 fprintf (stderr, "unlinking %s\n", fname2);
80 fprintf(stderr, "unlink %s\n", strerror(errno));
84 printf("unlink %s and press enter\n", fname);
88 fprintf(stderr, "accessing (1)\n");
89 if (access(fname, F_OK) == 0) {
90 fprintf(stderr, "%s still exists\n", fname);
94 fprintf(stderr, "seeking (1)\n");
95 rc = lseek(fd, 0, SEEK_SET);
97 fprintf(stderr, "seek %s\n", strerror(errno));
101 fprintf(stderr, "accessing (2)\n");
102 if (access(fname, F_OK) == 0) {
103 fprintf(stderr, "%s still exists\n", fname);
107 fprintf(stderr, "fstat...\n");
110 fprintf(stderr, "fstat (unlink) %s\n", strerror(errno));
113 if (st.st_nlink != 0)
114 fprintf(stderr, "st_nlink = %d\n", (int)st.st_nlink);
116 fprintf(stderr, "reading\n");
117 rc = read(fd, buf, strlen(T1) + 1);
118 if (rc != strlen(T1) + 1) {
119 fprintf(stderr, "read (unlink) %s (rc %d)\n",
120 strerror(errno), rc);
124 fprintf(stderr, "comparing data\n");
125 if (memcmp(buf, T1, strlen(T1) + 1) ) {
126 fprintf(stderr, "FAILURE: read wrong data after unlink\n");
130 fprintf(stderr, "truncating\n");
131 rc = ftruncate(fd, 0);
133 fprintf(stderr, "truncate (unlink) %s\n", strerror(errno));
137 fprintf(stderr, "seeking (2)\n");
138 rc = lseek(fd, 0, SEEK_SET);
140 fprintf(stderr, "seek (after unlink trunc) %s\n",
145 fprintf(stderr, "writing again\n");
146 rc = write(fd, T2, strlen(T2) + 1);
147 if (rc != strlen(T2) + 1) {
148 fprintf(stderr, "write (after unlink trunc) %s (rc %d)\n",
149 strerror(errno), rc);
153 fprintf(stderr, "seeking (3)\n");
154 rc = lseek(fd, 0, SEEK_SET);
156 fprintf(stderr, "seek (before unlink read) %s\n",
161 fprintf(stderr, "reading again\n");
162 rc = read(fd, buf, strlen(T2) + 1);
163 if (rc != strlen(T2) + 1) {
164 fprintf(stderr, "read (after unlink rewrite) %s (rc %d)\n",
165 strerror(errno), rc);
169 fprintf(stderr, "comparing data again\n");
170 if (memcmp(buf, T2, strlen(T2) + 1)) {
171 fprintf(stderr, "FAILURE: read wrong data after rewrite\n");
175 fprintf(stderr, "closing\n");
178 fprintf(stderr, "close (unlink) %s\n", strerror(errno));
182 fprintf(stderr, "SUCCESS - goto beer\n");