From 29096ccb61fe961fa88ee32f35805a0ff62469f9 Mon Sep 17 00:00:00 2001 From: WangRunji Date: Mon, 8 Jul 2019 16:36:31 +0800 Subject: [PATCH] add explicit 'dyn' --- rcore-fs-ext2/src/lib.rs | 6 ++--- rcore-fs-fuse/src/main.rs | 8 +++---- rcore-fs-fuse/src/zip.rs | 4 ++-- rcore-fs-sefs/src/dev/mod.rs | 4 ++-- rcore-fs-sefs/src/dev/std_impl.rs | 4 ++-- rcore-fs-sefs/src/lib.rs | 37 ++++++++++++++++++------------- rcore-fs-sfs/src/lib.rs | 35 ++++++++++++++++------------- rcore-fs-sfs/src/structs.rs | 4 ++-- rcore-fs/src/file.rs | 4 ++-- rcore-fs/src/vfs.rs | 32 +++++++++++++++----------- 10 files changed, 76 insertions(+), 62 deletions(-) diff --git a/rcore-fs-ext2/src/lib.rs b/rcore-fs-ext2/src/lib.rs index d1493b3..0fb8963 100644 --- a/rcore-fs-ext2/src/lib.rs +++ b/rcore-fs-ext2/src/lib.rs @@ -23,7 +23,7 @@ use rcore_fs::vfs; #[derive(Clone)] struct Ext2Volume { - inner: Arc, + inner: Arc, } #[derive(Clone)] @@ -67,11 +67,11 @@ impl core::convert::From for Ext2Error { } impl Ext2FileSystem { - pub fn open(device: Arc) -> vfs::Result> { + pub fn open(device: Arc) -> vfs::Result> { Ok(Self::open_internal(device)?) } - fn open_internal(device: Arc) -> Result, Ext2Error> { + fn open_internal(device: Arc) -> Result, Ext2Error> { let volume = Ext2Volume { inner: device }; let fs = Synced::new(volume.clone())?; Ok(Arc::new(Ext2FileSystem { inner: fs, volume })) diff --git a/rcore-fs-fuse/src/main.rs b/rcore-fs-fuse/src/main.rs index c2a11d1..60e3630 100644 --- a/rcore-fs-fuse/src/main.rs +++ b/rcore-fs-fuse/src/main.rs @@ -9,9 +9,9 @@ use rcore_fs::vfs::FileSystem; #[cfg(feature = "use_fuse")] use rcore_fs_fuse::fuse::VfsFuse; use rcore_fs_fuse::zip::{unzip_dir, zip_dir}; +use rcore_fs_ramfs as ramfs; use rcore_fs_sefs as sefs; use rcore_fs_sfs as sfs; -use rcore_fs_ramfs as ramfs; use git_version::git_version; @@ -69,7 +69,7 @@ fn main() { } }; - let fs: Arc = match opt.fs.as_str() { + let fs: Arc = match opt.fs.as_str() { "sfs" => { let file = OpenOptions::new() .read(true) @@ -96,9 +96,7 @@ fn main() { .expect("failed to open sefs"), } } - "ramfs" => { - ramfs::RamFS::new() - } + "ramfs" => ramfs::RamFS::new(), _ => panic!("unsupported file system"), }; match opt.cmd { diff --git a/rcore-fs-fuse/src/zip.rs b/rcore-fs-fuse/src/zip.rs index 6ad3a71..5111432 100644 --- a/rcore-fs-fuse/src/zip.rs +++ b/rcore-fs-fuse/src/zip.rs @@ -12,7 +12,7 @@ use rcore_fs::vfs::{FileType, INode}; const DEFAULT_MODE: u32 = 0o664; const BUF_SIZE: usize = 0x1000; -pub fn zip_dir(path: &Path, inode: Arc) -> Result<(), Box> { +pub fn zip_dir(path: &Path, inode: Arc) -> Result<(), Box> { let dir = fs::read_dir(path)?; for entry in dir { let entry = entry?; @@ -45,7 +45,7 @@ pub fn zip_dir(path: &Path, inode: Arc) -> Result<(), Box> { Ok(()) } -pub fn unzip_dir(path: &Path, inode: Arc) -> Result<(), Box> { +pub fn unzip_dir(path: &Path, inode: Arc) -> Result<(), Box> { let files = inode.list()?; for name in files.iter().skip(2) { let inode = inode.lookup(name.as_str())?; diff --git a/rcore-fs-sefs/src/dev/mod.rs b/rcore-fs-sefs/src/dev/mod.rs index 984611c..57cf4fb 100644 --- a/rcore-fs-sefs/src/dev/mod.rs +++ b/rcore-fs-sefs/src/dev/mod.rs @@ -36,8 +36,8 @@ pub trait File: Send + Sync { /// The collection of all files in the FS. pub trait Storage: Send + Sync { - fn open(&self, file_id: usize) -> DevResult>; - fn create(&self, file_id: usize) -> DevResult>; + fn open(&self, file_id: usize) -> DevResult>; + fn create(&self, file_id: usize) -> DevResult>; fn remove(&self, file_id: usize) -> DevResult<()>; } diff --git a/rcore-fs-sefs/src/dev/std_impl.rs b/rcore-fs-sefs/src/dev/std_impl.rs index 6c1ae05..e6beaba 100644 --- a/rcore-fs-sefs/src/dev/std_impl.rs +++ b/rcore-fs-sefs/src/dev/std_impl.rs @@ -20,14 +20,14 @@ impl StdStorage { } impl super::Storage for StdStorage { - fn open(&self, file_id: usize) -> DevResult> { + fn open(&self, file_id: usize) -> DevResult> { let mut path = self.path.to_path_buf(); path.push(format!("{}", file_id)); let file = OpenOptions::new().read(true).write(true).open(path)?; Ok(Box::new(Mutex::new(file))) } - fn create(&self, file_id: usize) -> DevResult> { + fn create(&self, file_id: usize) -> DevResult> { let mut path = self.path.to_path_buf(); path.push(format!("{}", file_id)); let file = OpenOptions::new() diff --git a/rcore-fs-sefs/src/lib.rs b/rcore-fs-sefs/src/lib.rs index 91c8d27..9210889 100644 --- a/rcore-fs-sefs/src/lib.rs +++ b/rcore-fs-sefs/src/lib.rs @@ -28,7 +28,7 @@ pub mod dev; mod structs; /// Helper methods for `File` -impl File { +impl dyn File { fn read_block(&self, id: BlockId, buf: &mut [u8]) -> DevResult<()> { assert!(buf.len() <= BLKSIZE); self.read_exact_at(buf, id * BLKSIZE) @@ -60,7 +60,7 @@ pub struct INodeImpl { /// on-disk inode disk_inode: RwLock>, /// back file - file: Box, + file: Box, /// Reference to FS fs: Arc, } @@ -232,7 +232,12 @@ impl vfs::INode for INodeImpl { self.disk_inode.write().size = len as u32; Ok(()) } - fn create(&self, name: &str, type_: vfs::FileType, mode: u32) -> vfs::Result> { + fn create( + &self, + name: &str, + type_: vfs::FileType, + mode: u32, + ) -> vfs::Result> { let type_ = match type_ { vfs::FileType::File => FileType::File, vfs::FileType::Dir => FileType::Dir, @@ -309,7 +314,7 @@ impl vfs::INode for INodeImpl { Ok(()) } - fn link(&self, name: &str, other: &Arc) -> vfs::Result<()> { + fn link(&self, name: &str, other: &Arc) -> vfs::Result<()> { let info = self.metadata()?; if info.type_ != vfs::FileType::Dir { return Err(FsError::NotDir); @@ -337,7 +342,7 @@ impl vfs::INode for INodeImpl { child.nlinks_inc(); Ok(()) } - fn move_(&self, old_name: &str, target: &Arc, new_name: &str) -> vfs::Result<()> { + fn move_(&self, old_name: &str, target: &Arc, new_name: &str) -> vfs::Result<()> { let info = self.metadata()?; if info.type_ != vfs::FileType::Dir { return Err(FsError::NotDir); @@ -398,7 +403,7 @@ impl vfs::INode for INodeImpl { Ok(()) } - fn find(&self, name: &str) -> vfs::Result> { + fn find(&self, name: &str) -> vfs::Result> { let info = self.metadata()?; if info.type_ != vfs::FileType::Dir { return Err(FsError::NotDir); @@ -419,10 +424,10 @@ impl vfs::INode for INodeImpl { fn io_control(&self, _cmd: u32, _data: usize) -> vfs::Result<()> { Err(FsError::NotSupported) } - fn fs(&self) -> Arc { + fn fs(&self) -> Arc { self.fs.clone() } - fn as_any_ref(&self) -> &Any { + fn as_any_ref(&self) -> &dyn Any { self } } @@ -449,11 +454,11 @@ pub struct SEFS { /// inode list inodes: RwLock>>, /// device - device: Box, + device: Box, /// metadata file - meta_file: Box, + meta_file: Box, /// Time provider - time_provider: &'static TimeProvider, + time_provider: &'static dyn TimeProvider, /// Pointer to self, used by INodes self_ptr: Weak, } @@ -461,8 +466,8 @@ pub struct SEFS { impl SEFS { /// Load SEFS pub fn open( - device: Box, - time_provider: &'static TimeProvider, + device: Box, + time_provider: &'static dyn TimeProvider, ) -> vfs::Result> { let meta_file = device.open(0)?; let super_block = meta_file.load_struct::(BLKN_SUPER)?; @@ -496,8 +501,8 @@ impl SEFS { } /// Create a new SEFS pub fn create( - device: Box, - time_provider: &'static TimeProvider, + device: Box, + time_provider: &'static dyn TimeProvider, ) -> vfs::Result> { let blocks = BLKBITS; @@ -683,7 +688,7 @@ impl vfs::FileSystem for SEFS { Ok(()) } - fn root_inode(&self) -> Arc { + fn root_inode(&self) -> Arc { self.get_inode(BLKN_ROOT) } diff --git a/rcore-fs-sfs/src/lib.rs b/rcore-fs-sfs/src/lib.rs index 1587313..3ce9c9c 100644 --- a/rcore-fs-sfs/src/lib.rs +++ b/rcore-fs-sfs/src/lib.rs @@ -7,7 +7,6 @@ extern crate alloc; extern crate log; use alloc::{ - boxed::Box, collections::BTreeMap, string::String, sync::{Arc, Weak}, @@ -19,7 +18,7 @@ use core::fmt::{Debug, Error, Formatter}; use core::mem::uninitialized; use bitvec::BitVec; -use spin::{RwLock, RwLockReadGuard, RwLockWriteGuard}; +use spin::RwLock; use rcore_fs::dev::Device; use rcore_fs::dirty::Dirty; @@ -55,7 +54,7 @@ trait DeviceExt: Device { } } -impl DeviceExt for Device {} +impl DeviceExt for dyn Device {} /// INode for SFS pub struct INodeImpl { @@ -322,7 +321,7 @@ impl INodeImpl { /// Read/Write content, no matter what type it is fn _io_at(&self, begin: usize, end: usize, mut f: F) -> vfs::Result where - F: FnMut(&Arc, &BlockRange, usize) -> vfs::Result<()>, + F: FnMut(&Arc, &BlockRange, usize) -> vfs::Result<()>, { let size = self.disk_inode.read().size as usize; let iter = BlockIter { @@ -394,7 +393,7 @@ impl INodeImpl { id: child.id as u32, name: Str256::from(name), }; - let mut disk_inode = self.disk_inode.write(); + let disk_inode = self.disk_inode.write(); let old_size = disk_inode.size as usize; self._resize(old_size + BLKSIZE)?; self._write_at(old_size, entry.as_buf()).unwrap(); @@ -498,7 +497,13 @@ impl vfs::INode for INodeImpl { } self._resize(len) } - fn create2(&self, name: &str, type_: vfs::FileType, _mode: u32, data: usize)->vfs::Result>{ + fn create2( + &self, + name: &str, + type_: vfs::FileType, + _mode: u32, + data: usize, + ) -> vfs::Result> { let info = self.metadata()?; if info.type_ != vfs::FileType::Dir { return Err(FsError::NotDir); @@ -535,7 +540,7 @@ impl vfs::INode for INodeImpl { Ok(inode) } - fn link(&self, name: &str, other: &Arc) -> vfs::Result<()> { + fn link(&self, name: &str, other: &Arc) -> vfs::Result<()> { let info = self.metadata()?; if info.type_ != vfs::FileType::Dir { return Err(FsError::NotDir); @@ -598,7 +603,7 @@ impl vfs::INode for INodeImpl { Ok(()) } - fn move_(&self, old_name: &str, target: &Arc, new_name: &str) -> vfs::Result<()> { + fn move_(&self, old_name: &str, target: &Arc, new_name: &str) -> vfs::Result<()> { let info = self.metadata()?; if info.type_ != vfs::FileType::Dir { return Err(FsError::NotDir); @@ -658,7 +663,7 @@ impl vfs::INode for INodeImpl { } Ok(()) } - fn find(&self, name: &str) -> vfs::Result> { + fn find(&self, name: &str) -> vfs::Result> { let info = self.metadata()?; if info.type_ != vfs::FileType::Dir { return Err(FsError::NotDir); @@ -690,10 +695,10 @@ impl vfs::INode for INodeImpl { } } } - fn fs(&self) -> Arc { + fn fs(&self) -> Arc { self.fs.clone() } - fn as_any_ref(&self) -> &Any { + fn as_any_ref(&self) -> &dyn Any { self } } @@ -725,7 +730,7 @@ pub struct SimpleFileSystem { /// inode list inodes: RwLock>>, /// device - device: Arc, + device: Arc, /// Pointer to self, used by INodes self_ptr: Weak, /// device inode @@ -734,7 +739,7 @@ pub struct SimpleFileSystem { impl SimpleFileSystem { /// Load SFS from device - pub fn open(device: Arc) -> vfs::Result> { + pub fn open(device: Arc) -> vfs::Result> { let super_block = device.load_struct::(BLKN_SUPER)?; if !super_block.check() { return Err(FsError::WrongFs); @@ -759,7 +764,7 @@ impl SimpleFileSystem { .wrap()) } /// Create a new SFS on blank disk - pub fn create(device: Arc, space: usize) -> vfs::Result> { + pub fn create(device: Arc, space: usize) -> vfs::Result> { let blocks = (space + BLKSIZE - 1) / BLKSIZE; let freemap_blocks = (space + BLKBITS * BLKSIZE - 1) / BLKBITS / BLKSIZE; assert!(blocks >= 16, "space too small"); @@ -945,7 +950,7 @@ impl vfs::FileSystem for SimpleFileSystem { Ok(()) } - fn root_inode(&self) -> Arc { + fn root_inode(&self) -> Arc { self.get_inode(BLKN_ROOT) // let root = self.get_inode(BLKN_ROOT); // root.create("dev", vfs::FileType::Dir, 0).expect("fail to create dev"); // what's mode? diff --git a/rcore-fs-sfs/src/structs.rs b/rcore-fs-sfs/src/structs.rs index 937eb51..c0fcaaa 100644 --- a/rcore-fs-sfs/src/structs.rs +++ b/rcore-fs-sfs/src/structs.rs @@ -2,7 +2,7 @@ use crate::vfs; use alloc::str; -use core::any::Any; + use core::fmt::{Debug, Error, Formatter}; use core::mem::{size_of, size_of_val}; use core::slice; @@ -55,7 +55,7 @@ pub trait DeviceINode : Any + Sync + Send{ } */ -pub type DeviceINode = vfs::INode; +pub type DeviceINode = dyn vfs::INode; #[repr(C)] pub struct IndirectBlock { diff --git a/rcore-fs/src/file.rs b/rcore-fs/src/file.rs index 7ad18f5..e5acf84 100644 --- a/rcore-fs/src/file.rs +++ b/rcore-fs/src/file.rs @@ -2,14 +2,14 @@ use crate::vfs::{INode, Metadata, Result}; use alloc::{string::String, sync::Arc}; pub struct File { - inode: Arc, + inode: Arc, offset: usize, readable: bool, writable: bool, } impl File { - pub fn new(inode: Arc, readable: bool, writable: bool) -> Self { + pub fn new(inode: Arc, readable: bool, writable: bool) -> Self { File { inode, offset: 0, diff --git a/rcore-fs/src/vfs.rs b/rcore-fs/src/vfs.rs index 30fa01d..e725dc4 100644 --- a/rcore-fs/src/vfs.rs +++ b/rcore-fs/src/vfs.rs @@ -32,27 +32,33 @@ pub trait INode: Any + Sync + Send { fn resize(&self, len: usize) -> Result<()>; /// Create a new INode in the directory - fn create(&self, name: &str, type_: FileType, mode: u32) -> Result>{ + fn create(&self, name: &str, type_: FileType, mode: u32) -> Result> { self.create2(name, type_, mode, 0) } /// Create a new INode in the directory, with a data field for usages like device file. - fn create2(&self, name: &str, type_: FileType, mode: u32, data: usize) -> Result>{ + fn create2( + &self, + name: &str, + type_: FileType, + mode: u32, + _data: usize, + ) -> Result> { self.create(name, type_, mode) } /// Create a hard link `name` to `other` - fn link(&self, name: &str, other: &Arc) -> Result<()>; + fn link(&self, name: &str, other: &Arc) -> Result<()>; /// Delete a hard link `name` fn unlink(&self, name: &str) -> Result<()>; /// Move INode `self/old_name` to `target/new_name`. /// If `target` equals `self`, do rename. - fn move_(&self, old_name: &str, target: &Arc, new_name: &str) -> Result<()>; + fn move_(&self, old_name: &str, target: &Arc, new_name: &str) -> Result<()>; /// Find the INode `name` in the directory - fn find(&self, name: &str) -> Result>; + fn find(&self, name: &str) -> Result>; /// Get the name of directory entry fn get_entry(&self, id: usize) -> Result; @@ -61,14 +67,14 @@ pub trait INode: Any + Sync + Send { fn io_control(&self, cmd: u32, data: usize) -> Result<()>; /// Get the file system of the INode - fn fs(&self) -> Arc; + fn fs(&self) -> Arc; /// This is used to implement dynamics cast. /// Simply return self in the implement of the function. - fn as_any_ref(&self) -> &Any; + fn as_any_ref(&self) -> &dyn Any; } -impl INode { +impl dyn INode { /// Downcast the INode to specific struct pub fn downcast_ref(&self) -> Option<&T> { self.as_any_ref().downcast_ref::() @@ -88,12 +94,12 @@ impl INode { } /// Lookup path from current INode, and do not follow symlinks - pub fn lookup(&self, path: &str) -> Result> { + pub fn lookup(&self, path: &str) -> Result> { self.lookup_follow(path, 0) } /// Lookup path from current INode, and follow symlinks at most `follow_times` times - pub fn lookup_follow(&self, path: &str, mut follow_times: usize) -> Result> { + pub fn lookup_follow(&self, path: &str, mut follow_times: usize) -> Result> { if self.metadata()?.type_ != FileType::Dir { return Err(FsError::NotDir); } @@ -263,7 +269,7 @@ pub enum FsError { DeviceError, IOCTLError, NoDevice, - Again, // E_AGAIN, when no data is available, never happens in fs + Again, // E_AGAIN, when no data is available, never happens in fs SymLoop, //E_LOOP } @@ -285,12 +291,12 @@ impl std::error::Error for FsError {} pub type Result = result::Result; /// Abstract file system -pub trait FileSystem: Sync+Send { +pub trait FileSystem: Sync + Send { /// Sync all data to the storage fn sync(&self) -> Result<()>; /// Get the root INode of the file system - fn root_inode(&self) -> Arc; + fn root_inode(&self) -> Arc; /// Get the file system information fn info(&self) -> FsInfo;