Skip to main content

vfat_rs/api/
metadata.rs

1use crate::ClusterId;
2use crate::PathBuf;
3use crate::api::raw_directory_entry::Attributes;
4use crate::api::timestamp::VfatTimestamp;
5use alloc::string::String;
6
7/// Metadatas are common to every entry type.
8#[derive(Debug, Clone)]
9pub struct Metadata {
10    creation: VfatTimestamp,
11    last_update: VfatTimestamp,
12    //last_access: VfatTimestamp,
13    pub(crate) name: String,
14    /// Size of this file in bytes. For directories, it should be the sum of the sizes
15    /// occupied by the metadatas of the contained files.
16    pub(crate) size: u32,
17    /// The path to this file - it does include the file name.
18    path: PathBuf,
19    /// empty files with size 0 should have first Cluster == 0.
20    pub(crate) cluster: ClusterId,
21    /// The path to this file - it doesn't include the file name.
22    parent: PathBuf,
23    pub(crate) attributes: Attributes,
24}
25
26impl Metadata {
27    pub(crate) fn new<S: AsRef<str>>(
28        creation: VfatTimestamp,
29        last_update: VfatTimestamp,
30        //last_access: VfatTimestamp,
31        name: S,
32        size: u32,
33        path: PathBuf,
34        cluster: ClusterId,
35        parent: PathBuf,
36        attributes: Attributes,
37    ) -> Self {
38        Self {
39            creation,
40            last_update,
41            //last_access,
42            name: String::from(name.as_ref()),
43            size,
44            path,
45            cluster,
46            parent,
47            attributes,
48        }
49    }
50}
51impl Metadata {
52    /// Returns the file size in bytes.
53    pub fn size(&self) -> usize {
54        self.size as usize
55    }
56
57    /*
58    fn last_access(&self) -> Option<VfatTimestamp> {
59        //Some(self.last_access)
60        None
61    }
62    */
63
64    pub(crate) fn last_update(&self) -> Option<VfatTimestamp> {
65        Some(self.last_update)
66    }
67    // TODO: why are these optional?
68    pub(crate) fn creation(&self) -> Option<VfatTimestamp> {
69        Some(self.creation)
70    }
71
72    /// Returns the full path to this entry (including the entry name).
73    pub fn full_path(&self) -> &PathBuf {
74        &self.path
75    }
76    pub(crate) fn parent(&self) -> &PathBuf {
77        &self.parent
78    }
79    /// Returns the entry's name (file or directory name, without path).
80    pub fn name(&self) -> &str {
81        &self.name
82    }
83    pub(crate) fn has_no_cluster_allocated(&self) -> bool {
84        self.cluster == ClusterId::new(0)
85    }
86}