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
//! ### FAT directory structures
//! 
//! This module encapsulates the FAT directory.  The FAT itself is implemented in
//! `crate::bios::fat`.  The BPB is in `crate::bios::bpb`.

use std::collections::BTreeMap;
use chrono::{NaiveDate,NaiveTime};
use log::{debug,warn,trace};
use super::types::*;
use crate::fs::{FileImage,Attributes};
use crate::{STDRESULT,DYNERR};

// a2kit_macro automatically derives `new`, `to_bytes`, `from_bytes`, and `length` from a DiskStruct.
// This spares us having to manually write code to copy bytes in and out for every new structure.
// The auto-derivation is not used for structures with variable length fields (yet).
use a2kit_macro::{DiskStructError,DiskStruct};
use a2kit_macro_derive::DiskStruct;

/// Size of the directory entry in bytes, always 32
pub const DIR_ENTRY_SIZE: usize = 32;
/// first name byte for a free entry.
const FREE: u8 = 0xe5;
/// first name byte for a free entry, but also indicating no more entries to follow.
const FREE_AND_NO_MORE: u8 = 0x00;

pub const READ_ONLY: u8 = 1;
pub const HIDDEN: u8 = 2;
pub const SYSTEM: u8 = 4;
pub const VOLUME_ID: u8 = 8;
pub const DIRECTORY: u8 = 16;
pub const ARCHIVE: u8 = 32;
pub const LONG_NAME: u8 = 15;
//pub const LONG_NAME_SUB: u8 = 63;

fn update_attrib(curr: u8,what: Attributes) -> u8 {
    let mut ans = curr;
    let mut change_flag = |setting: bool,flag: u8| {
        if setting {
            ans |= flag;
        } else {
            ans &= u8::MAX ^ flag;
        }
    };
    if let Some(setting) = what.backup {
        change_flag(setting,ARCHIVE);
    }
    if let Some(setting) = what.write {
        change_flag(!setting,READ_ONLY);
    }
    if let Some(setting) = what.hidden {
        change_flag(setting,HIDDEN);
    }
    if let Some(setting) = what.system {
        change_flag(setting,SYSTEM);
    }
    if let Some(setting) = what.dir {
        change_flag(setting,DIRECTORY);
    }
    if let Some(setting) = what.vol {
        change_flag(setting,VOLUME_ID);
    }
    ans
}

/// Convenient collection of information about a file.
/// Flags are broken out into their own variables.
/// This is the value of the map produced by Directory::build_files.
#[derive(Clone)]
pub struct FileInfo {
    pub wildcard: String,
    pub idx: usize,
    pub name: String,
    pub typ: String,
    pub read_only: bool,
    pub hidden: bool,
    pub system: bool,
    pub volume_id: bool,
    pub directory: bool,
    pub archived: bool,
    pub create_date: Option<NaiveDate>,
    pub create_time: Option<NaiveTime>,
    pub write_date: Option<NaiveDate>,
    pub write_time: Option<NaiveTime>,
    pub access_date: Option<NaiveDate>,
    pub eof: usize,
    pub cluster1: Option<Ptr>
}

#[derive(PartialEq)]
pub enum EntryType {
    Free,
    FreeAndNoMore,
    File,
    Directory,
    VolumeLabel,
    LongName
}

/// encapsulates information needed to manipulate a directory entry
pub struct EntryLocation {
    /// starting cluster of the directory (not the entry!), if cluster1.is_none(), this is a FAT12/16 root directory
    pub cluster1: Option<Ptr>,
    /// the entry in this directory we are interested in
    pub entry: Ptr,
    /// the entire directory as a vector of entries
    pub dir: Directory
}

#[derive(DiskStruct)]
pub struct Entry {
    name: [u8;8],
    ext: [u8;3],
    /// RO=1,hidden=2,sys=4,vol=8,dir=16,archive=32,long_name=15.
    /// If this is the volume label, cluster1=0.
    /// If this is a directory, file_size=0.
    attr: u8,
    nt_res: u8,
    /// tenths of a second, 0-199 according to MS (typo?)
    creation_tenth: u8,
    /// to the nearest 2 secs
    creation_time: [u8;2],
    creation_date: [u8;2],
    access_date: [u8;2],
    cluster1_high: [u8;2],
    /// set at creation time also
    write_time: [u8;2],
    /// set at creation date also
    write_date: [u8;2],
    cluster1_low: [u8;2],
    file_size: [u8;4]
}

/// Directory is merely a packed sequence of entries.
pub struct Directory {
    entries: Vec<[u8;DIR_ENTRY_SIZE]>
}

impl FileInfo {
    /// for FAT12 or FAT16 set cluster1=0
    pub fn create_root(cluster1: usize) -> Self {
        Self {
            wildcard: String::new(),
            idx: 0,
            name: "".to_string(),
            typ: "".to_string(),
            read_only: false,
            hidden: false,
            system: false,
            volume_id: false,
            directory: true,
            archived: false,
            create_date: None,
            create_time: None,
            write_date: None,
            write_time: None,
            access_date: None,
            eof: 0,
            cluster1: match cluster1 {
                0 => None,
                _ => Some(Ptr::Cluster(cluster1))
            }
        }
    }
    /// represent file info as a wildcard pattern
    pub fn create_wildcard(pattern: &str) -> Self {
        Self {
            wildcard: String::from(pattern),
            idx: 0,
            name: "".to_string(),
            typ: "".to_string(),
            read_only: false,
            hidden: false,
            system: false,
            volume_id: false,
            directory: true,
            archived: false,
            create_date: None,
            create_time: None,
            write_date: None,
            write_time: None,
            access_date: None,
            eof: 0,
            cluster1: None
        }
    }
}

impl Entry {
    /// Create an entry with given name and timestamp (time==None means use current time).
    /// Not to be used to create a label entry.
    pub fn create(name: &str, time: Option<chrono::NaiveDateTime>) -> Self {
        let now = match time {
            Some(t) => t,
            None => chrono::Local::now().naive_local()
        };
        let tenths = super::pack::pack_tenths(Some(now));
        let time = super::pack::pack_time(Some(now));
        let date = super::pack::pack_date(Some(now));
        let (base,ext) = super::pack::string_to_file_name(name);
        Self {
            name: base,
            ext,
            attr: 0,
            nt_res: 0,
            creation_tenth: tenths,
            creation_time: time,
            creation_date: date,
            access_date: date,
            cluster1_high: [0,0],
            write_time: time,
            write_date: date,
            cluster1_low: [0,0],
            file_size: [0,0,0,0]
        }
    }
    /// Create a label entry with given name and timestamp (time==None means use current time).
    pub fn create_label(name: &str, time: Option<chrono::NaiveDateTime>) -> Self {
        let now = match time {
            Some(t) => t,
            None => chrono::Local::now().naive_local()
        };
        let tenths = super::pack::pack_tenths(Some(now));
        let time = super::pack::pack_time(Some(now));
        let date = super::pack::pack_date(Some(now));
        let (base,ext) = super::pack::string_to_label_name(name);
        Self {
            name: base,
            ext,
            attr: VOLUME_ID,
            nt_res: 0,
            creation_tenth: tenths,
            creation_time: time,
            creation_date: date,
            access_date: date,
            cluster1_high: [0,0],
            write_time: time,
            write_date: date,
            cluster1_low: [0,0],
            file_size: [0,0,0,0]
        }
    }
    pub fn erase(&mut self,none_follow: bool) {
        match none_follow {
            true => self.name[0] = FREE_AND_NO_MORE,
            false => self.name[0] = FREE
        }
    }
    pub fn set_cluster(&mut self,cluster: usize) {
        let [b1,b2,b3,b4] = u32::to_le_bytes(cluster as u32);
        self.cluster1_low = [b1,b2];
        self.cluster1_high = [b3,b4];
    }
    /// Create a subdirectory at `new_cluster`, in directory at `parent_cluster` (0 if root even for FAT32).
    /// Return the (parent entry, directory buffer), where the buffer includes the dot and dotdot entries.
    /// The clusters are expected to be written by the caller.
    pub fn create_subdir(name: &str,parent_cluster: usize,new_cluster: usize,block_size: usize,time: Option<chrono::NaiveDateTime>) -> (Self,Vec<u8>) {
        let mut dot = Entry::create(".",time);
        let mut dotdot = Entry::create("..",time);
        dot.attr = DIRECTORY;
        dot.set_cluster(new_cluster);
        dotdot.attr = DIRECTORY;
        dotdot.set_cluster(parent_cluster);
        let mut dir = Directory::new();
        dir.expand(block_size/DIR_ENTRY_SIZE);
        dir.set_entry(&Ptr::Entry(0),&dot);
        dir.set_entry(&Ptr::Entry(1),&dotdot);
        dot.rename(name);
        (dot,dir.to_bytes())
    }
    pub fn name(&self,label: bool) -> String {
        let prim = super::pack::file_name_to_string(self.name, self.ext);
        match label {
            true => prim.replace(".",""),
            false => prim
        }
    }
    pub fn rename(&mut self,new_name: &str) {
        let (name,ext) = super::pack::string_to_file_name(new_name);
        self.name = name;
        self.ext = ext;
    }
    pub fn eof(&self) -> usize {
        u32::from_le_bytes(self.file_size) as usize
    }
    /// access date is lost with this version of file image
    pub fn metadata_to_fimg(&self,fimg: &mut FileImage) {
        fimg.set_eof(self.eof());
        fimg.access = vec![self.attr];
        fimg.fs_type = self.ext.to_vec();
        fimg.aux = vec![];
        fimg.created = [vec![self.creation_tenth],self.creation_time.to_vec(),self.creation_date.to_vec()].concat();
        fimg.modified = [self.write_time.to_vec(),self.write_date.to_vec()].concat();
        fimg.version = vec![];
        fimg.min_version = vec![];
    }
    /// access date is set to modified date
    pub fn fimg_to_metadata(&mut self,fimg: &FileImage,use_fimg_time: bool) -> STDRESULT {
        self.file_size = match fimg.eof[0..4].try_into() {
            Ok(x) => x,
            Err(e) => return Err(Box::new(e))
        };
        self.attr = fimg.access[0];
        if use_fimg_time {
            self.creation_tenth =fimg.created[0];
            self.creation_time = match fimg.created[1..3].try_into() {
                Ok(x) => x,
                Err(e) => return Err(Box::new(e))
            };
            self.creation_date = match fimg.created[3..5].try_into() {
                Ok(x) => x,
                Err(e) => return Err(Box::new(e))
            };
            self.write_time = match fimg.modified[0..2].try_into() {
                Ok(x) => x,
                Err(e) => return Err(Box::new(e))
            };
            self.write_date = match fimg.modified[2..4].try_into() {
                Ok(x) => x,
                Err(e) => return Err(Box::new(e))
            };
            self.access_date = match fimg.modified[2..4].try_into() {
                Ok(x) => x,
                Err(e) => return Err(Box::new(e))
            };
        }
        Ok(())
    }
    pub fn get_attr(&self,mask: u8) -> bool {
        (self.attr & mask) > 0
    }
    /// set, unset, or leave the given attributes
    pub fn set_attr(&mut self,attrib: Attributes) {
        self.attr = update_attrib(self.attr, attrib);
    }
    pub fn standardize(offset: usize) -> Vec<usize> {
        // relative to the entry start
        // creation date, access date, write date
        let ans = vec![13,14,15,16,17,18,19,22,23,24,25];
        ans.iter().map(|x| x + offset).collect()
    }
    // pub fn cluster1(&self) -> usize {
    //     u32::from_le_bytes([self.cluster1_low[0],self.cluster1_low[1],self.cluster1_high[0],self.cluster1_high[1]]) as usize
    // }
}

impl DiskStruct for Directory {
    fn new() -> Self {
        let entries: Vec<[u8;DIR_ENTRY_SIZE]> = Vec::new();
        Self {
            entries
        }
    }
    fn to_bytes(&self) -> Vec<u8> {
        let mut ans: Vec<u8> = Vec::new();
        for x in &self.entries {
            ans.append(&mut x.to_vec());
        }
        return ans;
    }
    fn update_from_bytes(&mut self,bytes: &[u8]) -> Result<(),DiskStructError> {
        self.entries = Vec::new();
        let num_entries = bytes.len()/DIR_ENTRY_SIZE;
        if bytes.len()%DIR_ENTRY_SIZE!=0 {
            warn!("directory buffer wrong size");
        }
        for i in 0..num_entries {
            let entry_buf = match bytes[i*DIR_ENTRY_SIZE..(i+1)*DIR_ENTRY_SIZE].try_into() {
                Ok(buf) => buf,
                Err(_) => return Err(DiskStructError::OutOfData)
            };
            self.entries.push(entry_buf);
        }
        Ok(())
    }
    fn from_bytes(bytes: &[u8]) -> Result<Self,DiskStructError> {
        let mut ans = Self::new();
        ans.update_from_bytes(bytes)?;
        Ok(ans)
    }
    fn len(&self) -> usize {
        return DIR_ENTRY_SIZE*(self.entries.len());
    }
}

impl Directory {
    /// number of entries (used or not) in the directory
    pub fn num_entries(&self) -> usize {
        self.entries.len()
    }
    pub fn expand(&mut self,count: usize) {
        for _i in 0..count {
            self.entries.push([0;32]);
        }
    }
    pub fn get_type(&self,ptr: &Ptr) -> EntryType {
        let (idx,nm0,attr) = match ptr {
            Ptr::Entry(i) => (*i,self.entries[*i][0],self.entries[*i][11]),
            _ => panic!("wrong pointer type")
        };
        trace!("entry {} has name[0] {} and attr {}",idx,nm0,attr);
        match (nm0,attr) {
            (0xe5,_) => EntryType::Free,
            (0x00,_) => EntryType::FreeAndNoMore,
            (_,a) if a & LONG_NAME >= LONG_NAME => EntryType::LongName,
            (_,a) if a & VOLUME_ID > 0 => EntryType::VolumeLabel,
            (_,a) if a & DIRECTORY > 0 => EntryType::Directory,
            _ => EntryType::File
        }
    }
    pub fn get_raw_entry(&self,ptr: &Ptr) -> [u8;DIR_ENTRY_SIZE] {
        match ptr {
            Ptr::Entry(i) => self.entries[*i].clone(),
            _ => panic!("wrong pointer type")
        }
    }
    pub fn get_entry(&self,ptr: &Ptr) -> Entry {
        match ptr {
            Ptr::Entry(idx) => Entry::from_bytes(&self.entries[*idx]).expect("unexpected size"),
            _ => panic!("wrong pointer type")
        }
    }
    pub fn set_entry(&mut self,ptr: &Ptr,entry: &Entry) {
        match ptr {
            Ptr::Entry(idx) => {
                self.entries[*idx] = entry.to_bytes().try_into().expect("unexpected size")
            },
            _ => panic!("wrong pointer type")
        }
    }
    /// If this is the root directory there may be a disk label entry
    pub fn find_label(&self) -> Option<Entry> {
        for i in 0..self.num_entries() {
            let ptr = Ptr::Entry(i);
            if self.get_type(&ptr)==EntryType::VolumeLabel {
                return Some(self.get_entry(&ptr));
            }
        }
        None
    }
    fn add_file(&self,ans: &mut BTreeMap<String,FileInfo>,fat_typ: usize,entry_idx: usize) -> Result<bool,DYNERR> {
        let entry = self.get_entry(&Ptr::Entry(entry_idx));    
        let (name,typ) = super::pack::file_name_to_split_string(entry.name, entry.ext);
        let key = [name.clone(),".".to_string(),typ.clone()].concat();
        trace!("entry in use: {}",key);
        if ans.contains_key(&key) {
            debug!("duplicate file {} in directory",key);
            return Err(Box::new(Error::DuplicateFile));
        }
        let mut cluster1 = u16::from_le_bytes(entry.cluster1_low) as usize;
        if fat_typ == 32 {
            // Don't add for FAT12/16 in case we have wrongly set bits here
            cluster1 += (u16::MAX as usize) * (u16::from_le_bytes(entry.cluster1_high) as usize);
        }
        let finfo: FileInfo = FileInfo {
            wildcard: String::new(),
            idx: entry_idx,
            name,
            typ,
            read_only: (entry.attr & READ_ONLY) > 0,
            hidden: (entry.attr & HIDDEN) > 0,
            system: (entry.attr & SYSTEM) > 0,
            volume_id: (entry.attr & VOLUME_ID) > 0,
            directory: (entry.attr & DIRECTORY) > 0,
            archived: (entry.attr & ARCHIVE) > 0,
            write_date: super::pack::unpack_date(entry.write_date),
            write_time: super::pack::unpack_time(entry.write_time,0),
            create_date: super::pack::unpack_date(entry.creation_date),
            create_time: super::pack::unpack_time(entry.creation_time,entry.creation_tenth),
            access_date: super::pack::unpack_date(entry.access_date),
            eof: u32::from_le_bytes(entry.file_size) as usize,
            cluster1: Some(Ptr::Cluster(cluster1))
        };
        ans.insert(key.clone(),finfo);
        Ok(super::pack::is_name_valid(&key))
    }
    /// Build an alphabetized map of file names to file info.
    pub fn build_files(&self,fat_typ: usize) -> Result<BTreeMap<String,FileInfo>,DYNERR> {
        let mut bad_names = 0;
        let mut ans = BTreeMap::new();
        // first pass collects everything except passwords
        for i in 0..self.num_entries() {
            let etyp = self.get_type(&Ptr::Entry(i));
            if etyp==EntryType::Free {
                continue;
            }
            if etyp==EntryType::FreeAndNoMore {
                break;
            }
            if bad_names > 2 {
                debug!("after {} bad file names rejecting disk",bad_names);
                return Err(Box::new(Error::Syntax));
            }
            if !self.add_file(&mut ans,fat_typ,i)? {
                bad_names += 1;
            }
        }
        Ok(ans)
    }
    /// Sort the files based on the order of appearance in the directory.
    /// Panics if there is a file with an empty entry list.
    pub fn sort_on_entry_index(&self,files: &BTreeMap<String,FileInfo>) -> BTreeMap<usize,FileInfo> {
        let mut ans = BTreeMap::new();
        for f in files.values() {
            ans.insert(f.idx,f.clone());
        }
        ans 
    }
 }

/// Search for a file in the map produced by `Directory::build_files`.
/// This will try with the given case, and then with upper case.
/// This will also handle empty extensions reliably.
pub fn get_file<'a>(name: &str,files: &'a BTreeMap<String,FileInfo>) -> Option<&'a FileInfo> {
    let mut trimmed = name.trim_end().to_string();
    if !name.contains(".") {
        trimmed += ".";
    }
    // the order of these attempts is significant
    if let Some(finfo) = files.get(&trimmed) {
        return Some(finfo);
    }
    if let Some(finfo) = files.get(&trimmed.to_uppercase()) {
        return Some(finfo);
    }
    return None;
}