-
Notifications
You must be signed in to change notification settings - Fork 0
/
RISC.FileDir.Mod.txt
352 lines (323 loc) · 12.7 KB
/
RISC.FileDir.Mod.txt
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
MODULE FileDir; (*NW 12.1.86 / 23.8.90 / 15.8.2013*)
IMPORT SYSTEM, Kernel;
(*File Directory is a B-tree with its root page at DirRootAdr.
Each entry contains a file name and the disk address of the file's head sector*)
CONST FnLength* = 32;
SecTabSize* = 64;
ExTabSize* = 12;
SectorSize* = 1024;
IndexSize* = SectorSize DIV 4;
HeaderSize* = 352;
DirRootAdr* = 29;
DirPgSize* = 24;
N = DirPgSize DIV 2;
DirMark* = 9B1EA38DH;
HeaderMark* = 9BA71D86H;
FillerSize = 52;
TYPE DiskAdr = INTEGER;
FileName* = ARRAY FnLength OF CHAR;
SectorTable* = ARRAY SecTabSize OF DiskAdr;
ExtensionTable* = ARRAY ExTabSize OF DiskAdr;
EntryHandler* = PROCEDURE (name: FileName; sec: DiskAdr; VAR continue: BOOLEAN);
FileHeader* =
RECORD (*first page of each file on disk*)
mark*: INTEGER;
name*: FileName;
aleng*, bleng*, date*: INTEGER;
ext*: ExtensionTable;
sec*: SectorTable;
fill: ARRAY SectorSize - HeaderSize OF BYTE;
END ;
FileHd* = POINTER TO FileHeader;
IndexSector* = ARRAY IndexSize OF DiskAdr;
DataSector* = ARRAY SectorSize OF BYTE;
DirEntry* = (*B-tree node*)
RECORD
name*: FileName;
adr*: DiskAdr; (*sec no of file header*)
p*: DiskAdr (*sec no of descendant in directory*)
END ;
DirPage* =
RECORD mark*: INTEGER;
m*: INTEGER;
p0*: DiskAdr; (*sec no of left descendant in directory*)
fill: ARRAY FillerSize OF BYTE;
e*: ARRAY DirPgSize OF DirEntry
END ;
(*Exported procedures: Search, Insert, Delete, Enumerate, Init*)
PROCEDURE Search*(name: FileName; VAR A: DiskAdr);
VAR i, L, R: INTEGER; dadr: DiskAdr;
a: DirPage;
BEGIN dadr := DirRootAdr; A := 0;
REPEAT Kernel.GetSector(dadr, a); ASSERT(a.mark = DirMark);
L := 0; R := a.m; (*binary search*)
WHILE L < R DO
i := (L+R) DIV 2;
IF name <= a.e[i].name THEN R := i ELSE L := i+1 END
END ;
IF (R < a.m) & (name = a.e[R].name) THEN A := a.e[R].adr (*found*)
ELSIF R = 0 THEN dadr := a.p0
ELSE dadr := a.e[R-1].p
END ;
UNTIL (dadr = 0) OR (A # 0)
END Search;
PROCEDURE insert(name: FileName;
dpg0: DiskAdr;
VAR h: BOOLEAN;
VAR v: DirEntry;
fad: DiskAdr);
(*h = "tree has become higher and v is ascending element"*)
VAR ch: CHAR;
i, j, L, R: INTEGER;
dpg1: DiskAdr;
u: DirEntry;
a: DirPage;
BEGIN (*~h*) Kernel.GetSector(dpg0, a); ASSERT(a.mark = DirMark);
L := 0; R := a.m; (*binary search*)
WHILE L < R DO
i := (L+R) DIV 2;
IF name <= a.e[i].name THEN R := i ELSE L := i+1 END
END ;
IF (R < a.m) & (name = a.e[R].name) THEN
a.e[R].adr := fad; Kernel.PutSector(dpg0, a) (*replace*)
ELSE (*not on this page*)
IF R = 0 THEN dpg1 := a.p0 ELSE dpg1 := a.e[R-1].p END ;
IF dpg1 = 0 THEN (*not in tree, insert*)
u.adr := fad; u.p := 0; h := TRUE; j := 0;
REPEAT ch := name[j]; u.name[j] := ch; INC(j)
UNTIL ch = 0X;
WHILE j < FnLength DO u.name[j] := 0X; INC(j) END ;
ELSE
insert(name, dpg1, h, u, fad)
END ;
IF h THEN (*insert u to the left of e[R]*)
IF a.m < DirPgSize THEN
h := FALSE; i := a.m;
WHILE i > R DO DEC(i); a.e[i+1] := a.e[i] END ;
a.e[R] := u; INC(a.m)
ELSE (*split page and assign the middle element to v*)
a.m := N; a.mark := DirMark;
IF R < N THEN (*insert in left half*)
v := a.e[N-1]; i := N-1;
WHILE i > R DO DEC(i); a.e[i+1] := a.e[i] END ;
a.e[R] := u; Kernel.PutSector(dpg0, a);
Kernel.AllocSector(dpg0, dpg0); i := 0;
WHILE i < N DO a.e[i] := a.e[i+N]; INC(i) END
ELSE (*insert in right half*)
Kernel.PutSector(dpg0, a);
Kernel.AllocSector(dpg0, dpg0); DEC(R, N); i := 0;
IF R = 0 THEN v := u
ELSE v := a.e[N];
WHILE i < R-1 DO a.e[i] := a.e[N+1+i]; INC(i) END ;
a.e[i] := u; INC(i)
END ;
WHILE i < N DO a.e[i] := a.e[N+i]; INC(i) END
END ;
a.p0 := v.p; v.p := dpg0
END ;
Kernel.PutSector(dpg0, a)
END
END
END insert;
PROCEDURE Insert*(name: FileName; fad: DiskAdr);
VAR oldroot: DiskAdr;
h: BOOLEAN; U: DirEntry;
a: DirPage;
BEGIN h := FALSE;
insert(name, DirRootAdr, h, U, fad);
IF h THEN (*root overflow*)
Kernel.GetSector(DirRootAdr, a); ASSERT(a.mark = DirMark);
Kernel.AllocSector(DirRootAdr, oldroot); Kernel.PutSector(oldroot, a);
a.mark := DirMark; a.m := 1; a.p0 := oldroot; a.e[0] := U;
Kernel.PutSector(DirRootAdr, a)
END
END Insert;
PROCEDURE underflow(VAR c: DirPage; (*ancestor page*)
dpg0: DiskAdr;
s: INTEGER; (*insertion point in c*)
VAR h: BOOLEAN); (*c undersize*)
VAR i, k: INTEGER;
dpg1: DiskAdr;
a, b: DirPage; (*a := underflowing page, b := neighbouring page*)
BEGIN Kernel.GetSector(dpg0, a); ASSERT(a.mark = DirMark);
(*h & a.m = N-1 & dpg0 = c.e[s-1].p*)
IF s < c.m THEN (*b := page to the right of a*)
dpg1 := c.e[s].p; Kernel.GetSector(dpg1, b); ASSERT(b.mark = DirMark);
k := (b.m-N+1) DIV 2; (*k = no. of items available on page b*)
a.e[N-1] := c.e[s]; a.e[N-1].p := b.p0;
IF k > 0 THEN
(*move k-1 items from b to a, one to c*) i := 0;
WHILE i < k-1 DO a.e[i+N] := b.e[i]; INC(i) END ;
c.e[s] := b.e[i]; b.p0 := c.e[s].p;
c.e[s].p := dpg1; b.m := b.m - k; i := 0;
WHILE i < b.m DO b.e[i] := b.e[i+k]; INC(i) END ;
Kernel.PutSector(dpg1, b); a.m := N-1+k; h := FALSE
ELSE (*merge pages a and b, discard b*) i := 0;
WHILE i < N DO a.e[i+N] := b.e[i]; INC(i) END ;
i := s; DEC(c.m);
WHILE i < c.m DO c.e[i] := c.e[i+1]; INC(i) END ;
a.m := 2*N; h := c.m < N
END ;
Kernel.PutSector(dpg0, a)
ELSE (*b := page to the left of a*) DEC(s);
IF s = 0 THEN dpg1 := c.p0 ELSE dpg1 := c.e[s-1].p END ;
Kernel.GetSector(dpg1, b); ASSERT(b.mark = DirMark);
k := (b.m-N+1) DIV 2; (*k = no. of items available on page b*)
IF k > 0 THEN
i := N-1;
WHILE i > 0 DO DEC(i); a.e[i+k] := a.e[i] END ;
i := k-1; a.e[i] := c.e[s]; a.e[i].p := a.p0;
(*move k-1 items from b to a, one to c*) b.m := b.m - k;
WHILE i > 0 DO DEC(i); a.e[i] := b.e[i+b.m+1] END ;
c.e[s] := b.e[b.m]; a.p0 := c.e[s].p;
c.e[s].p := dpg0; a.m := N-1+k; h := FALSE;
Kernel.PutSector(dpg0, a)
ELSE (*merge pages a and b, discard a*)
c.e[s].p := a.p0; b.e[N] := c.e[s]; i := 0;
WHILE i < N-1 DO b.e[i+N+1] := a.e[i]; INC(i) END ;
b.m := 2*N; DEC(c.m); h := c.m < N
END ;
Kernel.PutSector(dpg1, b)
END
END underflow;
PROCEDURE delete(name: FileName;
dpg0: DiskAdr;
VAR h: BOOLEAN;
VAR fad: DiskAdr);
(*search and delete entry with key name; if a page underflow arises,
balance with adjacent page or merge; h := "page dpg0 is undersize"*)
VAR i, L, R: INTEGER;
dpg1: DiskAdr;
a: DirPage;
PROCEDURE del(VAR a: DirPage; R: INTEGER; dpg1: DiskAdr; VAR h: BOOLEAN);
VAR dpg2: DiskAdr; (*global: a, R*)
b: DirPage;
BEGIN Kernel.GetSector(dpg1, b); ASSERT(b.mark = DirMark); dpg2 := b.e[b.m-1].p;
IF dpg2 # 0 THEN del(a, R, dpg2, h);
IF h THEN underflow(b, dpg2, b.m, h); Kernel.PutSector(dpg1, b) END
ELSE
b.e[b.m-1].p := a.e[R].p; a.e[R] := b.e[b.m-1];
DEC(b.m); h := b.m < N; Kernel.PutSector(dpg1, b)
END
END del;
BEGIN (*~h*) Kernel.GetSector(dpg0, a); ASSERT(a.mark = DirMark);
L := 0; R := a.m; (*binary search*)
WHILE L < R DO
i := (L+R) DIV 2;
IF name <= a.e[i].name THEN R := i ELSE L := i+1 END
END ;
IF R = 0 THEN dpg1 := a.p0 ELSE dpg1 := a.e[R-1].p END ;
IF (R < a.m) & (name = a.e[R].name) THEN
(*found, now delete*) fad := a.e[R].adr;
IF dpg1 = 0 THEN (*a is a leaf page*)
DEC(a.m); h := a.m < N; i := R;
WHILE i < a.m DO a.e[i] := a.e[i+1]; INC(i) END
ELSE del(a, R, dpg1, h);
IF h THEN underflow(a, dpg1, R, h) END
END ;
Kernel.PutSector(dpg0, a)
ELSIF dpg1 # 0 THEN
delete(name, dpg1, h, fad);
IF h THEN underflow(a, dpg1, R, h); Kernel.PutSector(dpg0, a) END
ELSE (*not in tree*) fad := 0
END
END delete;
PROCEDURE Delete*(name: FileName; VAR fad: DiskAdr);
VAR h: BOOLEAN; newroot: DiskAdr;
a: DirPage;
BEGIN h := FALSE;
delete(name, DirRootAdr, h, fad);
IF h THEN (*root underflow*)
Kernel.GetSector(DirRootAdr, a); ASSERT(a.mark = DirMark);
IF (a.m = 0) & (a.p0 # 0) THEN
newroot := a.p0; Kernel.GetSector(newroot, a); ASSERT(a.mark = DirMark);
Kernel.PutSector(DirRootAdr, a) (*discard newroot*)
END
END
END Delete;
PROCEDURE enumerate(prefix: ARRAY OF CHAR;
dpg: DiskAdr;
proc: EntryHandler;
VAR continue: BOOLEAN);
VAR i, j: INTEGER; pfx, nmx: CHAR;
dpg1: DiskAdr; a: DirPage;
BEGIN Kernel.GetSector(dpg, a); ASSERT(a.mark = DirMark); i := 0;
WHILE (i < a.m) & continue DO
j := 0;
REPEAT pfx := prefix[j]; nmx := a.e[i].name[j]; INC(j)
UNTIL (nmx # pfx) OR (pfx = 0X);
IF nmx >= pfx THEN
IF i = 0 THEN dpg1 := a.p0 ELSE dpg1 := a.e[i-1].p END ;
IF dpg1 # 0 THEN enumerate(prefix, dpg1, proc, continue) END ;
IF pfx = 0X THEN
IF continue THEN proc(a.e[i].name, a.e[i].adr, continue) END
ELSE continue := FALSE
END
END ;
INC(i)
END ;
IF continue & (i > 0) & (a.e[i-1].p # 0) THEN
enumerate(prefix, a.e[i-1].p, proc, continue)
END
END enumerate;
PROCEDURE Enumerate*(prefix: ARRAY OF CHAR; proc: EntryHandler);
VAR b: BOOLEAN;
BEGIN b := TRUE; enumerate(prefix, DirRootAdr, proc, b)
END Enumerate;
(* ----- initialization ----- *)
PROCEDURE Init*;
VAR k: INTEGER;
A: ARRAY 2000 OF DiskAdr;
PROCEDURE MarkSectors(VAR A: ARRAY OF DiskAdr; k: INTEGER);
VAR L, R, i, j, n: INTEGER; x: DiskAdr;
hd: FileHeader;
B: IndexSector;
PROCEDURE sift(VAR A: ARRAY OF DiskAdr; L, R: INTEGER);
VAR i, j: INTEGER; x: DiskAdr;
BEGIN j := L; x := A[j];
REPEAT i := j; j := 2*j + 1;
IF (j+1 < R) & (A[j] < A[j+1]) THEN INC(j) END ;
IF (j < R) & (x <= A[j]) THEN A[i] := A[j] END
UNTIL (j >= R) OR (x > A[j]);
A[i] := x
END sift;
BEGIN L := k DIV 2; R := k; (*heapsort*)
WHILE L > 0 DO DEC(L); sift(A, L, R) END ;
WHILE R > 0 DO
DEC(R); x := A[0]; A[0] := A[R]; A[R] := x; sift(A, L, R)
END ;
WHILE L < k DO
Kernel.GetSector(A[L], hd); ASSERT(hd.mark = HeaderMark);
IF hd.aleng < SecTabSize THEN j := hd.aleng + 1;
REPEAT DEC(j); Kernel.MarkSector(hd.sec[j]) UNTIL j = 0
ELSE j := SecTabSize;
REPEAT DEC(j); Kernel.MarkSector(hd.sec[j]) UNTIL j = 0;
n := (hd.aleng - SecTabSize) DIV 256; i := 0;
WHILE i <= n DO
Kernel.MarkSector(hd.ext[i]);
Kernel.GetSector(hd.ext[i], B); (*index sector*)
IF i < n THEN j := 256 ELSE j := (hd.aleng - SecTabSize) MOD 256 + 1 END ;
REPEAT DEC(j); Kernel.MarkSector(B[j]) UNTIL j = 0;
INC(i)
END
END ;
INC(L)
END
END MarkSectors;
PROCEDURE TraverseDir(VAR A: ARRAY OF DiskAdr; VAR k: INTEGER; dpg: DiskAdr);
VAR i: INTEGER; a: DirPage;
BEGIN Kernel.GetSector(dpg, a); ASSERT(a.mark = DirMark); Kernel.MarkSector(dpg); i := 0;
WHILE i < a.m DO
A[k] := a.e[i].adr; INC(k); INC(i);
IF k = 2000 THEN MarkSectors(A, k); k := 0 END
END ;
IF a.p0 # 0 THEN
TraverseDir(A, k, a.p0); i := 0;
WHILE i < a.m DO
TraverseDir(A, k, a.e[i].p); INC(i)
END
END
END TraverseDir;
BEGIN k := 0; TraverseDir(A, k, DirRootAdr); MarkSectors(A, k)
END Init;
END FileDir.