Skip to content

Commit

Permalink
distinguish root_inode() & mountpoint_root_inode() in mountfs
Browse files Browse the repository at this point in the history
  • Loading branch information
equation314 authored and guzongmin committed Nov 18, 2021
1 parent 745ebee commit 6eaf1d2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
20 changes: 13 additions & 7 deletions rcore-fs-mountfs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl MountFS {
}

/// Strong type version of `root_inode`
pub fn root_inode(&self) -> Arc<MNode> {
pub fn mountpoint_root_inode(&self) -> Arc<MNode> {
MNode {
inode: self.inner.root_inode(),
vfs: self.self_ref.upgrade().unwrap(),
Expand All @@ -94,18 +94,21 @@ impl MNode {

/// Mount file system `fs` at this INode
pub fn mount(&self, fs: Arc<dyn FileSystem>) -> Result<Arc<MountFS>> {
let metadata = self.inode.metadata()?;
if metadata.type_ != FileType::Dir {
return Err(FsError::NotDir);
}
let new_fs = MountFS {
inner: fs,
mountpoints: RwLock::new(BTreeMap::new()),
self_mountpoint: Some(self.self_ref.upgrade().unwrap()),
self_ref: Weak::default(),
}
.wrap();
let inode_id = self.inode.metadata()?.inode;
self.vfs
.mountpoints
.write()
.insert(inode_id, new_fs.clone());
.insert(metadata.inode, new_fs.clone());
Ok(new_fs)
}

Expand All @@ -114,14 +117,14 @@ impl MNode {
fn overlaid_inode(&self) -> Arc<MNode> {
let inode_id = self.metadata().unwrap().inode;
if let Some(sub_vfs) = self.vfs.mountpoints.read().get(&inode_id) {
sub_vfs.root_inode()
sub_vfs.mountpoint_root_inode()
} else {
self.self_ref.upgrade().unwrap()
}
}

/// Is the root INode of its FS?
fn is_root(&self) -> bool {
fn is_mountpoint_root(&self) -> bool {
self.inode.fs().root_inode().metadata().unwrap().inode
== self.inode.metadata().unwrap().inode
}
Expand Down Expand Up @@ -149,7 +152,7 @@ impl MNode {
// TODO: check going up.
if root {
Ok(self.self_ref.upgrade().unwrap())
} else if self.is_root() {
} else if self.is_mountpoint_root() {
// Here is mountpoint.
match &self.vfs.self_mountpoint {
Some(inode) => inode.find(root, ".."),
Expand Down Expand Up @@ -212,7 +215,10 @@ impl FileSystem for MountFS {
}

fn root_inode(&self) -> Arc<dyn INode> {
self.root_inode()
match &self.self_mountpoint {
Some(inode) => inode.vfs.root_inode(),
None => self.mountpoint_root_inode(),
}
}

fn info(&self) -> FsInfo {
Expand Down
14 changes: 7 additions & 7 deletions rcore-fs-mountfs/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@ use rcore_fs_ramfs::RamFS;

#[test]
fn mount() {
let rootfs = MountFS::new(RamFS::new()) as Arc<dyn FileSystem>;
let root = rootfs.root_inode();
let rootfs = MountFS::new(RamFS::new());
let root = rootfs.mountpoint_root_inode();
let mnt = root.create("mnt", FileType::Dir, 0o777).unwrap();

let ramfs = RamFS::new();
let root1 = ramfs.root_inode();
root1.create("file", FileType::File, 0o777).unwrap();

mnt.downcast_ref::<MNode>().unwrap().mount(ramfs).unwrap();
assert!(mnt.find("file").is_ok());
mnt.mount(ramfs).unwrap();
assert!((mnt as Arc<dyn INode>).find("file").is_ok());
assert!(root.lookup("mnt/file").is_ok());
}

#[test]
fn remove_busy() {
let rootfs = MountFS::new(RamFS::new()) as Arc<dyn FileSystem>;
let root = rootfs.root_inode();
let rootfs = MountFS::new(RamFS::new());
let root = rootfs.mountpoint_root_inode();
let mnt = root.create("mnt", FileType::Dir, 0o777).unwrap();
let ramfs = RamFS::new();
mnt.downcast_ref::<MNode>().unwrap().mount(ramfs).unwrap();
mnt.mount(ramfs).unwrap();
assert_eq!(root.unlink("mnt"), Err(FsError::Busy));
}
2 changes: 1 addition & 1 deletion rcore-fs/src/vfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ pub trait INode: Any + Sync + Send {
rest_path = String::from(rest_path[pos + 1..].trim_start_matches('/'));
}
};
if name == "" {
if name.is_empty() {
continue;
}
let inode = result.find(&name)?;
Expand Down

0 comments on commit 6eaf1d2

Please sign in to comment.