1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
41 #include <sys/types.h>
46 #define T1 "write data before unlink\n"
47 #define T2 "write data after unlink\n"
50 int main(int argc, char **argv)
56 if (argc < 2 || argc > 3) {
57 fprintf(stderr, "usage: %s filename [filename2]\n", argv[0]);
67 fprintf(stderr, "opening\n");
68 fd = open(fname, O_RDWR | O_TRUNC | O_CREAT, 0644);
70 fprintf(stderr, "open (normal) %s\n", strerror(errno));
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",
83 fprintf (stderr, "unlinking %s\n", fname2);
86 fprintf(stderr, "unlink %s\n", strerror(errno));
90 printf("unlink %s and press enter\n", fname);
94 if (access(fname, F_OK) == 0) {
95 fprintf(stderr, "%s still exists\n", fname2);
99 fprintf(stderr, "resetting fd offset\n");
100 rc = lseek(fd, 0, SEEK_SET);
102 fprintf(stderr, "seek %s\n", strerror(errno));
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\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\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");