forked from bohaoist/yfs2012
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-lab-2-b.pl
executable file
·209 lines (165 loc) · 4.29 KB
/
test-lab-2-b.pl
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
#!/usr/bin/perl -w
# Case 1: Single file system
# 1. create new files
# 2. open an existing file
# 3. write to a file
# - write to the middle
# - append
# - write from middle and beyond file length
# - seek beyond file length and write
# 4. read file content
# from each case above
#
# Make sure file handles/file type for new files are correct.
# Case 2: Two file systems mounted under same root dir.
# 0. Start two fs with same rootdir
# 1. create files in dir1
# 2. read the files from dir2
#
#
use strict;
$| = 1;
if($#ARGV != 1){
print STDERR "Usage: test-lab-2-b.pl directory1 directory2\n";
exit(1);
}
my $dir1 = $ARGV[0];
my $dir2 = $ARGV[1];
my $f1 = "a$$";
my $f2 = "b$$";
my $files = { };
print "Write and read one file: ";
writeone($dir1, $f1, 600);
checkcontent($dir1, $f1);
print "OK\n";
print "Write and read a second file: ";
writeone($dir1, $f2, 4111);
checkcontent($dir1, $f2);
checkcontent($dir1, $f1);
print "OK\n";
print "Overwrite an existing file: ";
writeone($dir1, $f1, 275); # shorter than before...
checkcontent($dir1, $f1);
checkcontent($dir1, $f2);
print "OK\n";
print "Append to an existing file: ";
writeone($dir1, $f1, 8192);
append($dir1, $f1, 7007);
checkcontent($dir1, $f1);
print "OK\n";
print "Write into the middle of an existing file: ";
writeat($dir1, $f1, 190);
checkcontent($dir1, $f1);
print "OK\n";
print "Write beyond the end of an existing file: ";
writeat($dir1, $f1, 65536);
checkcontent($dir1, $f1);
print "OK\n";
print "Check that one cannot open non-existant file: ";
checknot($dir1, "z-$$-z");
print "OK\n";
print "Check directory listing: ";
dircheck($dir1);
print "OK\n";
print "Read files via second server: ";
checkcontent($dir2, $f1);
checkcontent($dir2, $f2);
print "OK\n";
print "Check directory listing on second server: ";
dircheck($dir2);
print "OK\n";
print "Passed all tests\n";
sub writeone {
my($d, $name, $len) = @_;
my $contents = "";
my $f = $d . "/" . $name;
use FileHandle;
sysopen F, $f, O_TRUNC|O_RDWR|O_CREAT
or die "cannot create $f\n";
while(length($contents) < $len){
$contents .= rand();
}
$contents = substr($contents, 0, $len);
$files->{$name} = $contents;
syswrite F, $files->{$name}, length($files->{$name})
or die "cannot write to $f";
close(F);
}
sub checkcontent {
my($d, $name) = @_;
my $f = $d . "/" . $name;
open F, "$f" or die "could not open $f for reading";
my $c2 = "";
while(<F>) {
$c2 .= $_;
}
close(F);
$files->{$name} eq $c2 or die "content of $f is incorrect\n";
}
sub checknot {
my($d, $name) = @_;
my $f = $d . "/" . $name;
my $x = open(F, $f);
if(defined($x)){
print STDERR "$x exists but should not\n";
exit(1);
}
}
sub append {
my($d, $name, $n) = @_;
my $f = $d . "/" . $name;
use FileHandle;
sysopen F, "$f", O_RDWR
or die "cannot open $f for append\n";
my $contents = "";
while(length($contents) < $n){
$contents .= rand();
}
$contents = substr($contents, 0, $n);
$files->{$name} .= $contents; ## Append the file content
seek(F, 0, 2); ## goto end of file
syswrite(F, $contents, length($contents), 0) or die "cannot append to $f";
close(F);
}
sub writeat {
my($d, $name, $off) = @_;
my $f = $d . "/" . $name;
use FileHandle;
sysopen F, "$f", O_RDWR
or die "cannot open $f for read/write\n";
my $contents = rand();
my $x = $files->{$name};
if (length($x) < $off + length($contents)) {
my $nappend = $off + length($contents) - length($x);
for (my $i=0; $i < $nappend; $i++) {
$x .= "\0";
}
}
substr($x, $off, length($contents)) = $contents;
$files->{$name} = $x;
seek(F, $off, 0);
syswrite(F, $contents, length($contents), 0)
or die "cannot write $f at offset $off";
close(F);
}
sub dircheck {
my($dir) = @_;
opendir(D, $dir);
my %h;
my $f;
while(defined($f = readdir(D))){
if(defined($h{$f})){
print STDERR "$f appears more than once in directory $dir\n";
exit(1);
}
$h{$f} = 1;
}
closedir(D);
foreach $f (keys(%$files)){
if(!defined($h{$f})){
print STDERR "$f is missing from directory $dir\n";
exit(1);
}
}
}
exit(0);