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
|
implement Walk, Fsmodule;
include "sys.m";
sys: Sys;
include "readdir.m";
readdir: Readdir;
include "draw.m";
include "sh.m";
include "alphabet/reports.m";
reports: Reports;
Report, quit, report: import reports;
include "alphabet/fs.m";
fs: Fs;
Value: import fs;
Fschan, Fsdata, Entrychan, Entry,
Gatechan, Gatequery, Nilentry, Option,
Next, Down, Skip, Quit: import Fs;
Walk: module {};
Loopcheck: adt {
a: array of list of ref Sys->Dir;
new: fn(): ref Loopcheck;
enter: fn(l: self ref Loopcheck, d: ref Sys->Dir): int;
leave: fn(l: self ref Loopcheck, d: ref Sys->Dir);
};
types(): string
{
return "xs-bs";
}
badmod(p: string)
{
sys->fprint(sys->fildes(2), "fs: walk: cannot load %s: %r\n", p);
raise "fail:bad module";
}
init()
{
sys = load Sys Sys->PATH;
fs = load Fs Fs->PATH;
if(fs == nil)
badmod(Fs->PATH);
fs->init();
reports = load Reports Reports->PATH;
if(reports == nil)
badmod(Reports->PATH);
readdir = load Readdir Readdir->PATH;
if(readdir == nil)
badmod(Readdir->PATH);
}
run(nil: ref Draw->Context, report: ref Report,
nil: list of Option, args: list of ref Value): ref Value
{
path := (hd args).s().i;
(ok, d) := sys->stat(path);
if(ok== -1){
sys->fprint(sys->fildes(2), "fs: walk: cannot stat %q: %r\n", path);
return nil;
}
if((d.mode & Sys->DMDIR) == 0){
# XXX could produce an fs containing just the single file.
# would have to split the path though.
sys->fprint(sys->fildes(2), "fs: walk: %q is not a directory\n", path);
return nil;
}
sync := chan of int;
c := chan of (Fsdata, chan of int);
spawn fswalkproc(sync, path, c, Sys->ATOMICIO, report.start("walk"));
<-sync;
return ref Value.Vx(c);
}
# XXX need to avoid loops in the filesystem...
fswalkproc(sync: chan of int, path: string, c: Fschan, blocksize: int, errorc: chan of string)
{
sys->pctl(Sys->FORKNS, nil);
sync <-= 1;
# XXX could allow a single root file?
if(sys->chdir(path) == -1){
report(errorc, sys->sprint("cannot cd to %q: %r", path));
fs->sendnulldir(c);
quit(errorc);
}
(ok, d) := sys->stat(".");
if(ok == -1){
report(errorc, sys->sprint("cannot stat %q: %r", path));
fs->sendnulldir(c);
quit(errorc);
}
d.name = path;
reply := chan of int;
c <-= ((ref d, nil), reply);
if(<-reply == Down){
loopcheck := Loopcheck.new();
loopcheck.enter(ref d);
if(path[len path - 1] != '/')
path[len path] = '/';
fswalkdir(path, c, blocksize, loopcheck, errorc);
c <-= ((nil, nil), reply);
<-reply;
}
quit(errorc);
}
fswalkdir(path: string, c: Fschan, blocksize: int, loopcheck: ref Loopcheck, errorc: chan of string)
{
reply := chan of int;
(a, n) := readdir->init(".", Readdir->NAME|Readdir->COMPACT);
if(n == -1){
report(errorc, sys->sprint("cannot readdir %q: %r", path));
return;
}
for(i := 0; i < n; i++)
if(a[i].mode & Sys->DMDIR)
if(loopcheck.enter(a[i]) == 0)
a[i].dtype = ~0;
directory:
for(i = 0; i < n; i++){
if(a[i].mode & Sys->DMDIR){
d := a[i];
if(d.dtype == ~0){
report(errorc, sys->sprint("filesystem loop at %#q", path + d.name));
continue;
}
if(sys->chdir("./" + d.name) == -1){
report(errorc, sys->sprint("cannot cd to %#q: %r", path + a[i].name));
continue;
}
c <-= ((d, nil), reply);
case <-reply {
Quit =>
quit(errorc);
Down =>
fswalkdir(path + a[i].name + "/", c, blocksize, loopcheck, errorc);
c <-= ((nil, nil), reply);
if(<-reply == Quit)
quit(errorc);
Skip =>
sys->chdir("..");
i++;
break directory;
Next =>
break;
}
if(sys->chdir("..") == -1) # XXX what should we do if this fails?
report(errorc, sys->sprint("failed to cd .. from %#q: %r\n", path + a[i].name));
} else {
if(fswalkfile(path, a[i], c, blocksize, errorc) == Skip)
break directory;
}
}
for(i = n - 1; i >= 0; i--)
if(a[i].mode & Sys->DMDIR && a[i].dtype != ~0)
loopcheck.leave(a[i]);
}
fswalkfile(path: string, d: ref Sys->Dir, c: Fschan, blocksize: int, errorc: chan of string): int
{
reply := chan of int;
fd := sys->open(d.name, Sys->OREAD);
if(fd == nil){
report(errorc, sys->sprint("cannot open %q: %r", path+d.name));
return Next;
}
c <-= ((d, nil), reply);
case <-reply {
Quit =>
quit(errorc);
Skip =>
return Skip;
Next =>
return Next;
Down =>
break;
}
length := d.length;
for(n := big 0; n < length; ){
nr := blocksize;
if(n + big blocksize > length)
nr = int (length - n);
buf := array[nr] of byte;
nr = sys->read(fd, buf, nr);
if(nr <= 0){
if(nr < 0)
report(errorc, sys->sprint("error reading %q: %r", path + d.name));
else
report(errorc, sys->sprint("%q is shorter than expected (%bd/%bd)",
path + d.name, n, length));
break;
}else if(nr < len buf)
buf = buf[0:nr];
c <-= ((nil, buf), reply);
case <-reply {
Quit =>
quit(errorc);
Skip =>
return Next;
}
n += big nr;
}
c <-= ((nil, nil), reply);
if(<-reply == Quit)
quit(errorc);
return Next;
}
HASHSIZE: con 32;
issamedir(d0, d1: ref Sys->Dir): int
{
(q0, q1) := (d0.qid, d1.qid);
return q0.path == q1.path &&
q0.qtype == q1.qtype &&
d0.dtype == d1.dtype &&
d0.dev == d1.dev;
}
Loopcheck.new(): ref Loopcheck
{
return ref Loopcheck(array[HASHSIZE] of list of ref Sys->Dir);
}
# XXX we're assuming no-one modifies the values in d behind our back...
Loopcheck.enter(l: self ref Loopcheck, d: ref Sys->Dir): int
{
slot := int d.qid.path & (HASHSIZE-1);
for(ll := l.a[slot]; ll != nil; ll = tl ll)
if(issamedir(d, hd ll))
return 0;
l.a[slot] = d :: l.a[slot];
return 1;
}
Loopcheck.leave(l: self ref Loopcheck, d: ref Sys->Dir)
{
slot := int d.qid.path & (HASHSIZE-1);
l.a[slot] = tl l.a[slot];
}
|