forked from kgober/FSX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Unix.cs
2471 lines (2342 loc) · 133 KB
/
Unix.cs
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Unix.cs
// Copyright © 2019-2020 Kenneth Gober
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// Unix File System Structure
//
// https://www.tuhs.org/Archive/Distributions/Research/Dennis_v5/v5man.pdf
// https://archive.org/download/v6-manual/v6-manual.pdf
// http://web.cuzuco.com/~cuzuco/v7/v7vol1.pdf
// http://web.cuzuco.com/~cuzuco/v7/v7vol2b.pdf
//
// The Unix v5 file system format is described by the "FILE SYSTEM(V)" man page.
// The v6 format is similar, except that the maximum file size increases from
// 1048576 to 16777216 by changing the last indirect block to doubly indirect.
// The formats are identical as long as no file is larger than 917504 bytes. If
// the largest file is between 917505 and 1048576 bytes inclusive, the formats
// can be (mostly) differentiated by looking at the last indirect block.
//
// The Unix v7 file system format increases block pointers to 24 bits, and
// increases the size of an i-node to contain additional block pointers. The
// 'large file' flag is removed; i-node block pointers are interpreted the same
// way for all files: 10 direct pointers, then 1 indirect, 1 double-indirect,
// and 1 triple-indirect pointer. Each indirect block contains 128 4-byte
// pointers, so the maximum file size increases to:
// 512 * (10 + 128 + 128*128 + 128*128*128) = 1082201087 bytes
//
// 2.8BSD doubles the block size to 1024 bytes. As a result, each indirect
// block contains 256 4-byte pointers.
//
// 2.11BSD changes the i-node format to contain 4 direct pointers rather than
// 10, but the size pointers (both direct and indirect) are increased from 3
// bytes each to 4. Additionally, 2.11BSD changes the format of directories
// to support file names up to 63 characters.
// Improvements / To Do
// decouple block size, inode and directory format to reuse more v5/v7/2BSD code
// in Test, check for duplicate names in a directory
// relax link count check for inodes (file systems are readable despite this)
// relax block allocation checks (file systems are readable despite this)
// support BSD Fast File System (FFS)
// allow files to be written/deleted in images
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace FSX
{
class Inode
{
protected Volume mVol;
public Int32 iNum;
public UInt16 flags;
public Int16 nlinks;
public Int16 uid;
public Int16 gid;
public Int32 size;
public Int32[] addr;
public DateTime atime;
public DateTime mtime;
public DateTime ctime;
public Inode(Volume volume, Int32 iNumber)
{
mVol = volume;
iNum = iNumber;
}
public Volume Volume
{
get { return mVol; }
}
public virtual Char Type
{
get { return '\0'; }
}
public virtual String Mode
{
get { return null; }
}
public virtual Int32 this[Int32 blockNum]
{
get { return 0; }
}
}
// v5/v6 super block
// 0 isize - i-list size (in blocks)
// 2 fsize - file system size
// 4 nfree - number of free blocks listed in superblock (0-100)
// 6 free - free block list
// 206 ninode - number of fre einodes listed in superblock (0-100)
// 208 inode - free inode list
// 408 flock
// 409 ilock
// 410 fmod
// 411 time
// 415
//
// v5/v6 i-node flags
// 0x8000 allocated / in use
// 0x6000 file type (file=0x0000 cdev=0x2000 dir=0x4000 bdev=0x6000)
// 0x1000 large file (addr[] has indirect blocks, instead of direct)
// 0x0800 setuid
// 0x0400 setgid
// 0x01c0 owner permissions (r=0x0100 w=0x0080 x=0x0040)
// 0x0038 group permissions (r=0x0020 w=0x0010 x=0x0008)
// 0x0007 world permissions (r=0x0004 w=0x0002 x=0x0001)
partial class UnixV5 : FileSystem
{
protected class InodeV5 : Inode
{
public InodeV5(Volume volume, Int32 iNumber) : base(volume, iNumber)
{
Block B = volume[2 + (iNumber - 1) / 16];
Int32 offset = ((iNumber - 1) % 16) * 32;
flags = B.GetUInt16L(ref offset);
nlinks = B.GetByte(ref offset);
uid = B.GetByte(ref offset);
gid = B.GetByte(ref offset);
size = B.GetByte(ref offset) << 16;
size |= B.GetUInt16L(ref offset);
addr = new Int32[8];
for (Int32 i = 0; i < 8; i++) addr[i] = B.GetUInt16L(ref offset);
atime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(B.GetInt32P(ref offset));
mtime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(B.GetInt32P(ref offset));
}
public override Char Type
{
get
{
if ((flags & 0xe000) == 0x8000) return '-';
if ((flags & 0xe000) == 0xc000) return 'd';
if ((flags & 0xe000) == 0xa000) return 'c';
if ((flags & 0xe000) == 0xe000) return 'b';
return ' ';
}
}
public override String Mode
{
get
{
StringBuilder buf = new StringBuilder(9);
buf.Append(((flags & 0x0100) == 0) ? '-' : 'r');
buf.Append(((flags & 0x0080) == 0) ? '-' : 'w');
buf.Append(((flags & 0x0040) == 0) ? '-' : ((flags & 0x0800) == 0) ? 'x' : 's');
buf.Append(((flags & 0x0020) == 0) ? '-' : 'r');
buf.Append(((flags & 0x0010) == 0) ? '-' : 'w');
buf.Append(((flags & 0x0008) == 0) ? '-' : ((flags & 0x0400) == 0) ? 'x' : 's');
buf.Append(((flags & 0x0004) == 0) ? '-' : 'r');
buf.Append(((flags & 0x0002) == 0) ? '-' : 'w');
buf.Append(((flags & 0x0001) == 0) ? '-' : 'x');
return buf.ToString();
}
}
public override Int32 this[Int32 blockNum]
{
get
{
if ((flags & 0x1000) == 0) return addr[blockNum];
Block B = mVol[addr[blockNum / 256]];
return B.GetUInt16L(2 * (blockNum % 256));
}
}
}
protected Int32 BLOCK_SIZE = 512;
protected Int32 ROOT_INUM = 1;
protected Int32 INOPB = 16;
protected Int32 ISIZE = 32;
protected Volume mVol;
protected String mType;
protected Inode mDirNode;
protected String mDir;
protected UnixV5()
{
}
public UnixV5(Volume volume)
{
mVol = volume;
mType = "Unix/V5";
mDirNode = new InodeV5(volume, ROOT_INUM);
mDir = "/";
}
public override String Source
{
get { return mVol.Source; }
}
public override String Type
{
get { return mType; }
}
public override String Info
{
get { return mVol.Info; }
}
public override String Dir
{
get { return mDir; }
}
public override Encoding DefaultEncoding
{
get { return Encoding.ASCII; }
}
public override void ChangeDir(String dirSpec)
{
Inode dirNode;
String dirName;
if (!FindFile(dirSpec, out dirNode, out dirName))
{
Console.Error.WriteLine("Not found: {0}", dirSpec);
return;
}
if (dirNode.Type != 'd')
{
Console.Error.WriteLine("Not a directory: {0}", dirName);
return;
}
mDirNode = dirNode;
mDir = dirName;
}
public override void ListDir(String fileSpec, TextWriter output)
{
Int32 p;
Inode dirNode = mDirNode;
String dirName = mDir;
if ((fileSpec == null) || (fileSpec.Length == 0))
{
fileSpec = "*";
}
else if ((p = fileSpec.LastIndexOf('/')) == 0)
{
dirNode = GetInode(ROOT_INUM);
dirName = "/";
while ((fileSpec.Length != 0) && (fileSpec[0] == '/')) fileSpec = fileSpec.Substring(1);
if (fileSpec.Length == 0) fileSpec = "*";
}
else if (p != -1)
{
if ((!FindFile(fileSpec.Substring(0, p), out dirNode, out dirName)) || (dirNode.Type != 'd'))
{
Console.Error.WriteLine("Not found: {0}", fileSpec);
return;
}
fileSpec = fileSpec.Substring(p + 1);
while ((fileSpec.Length != 0) && (fileSpec[0] == '/')) fileSpec = fileSpec.Substring(1);
if (fileSpec.Length == 0) fileSpec = "*";
}
// if fileSpec names a single directory, show the content of that directory
Inode iNode;
String pathName;
if ((FindDirEntry(dirNode, dirName, fileSpec, out iNode, out pathName)) && (iNode.Type == 'd'))
{
dirNode = iNode;
dirName = pathName;
fileSpec = "*";
output.WriteLine("{0}:", pathName);
}
// count number of blocks used
Int32 n = 0;
Regex RE = Regex(fileSpec);
Byte[] data = ReadFile(dirNode, BLOCK_SIZE);
for (Int32 bp = 0; bp < data.Length; bp += 16)
{
Int32 iNum = Buffer.GetUInt16L(data, bp);
if (iNum == 0) continue;
String name = Buffer.GetCString(data, bp + 2, 14, Encoding.ASCII);
if (!RE.IsMatch(name)) continue;
iNode = GetInode(iNum);
n += (iNode.size + 511) / 512;
}
// show directory listing
output.WriteLine("total {0:D0}", n);
for (Int32 bp = 0; bp < data.Length; bp += 16)
{
Int32 iNum = Buffer.GetUInt16L(data, bp);
if (iNum == 0) continue;
String name = Buffer.GetCString(data, bp + 2, 14, Encoding.ASCII);
if (!RE.IsMatch(name)) continue;
iNode = GetInode(iNum);
Char ft = iNode.Type;
switch (ft)
{
case '-':
case 'd':
output.WriteLine("{0,5:D0} {1}{2} {3,2:D0} {4,-5} {5,7:D0} {6} {7,-14}", iNum, ft, iNode.Mode, iNode.nlinks, iNode.uid.ToString(), iNode.size, iNode.mtime.ToString("MM/dd/yyyy HH:mm"), name);
break;
case 'b':
case 'c':
output.WriteLine("{0,5:D0} {1}{2} {3,2:D0} {4,-5} {5,3:D0},{6,3:D0} {7} {8,-14}", iNum, ft, iNode.Mode, iNode.nlinks, iNode.uid.ToString(), iNode.addr[0] >> 8, iNode.addr[0] & 0x00ff, iNode.mtime.ToString("MM/dd/yyyy HH:mm"), name);
break;
}
}
}
public override void DumpDir(String fileSpec, TextWriter output)
{
ListDir(fileSpec, output);
}
public override void ListFile(String fileSpec, Encoding encoding, TextWriter output)
{
Inode iNode;
String name;
if (!FindFile(fileSpec, out iNode, out name)) return;
String buf = encoding.GetString(ReadFile(iNode, BLOCK_SIZE));
Int32 p = 0;
for (Int32 i = 0; i < buf.Length; i++)
{
if (buf[i] != '\n') continue;
output.WriteLine(buf.Substring(p, i - p));
p = i + 1;
}
}
public override void DumpFile(String fileSpec, TextWriter output)
{
Inode iNode;
String name;
if (!FindFile(fileSpec, out iNode, out name)) return;
Program.Dump(null, ReadFile(iNode, BLOCK_SIZE), output, 16, BLOCK_SIZE, Program.DumpOptions.ASCII);
}
public override String FullName(String fileSpec)
{
Inode iNode;
String name;
if (!FindFile(fileSpec, out iNode, out name)) return null;
return name;
}
public override Byte[] ReadFile(String fileSpec)
{
Inode iNode;
String name;
if (!FindFile(fileSpec, out iNode, out name)) return null;
return ReadFile(iNode, BLOCK_SIZE);
}
public override Boolean SaveFS(String fileName, String format)
{
if ((fileName == null) || (fileName.Length == 0)) return false;
FileStream f = new FileStream(fileName, FileMode.Create);
Byte[] buf = new Byte[BLOCK_SIZE];
for (Int32 i = 0; i < mVol.BlockCount; i++)
{
mVol[i].CopyTo(buf, 0);
f.Write(buf, 0, BLOCK_SIZE);
}
f.Close();
return true;
}
protected virtual Inode GetInode(Int32 iNumber)
{
return new InodeV5(mVol, iNumber);
}
protected Boolean FindFile(String pathSpec, out Inode iNode, out String pathName)
{
iNode = mDirNode;
pathName = mDir;
if ((pathSpec == null) || (pathSpec.Length == 0)) return false;
if (pathSpec[0] == '/')
{
iNode = GetInode(ROOT_INUM);
pathName = "/";
while (pathSpec[0] == '/')
{
pathSpec = pathSpec.Substring(1);
if (pathSpec.Length == 0) return true;
}
}
while (pathSpec.Length != 0)
{
String s;
Int32 p = pathSpec.IndexOf('/');
if (p == -1)
{
s = pathSpec;
pathSpec = String.Empty;
}
else
{
s = pathSpec.Substring(0, p);
pathSpec = pathSpec.Substring(p);
}
if (!FindDirEntry(iNode, pathName, s, out iNode, out pathName)) return false;
if ((pathSpec.Length != 0) && (iNode.Type != 'd')) return false;
while ((pathSpec.Length != 0) && (pathSpec[0] == '/')) pathSpec = pathSpec.Substring(1);
}
return true;
}
protected virtual Boolean FindDirEntry(Inode dirNode, String dirName, String entryName, out Inode iNode, out String pathName)
{
iNode = null;
pathName = null;
Int32 n = 0;
Regex RE = Regex(entryName);
Byte[] dir = ReadFile(dirNode, BLOCK_SIZE);
for (Int32 dp = 0; dp < dir.Length; dp += 16)
{
Int32 iNum = Buffer.GetUInt16L(dir, dp);
if (iNum == 0) continue;
String name = Buffer.GetCString(dir, dp + 2, 14, Encoding.ASCII);
if (RE.IsMatch(name))
{
n++;
iNode = GetInode(iNum);
if ((name == ".") || ((name == "..") && (dirName == "/")))
{
pathName = dirName;
}
else if (name == "..")
{
pathName = dirName.Substring(0, dirName.Length - 1);
pathName = pathName.Substring(0, pathName.LastIndexOf('/') + 1);
}
else
{
pathName = String.Concat(dirName, name, (iNode.Type == 'd') ? "/" : null);
}
}
}
return (n == 1);
}
}
partial class UnixV5
{
protected static Byte[] ReadFile(Inode iNode, Int32 blockSize)
{
Byte[] buf = new Byte[iNode.size];
Int32 bp = 0;
while (bp < buf.Length)
{
Int32 n = bp / blockSize; // file block number
Int32 b = iNode[n]; // volume block number
if (b != 0) iNode.Volume[b].CopyTo(buf, bp);
bp += blockSize;
}
return buf;
}
protected static Regex Regex(String pattern)
{
String p = pattern;
p = p.Replace(".", "\ufffd");
p = p.Replace("?", ".").Replace("*", @".*");
p = p.Replace("\ufffd", "\\.");
p = String.Concat("^", p, "$");
Debug.WriteLine(2, "Regex: {0} => {1}", pattern, p);
return new Regex(p);
}
}
partial class UnixV5 : IFileSystemAuto
{
public static TestDelegate GetTest()
{
return UnixV5.Test;
}
// level 0 - check basic volume parameters (return required block size and volume type)
// level 1 - check boot block (return volume size and type)
// level 2 - check volume descriptor (aka home/super block) (return file system size and type)
// level 3 - check file headers (aka inodes) (return file system size and type)
// level 4 - check directory structure (return file system size and type)
// level 5 - check file header allocation (return file system size and type)
// level 6 - check data block allocation (return file system size and type)
public static Boolean Test(Volume volume, Int32 level, out Int32 size, out Type type)
{
Int32 BLOCK_SIZE = 512;
Int32 ROOT_INUM = 1;
Int32 INOPB = 16;
// level 0 - check basic volume parameters (return required block size and volume type)
size = BLOCK_SIZE;
type = typeof(Volume);
if (volume == null) return false;
if (volume.BlockSize != size) return Debug.WriteInfo(false, "UnixV5.Test: invalid block size (is {0:D0}, require {1:D0})", volume.BlockSize, size);
if (level == 0) return true;
// level 1 - check boot block (return volume size and type)
size = -1; // Unix V5 doesn't support disk labels
if (volume.BlockCount < 1) return Debug.WriteInfo(false, "UnixV5.Test: volume too small to contain boot block");
if (level == 1) return true;
// level 2 - check volume descriptor (aka home/super block) (return file system size and type)
type = null;
if (volume.BlockCount < 2) return Debug.WriteInfo(false, "UnixV5.Test: volume too small to contain super-block");
Block SB = volume[1]; // super-block
Int32 bp = 0;
Int32 isize = SB.GetUInt16L(ref bp); // SB[0] - number of blocks used for inodes
if (volume.BlockCount < isize + 2) return Debug.WriteInfo(false, "UnixV5.Test: volume too small to contain i-node list (blocks {0:D0}-{1:D0} > {2:D0})", 2, isize + 1, volume.BlockCount - 1);
Int32 fsize = SB.GetUInt16L(ref bp); // SB[2] - file system size (in blocks)
if (isize + 2 > fsize) return Debug.WriteInfo(false, "UnixV5.Test: super-block i-list exceeds file system size ({0:D0} > {1:D0})", isize + 2, fsize);
Int32 n = SB.GetUInt16L(ref bp); // SB[4] - number of blocks in super-block free block list
if (n > 100) return Debug.WriteInfo(false, "UnixV5.Test: super-block free block count invalid (is {0:D0}, require 0 <= n <= 100)", n);
Int32 p = SB.GetUInt16L(bp); // SB[6] - free block chain next pointer
if ((p != 0) && ((p < isize + 2) || (p >= fsize))) return Debug.WriteInfo(false, "UnixV5.Test: super-block free chain pointer invalid (is {0:D0}, require {1:D0} <= n < {2:D0})", p, isize + 2, fsize);
for (Int32 i = 1; i < n; i++) // SB[8] - free block list
{
if (((p = SB.GetUInt16L(bp + 2 * i)) < isize + 2) || (p >= fsize)) return Debug.WriteInfo(false, "UnixV5.Test: super-block free block {0:D0} invalid (is {1:D0}, require {2:D0} <= n < {3:D0})", i, p, isize + 2, fsize);
}
bp += 100 * 2;
n = SB.GetUInt16L(ref bp); // SB[206] - number of inodes in super-block free inode list
if (n > 100) return Debug.WriteInfo(false, "UnixV5.Test: super-block free i-node count invalid (is {0:D0}, require 0 <= n <= 100)", n);
for (Int32 i = 0; i < n; i++)
{
if (((p = SB.GetUInt16L(bp + 2 * i)) < 1) || (p > isize * INOPB)) return Debug.WriteInfo(false, "UnixV5.Test: super-block free i-number {0:D0} invalid (is {1:D0}, require 1 <= n < {2:D0})", i, p, isize * INOPB);
}
size = fsize;
type = typeof(UnixV5);
if (level == 2) return Debug.WriteInfo(true, "UnixV5.Test: file system size: {0:D0} blocks ({1:D0} i-nodes in blocks 2-{2:D0}, data in blocks {3:D0}-{4:D0}", fsize, isize * INOPB, isize + 1, isize + 2, fsize - 1);
// level 3 - check file headers (aka inodes) (return file system size and type)
Inode iNode;
for (UInt16 iNum = 1; iNum <= isize * INOPB; iNum++)
{
iNode = new InodeV5(volume, iNum);
if ((iNode.Type != ' ') && (iNode.nlinks == 0)) return Debug.WriteInfo(false, "UnixV5.Test: i-node {0:D0} is used but has zero link count", iNum);
//if ((iNode.Type == ' ') && (iNode.nlinks != 0)) return Debug.WriteInfo(false, "UnixV5.Test: i-node {0:D0} is free but has non-zero link count {1:D0}", iNum, iNode.nlinks);
if ((iNode.size < 0) || (iNode.size > 1048576)) return Debug.WriteInfo(false, "UnixV5.Test: i-node {0:D0} size invalid (is {1:D0}, require 0 <= n <= 1048576)", iNum, iNode.size);
if ((iNode.Type == ' ') && (iNode.size != 0)) return Debug.WriteInfo(false, "UnixV5.Test: i-node {0:D0} is free but has non-zero size {1:D0}", iNum, iNode.size);
if (((iNode.flags & 0x1000) == 0) && (iNode.size > 4096)) return Debug.WriteInfo(false, "UnixV5.Test: i-node {0:D0} size exceeds small file limit (is {1:D0}, require n <= 4096)", iNum, iNode.size);
if ((iNode.Type != '-') && (iNode.Type != 'd')) continue; // only check blocks for file and directory i-nodes
for (Int32 i = 0; i < 8; i++)
{
if ((p = iNode.addr[i]) == 0) continue;
if ((p < isize + 2) || (p >= fsize)) return Debug.WriteInfo(false, "UnixV5.Test: i-node {0:D0} contains invalid block pointer (is {1:D0}, require {2:D0} <= n < {3:D0})", iNum, p, isize + 2, fsize);
if (p >= volume.BlockCount) return Debug.WriteInfo(false, "UnixV5.Test: volume too small to contain i-node {0:D0} {1} block {2:D0}", iNum, ((iNode.flags & 0x1000) == 0) ? "data" : "indirect", p);
if ((iNode.flags & 0x1000) != 0)
{
Block B = volume[p];
for (Int32 j = 0; j < 256; j++)
{
if ((p = B.GetUInt16L(2 * j)) == 0) continue;
if ((p < isize + 2) || (p >= fsize)) return Debug.WriteInfo(false, "UnixV5.Test: i-node {0:D0} contains invalid block pointer (is {1:D0}, require {2:D0} <= n < {3:D0})", iNum, p, isize + 2, fsize);
if (p >= volume.BlockCount) return Debug.WriteInfo(false, "UnixV5.Test: volume too small to contain i-node {0:D0} data block {1:D0}", iNum, p);
}
}
}
}
if (level == 3) return true;
// level 4 - check directory structure (return file system size and type)
iNode = new InodeV5(volume, ROOT_INUM);
if (iNode.Type != 'd') return Debug.WriteInfo(false, "UnixV5.Test: root directory i-node mode invalid (is 0x{0:x4}, require 0xcnnn)", iNode.flags);
UInt16[] IMap = new UInt16[isize * INOPB + 1]; // inode usage map
Queue<Int32> DirList = new Queue<Int32>(); // queue of directories to examine
DirList.Enqueue((ROOT_INUM << 16) + ROOT_INUM); // parent inum in high word, directory inum in low word (root is its own parent)
while (DirList.Count != 0)
{
Int32 dNum = DirList.Dequeue();
Int32 pNum = dNum >> 16;
dNum &= 0xffff;
if (IMap[dNum] != 0) return Debug.WriteInfo(false, "UnixV5.Test: directory i-node {0:D0} appears more than once in directory structure", dNum);
IMap[dNum]++; // assume a link to this directory from its parent (root directory gets fixed later)
Byte[] buf = ReadFile(new InodeV5(volume, dNum), BLOCK_SIZE);
Boolean df = false;
Boolean ddf = false;
for (bp = 0; bp < buf.Length; bp += 16)
{
UInt16 iNum = Buffer.GetUInt16L(buf, bp);
if (iNum == 0) continue;
String name = Buffer.GetCString(buf, bp + 2, 14, Encoding.ASCII);
if (String.Compare(name, ".") == 0)
{
if (iNum != dNum) return Debug.WriteInfo(false, "UnixV5.Test: in directory i={0:D0}, entry \".\" does not refer to itself (is {1:D0}, require {2:D0})", dNum, iNum, dNum);
IMap[iNum]++;
df = true;
}
else if (String.Compare(name, "..") == 0)
{
if (iNum != pNum) return Debug.WriteInfo(false, "UnixV5.Test: in directory i={0:D0}, entry \"..\" does not refer to parent (is {1:D0}, require {2:D0})", dNum, iNum, pNum);
IMap[iNum]++;
ddf = true;
}
else if ((iNum < 2) || (iNum > isize * INOPB))
{
return Debug.WriteInfo(false, "UnixV5.Test: in directory i={0:D0}, entry \"{1}\" has invalid i-number (is {2:D0}, require 2 <= n <= {3:D0})", dNum, name, iNum, isize * INOPB);
}
else if ((iNode = new InodeV5(volume, iNum)).Type == 'd') // directory
{
DirList.Enqueue((dNum << 16) + iNum);
}
else // non-directory
{
IMap[iNum]++;
}
}
if (!df) return Debug.WriteInfo(false, "UnixV5.Test: in directory i={0:D0}, entry \".\" is missing", dNum);
if (!ddf) return Debug.WriteInfo(false, "UnixV5.Test: in directory i={0:D0}, entry \"..\" is missing", dNum);
}
IMap[ROOT_INUM]--; // root directory has no parent, so back out the assumed parent link
if (level == 4) return true;
// level 5 - check file header allocation (return file system size and type)
for (UInt16 iNum = 1; iNum <= isize * INOPB; iNum++)
{
iNode = new InodeV5(volume, iNum);
if ((iNode.Type == ' ') && (IMap[iNum] != 0)) return Debug.WriteInfo(false, "UnixV5.Test: i-node {0:D0} is free but has {1:D0} link(s)", iNum, IMap[iNum]);
if ((iNode.Type != ' ') && (IMap[iNum] == 0)) return Debug.WriteInfo(false, "UnixV5.Test: i-node {0:D0} is used but has no links", iNum);
if ((iNode.Type != ' ') && (IMap[iNum] != iNode.nlinks)) return Debug.WriteInfo(false, "UnixV5.Test: i-node {0:D0} link count mismatch (is {1:D0}, expect {2:D0})", iNum, iNode.nlinks, IMap[iNum]);
}
// also check super-block free list
bp = 206;
n = SB.GetUInt16L(ref bp);
for (Int32 i = 0; i < n; i++)
{
UInt16 iNum = SB.GetUInt16L(ref bp);
if (IMap[iNum] != 0) return Debug.WriteInfo(false, "UnixV5.Test: i-node {0:D0} is in super-block free list, but has {1:D0} link(s)", iNum, IMap[iNum]);
}
if (level == 5) return true;
// level 6 - check data block allocation (return file system size and type)
UInt16[] BMap = new UInt16[fsize]; // block usage map
for (UInt16 iNum = 1; iNum <= isize * INOPB; iNum++)
{
iNode = new InodeV5(volume, iNum);
if ((iNode.Type != '-') && (iNode.Type != 'd')) continue; // only file and directory i-nodes have blocks allocated
n = (iNode.size + 511) / 512; // number of blocks required for file
// mark data blocks used
for (Int32 i = 0; i < n; i++)
{
if ((p = iNode[i]) == 0) continue;
if (BMap[p] != 0) return Debug.WriteInfo(false, "UnixV5.Test: data block #{0:D0} ({1:D0}) of i-node {2:D0} is also allocated to i-node {3:D0}", i, p, iNum, BMap[p]);
BMap[p] = iNum;
}
if ((iNode.flags & 0x1000) == 0) continue; // small files have no indirect blocks
// mark indirect blocks used
for (Int32 i = 0; i < 8; i++)
{
if ((p = iNode.addr[i]) == 0) continue;
if (BMap[p] != 0) return Debug.WriteInfo(false, "UnixV5.Test: indirect block {0:D0} of i-node {1:D0} is also allocated to i-node {2:D0}", p, iNum, BMap[p]);
BMap[p] = iNum;
}
}
// mark used blocks with 1
for (Int32 i = 0; i < isize + 2; i++) BMap[i] = 1;
for (Int32 i = isize + 2; i < fsize; i++) if (BMap[i] != 0) BMap[i] = 1;
// mark free blocks with 2
bp = 4;
n = SB.GetUInt16L(ref bp); // number of blocks in super-block free block list
if ((p = SB.GetUInt16L(ref bp)) != 0) // free block chain next pointer
{
if (BMap[p] != 0) return Debug.WriteInfo(false, "UnixV5.Test: list block {0:D0} in super-block free list is allocated", p);
BMap[p] = 2;
}
for (Int32 i = 1; i < n; i++)
{
Int32 q = SB.GetUInt16L(ref bp);
if (BMap[q] != 0) return Debug.WriteInfo(false, "UnixV5.Test: data block {0:D0} in super-block free list is allocated", q);
BMap[q] = 2;
}
// enumerate blocks in free block chain
while (p != 0)
{
Block B = volume[p];
bp = 0;
n = B.GetUInt16L(ref bp); // number of entries listed in this block
if ((p = B.GetUInt16L(ref bp)) != 0)
{
if (BMap[p] != 0) return Debug.WriteInfo(false, "UnixV5.Test: list block {0:D0} in free block chain is allocated", p);
BMap[p] = 2;
}
for (Int32 i = 1; i < n; i++)
{
Int32 q = B.GetUInt16L(ref bp);
if ((q < isize + 2) || (q >= fsize)) return Debug.WriteInfo(false, "UnixV5.Test: data block {0:D0} in free block chain falls outside volume block range (require {1:D0} <= n < {2:D0})", q, isize + 2, fsize);
if (BMap[q] != 0) return Debug.WriteInfo(false, "UnixV5.Test: data block {0:D0} in free block chain is allocated", q);
BMap[q] = 2;
}
}
// unmarked blocks are lost -- not allocated and not in free list
for (Int32 i = 0; i < fsize; i++)
{
if (BMap[i] == 0) return Debug.WriteInfo(false, "UnixV5.Test: block {0:D0} is not allocated and not in free list", i);
}
if (level == 6) return true;
return false;
}
}
partial class UnixV6 : UnixV5
{
protected class InodeV6 : InodeV5
{
public InodeV6(Volume volume, Int32 iNumber) : base(volume, iNumber)
{
}
public override Int32 this[Int32 blockNum]
{
get
{
if ((flags & 0x1000) == 0) return addr[blockNum];
Int32 n = blockNum / 256;
if (n < 7) n = addr[n];
else if (addr[7] == 0) return 0;
else n = mVol[addr[7]].GetUInt16L(2 * (n - 7));
return mVol[n].GetUInt16L(2 * (blockNum % 256));
}
}
}
public UnixV6(Volume volume)
{
mVol = volume;
mType = "Unix/V6";
mDirNode = new InodeV6(volume, ROOT_INUM);
mDir = "/";
}
protected override Inode GetInode(Int32 iNumber)
{
return new InodeV6(mVol, iNumber);
}
}
partial class UnixV6 : IFileSystemAuto
{
public static new TestDelegate GetTest()
{
return UnixV6.Test;
}
// level 0 - check basic volume parameters (return required block size and volume type)
// level 1 - check boot block (return volume size and type)
// level 2 - check volume descriptor (aka home/super block) (return file system size and type)
// level 3 - check file headers (aka inodes) (return file system size and type)
// level 4 - check directory structure (return file system size and type)
// level 5 - check file header allocation (return file system size and type)
// level 6 - check data block allocation (return file system size and type)
public static new Boolean Test(Volume volume, Int32 level, out Int32 size, out Type type)
{
Int32 BLOCK_SIZE = 512;
Int32 ROOT_INUM = 1;
Int32 INOPB = 16;
// level 0 - check basic volume parameters (return required block size and volume type)
size = BLOCK_SIZE;
type = typeof(Volume);
if (volume == null) return false;
if (volume.BlockSize != size) return Debug.WriteInfo(false, "UnixV6.Test: invalid block size (is {0:D0}, require {1:D0})", volume.BlockSize, size);
if (level == 0) return true;
// level 1 - check boot block (return volume size and type)
size = -1; // Unix V6 doesn't support disk labels
if (volume.BlockCount < 1) return Debug.WriteInfo(false, "UnixV6.Test: volume too small to contain boot block");
if (level == 1) return true;
// level 2 - check volume descriptor (aka home/super block) (return file system size and type)
type = null;
if (volume.BlockCount < 2) return Debug.WriteInfo(false, "UnixV6.Test: volume too small to contain super-block");
Block SB = volume[1]; // super-block
Int32 bp = 0;
Int32 isize = SB.GetUInt16L(ref bp); // SB[0] - number of blocks used for inodes
if (volume.BlockCount < isize + 2) return Debug.WriteInfo(false, "UnixV6.Test: volume too small to contain i-node list (blocks {0:D0}-{1:D0} > {2:D0})", 2, isize + 1, volume.BlockCount - 1);
Int32 fsize = SB.GetUInt16L(ref bp); // SB[2] - file system size (in blocks)
if (isize + 2 > fsize) return Debug.WriteInfo(false, "UnixV6.Test: super-block i-list exceeds file system size ({0:D0} > {1:D0})", isize + 2, fsize);
Int32 n = SB.GetUInt16L(ref bp); // SB[4] - number of blocks in super-block free block list
if (n > 100) return Debug.WriteInfo(false, "UnixV6.Test: super-block free block count invalid (is {0:D0}, require 0 <= n <= 100)", n);
Int32 p = SB.GetUInt16L(bp); // SB[6] - free block chain next pointer
if ((p != 0) && ((p < isize + 2) || (p >= fsize))) return Debug.WriteInfo(false, "UnixV6.Test: super-block free chain pointer invalid (is {0:D0}, require {1:D0} <= n < {2:D0})", p, isize + 2, fsize);
for (Int32 i = 1; i < n; i++) // SB[8] - free block list
{
if (((p = SB.GetUInt16L(bp + 2 * i)) < isize + 2) || (p >= fsize)) return Debug.WriteInfo(false, "UnixV6.Test: super-block free block {0:D0} invalid (is {1:D0}, require {2:D0} <= n < {3:D0})", i, p, isize + 2, fsize);
}
bp += 100 * 2;
n = SB.GetUInt16L(ref bp); // SB[206] - number of inodes in super-block free inode list
if (n > 100) return Debug.WriteInfo(false, "UnixV6.Test: super-block free i-node count invalid (is {0:D0}, require 0 <= n <= 100)", n);
for (Int32 i = 0; i < n; i++)
{
if (((p = SB.GetUInt16L(bp + 2 * i)) < 1) || (p > isize * INOPB)) return Debug.WriteInfo(false, "UnixV6.Test: super-block free i-number {0:D0} invalid (is {1:D0}, require 1 <= n < {2:D0})", i, p, isize * INOPB);
}
size = fsize;
type = typeof(UnixV6);
if (level == 2) return Debug.WriteInfo(true, "UnixV6.Test: file system size: {0:D0} blocks ({1:D0} i-nodes in blocks 2-{2:D0}, data in blocks {3:D0}-{4:D0}", fsize, isize * INOPB, isize + 1, isize + 2, fsize - 1);
// level 3 - check file headers (aka inodes) (return file system size and type)
Inode iNode;
for (UInt16 iNum = 1; iNum <= isize * INOPB; iNum++)
{
iNode = new InodeV6(volume, iNum);
if ((iNode.Type != ' ') && (iNode.nlinks == 0)) return Debug.WriteInfo(false, "UnixV6.Test: i-node {0:D0} is used but has zero link count", iNum);
//if ((iNode.Type == ' ') && (iNode.nlinks != 0)) return Debug.WriteInfo(false, "UnixV6.Test: i-node {0:D0} is free but has non-zero link count {1:D0}", iNum, iNode.nlinks);
// since iNode.size is a 24-bit number, the below test can't actually ever fail
if ((iNode.size < 0) || (iNode.size > 16777215)) return Debug.WriteInfo(false, "UnixV6.Test: i-node {0:D0} size invalid (is {1:D0}, require 0 <= n <= 16777215)", iNum, iNode.size);
if ((iNode.Type == ' ') && (iNode.size != 0)) return Debug.WriteInfo(false, "UnixV6.Test: i-node {0:D0} is free but has non-zero size {1:D0}", iNum, iNode.size);
if (((iNode.flags & 0x1000) == 0) && (iNode.size > 4096)) return Debug.WriteInfo(false, "UnixV6.Test: i-node {0:D0} size exceeds small file limit (is {1:D0}, require n <= 4096)", iNum, iNode.size);
if ((iNode.Type != '-') && (iNode.Type != 'd')) continue; // only check blocks for file and directory i-nodes
for (Int32 i = 0; i < 8; i++)
{
if ((p = iNode.addr[i]) == 0) continue;
if ((p < isize + 2) || (p >= fsize)) return Debug.WriteInfo(false, "UnixV6.Test: i-node {0:D0} contains invalid block pointer (is {1:D0}, require {2:D0} <= n < {3:D0})", iNum, p, isize + 2, fsize);
if (p >= volume.BlockCount) return Debug.WriteInfo(false, "UnixV6.Test: volume too small to contain i-node {0:D0} {1} block {2:D0}", iNum, ((iNode.flags & 0x1000) == 0) ? "data" : "indirect", p);
if ((iNode.flags & 0x1000) != 0)
{
Block B = volume[p];
for (Int32 j = 0; j < 256; j++)
{
if ((p = B.GetUInt16L(2 * j)) == 0) continue;
if ((p < isize + 2) || (p >= fsize)) return Debug.WriteInfo(false, "UnixV6.Test: i-node {0:D0} contains invalid block pointer (is {1:D0}, require {2:D0} <= n < {3:D0})", iNum, p, isize + 2, fsize);
if (p >= volume.BlockCount) return Debug.WriteInfo(false, "UnixV6.Test: volume too small to contain i-node {0:D0} {1} block {2:D0}", iNum, (i < 7) ? "data" : "indirect", p);
if (i == 7)
{
Block B2 = volume[p];
for (Int32 k = 0; k < 256; k++)
{
if ((p = B2.GetUInt16L(2 * k)) == 0) continue;
if ((p < isize + 2) || (p >= fsize)) return Debug.WriteInfo(false, "UnixV6.Test: i-node {0:D0} contains invalid block pointer (is {1:D0}, require {2:D0} <= n < {3:D0})", iNum, p, isize + 2, fsize);
if (p >= volume.BlockCount) return Debug.WriteInfo(false, "UnixV6.Test: volume too small to contain i-node {0:D0} data block {1:D0}", iNum, p);
}
}
}
}
}
}
if (level == 3) return true;
// level 4 - check directory structure (return file system size and type)
iNode = new InodeV6(volume, ROOT_INUM);
if (iNode.Type != 'd') return Debug.WriteInfo(false, "UnixV6.Test: root directory i-node mode invalid (is 0x{0:x4}, require 0xcnnn)", iNode.flags);
UInt16[] IMap = new UInt16[isize * INOPB + 1]; // inode usage map
Queue<Int32> DirList = new Queue<Int32>(); // queue of directories to examine
DirList.Enqueue((ROOT_INUM << 16) + ROOT_INUM); // parent inum in high word, directory inum in low word (root is its own parent)
while (DirList.Count != 0)
{
Int32 dNum = DirList.Dequeue();
Int32 pNum = dNum >> 16;
dNum &= 0xffff;
if (IMap[dNum] != 0) return Debug.WriteInfo(false, "UnixV6.Test: directory i-node {0:D0} appears more than once in directory structure", dNum);
IMap[dNum]++; // assume a link to this directory from its parent (root directory gets fixed later)
Byte[] buf = ReadFile(new InodeV6(volume, dNum), BLOCK_SIZE);
Boolean df = false;
Boolean ddf = false;
for (bp = 0; bp < buf.Length; bp += 16)
{
UInt16 iNum = Buffer.GetUInt16L(buf, bp);
if (iNum == 0) continue;
String name = Buffer.GetCString(buf, bp + 2, 14, Encoding.ASCII);
if (String.Compare(name, ".") == 0)
{
if (iNum != dNum) return Debug.WriteInfo(false, "UnixV6.Test: in directory i={0:D0}, entry \".\" does not refer to itself (is {1:D0}, require {2:D0})", dNum, iNum, dNum);
IMap[iNum]++;
df = true;
}
else if (String.Compare(name, "..") == 0)
{
if (iNum != pNum) return Debug.WriteInfo(false, "UnixV6.Test: in directory i={0:D0}, entry \"..\" does not refer to parent (is {1:D0}, require {2:D0})", dNum, iNum, pNum);
IMap[iNum]++;
ddf = true;
}
else if ((iNum < 2) || (iNum > isize * INOPB))
{
return Debug.WriteInfo(false, "UnixV6.Test: in directory i={0:D0}, entry \"{1}\" has invalid i-number (is {2:D0}, require 2 <= n <= {3:D0})", dNum, name, iNum, isize * INOPB);
}
else if ((iNode = new InodeV6(volume, iNum)).Type == 'd') // directory
{
DirList.Enqueue((dNum << 16) + iNum);
}
else // non-directory
{
IMap[iNum]++;
}
}
if (!df) return Debug.WriteInfo(false, "UnixV6.Test: in directory i={0:D0}, entry \".\" is missing", dNum);
if (!ddf) return Debug.WriteInfo(false, "UnixV6.Test: in directory i={0:D0}, entry \"..\" is missing", dNum);
}
IMap[ROOT_INUM]--; // root directory has no parent, so back out the assumed parent link
if (level == 4) return true;
// level 5 - check file header allocation (return file system size and type)
for (UInt16 iNum = 1; iNum <= isize * INOPB; iNum++)
{
iNode = new InodeV6(volume, iNum);
if ((iNode.Type == ' ') && (IMap[iNum] != 0)) return Debug.WriteInfo(false, "UnixV6.Test: i-node {0:D0} is free but has {1:D0} link(s)", iNum, IMap[iNum]);
if ((iNode.Type != ' ') && (IMap[iNum] == 0)) return Debug.WriteInfo(false, "UnixV6.Test: i-node {0:D0} is used but has no links", iNum);
if ((iNode.Type != ' ') && (IMap[iNum] != iNode.nlinks)) return Debug.WriteInfo(false, "UnixV6.Test: i-node {0:D0} link count mismatch (is {1:D0}, expect {2:D0})", iNum, iNode.nlinks, IMap[iNum]);
}
// also check super-block free list
bp = 206;
n = SB.GetUInt16L(ref bp);
for (Int32 i = 0; i < n; i++)
{
UInt16 iNum = SB.GetUInt16L(ref bp);
if (IMap[iNum] != 0) return Debug.WriteInfo(false, "UnixV6.Test: i-node {0:D0} is in super-block free list, but has {1:D0} link(s)", iNum, IMap[iNum]);
}
if (level == 5) return true;
// level 6 - check data block allocation (return file system size and type)
UInt16[] BMap = new UInt16[fsize]; // block usage map
for (UInt16 iNum = 1; iNum <= isize * INOPB; iNum++)
{
iNode = new InodeV6(volume, iNum);
if ((iNode.Type != '-') && (iNode.Type != 'd')) continue; // only file and directory i-nodes have blocks allocated
n = (iNode.size + 511) / 512; // number of blocks required for file
// mark data blocks used
for (Int32 i = 0; i < n; i++)
{
if ((p = iNode[i]) == 0) continue;
if (BMap[p] != 0) return Debug.WriteInfo(false, "UnixV6.Test: data block #{0:D0} ({1:D0}) of i-node {2:D0} is also allocated to i-node {3:D0}", i, p, iNum, BMap[p]);
BMap[p] = iNum;
}
if ((iNode.flags & 0x1000) == 0) continue; // small files have no indirect blocks
// mark indirect blocks used
for (Int32 i = 0; i < 8; i++)
{
if ((p = iNode.addr[i]) == 0) continue;
if (BMap[p] != 0) return Debug.WriteInfo(false, "UnixV6.Test: indirect block {0:D0} of i-node {1:D0} is also allocated to i-node {2:D0}", p, iNum, BMap[p]);
BMap[p] = iNum;
if (i == 7)
{
// mark double-indirect blocks used
Block B = volume[p];
for (Int32 j = 0; j < 256; j++)
{
if ((p = B.GetUInt16L(2 * j)) == 0) continue;
if (BMap[p] != 0) return Debug.WriteInfo(false, "UnixV6.Test: indirect block {0:D0} of i-node {1:D0} is also allocated to i-node {2:D0}", p, iNum, BMap[p]);
BMap[p] = iNum;
}
}
}
}
// mark used blocks with 1
for (Int32 i = 0; i < isize + 2; i++) BMap[i] = 1;
for (Int32 i = isize + 2; i < fsize; i++) if (BMap[i] != 0) BMap[i] = 1;
// mark free blocks with 2
bp = 4;
n = SB.GetUInt16L(ref bp); // number of blocks in super-block free block list
if ((p = SB.GetUInt16L(ref bp)) != 0) // free block chain next pointer
{
if (BMap[p] != 0) return Debug.WriteInfo(false, "UnixV6.Test: list block {0:D0} in super-block free list is allocated", p);
BMap[p] = 2;
}
for (Int32 i = 1; i < n; i++)
{
Int32 q = SB.GetUInt16L(ref bp);
if (BMap[q] != 0) return Debug.WriteInfo(false, "UnixV6.Test: data block {0:D0} in super-block free list is allocated", q);
BMap[q] = 2;
}
// enumerate blocks in free block chain
while (p != 0)
{
Block B = volume[p];
bp = 0;
n = B.GetUInt16L(ref bp); // number of entries listed in this block
if ((p = B.GetUInt16L(ref bp)) != 0)
{
if (BMap[p] != 0) return Debug.WriteInfo(false, "UnixV6.Test: list block {0:D0} in free block chain is allocated", p);
BMap[p] = 2;