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/
34 #include <sys/types.h>
39 #define T1 "write data before unlink\n"
40 #define T2 "write data after unlink\n"
43 int main(int argc, char **argv)
49 if (argc < 2 || argc > 3) {
50 fprintf(stderr, "usage: %s filename [filename2]\n", argv[0]);
60 fprintf(stderr, "opening\n");
61 fd = open(fname, O_RDWR | O_TRUNC | O_CREAT, 0644);
63 fprintf(stderr, "open (normal) %s\n", strerror(errno));
67 fprintf(stderr, "writing\n");
68 rc = write(fd, T1, strlen(T1) + 1);
69 if (rc != strlen(T1) + 1) {
70 fprintf(stderr, "write (normal) %s (rc %d)\n",
76 fprintf(stderr, "unlinking %s\n", fname2);
79 fprintf(stderr, "unlink %s\n", strerror(errno));
83 printf("unlink %s and press enter\n", fname);
87 fprintf(stderr, "accessing (1)\n");
88 if (access(fname, F_OK) == 0) {
89 fprintf(stderr, "%s still exists\n", fname);
93 fprintf(stderr, "seeking (1)\n");
94 rc = lseek(fd, 0, SEEK_SET);
96 fprintf(stderr, "seek %s\n", strerror(errno));
100 fprintf(stderr, "accessing (2)\n");
101 if (access(fname, F_OK) == 0) {
102 fprintf(stderr, "%s still exists\n", fname);
106 fprintf(stderr, "fstat...\n");
109 fprintf(stderr, "fstat (unlink) %s\n", strerror(errno));
112 if (st.st_nlink != 0)
113 fprintf(stderr, "st_nlink = %d\n", (int)st.st_nlink);
115 fprintf(stderr, "reading\n");
116 rc = read(fd, buf, strlen(T1) + 1);
117 if (rc != strlen(T1) + 1) {
118 fprintf(stderr, "read (unlink) %s (rc %d)\n",
119 strerror(errno), rc);
123 fprintf(stderr, "comparing data\n");
124 if (memcmp(buf, T1, strlen(T1) + 1)) {
125 fprintf(stderr, "FAILURE: read wrong data after unlink\n");
129 fprintf(stderr, "truncating\n");
130 rc = ftruncate(fd, 0);
132 fprintf(stderr, "truncate (unlink) %s\n", strerror(errno));
136 fprintf(stderr, "seeking (2)\n");
137 rc = lseek(fd, 0, SEEK_SET);
139 fprintf(stderr, "seek (after unlink trunc) %s\n",
144 fprintf(stderr, "writing again\n");
145 rc = write(fd, T2, strlen(T2) + 1);
146 if (rc != strlen(T2) + 1) {
147 fprintf(stderr, "write (after unlink trunc) %s (rc %d)\n",
148 strerror(errno), rc);
152 fprintf(stderr, "seeking (3)\n");
153 rc = lseek(fd, 0, SEEK_SET);
155 fprintf(stderr, "seek (before unlink read) %s\n",
160 fprintf(stderr, "reading again\n");
161 rc = read(fd, buf, strlen(T2) + 1);
162 if (rc != strlen(T2) + 1) {
163 fprintf(stderr, "read (after unlink rewrite) %s (rc %d)\n",
164 strerror(errno), rc);
168 fprintf(stderr, "comparing data again\n");
169 if (memcmp(buf, T2, strlen(T2) + 1)) {
170 fprintf(stderr, "FAILURE: read wrong data after rewrite\n");
174 fprintf(stderr, "closing\n");
177 fprintf(stderr, "close (unlink) %s\n", strerror(errno));
181 fprintf(stderr, "SUCCESS - goto beer\n");