-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-lab-4-c.c
357 lines (317 loc) · 7.24 KB
/
test-lab-4-c.c
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
* test-lab-5 dir1 dir2
*
* Creates and deletes files in different directories
* on the same underlying file system. These operations
* should not require much put/get or lock traffic in
* a yfs with a write-back cache and lazy lock release.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <dirent.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
char d1[512], d2[512];
extern int errno;
void
create1(const char *d, const char *f, const char *in)
{
int fd;
char n[512];
/*
* The FreeBSD NFS client only invalidates its caches
* cache if the mtime changes by a whole second.
*/
sleep(1);
sprintf(n, "%s/%s", d, f);
fd = creat(n, 0666);
if(fd < 0){
fprintf(stderr, "test-lab-4-c: create(%s): %s\n",
n, strerror(errno));
exit(1);
}
if(write(fd, in, strlen(in)) != strlen(in)){
fprintf(stderr, "test-lab-4-c: write(%s): %s\n",
n, strerror(errno));
exit(1);
}
if(close(fd) != 0){
fprintf(stderr, "test-lab-4-c: close(%s): %s\n",
n, strerror(errno));
exit(1);
}
}
void
check1(const char *d, const char *f, const char *in)
{
int fd, cc;
char n[512], buf[512];
sprintf(n, "%s/%s", d, f);
fd = open(n, 0);
if(fd < 0){
fprintf(stderr, "test-lab-4-c: open(%s): %s\n",
n, strerror(errno));
exit(1);
}
errno = 0;
cc = read(fd, buf, sizeof(buf) - 1);
if(cc != strlen(in)){
fprintf(stderr, "test-lab-4-c: read(%s) returned too little %d%s%s\n",
n,
cc,
errno ? ": " : "",
errno ? strerror(errno) : "");
exit(1);
}
close(fd);
buf[cc] = '\0';
if(strncmp(buf, in, strlen(n)) != 0){
fprintf(stderr, "test-lab-4-c: read(%s) got \"%s\", not \"%s\"\n",
n, buf, in);
exit(1);
}
}
void
unlink1(const char *d, const char *f)
{
char n[512];
sleep(1);
sprintf(n, "%s/%s", d, f);
if(unlink(n) != 0){
fprintf(stderr, "test-lab-4-c: unlink(%s): %s\n",
n, strerror(errno));
exit(1);
}
}
void
checknot(const char *d, const char *f)
{
int fd;
char n[512];
sprintf(n, "%s/%s", d, f);
fd = open(n, 0);
if(fd >= 0){
fprintf(stderr, "test-lab-4-c: open(%s) succeeded for deleted file\n", n);
exit(1);
}
}
void
append1(const char *d, const char *f, const char *in)
{
int fd;
char n[512];
sleep(1);
sprintf(n, "%s/%s", d, f);
fd = open(n, O_WRONLY|O_APPEND);
if(fd < 0){
fprintf(stderr, "test-lab-4-c: append open(%s): %s\n",
n, strerror(errno));
exit(1);
}
if(write(fd, in, strlen(in)) != strlen(in)){
fprintf(stderr, "test-lab-4-c: append write(%s): %s\n",
n, strerror(errno));
exit(1);
}
if(close(fd) != 0){
fprintf(stderr, "test-lab-4-c: append close(%s): %s\n",
n, strerror(errno));
exit(1);
}
}
void
createn(const char *d, const char *prefix, int nf)
{
int fd, i;
char n[512];
/*
* The FreeBSD NFS client only invalidates its caches
* cache if the mtime changes by a whole second.
*/
sleep(1);
for(i = 0; i < nf; i++){
sprintf(n, "%s/%s-%d", d, prefix, i);
fd = creat(n, 0666);
if(fd < 0){
fprintf(stderr, "test-lab-4-c: create(%s): %s\n",
n, strerror(errno));
exit(1);
}
if(write(fd, &i, sizeof(i)) != sizeof(i)){
fprintf(stderr, "test-lab-4-c: write(%s): %s\n",
n, strerror(errno));
exit(1);
}
if(close(fd) != 0){
fprintf(stderr, "test-lab-4-c: close(%s): %s\n",
n, strerror(errno));
exit(1);
}
}
}
void
checkn(const char *d, const char *prefix, int nf)
{
int fd, i, cc, j;
char n[512];
for(i = 0; i < nf; i++){
sprintf(n, "%s/%s-%d", d, prefix, i);
fd = open(n, 0);
if(fd < 0){
fprintf(stderr, "test-lab-4-c: open(%s): %s\n",
n, strerror(errno));
exit(1);
}
j = -1;
cc = read(fd, &j, sizeof(j));
if(cc != sizeof(j)){
fprintf(stderr, "test-lab-4-c: read(%s) returned too little %d%s%s\n",
n,
cc,
errno ? ": " : "",
errno ? strerror(errno) : "");
exit(1);
}
if(j != i){
fprintf(stderr, "test-lab-4-c: checkn %s contained %d not %d\n",
n, j, i);
exit(1);
}
close(fd);
}
}
void
unlinkn(const char *d, const char *prefix, int nf)
{
char n[512];
int i;
sleep(1);
for(i = 0; i < nf; i++){
sprintf(n, "%s/%s-%d", d, prefix, i);
if(unlink(n) != 0){
fprintf(stderr, "test-lab-4-c: unlink(%s): %s\n",
n, strerror(errno));
exit(1);
}
}
}
int
compar(const void *xa, const void *xb)
{
char *a = *(char**)xa;
char *b = *(char**)xb;
return strcmp(a, b);
}
void
dircheck(const char *d, int nf)
{
DIR *dp;
struct dirent *e;
char *names[1000];
int nnames = 0, i;
dp = opendir(d);
if(dp == 0){
fprintf(stderr, "test-lab-4-c: opendir(%s): %s\n", d, strerror(errno));
exit(1);
}
while((e = readdir(dp))){
if(e->d_name[0] != '.'){
if(nnames >= sizeof(names)/sizeof(names[0])){
fprintf(stderr, "warning: too many files in %s\n", d);
}
names[nnames] = (char *) malloc(strlen(e->d_name) + 1);
strcpy(names[nnames], e->d_name);
nnames++;
}
}
closedir(dp);
if(nf != nnames){
fprintf(stderr, "test-lab-4-c: wanted %d dir entries, got %d\n", nf, nnames);
exit(1);
}
/* check for duplicate entries */
qsort(names, nnames, sizeof(names[0]), compar);
for(i = 0; i < nnames-1; i++){
if(strcmp(names[i], names[i+1]) == 0){
fprintf(stderr, "test-lab-4-c: duplicate directory entry for %s\n", names[i]);
exit(1);
}
}
for(i = 0; i < nnames; i++)
free(names[i]);
}
void
reap (int pid)
{
int wpid, status;
wpid = waitpid (pid, &status, 0);
if (wpid < 0) {
perror("waitpid");
exit(1);
}
if (wpid != pid) {
fprintf(stderr, "unexpected pid reaped: %d\n", wpid);
exit(1);
}
if(!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
fprintf(stderr, "child exited unhappily\n");
exit(1);
}
}
int
main(int argc, char *argv[])
{
int pid;
if(argc != 3){
fprintf(stderr, "Usage: test-lab-4-c dir1 dir2\n");
exit(1);
}
sprintf(d1, "%s/da%d", argv[1], getpid());
if(mkdir(d1, 0777) != 0){
fprintf(stderr, "test-lab-4-c: failed: mkdir(%s): %s\n",
d1, strerror(errno));
exit(1);
}
sprintf(d2, "%s/db%d", argv[2], getpid());
if(mkdir(d2, 0777) != 0){
fprintf(stderr, "test-lab-4-c: failed: mkdir(%s): %s\n",
d2, strerror(errno));
exit(1);
}
{
char dd[512];
sprintf(dd, "%s/da%d", argv[2], getpid());
if(access(dd, 0) != 0){
fprintf(stderr, "test-lab-4-c: failed: access(%s) after mkdir %s: %s\n",
dd, d1, strerror(errno));
exit(1);
}
}
setbuf(stdout, 0);
printf("Create/delete in separate directories: ");
pid = fork();
if(pid < 0){
perror("test-lab-4-c: fork");
exit(1);
}
if(pid == 0){
createn(d2, "xx", 100);
unlinkn(d2, "xx", 99);
exit(0);
}
createn(d1, "yy", 100);
unlinkn(d1, "yy", 99);
sleep(4);
reap(pid);
dircheck(d1, 1);
dircheck(d2, 1);
printf("tests completed OK\n");
exit(0);
return(0);
}