1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
/*
* @(#)op_ren.c 1.4 2003/12/30 Connectathon Testsuite
* 1.3 Lachman ONC Test Suite source
*
* tests operation on open file which has been unlinked.
* steps taken:
* 1. create file
* 2. open for read/write
* 3. unlink file
* 4. write data
* 5. rewind
* 6. read data back
*/
#if defined (DOS) || defined (WIN32)
#define DOSorWIN32
#include "../tests.h"
#endif
#ifndef DOSorWIN32
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#if defined(SVR3) || defined(SVR4)
#include <fcntl.h>
#else
#include <sys/file.h>
#endif
#include <errno.h>
#endif /* DOSorWIN32 */
#define TBUFSIZ 100
static char wbuf[TBUFSIZ], rbuf[TBUFSIZ];
static char bufa[BUFSIZ];
static char bufb[BUFSIZ];
#define TMSG "This is a test message written to the target file\n"
static void
xxit(s)
char *s;
{
perror(s);
exit(1);
}
/*ARGSUSED*/
int
main(argc, argv)
int argc;
char *argv[];
{
int fd, ret;
char *taname;
char *tbname;
int errcount = 0;
long lret;
#ifdef DOSorWIN32
/*
* This test is too Unix oriented to be useful on a Dos or
* Windows platform. Thus, we'll skip this test, for now.
*/
fprintf(stderr, "This Test Not Executable on DOS or Windows\n");
exit(0);
#endif
#ifdef SUNOS4X
/*
* SunOS 4.x has the bug for which I assume this is testing,
* i.e. it doesn't realize that rename(foo, bar) may remove
* "bar" if it exists, and therefore doesn't first rename it
* to ".nfsXXXX" if it's currently open.
*
* Thus, we'll skip this test, for now.
*/
printf("Test skipped because we know SunOS 4.x client has the bug\n");
exit(0);
#endif
setbuf(stdout, NULL);
#ifdef MACOSX
/* make sure TMPDIR isn't set */
unsetenv("TMPDIR");
#endif
if ((taname = tempnam(".", "nfsa")) == NULL) {
fprintf(stderr, "can't construct a temporary filename: ");
xxit("tempnam");
}
if ((fd = creat(taname, 0777)) < 0) {
fprintf(stderr, "can't create %s: ", taname);
xxit("creat");
}
close(fd);
if ((tbname = tempnam(".", "nfsb")) == NULL) {
fprintf(stderr, "can't construct a temporary filename: ");
xxit("tempnam");
}
#ifdef O_RDWR
if ((fd = open(tbname, O_CREAT|O_TRUNC|O_RDWR, 0777)) < 0) {
fprintf(stderr, "can't create %s: ", tbname);
(void) unlink(taname);
xxit("open");
}
#else
if ((fd = creat(tbname, 0777)) < 0) {
fprintf(stderr, "can't create %s: ", tbname);
(void) unlink(taname);
xxit("creat");
}
close(fd);
if ((fd = open(tbname, 2)) < 0) {
fprintf(stderr, "can't reopen %s: ", tbname);
(void) unlink(taname);
(void) unlink(tbname);
xxit("open");
}
#endif /* O_RDWR */
printf("nfsjunk files before rename:\n ");
sprintf(bufa, "ls -al %s", taname);
sprintf(bufb, "ls -al %s", tbname);
system(bufa);
printf(" ");
system(bufb);
ret = rename(taname, tbname);
printf("%s open; rename ret = %d\n", tbname, ret);
if (ret)
xxit(" unlink");
printf("nfsjunk files after rename:\n ");
system(bufa);
printf(" ");
system(bufb);
strcpy(wbuf, TMSG);
if ((ret = write(fd, wbuf, TBUFSIZ)) != TBUFSIZ) {
fprintf(stderr, "write ret %d; expected %d\n", ret, TBUFSIZ);
if (ret < 0)
perror(" write");
exit(1);
}
if ((lret = lseek(fd, 0L, 0)) != 0L) {
fprintf(stderr, "lseek ret %ld; expected 0\n", lret);
if (lret < 0)
perror(" lseek");
exit(1);
}
if ((ret = read(fd, rbuf, TBUFSIZ)) != TBUFSIZ) {
fprintf(stderr, "read ret %d; expected %d\n", ret, TBUFSIZ);
if (ret < 0)
perror(" read");
exit(1);
}
if (strcmp(wbuf, rbuf) != 0) {
errcount++;
printf("read data not same as written data\n");
printf(" written: '%s'\n read: '%s'\n", wbuf, rbuf);
} else {
printf("data compare ok\n");
}
if (unlink(tbname) < 0) {
errcount++;
perror("unlink");
}
if (ret = close(fd)) {
errcount++;
perror("close");
}
printf("nfsjunk files after close:\n ");
system(bufa);
printf(" ");
system(bufb);
if ((ret = close(fd)) == 0) {
errcount++;
fprintf(stderr, "second close didn't return error!??\n");
}
if (errcount == 0)
printf("test completed successfully.\n");
exit(errcount);
}
|