From 51cbe5c1297baebd7ccfe8aadc8c07b07339b878 Mon Sep 17 00:00:00 2001 From: WangRunji Date: Mon, 8 Jul 2019 16:31:38 +0800 Subject: [PATCH] support ramfs in rcore-fs-fuse. fix ramfs INode ID. --- rcore-fs-fuse/Cargo.toml | 1 + rcore-fs-fuse/src/fuse.rs | 1 - rcore-fs-fuse/src/lib.rs | 3 +++ rcore-fs-fuse/src/main.rs | 6 +++++- rcore-fs-ramfs/src/lib.rs | 38 +++++++++++++++----------------------- rcore-fs/src/vfs.rs | 2 +- 6 files changed, 25 insertions(+), 26 deletions(-) diff --git a/rcore-fs-fuse/Cargo.toml b/rcore-fs-fuse/Cargo.toml index 87402da..ff8fad0 100644 --- a/rcore-fs-fuse/Cargo.toml +++ b/rcore-fs-fuse/Cargo.toml @@ -18,3 +18,4 @@ git-version = "0.3" rcore-fs = { path = "../rcore-fs", features = ["std"] } rcore-fs-sfs = { path = "../rcore-fs-sfs" } rcore-fs-sefs = { path = "../rcore-fs-sefs", features = ["std"] } +rcore-fs-ramfs = { path = "../rcore-fs-ramfs" } diff --git a/rcore-fs-fuse/src/fuse.rs b/rcore-fs-fuse/src/fuse.rs index a57efd2..86193b8 100644 --- a/rcore-fs-fuse/src/fuse.rs +++ b/rcore-fs-fuse/src/fuse.rs @@ -6,7 +6,6 @@ use rcore_fs::vfs; use std::collections::btree_map::BTreeMap; use std::ffi::OsStr; use std::sync::Arc; -//use log::*; use time::Timespec; const TTL: Timespec = Timespec { sec: 1, nsec: 0 }; // 1 second diff --git a/rcore-fs-fuse/src/lib.rs b/rcore-fs-fuse/src/lib.rs index a8a7cef..31772d4 100644 --- a/rcore-fs-fuse/src/lib.rs +++ b/rcore-fs-fuse/src/lib.rs @@ -1,3 +1,6 @@ +#[macro_use] +extern crate log; + #[cfg(feature = "use_fuse")] pub mod fuse; pub mod zip; diff --git a/rcore-fs-fuse/src/main.rs b/rcore-fs-fuse/src/main.rs index 8e8e00c..c2a11d1 100644 --- a/rcore-fs-fuse/src/main.rs +++ b/rcore-fs-fuse/src/main.rs @@ -11,6 +11,7 @@ use rcore_fs_fuse::fuse::VfsFuse; use rcore_fs_fuse::zip::{unzip_dir, zip_dir}; use rcore_fs_sefs as sefs; use rcore_fs_sfs as sfs; +use rcore_fs_ramfs as ramfs; use git_version::git_version; @@ -28,7 +29,7 @@ struct Opt { #[structopt(parse(from_os_str))] dir: PathBuf, - /// File system: [sfs | sefs] + /// File system: [sfs | sefs | ramfs] #[structopt(short = "f", long = "fs", default_value = "sfs")] fs: String, } @@ -95,6 +96,9 @@ fn main() { .expect("failed to open sefs"), } } + "ramfs" => { + ramfs::RamFS::new() + } _ => panic!("unsupported file system"), }; match opt.cmd { diff --git a/rcore-fs-ramfs/src/lib.rs b/rcore-fs-ramfs/src/lib.rs index 625e8b2..e7e90ab 100644 --- a/rcore-fs-ramfs/src/lib.rs +++ b/rcore-fs-ramfs/src/lib.rs @@ -51,7 +51,7 @@ impl RamFS { content: Vec::new(), extra: Metadata { dev: 0, - inode: 0, + inode: new_inode_id(), size: 0, blk_size: 0, blocks: 0, @@ -59,7 +59,7 @@ impl RamFS { mtime: Timespec { sec: 0, nsec: 0 }, ctime: Timespec { sec: 0, nsec: 0 }, type_: FileType::Dir, - mode: 0, + mode: 0o777, nlinks: 1, uid: 0, gid: 0, @@ -137,24 +137,9 @@ impl INode for LockedINode { fn metadata(&self) -> Result { let file = self.0.read(); - let extra = &file.extra; - let size = file.content.len(); - Ok(Metadata { - dev: 0, - inode: extra.inode, - size: size, - blk_size: 4096, - blocks: size / 4096, - atime: extra.atime, - mtime: extra.mtime, - ctime: extra.ctime, - type_: extra.type_, - mode: extra.mode, - nlinks: extra.nlinks, - uid: extra.uid, - gid: extra.gid, - rdev: extra.rdev, - }) + let mut metadata = file.extra.clone(); + metadata.size = file.content.len(); + Ok(metadata) } fn set_metadata(&self, metadata: &Metadata) -> Result<()> { @@ -208,16 +193,16 @@ impl INode for LockedINode { content: Vec::new(), extra: Metadata { dev: 0, - inode: 0, + inode: new_inode_id(), size: 0, blk_size: 0, blocks: 0, atime: Timespec { sec: 0, nsec: 0 }, mtime: Timespec { sec: 0, nsec: 0 }, ctime: Timespec { sec: 0, nsec: 0 }, - type_: type_, + type_, mode: mode as u16, - nlinks: 0, + nlinks: 1, uid: 0, gid: 0, rdev: data, @@ -341,3 +326,10 @@ fn lock_multiple<'a>(locks: &[&'a RwLock]) -> Vec usize { + use core::sync::atomic::*; + static ID: AtomicUsize = AtomicUsize::new(1); + ID.fetch_add(1, Ordering::SeqCst) +} diff --git a/rcore-fs/src/vfs.rs b/rcore-fs/src/vfs.rs index 0aa974a..30fa01d 100644 --- a/rcore-fs/src/vfs.rs +++ b/rcore-fs/src/vfs.rs @@ -164,7 +164,7 @@ pub struct PollStatus { /// Metadata of INode /// /// Ref: [http://pubs.opengroup.org/onlinepubs/009604499/basedefs/sys/stat.h.html] -#[derive(Debug, Eq, PartialEq)] +#[derive(Debug, Eq, PartialEq, Clone)] pub struct Metadata { /// Device ID pub dev: usize, // (major << 8) | minor