Skip to main content

remotefs_memory/
inode.rs

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