-
Notifications
You must be signed in to change notification settings - Fork 6
/
test_fsck.py
1332 lines (1139 loc) · 34.8 KB
/
test_fsck.py
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
# coding=utf-8
#
# Run a variety of tests against fsck_msdos
#
# Usage:
# python test_fsck.py [<fsck_msdos> [<tmp_dir>]]
#
# where <tmp_dir> is a path to a directory where disk images will be
# temporarily created. If <path_to_fsck> is specified, it is used instead
# of 'fsck_msdos' to invoke the fsck_msdos program (for example, to test
# a new build that has not been installed).
#
from __future__ import with_statement
import sys
import os
import subprocess
import struct
from msdosfs import *
from HexDump import HexDump
class LaunchError(Exception):
def __init__(self, returncode):
self.returncode = returncode
if returncode < 0:
self.message = "Program exited with signal %d" % -returncode
else:
self.message = "Program exited with status %d" % returncode
def __str__(self):
return self.message
class FailureExpected(Exception):
def __init__(self, s):
self.s = s
def __str__(self):
return self.s
class RepairFailed(Exception):
def __init__(self, s):
self.s = s
def __str__(self):
return "RepairFailed({0})".format(self.s)
#
# launch -- A helper to run another process and collect the standard output
# and standard error streams. If the process returns a non-zero exit
# status, then raise an exception.
#
def launch(args, **kwargs):
print "launch:", args, kwargs
p = subprocess.Popen(args, **kwargs)
stdout, stderr = p.communicate()
if p.returncode != 0:
raise LaunchError(p.returncode)
return stdout, stderr
#
# 1. Make a disk image file
# 2. Attach the image file, without mounting
# ---- Begin per-test stuff ----
# 3. newfs_msdos the image
# 4. Fill image with content
# 5. fsck_msdos -n the image
# 6. fsck_msdos -y the image
# 7. Run /sbin/fsck_msdos against image
# ---- End per-test stuff ----
# 8. Detach the image
# 9. Delete the image file
#
#
# Run tests on 20GiB FAT32 sparse disk image
#
def test_fat32(dir, fsck, newfs):
#
# Create a 20GB disk image in @dir
#
dmg = os.path.join(dir, 'Test20GB.sparseimage')
launch('hdiutil create -size 20g -type SPARSE -layout NONE'.split()+[dmg])
newfs_opts = "-F 32 -b 4096 -v TEST20GB".split()
#
# Attach the image
#
disk = launch(['hdiutil', 'attach', '-nomount', dmg], stdout=subprocess.PIPE)[0].rstrip()
rdisk = disk.replace('/dev/disk', '/dev/rdisk')
#
# Run tests
#
# TODO: Known good disk
# empty file
# one cluster file
# larger file
# one cluster directory
# larger directory
#
test_quick(rdisk, fsck, newfs, newfs_opts)
test_bad_args(rdisk, fsck, newfs, newfs_opts)
test_maxmem(rdisk, fsck, newfs, newfs_opts)
test_empty(rdisk, fsck, newfs, newfs_opts)
test_boot_sector(rdisk, fsck, newfs, newfs_opts)
test_boot_fat32(rdisk, fsck, newfs, newfs_opts) # FAT32 only!
test_fsinfo(rdisk, fsck, newfs, newfs_opts) # FAT32 only!
fat_too_small(rdisk, fsck, newfs, newfs_opts)
orphan_clusters(rdisk, fsck, newfs, newfs_opts)
file_excess_clusters(rdisk, fsck, newfs, newfs_opts)
file_bad_clusters(rdisk, fsck, newfs, newfs_opts)
dir_bad_start(rdisk, fsck, newfs, newfs_opts)
root_bad_start(rdisk, fsck, newfs, newfs_opts) # FAT32 only!
root_bad_first_cluster(rdisk, fsck, newfs, newfs_opts) # FAT32 only!
dir_size_dots(rdisk, fsck, newfs, newfs_opts)
long_name(rdisk, fsck, newfs, newfs_opts)
past_end_of_dir(rdisk, fsck, newfs, newfs_opts)
past_end_of_dir(rdisk, fsck, newfs, newfs_opts, True)
fat_bad_0_or_1(rdisk, fsck, newfs, newfs_opts)
fat_mark_clean_corrupt(rdisk, fsck, newfs, newfs_opts)
fat_mark_clean_ok(rdisk, fsck, newfs, newfs_opts)
file_4GB(rdisk, fsck, newfs, newfs_opts)
file_4GB_excess_clusters(rdisk, fsck, newfs, newfs_opts)
directory_garbage(rdisk, fsck, newfs, newfs_opts)
#
# Detach the image
#
launch(['diskutil', 'eject', disk])
#
# Delete the image file
#
os.remove(dmg)
#
# Run tests on 160MiB FAT16 image
#
def test_fat16(dir, fsck, newfs):
#
# Create a 160MB disk image in @dir
#
dmg = os.path.join(dir, 'Test160MB.dmg')
f = file(dmg, "w")
f.truncate(160*1024*1024)
f.close()
newfs_opts = "-F 16 -b 4096 -v TEST160MB".split()
#
# Attach the image
#
disk = launch(['hdiutil', 'attach', '-nomount', dmg], stdout=subprocess.PIPE)[0].rstrip()
rdisk = disk.replace('/dev/disk', '/dev/rdisk')
#
# Run tests
#
# TODO: Known good disk
# empty file
# one cluster file
# larger file
# one cluster directory
# larger directory
#
test_quick(rdisk, fsck, newfs, newfs_opts)
test_bad_args(rdisk, fsck, newfs, newfs_opts)
test_maxmem(rdisk, fsck, newfs, newfs_opts)
test_empty(rdisk, fsck, newfs, newfs_opts)
test_boot_sector(rdisk, fsck, newfs, newfs_opts)
fat_too_small(rdisk, fsck, newfs, newfs_opts)
orphan_clusters(rdisk, fsck, newfs, newfs_opts)
file_excess_clusters(rdisk, fsck, newfs, newfs_opts)
file_bad_clusters(rdisk, fsck, newfs, newfs_opts)
dir_bad_start(rdisk, fsck, newfs, newfs_opts)
dir_size_dots(rdisk, fsck, newfs, newfs_opts)
long_name(rdisk, fsck, newfs, newfs_opts)
past_end_of_dir(rdisk, fsck, newfs, newfs_opts)
past_end_of_dir(rdisk, fsck, newfs, newfs_opts, True)
fat_bad_0_or_1(rdisk, fsck, newfs, newfs_opts)
fat_mark_clean_corrupt(rdisk, fsck, newfs, newfs_opts)
fat_mark_clean_ok(rdisk, fsck, newfs, newfs_opts)
directory_garbage(rdisk, fsck, newfs, newfs_opts)
#
# Detach the image
#
launch(['diskutil', 'eject', disk])
#
# Delete the image file
#
os.remove(dmg)
#
# Run tests on 15MiB FAT12 image
#
def test_fat12(dir, fsck, newfs):
#
# Create a 15MB disk image in @dir
#
dmg = os.path.join(dir, 'Test15MB.dmg')
f = file(dmg, "w")
f.truncate(15*1024*1024)
f.close()
newfs_opts = "-F 12 -b 4096 -v TEST15MB".split()
#
# Attach the image
#
disk = launch(['hdiutil', 'attach', '-nomount', dmg], stdout=subprocess.PIPE)[0].rstrip()
rdisk = disk.replace('/dev/disk', '/dev/rdisk')
#
# Run tests
#
# TODO: Known good disk
# empty file
# one cluster file
# larger file
# one cluster directory
# larger directory
#
test_quick(rdisk, fsck, newfs, newfs_opts)
test_bad_args(rdisk, fsck, newfs, newfs_opts)
test_maxmem(rdisk, fsck, newfs, newfs_opts)
test_empty(rdisk, fsck, newfs, newfs_opts)
test_boot_sector(rdisk, fsck, newfs, newfs_opts)
fat_too_small(rdisk, fsck, newfs, newfs_opts)
orphan_clusters(rdisk, fsck, newfs, newfs_opts)
file_excess_clusters(rdisk, fsck, newfs, newfs_opts)
file_bad_clusters(rdisk, fsck, newfs, newfs_opts)
dir_bad_start(rdisk, fsck, newfs, newfs_opts)
dir_size_dots(rdisk, fsck, newfs, newfs_opts)
long_name(rdisk, fsck, newfs, newfs_opts)
past_end_of_dir(rdisk, fsck, newfs, newfs_opts)
past_end_of_dir(rdisk, fsck, newfs, newfs_opts, True)
fat_bad_0_or_1(rdisk, fsck, newfs, newfs_opts)
directory_garbage(rdisk, fsck, newfs, newfs_opts)
#
# Detach the image
#
launch(['diskutil', 'eject', disk])
#
# Delete the image file
#
os.remove(dmg)
#
# Run tests on 100MiB FAT12 image
#
def test_fat12_100MB(dir, fsck, newfs):
#
# Create a 100MB disk image in @dir
#
dmg = os.path.join(dir, 'Test100MB.dmg')
f = file(dmg, "w")
f.truncate(100*1024*1024)
f.close()
newfs_opts = "-F 12 -b 32768 -v TEST100MB".split()
#
# Attach the image
#
disk = launch(['hdiutil', 'attach', '-nomount', dmg], stdout=subprocess.PIPE)[0].rstrip()
rdisk = disk.replace('/dev/disk', '/dev/rdisk')
#
# Run tests
#
test_18523205(rdisk, fsck, newfs, newfs_opts)
#
# Detach the image
#
launch(['diskutil', 'eject', disk])
#
# Delete the image file
#
os.remove(dmg)
#
# A minimal test -- make sure fsck_msdos runs on an empty image
#
def test_empty(disk, fsck, newfs, newfs_opts):
#
# newfs the disk
#
launch([newfs]+newfs_opts+[disk])
#
# fsck the disk
#
launch([fsck, '-n', disk])
#
# Make a volume with allocated but unreferenced cluster chains
#
def orphan_clusters(disk, fsck, newfs, newfs_opts):
launch([newfs]+newfs_opts+[disk])
#
# Create some cluster chains not referenced by any file or directory
#
f = file(disk, "r+")
v = msdosfs(f)
v.allocate(7, 100)
v.allocate(23, 150)
v.allocate(1, 190)
v.flush()
del v
f.close()
del f
try:
launch([fsck, '-n', disk])
except LaunchError:
pass
launch([fsck, '-p', disk])
launch(['/sbin/fsck_msdos', '-n', disk])
#
# Make a file with excess clusters allocated
# One file with EOF == 0
# One file with EOF != 0
# Files with excess clusters that are cross-linked
# First excess cluster is cross-linked
# Other excess cluster is cross-linked
# Excess clusters end with free/bad/reserved cluster
# First excess cluster is free/bad/reserved
# Other excess cluster is free/bad/reserved
#
def file_excess_clusters(disk, fsck, newfs, newfs_opts):
launch([newfs]+newfs_opts+[disk])
#
# Create files with too many clusters for their size
#
f = file(disk, "r+")
v = msdosfs(f)
head=v.allocate(7)
v.root().mkfile('FOO', head=head, length=6*v.bytesPerCluster)
head=v.allocate(1)
v.root().mkfile('BAR', head=head, length=0)
#
# LINK1 is OK.
# LINK2 contains excess clusters; the first is cross-linked with LINK1
# LINK3 contains excess clusters; the second is cross-linked with LINK1
#
clusters = v.fat.find(9)
head = v.fat.chain(clusters)
v.root().mkfile('LINK1', head=head, length=8*v.bytesPerCluster+1)
head = v.fat.allocate(3, last=clusters[7])
v.root().mkfile('LINK2', head=head, length=2*v.bytesPerCluster+3)
head = v.fat.allocate(5, last=clusters[8])
v.root().mkfile('LINK3', head=head, length=3*v.bytesPerCluster+5)
if v.fsinfo:
v.fsinfo.allocate(9+3+5)
#
# FREE1 has its first excess cluster marked free
# BAD3 has its third excess cluster marked bad
#
head = v.allocate(11, last=CLUST_BAD)
v.root().mkfile('BAD3', head=head, length=8*v.bytesPerCluster+300)
head = v.allocate(8, last=CLUST_FREE)
v.root().mkfile('FREE1', head=head, length=6*v.bytesPerCluster+100)
v.flush()
del v
f.close()
del f
try:
launch([fsck, '-n', disk])
except LaunchError:
pass
launch([fsck, '-y', disk])
launch(['/sbin/fsck_msdos', '-n', disk])
#
# Make files with bad clusters in their chains
# FILE1 file with middle cluster free
# FILE2 file with middle cluster bad/reserved
# FILE3 file with middle cluster points to out of range cluster
# FILE4 file with middle cluster that is cross-linked (to same file)
# FILE5 file whose head is "free"
# FILE6 file whose head is "bad"
# FILE7 file whose head is out of range
# FILE8 file whose head is cross-linked
#
def file_bad_clusters(disk, fsck, newfs, newfs_opts):
launch([newfs]+newfs_opts+[disk])
f = file(disk, "r+")
v = msdosfs(f)
clusters = v.fat.find(5)
to_free = clusters[2]
head = v.fat.chain(clusters)
v.root().mkfile('FILE1', head=head, length=6*v.bytesPerCluster+111)
if v.fsinfo:
v.fsinfo.allocate(5)
clusters = v.fat.find(5)
head = v.fat.chain(clusters)
v.root().mkfile('FILE2', head=head, length=4*v.bytesPerCluster+222)
v.fat[clusters[2]] = CLUST_RSRVD
if v.fsinfo:
v.fsinfo.allocate(5)
clusters = v.fat.find(5)
head = v.fat.chain(clusters)
v.root().mkfile('FILE3', head=head, length=4*v.bytesPerCluster+333)
v.fat[clusters[2]] = 1
if v.fsinfo:
v.fsinfo.allocate(5)
clusters = v.fat.find(5)
head = v.fat.chain(clusters)
v.root().mkfile('FILE4', head=head, length=4*v.bytesPerCluster+44)
v.fat[clusters[2]] = clusters[1]
if v.fsinfo:
v.fsinfo.allocate(5)
v.root().mkfile('FILE5', head=CLUST_FREE, length=4*v.bytesPerCluster+55)
v.root().mkfile('FILE6', head=CLUST_BAD, length=4*v.bytesPerCluster+66)
v.root().mkfile('FILE7', head=CLUST_RSRVD-1, length=4*v.bytesPerCluster+77)
head = v.allocate(5)
v.root().mkfile('FOO', head=head, length=4*v.bytesPerCluster+99)
v.root().mkfile('FILE8', head=head, length=4*v.bytesPerCluster+88)
# Free the middle cluster of FILE1 now that we've finished allocating
v.fat[to_free] = CLUST_FREE
v.flush()
del v
f.close()
del f
try:
launch([fsck, '-n', disk])
except LaunchError:
pass
launch([fsck, '-y', disk])
launch(['/sbin/fsck_msdos', '-n', disk])
#
# Make directories whose starting cluster number is free/bad/reserved/out of range
# DIR1 start cluster is free
# DIR2 start cluster is reserved
# DIR3 start cluster is bad
# DIR4 start cluster is EOF
# DIR5 start cluster is 1
# DIR6 start cluster is one more than max valid cluster
#
def dir_bad_start(disk, fsck, newfs, newfs_opts):
def mkdir(parent, name, head):
bytes = make_long_dirent(name, ATTR_DIRECTORY, head=head)
slots = len(bytes)/32
slot = parent.find_slots(slots, grow=True)
parent.write_slots(slot, bytes)
launch([newfs]+newfs_opts+[disk])
f = file(disk, "r+")
v = msdosfs(f)
root = v.root()
mkdir(root, 'DIR1', CLUST_FREE)
mkdir(root, 'DIR2', CLUST_RSRVD)
mkdir(root, 'DIR3', CLUST_BAD)
mkdir(root, 'DIR4', CLUST_EOF)
mkdir(root, 'DIR5', 1)
mkdir(root, 'DIR6', v.clusters+2)
v.flush()
del v
f.close()
del f
try:
launch([fsck, '-n', disk])
except LaunchError:
pass
launch([fsck, '-y', disk])
launch(['/sbin/fsck_msdos', '-n', disk])
#
# Root dir's starting cluster number is free/bad/reserved/out of range
#
# NOTE: This test is only applicable to FAT32!
#
def root_bad_start(disk, fsck, newfs, newfs_opts):
def set_root_start(disk, head):
dev = file(disk, "r+")
dev.seek(0)
bytes = dev.read(512)
bytes = bytes[0:44] + struct.pack("<I", head) + bytes[48:]
dev.seek(0)
dev.write(bytes)
dev.close()
del dev
launch([newfs]+newfs_opts+[disk])
f = file(disk, "r+")
v = msdosfs(f)
clusters = v.clusters
v.flush()
del v
f.close()
del f
for head in [CLUST_FREE, CLUST_RSRVD, CLUST_BAD, CLUST_EOF, 1, clusters+2]:
set_root_start(disk, head)
try:
launch([fsck, '-n', disk])
except LaunchError:
pass
try:
launch([fsck, '-y', disk])
except LaunchError:
pass
try:
launch(['/sbin/fsck_msdos', '-n', disk])
except LaunchError:
pass
#
# Root dir's first cluster is free/bad/reserved
#
# NOTE: This test is only applicable to FAT32!
#
def root_bad_first_cluster(disk, fsck, newfs, newfs_opts):
for link in [CLUST_FREE, CLUST_RSRVD, CLUST_BAD]:
launch([newfs]+newfs_opts+[disk])
f = file(disk, "r+")
v = msdosfs(f)
v.fat[v.rootCluster] = link
v.flush()
del v
f.close()
del f
try:
launch([fsck, '-n', disk])
except LaunchError:
pass
launch([fsck, '-y', disk])
launch(['/sbin/fsck_msdos', '-n', disk])
#
# Create subdirectories with the following problems:
# Size (length) field is non-zero
# "." entry has wrong starting cluster
# ".." entry start cluster is non-zero, and parent is root
# ".." entry start cluster is zero, and parent is not root
# ".." entry start cluster is incorrect
#
def dir_size_dots(disk, fsck, newfs, newfs_opts):
launch([newfs]+newfs_opts+[disk])
f = file(disk, "r+")
v = msdosfs(f)
root = v.root()
# Make a couple of directories without any problems
child = root.mkdir('CHILD')
grand = child.mkdir('GRAND')
# Directory has non-zero size
dir = root.mkdir('BADSIZE', length=666)
# "." entry has incorrect start cluster
dir = root.mkdir('BADDOT')
fields = parse_dirent(dir.read_slots(0))
fields['head'] = fields['head'] + 30
dir.write_slots(0, make_dirent(**fields))
# ".." entry has non-zero start cluster, but parent is root
dir = root.mkdir('DOTDOT.NZ')
fields = parse_dirent(dir.read_slots(0))
fields['head'] = 47
dir.write_slots(0, make_dirent(**fields))
# ".." entry has zero start cluster, but parent is not root
dir = child.mkdir('DOTDOT.ZER')
fields = parse_dirent(dir.read_slots(0))
fields['head'] = 0
dir.write_slots(0, make_dirent(**fields))
# ".." entry start cluster is incorrect (parent is not root)
dir = grand.mkdir('DOTDOT.BAD')
fields = parse_dirent(dir.read_slots(0))
fields['head'] = fields['head'] + 30
dir.write_slots(0, make_dirent(**fields))
v.flush()
del v
f.close()
del f
try:
launch([fsck, '-n', disk])
except LaunchError:
pass
launch([fsck, '-y', disk])
launch(['/sbin/fsck_msdos', '-n', disk])
def long_name(disk, fsck, newfs, newfs_opts):
launch([newfs]+newfs_opts+[disk])
f = file(disk, "r+")
v = msdosfs(f)
root = v.root()
# Long name entries (valid or not!) preceding volume label
bytes = make_long_dirent('Test1GB', ATTR_VOLUME_ID)
root.write_slots(0, bytes)
# Create a file with a known good long name
root.mkfile('The quick brown fox jumped over the lazy dog')
# Create a file with a known good short name
root.mkfile('foo.bar')
# Create a file with invalid long name entries (bad checksums)
bytes = make_long_dirent('Greetings and felicitations my friends', ATTR_ARCHIVE)
bytes = bytes[0:-32] + 'HELLO ' + bytes[-21:]
assert len(bytes) % 32 == 0
slots = len(bytes) / 32
slot = root.find_slots(slots)
root.write_slots(slot, bytes)
subdir = root.mkdir('SubDir')
# Create a file with incomplete long name entries
# Missing first (LONG_NAME_LAST) entry
bytes = make_long_dirent('To be or not to be', ATTR_ARCHIVE)[32:]
slots = len(bytes) / 32
slot = subdir.find_slots(slots)
subdir.write_slots(slot, bytes)
# Missing middle (second) long entry
bytes = make_long_dirent('A Man a Plan a Canal Panama', ATTR_ARCHIVE)
bytes = bytes[:32] + bytes[64:]
slots = len(bytes) / 32
slot = subdir.find_slots(slots)
subdir.write_slots(slot, bytes)
# Missing last long entry
bytes = make_long_dirent('We the People in order to form a more perfect union', ATTR_ARCHIVE)
bytes = bytes[0:-64] + bytes[-32:]
slots = len(bytes) / 32
slot = subdir.find_slots(slots)
subdir.write_slots(slot, bytes)
subdir = root.mkdir('Bad Orders')
# Bad order value: first
bytes = make_long_dirent('One is the loneliest number', ATTR_ARCHIVE)
bytes = chr(ord(bytes[0])+7) + bytes[1:]
slots = len(bytes) / 32
slot = subdir.find_slots(slots)
subdir.write_slots(slot, bytes)
# Bad order value: middle
bytes = make_long_dirent('It takes two to tango or so they say', ATTR_ARCHIVE)
bytes = bytes[:32] + chr(ord(bytes[32])+7) + bytes[33:]
slots = len(bytes) / 32
slot = subdir.find_slots(slots)
subdir.write_slots(slot, bytes)
# Bad order value: last
bytes = make_long_dirent('Threes Company becomes Threes A Crowd', ATTR_ARCHIVE)
bytes = bytes[:-64] + chr(ord(bytes[-64])+7) + bytes[-63:]
slots = len(bytes) / 32
slot = subdir.find_slots(slots)
subdir.write_slots(slot, bytes)
# Long name entries (valid or not, with no short entry) at end of directory
bytes = make_long_dirent('Four score and seven years ago', ATTR_ARCHIVE)
bytes = bytes[0:-32] # Remove the short name entry
assert len(bytes) % 32 == 0
slots = len(bytes) / 32
slot = root.find_slots(slots)
root.write_slots(slot, bytes)
v.flush()
del v
f.close()
del f
try:
launch([fsck, '-n', disk])
except LaunchError:
pass
launch([fsck, '-y', disk])
launch(['/sbin/fsck_msdos', '-n', disk])
def past_end_of_dir(disk, fsck, newfs, newfs_opts, multiple_clusters=False):
launch([newfs]+newfs_opts+[disk])
f = file(disk, "r+")
v = msdosfs(f)
root = v.root()
if multiple_clusters:
subdir_clusters = v.fat.find(10)
else:
subdir_clusters = None
subdir = root.mkdir('SubDir', subdir_clusters)
subdir.mkfile('Good Sub File')
root.mkfile('Good Root File')
# Make an entry that will be replaced by end-of-directory
slotEOF = root.find_slots(1)
root.mkfile('EOF')
# Make some valid file entries past end of directory
root.mkfile('BADFILE')
root.mkdir('Bad Dir')
root.mkfile('Bad File 2')
# Overwrite 'EOF' entry with end-of-directory marker
root.write_slots(slotEOF, '\x00' * 32)
# Make an entry that will be replaced by end-of-directory
slotEOF = subdir.find_slots(1)
subdir.mkfile('EOF')
# Make some valid file entries past end of directory
subdir.mkfile('BADFILE')
subdir.mkdir('Bad Dir')
subdir.mkfile('Bad File 2')
# If desired, make a whole bunch more entries that will cause
# the directory to grow into at least one more cluster.
# See Radar #xxxx.
if multiple_clusters:
base_name = "This file name is long so that it can take up plenty of room in the directory "
entry_length = len(make_long_dirent(base_name, 0))
num_entries = 4 * (v.bytesPerCluster // entry_length)
for i in xrange(num_entries):
subdir.mkfile(base_name+str(i))
# Overwrite 'EOF' entry with end-of-directory marker
subdir.write_slots(slotEOF, '\x00' * 32)
v.flush()
del v
f.close()
del f
try:
launch([fsck, '-n', disk])
except LaunchError:
pass
launch([fsck, '-y', disk])
launch(['/sbin/fsck_msdos', '-n', disk])
#
# Stomp the first two FAT entries.
#
def fat_bad_0_or_1(disk, fsck, newfs, newfs_opts):
launch([newfs]+newfs_opts+[disk])
f = file(disk, "r+")
v = msdosfs(f)
v.fat[0] = 0
v.fat[1] = 1
v.flush()
del v
f.close()
del f
try:
launch([fsck, '-n', disk])
except LaunchError:
pass
launch([fsck, '-y', disk])
launch(['/sbin/fsck_msdos', '-n', disk])
#
# Mark the volume dirty, and cause some minor damage (orphan clusters).
# Make sure the volume gets marked clean afterwards.
#
def fat_mark_clean_corrupt(disk, fsck, newfs, newfs_opts):
launch([newfs]+newfs_opts+[disk])
f = file(disk, "r+")
v = msdosfs(f)
# Mark the volume "dirty" by clearing the "clean" bit.
if v.type == 32:
v.fat[1] = v.fat[1] & 0x07FFFFFF
else:
v.fat[1] = v.fat[1] & 0x7FFF
# Allocate some clusters, so there is something to repair.
v.allocate(3)
v.flush()
del v
f.close()
del f
try:
launch([fsck, '-n', disk])
except LaunchError:
pass
launch([fsck, '-y', disk])
f = file(disk, "r")
v = msdosfs(f)
# Make sure the "clean" bit is now set.
if v.type == 32:
clean = v.fat[1] & 0x08000000
else:
clean = v.fat[1] & 0x8000
if not clean:
raise RuntimeError("Volume still dirty!")
v.flush()
del v
f.close()
del f
launch(['/sbin/fsck_msdos', '-n', disk])
#
# Mark the volume dirty (with no corruption).
# Make sure the volume gets marked clean afterwards.
# Make sure the exit status is 0, even with "-n".
#
def fat_mark_clean_ok(disk, fsck, newfs, newfs_opts):
launch([newfs]+newfs_opts+[disk])
f = file(disk, "r+")
v = msdosfs(f)
# Mark the volume "dirty" by clearing the "clean" bit.
if v.type == 32:
v.fat[1] = v.fat[1] & 0x07FFFFFF
else:
v.fat[1] = v.fat[1] & 0x7FFF
v.flush()
del v
f.close()
del f
# Make sure that we ask the user to mark the disk clean, but don't return
# a non-zero exit status if the user declines.
stdout, stderr = launch([fsck, '-n', disk], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
assert "\nMARK FILE SYSTEM CLEAN? no\n" in stdout
assert "\n***** FILE SYSTEM IS LEFT MARKED AS DIRTY *****\n" in stdout
stdout, stderr = launch([fsck, '-y', disk], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
assert "\nMARK FILE SYSTEM CLEAN? yes\n" in stdout
assert "\nMARKING FILE SYSTEM CLEAN\n" in stdout
f = file(disk, "r")
v = msdosfs(f)
# Make sure the "clean" bit is now set.
if v.type == 32:
clean = v.fat[1] & 0x08000000
else:
clean = v.fat[1] & 0x8000
if not clean:
raise RuntimeError("Volume still dirty!")
v.flush()
del v
f.close()
del f
launch(['/sbin/fsck_msdos', '-n', disk])
#
# Make a file whose physical size is 4GB. The logical size is 4GB-100.
# This is actually NOT corrupt; it's here to verify that fsck_msdos does not
# try to truncate the file due to overflow of the physical size. [4988133]
#
def file_4GB(disk, fsck, newfs, newfs_opts):
launch([newfs]+newfs_opts+[disk])
#
# Create a file whose size is 4GB-100. That means its physical size will
# be rounded up to the next multiple of the cluster size, meaning the
# physical size will be 4GB.
#
print "# Creating a 4GiB file. This may take some time."
f = file(disk, "r+")
v = msdosfs(f)
four_GB = 4*1024*1024*1024
clusters = four_GB / v.bytesPerCluster
head = v.allocate(clusters)
v.root().mkfile('4GB', head=head, length=four_GB-100)
v.flush()
del v
f.close()
del f
launch([fsck, '-n', disk])
#
# Make a file with excess clusters allocated: over 4GB worth of clusters
#
# TODO: What combination of files do we want to test with?
# TODO: A smallish logical size
# TODO: A logical size just under 4GB
# TODO: Cross-linked files?
# TODO: Cross linked beyond 4GB?
# TODO: Cross linked before 4GB?
#
def file_4GB_excess_clusters(disk, fsck, newfs, newfs_opts):
launch([newfs]+newfs_opts+[disk])
#
# Create files with too many clusters for their size
#
print "# Creating a 4GiB+ file. This may take some time."
f = file(disk, "r+")
v = msdosfs(f)
four_GB = 4*1024*1024*1024
clusters = four_GB / v.bytesPerCluster
head=v.allocate(clusters+7)
v.root().mkfile('FOO', head=head, length=5*v.bytesPerCluster-100)
head=v.allocate(clusters+3)
v.root().mkfile('BAR', head=head, length=four_GB-30)
v.flush()
del v
f.close()
del f
# TODO: Need a better way to assert that the disk is corrupt to start with
try:
launch([fsck, '-n', disk])
except LaunchError:
pass
launch([fsck, '-y', disk])
launch([fsck, '-n', disk])
#
# Test the "-q" ("quick") option which reports whether the "dirty" flag in
# the FAT has been set. The dirty flag is only defined for FAT16 and FAT32.
# For FAT12, we actually do a full verify of the volume and return that the
# volume is clean if it has no problems, dirty if a problem was detected.
#
# NOTE: Assumes newfs_opts[1] is "12", "16" or "32" to indicate which FAT
# type is being tested.
#
def test_quick(disk, fsck, newfs, newfs_opts):
assert newfs_opts[1] in ["12", "16", "32"]
launch([newfs]+newfs_opts+[disk])
# Try a quick check of a volume that is clean
launch([fsck, '-q', disk])
# Make the volume dirty
f = file(disk, "r+")
v = msdosfs(f)
if newfs_opts[1] in ["16", "32"]:
if newfs_opts[1] == "16":
v.fat[1] &= 0x7FFF
else:
v.fat[1] &= 0x07FFFFFF
else:
# Corrupt a FAT12 volume so that it looks dirty.
# Allocate some clusters, so there is something to repair.
v.allocate(3)
v.flush()
del v
f.close()
del f
# Quick check a dirty volume
try:
launch([fsck, '-q', disk])
except LaunchError:
pass
else:
raise FailureExpected("Volume not dirty?")
#