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 (c) 2002, 2010, Oracle and/or its affiliates. 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 fprintf(stderr, "accessing (1)\n");
95 if (access(fname, F_OK) == 0) {
96 fprintf(stderr, "%s still exists\n", fname2);
100 fprintf(stderr, "seeking (1)\n");
101 rc = lseek(fd, 0, SEEK_SET);
103 fprintf(stderr, "seek %s\n", strerror(errno));
107 fprintf(stderr, "accessing (2)\n");
108 if (access(fname, F_OK) == 0) {
109 fprintf(stderr, "%s still exists\n", fname);
113 fprintf(stderr, "fstat...\n");
116 fprintf(stderr, "fstat (unlink) %s\n", strerror(errno));
119 if (st.st_nlink != 0)
120 fprintf(stderr, "st_nlink = %d\n", (int)st.st_nlink);
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);
130 fprintf(stderr, "comparing data\n");
131 if (memcmp(buf, T1, strlen(T1) + 1) ) {
132 fprintf(stderr, "FAILURE: read wrong data after unlink\n");
136 fprintf(stderr, "truncating\n");
137 rc = ftruncate(fd, 0);
139 fprintf(stderr, "truncate (unlink) %s\n", strerror(errno));
143 fprintf(stderr, "seeking (2)\n");
144 rc = lseek(fd, 0, SEEK_SET);
146 fprintf(stderr, "seek (after unlink trunc) %s\n",
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);
159 fprintf(stderr, "seeking (3)\n");
160 rc = lseek(fd, 0, SEEK_SET);
162 fprintf(stderr, "seek (before unlink read) %s\n",
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);
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");
181 fprintf(stderr, "closing\n");
184 fprintf(stderr, "close (unlink) %s\n", strerror(errno));
188 fprintf(stderr, "SUCCESS - goto beer\n");