axfs_ramfs/
lib.rs

1//! RAM filesystem used by [ArceOS](https://github.com/arceos-org/arceos).
2//!
3//! The implementation is based on [`axfs_vfs`].
4
5#![cfg_attr(not(test), no_std)]
6
7extern crate alloc;
8
9mod dir;
10mod file;
11
12#[cfg(test)]
13mod tests;
14
15pub use self::dir::DirNode;
16pub use self::file::FileNode;
17
18use alloc::sync::Arc;
19use axfs_vfs::{VfsNodeRef, VfsOps, VfsResult};
20use spin::once::Once;
21
22/// A RAM filesystem that implements [`axfs_vfs::VfsOps`].
23pub struct RamFileSystem {
24    parent: Once<VfsNodeRef>,
25    root: Arc<DirNode>,
26}
27
28impl RamFileSystem {
29    /// Create a new instance.
30    pub fn new() -> Self {
31        Self {
32            parent: Once::new(),
33            root: DirNode::new(None),
34        }
35    }
36
37    /// Returns the root directory node in [`Arc<DirNode>`](DirNode).
38    pub fn root_dir_node(&self) -> Arc<DirNode> {
39        self.root.clone()
40    }
41}
42
43impl VfsOps for RamFileSystem {
44    fn mount(&self, _path: &str, mount_point: VfsNodeRef) -> VfsResult {
45        if let Some(parent) = mount_point.parent() {
46            self.root.set_parent(Some(self.parent.call_once(|| parent)));
47        } else {
48            self.root.set_parent(None);
49        }
50        Ok(())
51    }
52
53    fn root_dir(&self) -> VfsNodeRef {
54        self.root.clone()
55    }
56}
57
58impl Default for RamFileSystem {
59    fn default() -> Self {
60        Self::new()
61    }
62}