a2kit 4.4.2

Retro disk image and language utility
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
//! ## ProDOS file system module
//! 
//! This manipulates disk images containing one ProDOS volume.
//! 
//! * Single volume images only

mod boot;
pub mod types;
mod directory;
mod pack;

use std::collections::HashMap;
use a2kit_macro::DiskStruct;
use std::str::FromStr;
use std::fmt::Write;
use colored::*;
use log::{trace,debug,error};
use types::*;
use pack::*;
use directory::*;
use super::{Block,Attributes};
use crate::img;
use crate::{DYNERR,STDRESULT};

pub const FS_NAME: &str = "prodos";
const IMAGE_TYPES: [img::DiskImageType;6] = [
    img::DiskImageType::DO,
    img::DiskImageType::PO,
    img::DiskImageType::NIB,
    img::DiskImageType::WOZ1,
    img::DiskImageType::WOZ2,
    img::DiskImageType::DOT2MG
];

/// The primary interface for disk operations.
pub struct Disk {
    img: Box<dyn img::DiskImage>,
    total_blocks: usize,
    maybe_bitmap: Option<Vec<u8>>,
    bitmap_blocks: Vec<usize>,
    /// only valid during glob
    curr_path: Vec<String>
}

/// put a u16 into an index block in the prescribed fashion
fn pack_index_ptr(buf: &mut [u8],ptr: u16,idx: usize) {
    let bytes = u16::to_le_bytes(ptr);
    buf[idx] = bytes[0];
    buf[idx+256] = bytes[1];
}

pub fn new_fimg(chunk_len: usize,set_time: bool,path: &str) -> Result<super::FileImage,DYNERR> {
    if !is_path_valid(path) {
        return Err(Box::new(Error::Syntax));
    }
    let created = match set_time {
        true => pack::pack_time(None).to_vec(),
        false => vec![0;4]
    };
    Ok(super::FileImage {
        fimg_version: super::FileImage::fimg_version(),
        file_system: String::from(FS_NAME),
        fs_type: vec![0],
        aux: vec![0;2],
        eof: vec![0;3],
        accessed: vec![],
        created,
        modified: vec![0;4],
        access: vec![0],
        version: vec![0],
        min_version: vec![0],
        chunk_len,
        full_path: path.to_string(),
        chunks: HashMap::new()
    })
}

pub struct Packer {
}

impl Disk {
    /// Use the given image as storage for a new DiskFS.
    /// The DiskFS takes ownership of the image.
    /// The image may or may not be formatted.
    pub fn from_img(img: Box<dyn img::DiskImage>) -> Result<Self,DYNERR> {
        if img.nominal_capacity().is_none() {
            return Err(Box::new(Error::IOError));
        }
        let total_blocks = img.nominal_capacity().unwrap()/512;
        Ok(Self {
            img,
            total_blocks,
            // bitmap buffer is designed to work transparently
            maybe_bitmap: None,
            bitmap_blocks: Vec::new(),
            curr_path: Vec::new()
        })
    }
    /// Test an image for the ProDOS file system. May try multiple track methods.
    /// Usually called before user parameters are applied.
    pub fn test_img(img: &mut Box<dyn img::DiskImage>) -> bool {
        if !IMAGE_TYPES.contains(&img.what_am_i()) {
            return false;
        }
        log::info!("trying ProDOS");
        let mut maybe_buf: Result<Vec<u8>,DYNERR> = Err(Box::new(Error::IOError));
        for test_method in [img::tracks::Method::Auto,img::tracks::Method::Emulate] {
            img.change_method(test_method);
            maybe_buf = img.read_block(Block::PO(2));
            if maybe_buf.is_ok() {
                break;
            }
        }
        // test the volume directory header to see if this is ProDOS
        match maybe_buf { Ok(buf) => {
            let first_char_patt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.";
            let char_patt = [first_char_patt,"0123456789"].concat();
            let vol_key: KeyBlock<VolDirHeader> = match KeyBlock::from_bytes(&buf) {
                Ok(k) => k,
                Err(_) => return false
            };
            let (nibs,name) = vol_key.header.fname();
            let total_blocks = u16::from_le_bytes([buf[0x29],buf[0x2A]]);
            if total_blocks<280 {
                debug!("peculiar block count {}",total_blocks);
                return false;
            }
            if buf[0x23]!=0x27 || (buf[0x24]!=0x0D && buf[0x24]!=0x0C) {
                debug!("unexpected header bytes {}, {}",buf[0x23],buf[0x24]);
                return false;
            }
            if vol_key.prev()!=0 || vol_key.next()!=3 || (nibs >> 4)!=15 {
                debug!("unexpected volume name length or links");
                return false;
            }
            if !first_char_patt.contains(name[0] as char) {
                debug!("volume name unexpected character");
                return false;
            }
            for i in 1..(nibs & 0x0F) {
                if !char_patt.contains(name[i as usize] as char) {
                    debug!("volume name unexpected character");
                    return false;
                }
            }
            return true;
        } Err(e) => {
            debug!("ProDOS volume directory was not readable: {}",e);
            return false;
        }}
    }
    /// Open buffer if not already present.  Will usually be called indirectly.
    fn open_bitmap_buffer(&mut self) -> STDRESULT {
        if self.maybe_bitmap==None {
            self.bitmap_blocks = Vec::new();
            let bitmap_block_count = 1 + self.total_blocks / 4096;
            let mut ans = Vec::new();
            let bptr = u16::from_le_bytes(self.get_vol_header()?.bitmap_ptr) as usize;
            for iblock in bptr..bptr+bitmap_block_count {
                let mut buf = [0;512].to_vec();
                self.read_block(&mut buf,iblock,0)?;
                ans.append(&mut buf);
                self.bitmap_blocks.push(iblock);
            }
            self.maybe_bitmap = Some(ans);
        }
        Ok(())
    }
    /// Get the buffer, if it doesn't exist it will be opened.
    fn get_bitmap_buffer(&mut self) -> Result<&mut Vec<u8>,DYNERR> {
        self.open_bitmap_buffer()?;
        if let Some(buf) = self.maybe_bitmap.as_mut() {
            return Ok(buf);
        }
        panic!("bitmap buffer failed to open");
    }
    /// Buffer needs to be written back when an external caller
    /// asks, directly or indirectly, for the underlying image.
    fn writeback_bitmap_buffer(&mut self) -> STDRESULT {
        let buf = match self.maybe_bitmap.as_ref() {
            Some(bitmap) => bitmap.clone(),
            None => return Ok(())
        };
        if self.bitmap_blocks.len()>0 {
            let first = self.bitmap_blocks[0];
            let bitmap_block_count = 1 + self.total_blocks / 4096;
            for iblock in first..first+bitmap_block_count {
                self.zap_block(&buf,iblock,(iblock-first)*512)?;
            }    
        }
        Ok(())
    }
    fn allocate_block(&mut self,iblock: usize) -> STDRESULT {
        let buf = self.get_bitmap_buffer()?;
        let byte = iblock / 8;
        let bit = 7 - iblock % 8;
        buf[byte] &= (1 << bit as u8) ^ u8::MAX;
        Ok(())
    }
    fn deallocate_block(&mut self,iblock: usize) -> STDRESULT {
        let buf = self.get_bitmap_buffer()?;
        let byte = iblock / 8;
        let bit = 7 - iblock % 8;
        buf[byte] |= 1 << bit as u8;
        Ok(())
    }
    fn is_block_free(&mut self,iblock: usize) -> Result<bool,DYNERR> {
        let buf = self.get_bitmap_buffer()?;
        let byte = iblock / 8;
        let bit = 7 - iblock % 8;
        Ok((buf[byte] & (1 << bit as u8)) > 0)
    }
    fn num_free_blocks(&mut self) -> Result<u16,DYNERR> {
        let mut free: u16 = 0;
        for i in 0..self.total_blocks {
            if self.is_block_free(i)? {
                free += 1;
            }
        }
        Ok(free)
    }
    /// Read a block; if it is a bitmap block get it from the buffer.
    fn read_block(&mut self,data: &mut [u8], iblock: usize, offset: usize) -> STDRESULT {
        let bytes = 512;
        let actual_len = match data.len() as i32 - offset as i32 {
            x if x<0 => panic!("invalid offset in read block"),
            x if x<=bytes => x,
            _ => bytes
        };
        if self.bitmap_blocks.contains(&iblock) {
            let first = self.bitmap_blocks[0];
            let buf = self.get_bitmap_buffer()?;
            for i in 0..actual_len as usize {
                data[offset + i] = buf[(iblock-first)*512 + i];
            }
            return Ok(());
        }
        match self.img.read_block(Block::PO(iblock)) {
            Ok(buf) => {
                for i in 0..actual_len as usize {
                    data[offset + i] = buf[i];
                }
                Ok(())
            }
            Err(e) => Err(e)
        }
    }
    /// Write and allocate the block in one step.
    /// If it is a bitmap block panic; we should only be zapping bitmap blocks.
    fn write_block(&mut self,data: &[u8], iblock: usize, offset: usize) -> STDRESULT {
        if self.bitmap_blocks.contains(&iblock) {
            panic!("attempt to write bitmap block, zap it instead");
        }
        self.zap_block(data,iblock,offset)?;
        self.allocate_block(iblock)
    }
    /// Writes a block of data from buffer `data`, starting at `offset` within the buffer.
    /// If `data` is shorter than the block, trailing bytes are unaffected.
    fn zap_block(&mut self,data: &[u8], iblock: usize, offset: usize) -> STDRESULT {
        let bytes = 512;
        let actual_len = match data.len() as i32 - offset as i32 {
            x if x<0 => panic!("invalid offset in write block"),
            x if x<=bytes => x as usize,
            _ => bytes as usize
        };
        if self.bitmap_blocks.contains(&iblock) {
            self.maybe_bitmap = None;
        }
        self.img.write_block(Block::PO(iblock), &data[offset..offset+actual_len])
    }
    fn get_available_block(&mut self) -> Result<Option<u16>,DYNERR> {
        for block in 0..self.total_blocks {
            if self.is_block_free(block)? {
                return Ok(Some(block as u16));
            }
        }
        return Ok(None);
    }
    /// Format a disk with the ProDOS file system
    pub fn format(&mut self, vol_name: &str, floppy: bool, time: Option<chrono::NaiveDateTime>) -> STDRESULT {
        // make sure we start with all 0
        trace!("formatting: zero all");
        for iblock in 0..self.total_blocks {
            self.zap_block(&[0;512],iblock,0)?;
        }
        // calculate volume parameters and setup volume directory
        let mut volume_dir = KeyBlock::<VolDirHeader>::new();
        let bitmap_blocks = 1 + self.total_blocks / 4096;
        volume_dir.set_links(Some(0), Some(VOL_KEY_BLOCK+1));
        volume_dir.header.format(self.total_blocks as u16,vol_name,time);
        let first = u16::from_le_bytes(volume_dir.header.bitmap_ptr) as usize;

        // zap in the volume key block
        trace!("formatting: volume key");
        self.zap_block(&volume_dir.to_bytes(),VOL_KEY_BLOCK as usize,0)?;

        // mark all blocks as free
        trace!("formatting: free all");
        for b in 0..self.total_blocks {
            self.deallocate_block(b)?;
        }

        // mark volume key and bitmap blocks as used
        trace!("formatting: allocate key and bitmap");
        self.allocate_block(VOL_KEY_BLOCK as usize)?;
        for b in first..first + bitmap_blocks {
            self.allocate_block(b)?;
        }
        
        // boot loader blocks
        trace!("formatting: boot loader");
        if floppy {
            self.write_block(&boot::FLOPPY_BLOCK0,0,0)?;
        }
        else {
            self.write_block(&boot::HD_BLOCK0, 0, 0)?;
        }
        self.write_block(&vec![0;512],1,0)?;

        // next 3 volume directory blocks
        trace!("formatting: volume directory");
        for b in 3..6 {
            let mut this = EntryBlock::new();
            if b==5 {
                this.set_links(Some(b-1), Some(0));
            } else {
                this.set_links(Some(b-1), Some(b+1));
            }
            self.write_block(&this.to_bytes(),b as usize,0)?;
        }
        Ok(())
    }
    fn get_vol_header(&mut self) -> Result<VolDirHeader,DYNERR> {
        let mut buf: Vec<u8> = vec![0;512];
        self.read_block(&mut buf,VOL_KEY_BLOCK as usize,0)?;
        let volume_dir = KeyBlock::<VolDirHeader>::from_bytes(&buf)?;
        Ok(volume_dir.header)
    }
    /// Return the correct trait object assuming this block is a directory block.
    /// May return a key block or an entry block.
    fn get_directory(&mut self,iblock: usize) -> Result<Box<dyn Directory>,DYNERR> {
        let mut buf: Vec<u8> = vec![0;512];
        self.read_block(&mut buf,iblock,0)?;
        match (iblock==VOL_KEY_BLOCK as usize,buf[0]==0 && buf[1]==0) {
            (true,true) => Ok(Box::new(KeyBlock::<VolDirHeader>::from_bytes(&buf)?)),
            (true,false) => Ok(Box::new(KeyBlock::<VolDirHeader>::from_bytes(&buf)?)),
            (false,true) => Ok(Box::new(KeyBlock::<SubDirHeader>::from_bytes(&buf)?)),
            (false,false) => Ok(Box::new(EntryBlock::from_bytes(&buf)?))
        }
    }
    /// Find the key block assuming this block is a directory block, and return the
    /// block pointer and corresponding trait object in a tuple.
    fn get_key_directory(&mut self,ptr: u16) -> Result<(u16,Box<dyn Directory>),DYNERR> {
        let mut curr = ptr;
        for _try in 0..100 {
            let test_dir = self.get_directory(curr as usize)?;
            if test_dir.prev()==0 {
                return Ok((curr,test_dir));
            }
            curr = test_dir.prev();
        }
        error!("directory block count not plausible, aborting");
        Err(Box::new(Error::EndOfData))
    }
    /// Given an entry location get the entry from disk
    fn read_entry(&mut self,loc: &EntryLocation) -> Result<Entry,DYNERR> {
        let dir = self.get_directory(loc.block as usize)?;
        Ok(dir.get_entry(loc))
    }
    /// Given a modified entry and location, write the change to disk.
    /// Any other unsaved changes in the block are lost.  Maybe this should go away.
    fn write_entry(&mut self,loc: &EntryLocation,entry: &Entry) -> STDRESULT {
        let mut dir = self.get_directory(loc.block as usize)?;
        dir.set_entry(loc,*entry);
        let buf = dir.to_bytes();
        self.write_block(&buf,loc.block as usize,0)
    }
    /// Try to add another entry block to the directory with the given parent entry.
    /// If successful return the location of the first entry in the new block.
    /// This is called when the directory runs out of entries.
    fn expand_directory(&mut self, parent_loc: &EntryLocation) -> Result<EntryLocation,DYNERR> {
        let mut entry = self.read_entry(&parent_loc)?;
        if entry.storage_type()!=StorageType::SubDirEntry {
            return Err(Box::new(Error::FileTypeMismatch));
        }
        let mut curr = entry.get_ptr();
        for _try in 0..100 {
            let mut dir = self.get_directory(curr as usize)?;
            if dir.next()==0 {
                match self.get_available_block()? { Some(avail) => {
                    // update the parent entry
                    entry.set_eof(entry.eof()+512);
                    entry.delta_blocks(1);
                    self.write_entry(parent_loc,&entry)?;
                    // link to new block
                    dir.set_links(None, Some(avail));
                    self.write_block(&dir.to_bytes(),curr as usize,0)?;
                    // fill new block
                    dir = Box::new(EntryBlock::new());
                    dir.set_links(Some(curr),Some(0));
                    self.write_block(&dir.to_bytes(),avail as usize,0)?;
                    return Ok(EntryLocation { block: avail, idx: 1});
                } _ => {
                    return Err(Box::new(Error::DiskFull));
                }}
            }
            curr = dir.next();
        }
        error!("directory block count not plausible, aborting");
        Err(Box::new(Error::EndOfData))
    }
    /// Get the next available entry location.
    /// Will try to expand the directory if necessary.
    fn get_available_entry(&mut self, key_block: u16) -> Result<EntryLocation,DYNERR> {
        let mut curr = key_block;
        for _try in 0..100 {
            let mut dir = self.get_directory(curr as usize)?;
            let locs = dir.entry_locations(curr);
            for loc in locs {
                if !dir.get_entry(&loc).is_active() {
                    return Ok(loc);
                }
            }
            curr = dir.next();
            if curr==0 {
                dir = self.get_directory(key_block as usize)?;
                match dir.parent_entry_loc() { Some(parent_loc) => {
                    return match self.expand_directory(&parent_loc) {
                        Ok(loc) => Ok(loc),
                        Err(e) => Err(e)
                    }
                } _ => {
                    // this is the volume directory which we cannot expand
                    return Err(Box::new(Error::DirectoryFull));
                }}
            }
        }
        error!("directory block count not plausible, aborting");
        Err(Box::new(Error::EndOfData))
    }
    // Find specific entry in directory with the given key block
    fn search_entries(&mut self,stype: &Vec<StorageType>,name: &String,key_block: u16) -> Result<Option<EntryLocation>,DYNERR> {
        if !is_name_valid(&name) {
            error!("invalid ProDOS name {}",&name);
            return Err(Box::new(Error::Syntax));
        }
        let mut curr = key_block;
        for _try in 0..100 {
            let dir = self.get_directory(curr as usize)?;
            let locs = dir.entry_locations(curr);
            for loc in locs {
                let entry = dir.get_entry(&loc);
                if entry.is_active() && is_file_match::<Entry>(stype,name,&entry) {
                    return Ok(Some(loc));
                }
            }
            curr = dir.next();
            if curr==0 {
                return Ok(None);
            }
        }
        error!("directory block count not plausible, aborting");
        Err(Box::new(Error::EndOfData))
    }
    /// Put path as [volume,subdir,subdir,...,last] where last could be an empty string,
    /// which indicates this is a directory.  If last is not empty, it could be either directory or file.
    /// Also check that the path is not too long accounting for prefix rules.
    fn normalize_path(&mut self,vol_name: &str,path: &str) -> Result<Vec<String>,DYNERR> {
        let mut path_nodes: Vec<String> = path.split("/").map(|s| s.to_string().to_uppercase()).collect();
        if &path[0..1]!="/" {
            path_nodes.insert(0,vol_name.to_string());
        } else {
            path_nodes = path_nodes[1..].to_vec();
        }
        // check prefix/path length
        let mut prefix_len = 0;
        let mut rel_path_len = 0;
        for s in path_nodes.iter() {
            if rel_path_len>0 {
                rel_path_len += 1 + s.len();
            } else {
                prefix_len += 1 + s.len();
                if prefix_len>64 {
                    prefix_len -= 1 + s.len();
                    rel_path_len += 1 + s.len();
                }
            }
        }
        if rel_path_len>64 {
            error!("ProDOS path too long, prefix {}, relative {}",prefix_len,rel_path_len);
            return Err(Box::new(Error::Range));
        }
        return Ok(path_nodes);
    }
    /// split the path into the last node (file or directory) and its parent path
    fn split_path(&mut self,vol_name: &str,path: &str) -> Result<[String;2],DYNERR> {
        let mut path_nodes = self.normalize_path(vol_name,path)?;
        // if last node is empty, remove it (means we have a directory)
        if path_nodes[path_nodes.len()-1].len()==0 {
            path_nodes = path_nodes[0..path_nodes.len()-1].to_vec();
        }
        let name = path_nodes[path_nodes.len()-1].clone();
        if path_nodes.len()<2 {
            return Err(Box::new(Error::PathNotFound));
        } else {
            path_nodes = path_nodes[0..path_nodes.len()-1].to_vec();
        }
        let parent_path: String = path_nodes.iter().map(|s| "/".to_string() + s).collect::<Vec<String>>().concat();
        return Ok([parent_path,name]);
    }
    fn search_volume(&mut self,file_types: &Vec<StorageType>,path: &str) -> Result<EntryLocation,DYNERR> {
        let vhdr = self.get_vol_header()?;
        let path_nodes = self.normalize_path(&vhdr.name(),path)?;
        if &path_nodes[0]!=&vhdr.name() {
            return Err(Box::new(Error::PathNotFound));
        }
        // path_nodes = [volume,dir,dir,...,dir|file|empty]
        let n = path_nodes.len();
        // There is no entry for the volume itself, so if that is the search, return an error
        if n<3 && path_nodes[n-1]=="" {
            return Err(Box::new(Error::PathNotFound));
        }
        // walk the tree
        let mut curr: u16 = VOL_KEY_BLOCK;
        for level in 1..n {
            let subdir = path_nodes[level].clone();
            let file_types_now = match level {
                l if l==n-1 => file_types.clone(),
                _ => vec![StorageType::SubDirEntry]
            };
            match self.search_entries(&file_types_now, &subdir, curr)? { Some(loc) => {
                // success conditions:
                // 1. this is the terminus
                // 2. this is the last subdirectory, terminus is empty, directory was requested
                if level==n-1 || level==n-2 && path_nodes[n-1]=="" && file_types.contains(&types::StorageType::SubDirEntry) {
                    return Ok(loc);
                }
                let entry = self.read_entry(&loc)?;
                curr = entry.get_ptr();
            } _ => {
                return Err(Box::new(Error::PathNotFound));
            }}
        }
        return Err(Box::new(Error::PathNotFound));
    }
    fn find_file(&mut self,path: &str) -> Result<EntryLocation,DYNERR> {
        self.search_volume(&vec![StorageType::Seedling,StorageType::Sapling,StorageType::Tree],path)
    }
    /// Find the directory and return the key block pointer
    fn find_dir_key_block(&mut self,path: &str) -> Result<u16,DYNERR> {
        let vhdr = self.get_vol_header()?;
        let vname = "/".to_string() + &vhdr.name().to_lowercase();
        let vname2 = vname.clone() + "/";
        if path=="/" || path=="" || path.to_lowercase()==vname || path.to_lowercase()==vname2 {
            return Ok(VOL_KEY_BLOCK);
        }
        if let Ok(loc) = self.search_volume(&vec![StorageType::SubDirEntry], path) {
            let entry = self.read_entry(&loc)?;
            return Ok(entry.get_ptr());
        }
        return Err(Box::new(Error::PathNotFound));
    }
    /// Read the data referenced by a single index block
    fn read_index_block(
        &mut self,entry: &Entry,index_ptr: u16,buf: &mut [u8],fimg: &mut super::FileImage,count: &mut usize,eof: &mut usize
    ) -> STDRESULT {
        self.read_block(buf,index_ptr as usize,0)?;
        let index_block = buf.to_vec();
        for idx in 0..256 {
            if *eof >= entry.eof() {
                break;
            }
            let ptr = u16::from_le_bytes([index_block[idx],index_block[idx+256]]);
            let mut bytes = 512;
            if *eof + bytes > entry.eof() {
                bytes = entry.eof() - *eof;
            }
            if ptr>0 {
                self.read_block(buf,ptr as usize,0)?;
                fimg.chunks.insert(*count,buf.to_vec());
            }
            *count += 1;
            *eof += bytes;
        }
        Ok(())
    }
    /// Deallocate the index block and all data blocks referenced by it
    fn deallocate_index_block(&mut self,index_ptr: u16,buf: &mut [u8]) -> STDRESULT {
        self.read_block(buf,index_ptr as usize,0)?;
        let index_block = buf.to_vec();
        for idx in 0..256 {
            let ptr = u16::from_le_bytes([index_block[idx],index_block[idx+256]]);
            if ptr>0 {
                self.deallocate_block(ptr as usize)?;
            }
        }
        // ProDOS evidently swaps the index block halves upon deletion (why?)
        let swapped = [index_block[256..512].to_vec(),index_block[0..256].to_vec()].concat();
        self.write_block(&swapped,index_ptr as usize,0)?;
        self.deallocate_block(index_ptr as usize)?;
        Ok(())
    }
    /// Deallocate all the blocks associated with any entry
    fn deallocate_file_blocks(&mut self,entry: &Entry) -> STDRESULT {
        let mut buf: Vec<u8> = vec![0;512];
        let master_ptr = entry.get_ptr();
        match entry.storage_type() {
            StorageType::Seedling => {
                self.deallocate_block(master_ptr as usize)?;
            },
            StorageType::Sapling => {
                self.deallocate_index_block(master_ptr, &mut buf)?;
            },
            StorageType::Tree => {
                self.read_block(&mut buf,master_ptr as usize,0)?;
                let master_block = buf.clone();
                for idx in 0..256 {
                    let ptr = u16::from_le_bytes([master_block[idx],master_block[idx+256]]);
                    if ptr>0 {
                        self.deallocate_index_block(ptr, &mut buf)?;
                    }
                }
                // ProDOS evidently swaps the master index block halves upon deletion (why?)
                let swapped = [master_block[256..512].to_vec(),master_block[0..256].to_vec()].concat();
                self.write_block(&swapped,master_ptr as usize,0)?;
                self.deallocate_block(master_ptr as usize)?;
            }
            _ => panic!("cannot read file of this type")
        }
        Ok(())
    }
    /// Read any file into the sparse file format.  Use `FileImage.sequence()` to flatten the result
    /// when it is expected to be sequential.
    fn read_file(&mut self,entry: &Entry) -> Result<super::FileImage,DYNERR> {
        let mut fimg = new_fimg(512,false,"temp")?;
        entry.metadata_to_fimg(&mut fimg);
        let mut buf: Vec<u8> = vec![0;512];
        let master_ptr = entry.get_ptr();
        let mut eof: usize = 0;
        let mut count: usize = 0;
        match entry.storage_type() {
            StorageType::Seedling => {
                self.read_block(&mut buf, master_ptr as usize, 0)?;
                fimg.chunks.insert(0, buf.clone());
                return Ok(fimg);
            },
            StorageType::Sapling => {
                self.read_index_block(&entry, master_ptr, &mut buf, &mut fimg, &mut count, &mut eof)?;
                return Ok(fimg);
            },
            StorageType::Tree => {
                self.read_block(&mut buf,master_ptr as usize,0)?;
                let master_block = buf.clone();
                for idx in 0..256 {
                    let ptr = u16::from_le_bytes([master_block[idx],master_block[idx+256]]);
                    if ptr>0 {
                        self.read_index_block(entry, ptr, &mut buf, &mut fimg, &mut count, &mut eof)?;
                    } else {
                        count += 256;
                        eof += 256*512;
                    }
                }
                return Ok(fimg);
            }
            _ => panic!("cannot read file of this type")
        }
    }
    /// Verify that the new name does not already exist
    fn ok_to_rename(&mut self,path: &str,new_name: &str) -> STDRESULT {
        if !is_name_valid(new_name) {
            error!("invalid ProDOS name {}",new_name);
            return Err(Box::new(Error::Syntax));
        }
        let types = vec![StorageType::Seedling,StorageType::Sapling,StorageType::Tree,StorageType::SubDirEntry];
        let vhdr = self.get_vol_header()?;
        let [parent_path,_old_name] = self.split_path(&vhdr.name(),path)?;
        if let Ok(key_block) = self.find_dir_key_block(&parent_path) {
            if let Some(_loc) = self.search_entries(&types, &new_name.to_string(), key_block)? {
                return Err(Box::new(Error::DuplicateFilename));
            }
        }
        return Ok(());
    }
    /// Prepare a directory for a new file or subdirectory.  This will modify the disk only if the directory needs to grow.
    fn prepare_to_write(&mut self,path: &str) -> Result<(String,u16,EntryLocation,u16),DYNERR> {
        let types = vec![StorageType::Seedling,StorageType::Sapling,StorageType::Tree,StorageType::SubDirEntry];
        let vhdr = self.get_vol_header()?;
        let [parent_path,name] = self.split_path(&vhdr.name(),path)?;
        if !is_name_valid(&name) {
            error!("invalid ProDOS name {}",&name);
            return Err(Box::new(Error::Syntax));
        }
        // find the parent key block, entry location, and new data block (or new key block if directory)
        match self.find_dir_key_block(&parent_path) { Ok(key_block) => {
            if let Some(_loc) = self.search_entries(&types, &name, key_block)? {
                return Err(Box::new(Error::DuplicateFilename));
            }
            match self.get_available_entry(key_block) {
                Ok(loc) => {
                    match self.get_available_block()? { Some(new_block) => {
                        return Ok((name,key_block,loc,new_block));
                    } _ => {
                        return Err(Box::new(Error::DiskFull));
                    }}
                },
                Err(e) => return Err(e)
            }
        } _ => {
            return Err(Box::new(Error::PathNotFound));
        }}
    }
    // Write a data block or account for a hole.
    // It is up to the creator of SparseFileData to ensure that the first block is not empty. 
    fn write_data_block_or_not(&mut self,count: usize,end: usize,ent: &mut Entry,buf_maybe: Option<&Vec<u8>>) -> Result<u16,DYNERR> {
        let mut eof = ent.eof();
        if let Some(buf) = buf_maybe {
            match self.get_available_block()? { Some(data_block) => {
                self.write_block(&buf,data_block as usize,0)?;
                eof += match count {
                    c if c+1 < end => 512,
                    _ => buf.len()
                };
                ent.delta_blocks(1);
                ent.set_eof(eof);
                return Ok(data_block);
            } _ => {
                error!("block not available, but it should have been");
                return Err(Box::new(Error::DiskFull));
            }}
        } else {
            eof += 512;
            ent.set_eof(eof);
            return Ok(0);
        }
    }
    /// Write any sparse or sequential file.  Use `FileImage::desequence` to put sequential data
    /// into the file image format, with no loss of generality.
    /// The entry must already exist and point to the next available block.
    /// The creator of `FileImage` must ensure that the first block is allocated.
    /// This writes blocks more often than necessary, would be inadequate for an actual disk.
    fn write_file(&mut self,loc: EntryLocation,fimg: &super::FileImage) -> Result<usize,DYNERR> {
        if fimg.chunks.len()==0 {
            error!("empty data is not allowed for ProDOS file images");
            return Err(Box::new(Error::EndOfData));
        }
        let mut storage = StorageType::Seedling;
        let mut master_buf: Vec<u8> = vec![0;512];
        let mut master_ptr: u16 = 0;
        let mut master_count: u16 = 0;
        let mut index_buf: Vec<u8> = vec![0;512];
        let mut index_ptr: u16 = 0;
        let mut index_count: u16 = 0;
        let dir = self.get_directory(loc.block as usize)?;
        let mut entry = dir.get_entry(&loc);
        entry.set_eof(0);

        for count in 0..fimg.end() {

            let buf_maybe = fimg.chunks.get(&count);

            if master_count > 127 {
                return Err(Box::new(Error::DiskFull));
            }

            // Check that enough free blocks are available to proceed with this stage.
            let blocks_needed = match storage {
                StorageType::Seedling => {
                    match (buf_maybe,count) {
                        (_,i) if i==0 => 1, // data block
                        (None,_) => 1, // index block
                        (Some(_v),_) => 2 // index and data blocks
                    }
                },
                StorageType::Sapling => {
                    match (buf_maybe,index_count) {
                        (None,i) if i<256 => 0,
                        (Some(_v),i) if i<256 => 1, // data block
                        (None,_) => 1, // master block
                        (Some(_v),_) => 3 // master, index, and data blocks
                    }
                },
                StorageType::Tree => {
                    match (buf_maybe,index_count,index_ptr) {
                        (None,_,_) => 0,
                        (Some(_v),i,p) if i<256 && p>0 => 1, // data block
                        (Some(_v),i,p) if i<256 && p==0 => 2, // index and data blocks
                        (Some(_v),_,_) => 2 // index and data blocks
                    }
                }
                _ => panic!("unexpected storage type during write")
            };
            if blocks_needed > self.num_free_blocks()? {
                return Err(Box::new(Error::DiskFull));
            }
            
            match storage {
                StorageType::Seedling => {
                    if count>0 {
                        storage = StorageType::Sapling;
                        entry.change_storage_type(storage);
                        index_ptr = self.get_available_block().expect("unreachable").unwrap();
                        self.allocate_block(index_ptr as usize)?;
                        entry.delta_blocks(1);
                        pack_index_ptr(&mut index_buf,entry.get_ptr(),0);
                        entry.set_ptr(index_ptr);
                        index_count += 1;
                        let curr = self.write_data_block_or_not(count,fimg.end(),&mut entry,buf_maybe)?;
                        pack_index_ptr(&mut index_buf, curr, index_count as usize);
                        self.write_block(&index_buf, index_ptr as usize, 0)?;
                        index_count += 1;
                    } else {
                        self.write_data_block_or_not(count,fimg.end(),&mut entry,buf_maybe)?;
                        // index does not exist yet
                    }
                },
                StorageType::Sapling => {
                    if index_count > 255 {
                        storage = StorageType::Tree;
                        entry.change_storage_type(storage);
                        master_ptr = self.get_available_block().expect("unreachable").unwrap();
                        self.allocate_block(master_ptr as usize)?;
                        entry.set_ptr(master_ptr);
                        entry.delta_blocks(1);
                        pack_index_ptr(&mut master_buf,index_ptr,0);
                        master_count += 1;
                        index_ptr = 0;
                        index_count = 0;
                        index_buf = vec![0;512];
                        if buf_maybe!=None {
                            index_ptr = self.get_available_block().expect("unreachable").unwrap();
                            self.allocate_block(index_ptr as usize)?;
                            entry.delta_blocks(1);
                            let curr = self.write_data_block_or_not(count,fimg.end(),&mut entry,buf_maybe)?;
                            pack_index_ptr(&mut index_buf, curr, 0);
                            self.write_block(&index_buf,index_ptr as usize,0)?;
                        } else {
                            self.write_data_block_or_not(count,fimg.end(),&mut entry,buf_maybe)?;
                        }
                        index_count += 1;
                    } else {
                        let curr = self.write_data_block_or_not(count,fimg.end(),&mut entry,buf_maybe)?;
                        pack_index_ptr(&mut index_buf,curr,index_count as usize);
                        self.write_block(&index_buf,index_ptr as usize,0)?;
                        index_count += 1;
                    }
                },
                StorageType::Tree => {
                    if index_count > 255 {
                        master_count += 1;
                        index_ptr = 0;
                        index_count = 0;
                        index_buf = vec![0;512];
                    }
                    if index_ptr==0 && buf_maybe!=None {
                        index_ptr = self.get_available_block().expect("unreachable").unwrap();
                        self.allocate_block(index_ptr as usize)?;
                        entry.delta_blocks(1);
                    }
                    let curr = self.write_data_block_or_not(count,fimg.end(),&mut entry,buf_maybe)?;
                    pack_index_ptr(&mut index_buf, curr, index_count as usize);
                    if index_ptr > 0 {
                        self.write_block(&index_buf,index_ptr as usize,0)?;
                    }
                    pack_index_ptr(&mut master_buf, index_ptr, master_count as usize);
                    self.write_block(&master_buf,master_ptr as usize,0)?;
                    index_count += 1;
                },
                _ => panic!("unexpected storage type during write")
            }
        }
        // update the entry, do last to capture all the changes
        let eof = fimg.get_eof();
        if eof>0 {
            entry.set_eof(eof);
        }
        entry.overwrite_all_access(fimg.access[0]);
        self.write_entry(&loc,&entry)?;
        return Ok(eof);
    }
    /// modify a file entry, optionally set access, rename, retype; attempt to change already locked file will fail.
    fn modify(&mut self,loc: &EntryLocation,permissions: Attributes,maybe_new_name: Option<&str>,
        maybe_new_type: Option<&str>,maybe_new_aux: Option<u16>) -> STDRESULT {  
        let dir = self.get_directory(loc.block as usize)?;
        let mut entry = dir.get_entry(&loc);
        if !entry.get_access(Access::Rename) && maybe_new_name!=None {
            return Err(Box::new(Error::WriteProtected));
        }
        entry.set_access(permissions);
        if let Some(new_name) = maybe_new_name {
            entry.rename(new_name);
        }
        if let Some(new_type) = maybe_new_type {
            match FileType::from_str(new_type) {
                Ok(typ) => entry.set_ftype(typ as u8),
                Err(e) => return Err(Box::new(e))
            }
        }
        if let Some(new_aux) = maybe_new_aux {
            entry.set_aux(new_aux);
        }
        self.write_entry(loc, &entry)?;
        return Ok(());
    }
    fn glob_node(&mut self,pattern: &str,dir_block: u16,case_sensitive: bool) -> Result<Vec<String>,DYNERR> {
        // this blindly searches everywhere, we could be more efficient by truncating based on the pattern
        let mut files = Vec::new();
        let glob = match case_sensitive {
            true => globset::GlobBuilder::new(&pattern).literal_separator(true).build()?.compile_matcher(),
            false => globset::GlobBuilder::new(&pattern.to_uppercase()).literal_separator(true).build()?.compile_matcher()
        };
        let mut curr = dir_block;
        while curr>0 {
            let dir = self.get_directory(curr as usize)?;
            for loc in dir.entry_locations(curr) {
                let entry = dir.get_entry(&loc);
                if entry.is_active() {
                    let key = entry.name();
                    let test = match case_sensitive {
                        true => [self.curr_path.concat(),key.clone()].concat(),
                        false => [self.curr_path.concat(),key.clone()].concat().to_uppercase(),
                    };
                    if entry.storage_type()!=StorageType::SubDirEntry && glob.is_match(&test) {
                        let mut full_path = self.curr_path.concat();
                        full_path += &key;
                        files.push(full_path);
                    }
                    if entry.storage_type()==StorageType::SubDirEntry {
                        trace!("descend into directory {}",key);
                        self.curr_path.push(key + "/");
                        files.append(&mut self.glob_node(pattern,entry.get_ptr(),case_sensitive)?);
                    }
                }
            }
            curr = dir.next();
        }
        self.curr_path.pop();
        Ok(files)
    }
    /// Output ProDOS directory as a JSON object, calls itself recursively
    fn tree_node(&mut self,dir_block: u16,include_meta: bool) -> Result<json::JsonValue,DYNERR> {
        let mut files = json::JsonValue::new_object();
        let mut curr = dir_block;
        while curr>0 {
            let dir = self.get_directory(curr as usize)?;
            for loc in dir.entry_locations(curr) {
                let entry = dir.get_entry(&loc);
                if entry.is_active() {
                    let key = entry.name();
                    files[&key] = json::JsonValue::new_object();
                    if entry.storage_type()==StorageType::SubDirEntry {
                        trace!("descend into directory {}",key);
                        files[&key]["files"] = self.tree_node(entry.get_ptr(),include_meta)?;
                    }
                    if include_meta {
                        files[&key]["meta"] = entry.meta_to_json();
                    }
                }
            }
            curr = dir.next();
        }
        Ok(files)
    }
}

impl super::DiskFS for Disk {
    fn new_fimg(&self, chunk_len: Option<usize>,set_time: bool,path: &str) -> Result<super::FileImage,DYNERR> {
        match chunk_len {
            Some(l) => new_fimg(l,set_time,path),
            None => new_fimg(BLOCK_SIZE,set_time,path)
        }
    }
    fn stat(&mut self) -> Result<super::Stat,DYNERR> {
        let vheader = self.get_vol_header()?;
        Ok(super::Stat {
            fs_name: FS_NAME.to_string(),
            label: vheader.name(),
            users: Vec::new(),
            block_size: BLOCK_SIZE,
            block_beg: 0,
            block_end: self.total_blocks,
            free_blocks: self.num_free_blocks()? as usize,
            raw: "".to_string()
        })
    }
    fn catalog_to_stdout(&mut self, path: &str) -> STDRESULT {
        let b = self.find_dir_key_block(path)?;
        let mut dir = self.get_directory(b as usize)?;
        println!();
        if b==2 {
            println!("{}{}","/".bright_blue().bold(),dir.name().bright_blue().bold());
        } else {
            println!("{}",dir.name().bright_blue().bold());
        }
        println!();
        println!(" {:15} {:4} {:6} {:16} {:16} {:7} {:7}",
            "NAME".bold(),"TYPE".bold(),"BLOCKS".bold(),
            "MODIFIED".bold(),"CREATED".bold(),"ENDFILE".bold(),"SUBTYPE".bold());
        println!();
        let mut curr = b;
        while curr>0 {
            dir = self.get_directory(curr as usize)?;
            for loc in dir.entry_locations(curr) {
                let entry = dir.get_entry(&loc);
                if entry.is_active() {
                    println!("{}",entry);
                }
            }
            curr = dir.next();
        }
        println!();
        let free = self.num_free_blocks()? as usize;
        let used = self.total_blocks-free;
        println!("BLOCKS FREE: {}  BLOCKS USED: {}  TOTAL BLOCKS: {}",free,used,self.total_blocks);
        println!();
        Ok(())
    }
    fn catalog_to_vec(&mut self, path: &str) -> Result<Vec<String>,DYNERR> {
        let mut ans = Vec::new();
        let mut curr = self.find_dir_key_block(path)?;
        while curr>0 {
            let dir = self.get_directory(curr as usize)?;
            for loc in dir.entry_locations(curr) {
                let entry = dir.get_entry(&loc);
                if entry.is_active() {
                    ans.push(entry.universal_row());
                }
            }
            curr = dir.next();
        }
        Ok(ans)
    }
    fn glob(&mut self,pattern: &str,case_sensitive: bool) -> Result<Vec<String>,DYNERR> {
        let vhdr = self.get_vol_header()?;
        let dir_block = self.find_dir_key_block("/")?;
        let vol_path = ["/",&vhdr.name(),"/"].concat();
        self.curr_path = vec![vol_path.clone()];
        if pattern.starts_with("/") {
            self.glob_node(pattern, dir_block, case_sensitive)
        } else {
            self.glob_node(&(vol_path + pattern), dir_block, case_sensitive)
        }
    }
    fn tree(&mut self,include_meta: bool,indent: Option<u16>) -> Result<String,DYNERR> {
        let vhdr = self.get_vol_header()?;
        let dir_block = self.find_dir_key_block("/")?;
        let mut tree = json::JsonValue::new_object();
        tree["file_system"] = json::JsonValue::String(FS_NAME.to_string());
        tree["files"] = self.tree_node(dir_block,include_meta)?;
        tree["label"] = json::JsonValue::new_object();
        tree["label"]["name"] = json::JsonValue::String(vhdr.name());
        if let Some(spaces) = indent {
            Ok(json::stringify_pretty(tree,spaces))
        } else {
            Ok(json::stringify(tree))
        }
    }
    fn create(&mut self,path: &str) -> STDRESULT {
        match self.prepare_to_write(path) {
            Ok((name,key_block,loc,new_block)) => {
                // update the file count in the parent key block
                let mut dir = self.get_directory(key_block as usize)?;
                dir.inc_file_count();
                self.write_block(&dir.to_bytes(),key_block as usize,0)?;
                // write the entry into the parent directory (may not be key block)
                let mut entry = Entry::create_subdir(&name,new_block,key_block,None);
                entry.delta_blocks(1);
                entry.set_eof(512);
                self.write_entry(&loc,&entry)?;
                // write the new directory's key block
                let mut subdir = KeyBlock::<SubDirHeader>::new();
                subdir.header.create(&name,loc.block,loc.idx as u8,None);
                self.write_block(&subdir.to_bytes(),new_block as usize,0)?;
                Ok(())
            },
            Err(e) => Err(e)
        }
    }
    fn delete(&mut self,path: &str) -> STDRESULT {
        if let Ok(loc) = self.find_file(path) {
            let entry = self.read_entry(&loc)?;
            if !entry.get_access(Access::Destroy) {
                return Err(Box::new(Error::WriteProtected));
            }
            self.deallocate_file_blocks(&entry)?;
            let mut dir = self.get_directory(loc.block as usize)?;
            dir.delete_entry(&loc);
            self.write_block(&dir.to_bytes(),loc.block as usize,0)?;
            let (key_ptr,mut key_dir) = self.get_key_directory(loc.block)?;
            key_dir.dec_file_count();
            self.write_block(&key_dir.to_bytes(),key_ptr as usize,0)?;
            return Ok(());
        }
        if let Ok(ptr) = self.find_dir_key_block(path) {
            let mut dir = self.get_directory(ptr as usize)?;
            match dir.parent_entry_loc() { Some(parent_loc) => {
                let mut parent_dir = self.get_directory(parent_loc.block as usize)?;
                if dir.file_count()>0 {
                    return Err(Box::new(Error::WriteProtected));
                }
                dir.delete();
                self.write_block(&dir.to_bytes(),ptr as usize,0)?;
                let mut next = ptr;
                for _try in 0..100 {
                    self.deallocate_block(next as usize)?;
                    next = dir.next();
                    if next==0 {
                        parent_dir.delete_entry(&parent_loc);
                        self.write_block(&parent_dir.to_bytes(),parent_loc.block as usize,0)?;
                        let (key_ptr,mut key_dir) = self.get_key_directory(parent_loc.block)?;
                        key_dir.dec_file_count();
                        self.write_block(&key_dir.to_bytes(),key_ptr as usize,0)?;
                        return Ok(());
                    }
                }
                error!("directory block count not plausible, aborting");
                return Err(Box::new(Error::EndOfData));
            } _ => {
                return Err(Box::new(Error::WriteProtected));
            }}
        }
        return Err(Box::new(Error::PathNotFound));
    }
    fn set_attrib(&mut self,path: &str,permissions: Attributes,_password: Option<&str>) -> STDRESULT {
        if let Ok(loc) = self.find_file(path) {
            return self.modify(&loc,permissions,None,None,None);
        }
        if let Ok(ptr) = self.find_dir_key_block(path) {
            let dir = self.get_directory(ptr as usize)?;
            if let Some(parent_loc) = dir.parent_entry_loc() {
                return self.modify(&parent_loc,permissions,None,None,None);
            }
        }
        return Err(Box::new(Error::PathNotFound));
    }
    fn rename(&mut self,path: &str,name: &str) -> STDRESULT {
        self.ok_to_rename(path, name)?;
        if let Ok(loc) = self.find_file(path) {
            return self.modify(&loc,Attributes::new(),Some(name),None,None);
        }
        if let Ok(ptr) = self.find_dir_key_block(path) {
            let dir = self.get_directory(ptr as usize)?;
            if let Some(parent_loc) = dir.parent_entry_loc() {
                return self.modify(&parent_loc,Attributes::new(),Some(name),None,None);
            }
        }
        return Err(Box::new(Error::PathNotFound));
    }
    fn retype(&mut self,path: &str,new_type: &str,sub_type: &str) -> STDRESULT {
        match u16::from_str(sub_type) {
            Ok(aux) => match self.find_file(path) {
                Ok(loc) => {
                    self.modify(&loc, Attributes::new(), None,Some(new_type),Some(aux))
                },
                Err(e) => Err(e)
            }
            Err(e) => Err(Box::new(e))
        }
    }
    fn get(&mut self,path: &str) -> Result<super::FileImage,DYNERR> {
        match self.find_file(path) {
            Ok(loc) => {
                let entry = self.read_entry(&loc)?;
                let mut fimg = self.read_file(&entry)?;
                fimg.full_path = path.to_string();
                return Ok(fimg);
            },
            Err(e) => return Err(e)
        }
    }
    fn put(&mut self,fimg: &super::FileImage) -> Result<usize,DYNERR> {
        if fimg.file_system!=FS_NAME {
            error!("cannot write {} file image to prodos",fimg.file_system);
            return Err(Box::new(Error::IOError));
        }
        if fimg.chunk_len!=512 {
            error!("chunk length {} is incompatible with ProDOS",fimg.chunk_len);
            return Err(Box::new(Error::Range));
        }
        match self.prepare_to_write(&fimg.full_path) {
            Ok((name,dir_key_block,loc,new_key_block)) => {
                // update the file count in the parent key block
                let mut dir = self.get_directory(dir_key_block as usize)?;
                dir.inc_file_count();
                self.write_block(&dir.to_bytes(),dir_key_block as usize,0)?;
                // create the entry
                match Entry::create_file(&name,fimg,new_key_block,dir_key_block,None) {
                    Ok(entry) => self.write_entry(&loc,&entry)?,
                    Err(e) => return Err(e)
                }
                // write blocks
                match self.write_file(loc,fimg) {
                    Ok(len) => Ok(len),
                    Err(e) => Err(e)
                }
            },
            Err(e) => Err(e)
        }
    }
    fn read_block(&mut self,num: &str) -> Result<Vec<u8>,DYNERR> {
        match usize::from_str(num) {
            Ok(block) => {
                let mut buf: Vec<u8> = vec![0;512];
                if block>=self.total_blocks {
                    return Err(Box::new(Error::Range));
                }
                self.read_block(&mut buf,block,0)?;
                Ok(buf)
            },
            Err(e) => Err(Box::new(e))
        }
    }
    fn write_block(&mut self, num: &str, dat: &[u8]) -> Result<usize,DYNERR> {
        match usize::from_str(num) {
            Ok(block) => {
                if dat.len() > 512 || block>=self.total_blocks {
                    return Err(Box::new(Error::Range));
                }
                self.zap_block(dat,block,0)?;
                Ok(dat.len())
            },
            Err(e) => Err(Box::new(e))
        }
    }
    fn standardize(&mut self,ref_con: u16) -> HashMap<Block,Vec<usize>> {
        let mut ans: HashMap<Block,Vec<usize>> = HashMap::new();
        let mut curr = ref_con;
        while curr>0 {
            let mut dir = self.get_directory(curr as usize).expect("disk error");
            let locs = dir.entry_locations(curr);
            super::add_ignorable_offsets(&mut ans,Block::PO(curr as usize),dir.standardize(0));
            for loc in locs {
                let mut entry = dir.get_entry(&loc);
                let offset = 4 + (loc.idx-1)*0x27;
                super::add_ignorable_offsets(&mut ans,Block::PO(curr as usize),entry.standardize(offset));
                if entry.storage_type()==StorageType::SubDirEntry {
                    // recursively call to get the things in the subdirectory entries we want to ignore
                    let sub_map = self.standardize(entry.get_ptr());
                    super::combine_ignorable_offsets(&mut ans, sub_map);
                }
            }
            curr = dir.next();
        }
        return ans;
    }
    fn compare(&mut self,path: &std::path::Path,ignore: &HashMap<Block,Vec<usize>>) {
        self.writeback_bitmap_buffer().expect("disk error");
        let mut emulator_disk = crate::create_fs_from_file(&path.to_str().unwrap(),None).expect("read error");
        let vhdr = self.get_vol_header().expect("disk error");
        for block in 0..vhdr.total_blocks() {
            let addr = Block::PO(block as usize);
            let mut actual = self.img.read_block(addr).expect("bad sector access");
            let mut expected = emulator_disk.get_img().read_block(addr).expect("bad sector access");
            if let Some(ignorable) = ignore.get(&addr) {
                for offset in ignorable {
                    actual[*offset] = 0;
                    expected[*offset] = 0;
                }
            }
            for row in 0..16 {
                let mut fmt_actual = String::new();
                let mut fmt_expected = String::new();
                let offset = row*32;
                write!(&mut fmt_actual,"{:02X?}",&actual[offset..offset+32].to_vec()).expect("format error");
                write!(&mut fmt_expected,"{:02X?}",&expected[offset..offset+32].to_vec()).expect("format error");
                assert_eq!(fmt_actual,fmt_expected," at block {}, row {}",block,row)
            }
        }
    }
    fn get_img(&mut self) -> &mut Box<dyn img::DiskImage> {
        self.writeback_bitmap_buffer().expect("could not write back bitmap buffer");
        &mut self.img
    }
}

#[test]
fn test_path_normalize() {
    let img = Box::new(crate::img::dsk_po::PO::create(280));
    let mut disk = Disk::from_img(img).expect("failed to create disk");
    disk.format(&String::from("NEW.DISK"),true,None).expect("disk error");
    match disk.normalize_path("NEW.DISK","DIR1") {
        Ok(res) => assert_eq!(res,["NEW.DISK","DIR1"]),
        Err(e) => panic!("{}",e)
    }
    match disk.normalize_path("NEW.DISK","dir1/") {
        Ok(res) => assert_eq!(res,["NEW.DISK","DIR1",""]),
        Err(e) => panic!("{}",e)
    }
    match disk.normalize_path("NEW.DISK","dir1/sub2") {
        Ok(res) => assert_eq!(res,["NEW.DISK","DIR1","SUB2"]),
        Err(e) => panic!("{}",e)
    }
    match disk.normalize_path("NEW.DISK","/new.disk/dir1/sub2") {
        Ok(res) => assert_eq!(res,["NEW.DISK","DIR1","SUB2"]),
        Err(e) => panic!("{}",e)
    }
    match disk.normalize_path("NEW.DISK","abcdefghijklmno/abcdefghijklmno/abcdefghijklmno/abcdefghijklmno/abcdefghijklmno/abcdefghijklmno/abcdefghijklmno/abcdefghijklmno") {
        Ok(_res) => panic!("normalize_path should have failed with path too long"),
        Err(e) => assert_eq!(e.to_string(),"RANGE ERROR")
    }
}