1#![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
22pub struct RamFileSystem {
24 parent: Once<VfsNodeRef>,
25 root: Arc<DirNode>,
26}
27
28impl RamFileSystem {
29 pub fn new() -> Self {
31 Self {
32 parent: Once::new(),
33 root: DirNode::new(None),
34 }
35 }
36
37 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}