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
use core::convert::TryFrom;
use crate::blockdevice::BlockIdx;
use crate::fat::{FatType, OnDiskDirEntry};
use crate::filesystem::{Attributes, Cluster, ShortFileName, Timestamp};
#[cfg_attr(feature = "defmt-log", derive(defmt::Format))]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct DirEntry {
pub name: ShortFileName,
pub mtime: Timestamp,
pub ctime: Timestamp,
pub attributes: Attributes,
pub cluster: Cluster,
pub size: u32,
pub entry_block: BlockIdx,
pub entry_offset: u32,
}
#[cfg_attr(feature = "defmt-log", derive(defmt::Format))]
#[derive(Debug)]
pub struct Directory {
pub(crate) cluster: Cluster,
pub(crate) entry: Option<DirEntry>,
}
impl DirEntry {
pub(crate) fn serialize(&self, fat_type: FatType) -> [u8; OnDiskDirEntry::LEN] {
let mut data = [0u8; OnDiskDirEntry::LEN];
data[0..11].copy_from_slice(&self.name.contents);
data[11] = self.attributes.0;
data[14..18].copy_from_slice(&self.ctime.serialize_to_fat()[..]);
let cluster_number = self.cluster.0;
let cluster_hi = if fat_type == FatType::Fat16 {
[0u8; 2]
} else {
u16::try_from((cluster_number >> 16) & 0x0000_FFFF)
.unwrap()
.to_le_bytes()
};
data[20..22].copy_from_slice(&cluster_hi[..]);
data[22..26].copy_from_slice(&self.mtime.serialize_to_fat()[..]);
let cluster_lo = u16::try_from(cluster_number & 0x0000_FFFF)
.unwrap()
.to_le_bytes();
data[26..28].copy_from_slice(&cluster_lo[..]);
data[28..32].copy_from_slice(&self.size.to_le_bytes()[..]);
data
}
pub(crate) fn new(
name: ShortFileName,
attributes: Attributes,
cluster: Cluster,
ctime: Timestamp,
entry_block: BlockIdx,
entry_offset: u32,
) -> Self {
Self {
name,
mtime: ctime,
ctime,
attributes,
cluster,
size: 0,
entry_block,
entry_offset,
}
}
}
impl Directory {}