remotefs_memory/
inode.rs

1use std::path::PathBuf;
2use std::time::SystemTime;
3
4use remotefs::fs::{FileType, Metadata, UnixPex};
5
6/// Inode is the data stored in each node of the filesystem.
7#[derive(Debug, Clone)]
8pub struct Inode {
9    /// File metadata
10    pub(crate) metadata: Metadata,
11    /// File content; if the node is a directory, this field is `None`.
12    pub(crate) content: Option<Vec<u8>>,
13}
14
15impl Inode {
16    /// Create a new [`Inode`] with type **Directory** with the given metadata and content.
17    pub fn dir(uid: u32, gid: u32, mode: UnixPex) -> Self {
18        Self {
19            metadata: Metadata::default()
20                .uid(uid)
21                .gid(gid)
22                .file_type(FileType::Directory)
23                .created(SystemTime::now())
24                .accessed(SystemTime::now())
25                .mode(mode),
26            content: None,
27        }
28    }
29
30    /// Create a new [`Inode`] with type **File** with the given metadata and content.
31    pub fn file(uid: u32, gid: u32, mode: UnixPex, data: Vec<u8>) -> Self {
32        Self {
33            metadata: Metadata::default()
34                .uid(uid)
35                .gid(gid)
36                .file_type(FileType::File)
37                .created(SystemTime::now())
38                .accessed(SystemTime::now())
39                .mode(mode)
40                .size(data.len() as u64),
41            content: Some(data),
42        }
43    }
44
45    /// Create a new [`Inode`] with type **Symlink** with the given metadata and target.
46    pub fn symlink(uid: u32, gid: u32, target: PathBuf) -> Self {
47        Self {
48            metadata: Metadata::default()
49                .uid(uid)
50                .gid(gid)
51                .file_type(FileType::Symlink)
52                .created(SystemTime::now())
53                .accessed(SystemTime::now())
54                .mode(UnixPex::from(0o777))
55                .symlink(target.clone()),
56            content: Some(target.to_string_lossy().as_bytes().to_vec()),
57        }
58    }
59
60    /// Return the [`Metadata`] of the file.
61    pub fn metadata(&self) -> &Metadata {
62        &self.metadata
63    }
64
65    /// Return the content of the file.
66    pub fn content(&self) -> Option<&[u8]> {
67        self.content.as_deref()
68    }
69}