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
|
implement Nametree;
include "sys.m";
sys: Sys;
include "styx.m";
include "styxservers.m";
Navop: import Styxservers;
Enotfound, Eexists: import Styxservers;
Fholder: adt {
parentqid: big;
d: Sys->Dir;
child: cyclic ref Fholder;
sibling: cyclic ref Fholder;
hash: cyclic ref Fholder;
};
init()
{
sys = load Sys Sys->PATH;
}
start(): (ref Tree, chan of ref Styxservers->Navop)
{
fs := ref Tree(chan of ref Treeop, chan of string);
c := chan of ref Styxservers->Navop;
spawn fsproc(c, fs.c);
return (fs, c);
}
Tree.quit(t: self ref Tree)
{
t.c <-= nil;
}
Tree.create(t: self ref Tree, parentq: big, d: Sys->Dir): string
{
t.c <-= ref Treeop.Create(t.reply, parentq, d);
return <-t.reply;
}
Tree.remove(t: self ref Tree, q: big): string
{
t.c <-= ref Treeop.Remove(t.reply, q);
return <-t.reply;
}
Tree.wstat(t: self ref Tree, q: big, d: Sys->Dir): string
{
t.c <-= ref Treeop.Wstat(t.reply, q, d);
return <-t.reply;
}
Tree.getpath(t: self ref Tree, q: big): string
{
t. c <-= ref Treeop.Getpath(t.reply, q);
return <-t.reply;
}
fsproc(c: chan of ref Styxservers->Navop, fsc: chan of ref Treeop)
{
tab := array[23] of ref Fholder;
for (;;) alt {
grq := <-c =>
if (grq == nil)
exit;
(q, reply) := (grq.path, grq.reply);
fh := findfile(tab, q);
if (fh == nil) {
reply <-= (nil, Enotfound);
continue;
}
pick rq := grq {
Stat =>
reply <-= (ref fh.d, nil);
Walk =>
d := fswalk(tab, fh, rq.name);
if (d == nil)
reply <-= (nil, Enotfound);
else
reply <-= (d, nil);
Readdir =>
(start, end) := (rq.offset, rq.offset + rq.count);
fh = fh.child;
for (i := 0; i < end && fh != nil; i++) {
if (i >= start)
reply <-= (ref fh.d, nil);
fh = fh.sibling;
}
reply <-= (nil, nil);
* =>
panic(sys->sprint("unknown op %d\n", tagof(grq)));
}
grq := <-fsc =>
if (grq == nil)
exit;
(q, reply) := (grq.q, grq.reply);
pick rq := grq {
Create =>
reply <-= fscreate(tab, q, rq.d);
Remove =>
reply <-= fsremove(tab, q);
Wstat =>
reply <-= fswstat(tab, q, rq.d);
Getpath =>
reply <-= fsgetpath(tab, q);
* =>
panic(sys->sprint("unknown fs op %d\n", tagof(grq)));
}
}
}
hashfn(q: big, n: int): int
{
h := int (q % big n);
if (h < 0)
h += n;
return h;
}
findfile(tab: array of ref Fholder, q: big): ref Fholder
{
for (fh := tab[hashfn(q, len tab)]; fh != nil; fh = fh.hash)
if (fh.d.qid.path == q)
return fh;
return nil;
}
fsgetpath(tab: array of ref Fholder, q: big): string
{
fh := findfile(tab, q);
if (fh == nil)
return nil;
s := fh.d.name;
while (fh.parentqid != fh.d.qid.path) {
fh = findfile(tab, fh.parentqid);
if (fh == nil)
return nil;
s = fh.d.name + "/" + s;
}
return s;
}
fswalk(tab: array of ref Fholder, fh: ref Fholder, name: string): ref Sys->Dir
{
if (name == "..")
return ref findfile(tab, fh.parentqid).d;
for (fh = fh.child; fh != nil; fh = fh.sibling)
if (fh.d.name == name)
return ref fh.d;
return nil;
}
fsremove(tab: array of ref Fholder, q: big): string
{
prev: ref Fholder;
# remove from hash table
slot := hashfn(q, len tab);
for (fh := tab[slot]; fh != nil; fh = fh.hash) {
if (fh.d.qid.path == q)
break;
prev = fh;
}
if (fh == nil)
return Enotfound;
if (prev == nil)
tab[slot] = fh.hash;
else
prev.hash = fh.hash;
fh.hash = nil;
# remove from parent's children
parent := findfile(tab, fh.parentqid);
if (parent != nil) {
prev = nil;
for (sfh := parent.child; sfh != nil; sfh = sfh.sibling) {
if (sfh == fh)
break;
prev = sfh;
}
if (sfh == nil)
panic("child not found in parent");
if (prev == nil)
parent.child = fh.sibling;
else
prev.sibling = fh.sibling;
}
fh.sibling = nil;
# now remove any descendents
sibling: ref Fholder;
for (sfh := fh.child; sfh != nil; sfh = sibling) {
sibling = sfh.sibling;
sfh.parentqid = sfh.d.qid.path; # make sure it doesn't disrupt things.
fsremove(tab, sfh.d.qid.path);
}
return nil;
}
fscreate(tab: array of ref Fholder, q: big, d: Sys->Dir): string
{
parent := findfile(tab, q);
if (findfile(tab, d.qid.path) != nil)
return Eexists;
# allow creation of a root directory only if its parent is itself
if (parent == nil && d.qid.path != q)
return Enotfound;
fh: ref Fholder;
if (parent == nil)
fh = ref Fholder(q, d, nil, nil, nil);
else {
if (fswalk(tab, parent, d.name) != nil)
return Eexists;
fh = ref Fholder(parent.d.qid.path, d, nil, nil, nil);
fh.sibling = parent.child;
parent.child = fh;
}
slot := hashfn(d.qid.path, len tab);
fh.hash = tab[slot];
tab[slot] = fh;
return nil;
}
fswstat(tab: array of ref Fholder, q: big, d: Sys->Dir): string
{
fh := findfile(tab, q);
if (fh == nil)
return Enotfound;
d = applydir(d, fh.d);
# if renaming a file, check for duplicates
if (d.name != fh.d.name) {
parent := findfile(tab, fh.parentqid);
if (parent != nil && parent != fh && fswalk(tab, parent, d.name) != nil)
return Eexists;
}
fh.d = d;
fh.d.qid.path = q; # ensure the qid can't be changed
return nil;
}
applydir(d: Sys->Dir, onto: Sys->Dir): Sys->Dir
{
if (d.name != nil)
onto.name = d.name;
if (d.uid != nil)
onto.uid = d.uid;
if (d.gid != nil)
onto.gid = d.gid;
if (d.muid != nil)
onto.muid = d.muid;
if (d.qid.vers != ~0)
onto.qid.vers = d.qid.vers;
if (d.qid.qtype != ~0)
onto.qid.qtype = d.qid.qtype;
if (d.mode != ~0)
onto.mode = d.mode;
if (d.atime != ~0)
onto.atime = d.atime;
if (d.mtime != ~0)
onto.mtime = d.mtime;
if (d.length != ~big 0)
onto.length = d.length;
if (d.dtype != ~0)
onto.dtype = d.dtype;
if (d.dev != ~0)
onto.dev = d.dev;
return onto;
}
panic(s: string)
{
sys->fprint(sys->fildes(2), "panic: %s\n", s);
raise "panic";
}
|