-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-lab3-part5-a.pl
executable file
·262 lines (213 loc) · 5.4 KB
/
test-lab3-part5-a.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
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
#!/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 != 0){
print STDERR "Usage: test-lab3-part5.pl directory1\n";
exit(1);
}
my $dir1 = $ARGV[0];
my $f1 = "a$$";
my $f2 = "b$$";
my $files = { };
my $seq = 0;
for(my $iters = 0; $iters < 30; $iters++){
createone();
}
# createone();
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, 411);
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, 819);
append($dir1, $f1, 1025);
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 "Check that one cannot open non-existant file: ";
checknot($dir1, "z-$$-z");
print "OK\n";
print "Passed all tests\n";
exit(0);
sub createone {
my $name = "file -\n-\t-";
for(my $i = 0; $i < 40; $i++){
$name .= sprintf("%c", ord('a') + int(rand(26)));
}
$name .= "-$$-" . $seq;
$seq = $seq + 1;
my $contents = rand();
print "create $name\n";
if(!open(F, ">$dir1/$name")){
print STDERR "cannot create $dir1/$name : $!\n";
exit(1);
}
close(F);
$files->{$name} = $contents;
}
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);
}
}
}
sub overallcheck {
checkcontent($dir1, $f2);
checkcontent($dir1, $f1);
checknot($dir1, "z-$$-z");
dircheck($dir1);
}
# to crash and restart chfs
sub chfsrestart {
# restart
print "===== ChFS Restart =====\n";
system './start.sh';
# wait until chfs restart
my $time = 5;
while($time--) {
print "Wait for ChFS to mount...\n";
if(mounted()) { # mounted successfully
last; # eq to 'break' in C
} else { # wait for mounting
sleep(0.1);
};
}
if (!mounted()) {
print "Fail to restart chfs!\n";
exit(1);
}
}
sub chfscrash {
print "===== ChFS Crash =====\n";
system './stop.sh';
# `pkill -SIGUSR1 chfs_client`;
if ($? == -1) {
print "Failed to crash ChFS: $!\n";
}
# wait for old chfs to exit on its own
while(mounted()) {
print "Wait for ChFS to unmount...\n";
sleep(0.1);
}
}
sub mounted {
return (`mount | grep $dir1 | grep -v grep | wc -l` == 1);
}