add explicit 'dyn'

master
WangRunji 5 years ago
parent 51cbe5c129
commit 29096ccb61

@ -23,7 +23,7 @@ use rcore_fs::vfs;
#[derive(Clone)]
struct Ext2Volume {
inner: Arc<Device>,
inner: Arc<dyn Device>,
}
#[derive(Clone)]
@ -67,11 +67,11 @@ impl core::convert::From<DevError> for Ext2Error {
}
impl Ext2FileSystem {
pub fn open(device: Arc<Device>) -> vfs::Result<Arc<Self>> {
pub fn open(device: Arc<dyn Device>) -> vfs::Result<Arc<Self>> {
Ok(Self::open_internal(device)?)
}
fn open_internal(device: Arc<Device>) -> Result<Arc<Self>, Ext2Error> {
fn open_internal(device: Arc<dyn Device>) -> Result<Arc<Self>, Ext2Error> {
let volume = Ext2Volume { inner: device };
let fs = Synced::new(volume.clone())?;
Ok(Arc::new(Ext2FileSystem { inner: fs, volume }))

@ -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<FileSystem> = match opt.fs.as_str() {
let fs: Arc<dyn FileSystem> = 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 {

@ -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<INode>) -> Result<(), Box<Error>> {
pub fn zip_dir(path: &Path, inode: Arc<dyn INode>) -> Result<(), Box<dyn Error>> {
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<INode>) -> Result<(), Box<Error>> {
Ok(())
}
pub fn unzip_dir(path: &Path, inode: Arc<INode>) -> Result<(), Box<Error>> {
pub fn unzip_dir(path: &Path, inode: Arc<dyn INode>) -> Result<(), Box<dyn Error>> {
let files = inode.list()?;
for name in files.iter().skip(2) {
let inode = inode.lookup(name.as_str())?;

@ -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<Box<File>>;
fn create(&self, file_id: usize) -> DevResult<Box<File>>;
fn open(&self, file_id: usize) -> DevResult<Box<dyn File>>;
fn create(&self, file_id: usize) -> DevResult<Box<dyn File>>;
fn remove(&self, file_id: usize) -> DevResult<()>;
}

@ -20,14 +20,14 @@ impl StdStorage {
}
impl super::Storage for StdStorage {
fn open(&self, file_id: usize) -> DevResult<Box<super::File>> {
fn open(&self, file_id: usize) -> DevResult<Box<dyn super::File>> {
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<Box<super::File>> {
fn create(&self, file_id: usize) -> DevResult<Box<dyn super::File>> {
let mut path = self.path.to_path_buf();
path.push(format!("{}", file_id));
let file = OpenOptions::new()

@ -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<Dirty<DiskINode>>,
/// back file
file: Box<File>,
file: Box<dyn File>,
/// Reference to FS
fs: Arc<SEFS>,
}
@ -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<Arc<vfs::INode>> {
fn create(
&self,
name: &str,
type_: vfs::FileType,
mode: u32,
) -> vfs::Result<Arc<dyn vfs::INode>> {
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<INode>) -> vfs::Result<()> {
fn link(&self, name: &str, other: &Arc<dyn INode>) -> 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<INode>, new_name: &str) -> vfs::Result<()> {
fn move_(&self, old_name: &str, target: &Arc<dyn INode>, 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<Arc<vfs::INode>> {
fn find(&self, name: &str) -> vfs::Result<Arc<dyn vfs::INode>> {
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<vfs::FileSystem> {
fn fs(&self) -> Arc<dyn vfs::FileSystem> {
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<BTreeMap<INodeId, Weak<INodeImpl>>>,
/// device
device: Box<Storage>,
device: Box<dyn Storage>,
/// metadata file
meta_file: Box<File>,
meta_file: Box<dyn File>,
/// Time provider
time_provider: &'static TimeProvider,
time_provider: &'static dyn TimeProvider,
/// Pointer to self, used by INodes
self_ptr: Weak<SEFS>,
}
@ -461,8 +466,8 @@ pub struct SEFS {
impl SEFS {
/// Load SEFS
pub fn open(
device: Box<Storage>,
time_provider: &'static TimeProvider,
device: Box<dyn Storage>,
time_provider: &'static dyn TimeProvider,
) -> vfs::Result<Arc<Self>> {
let meta_file = device.open(0)?;
let super_block = meta_file.load_struct::<SuperBlock>(BLKN_SUPER)?;
@ -496,8 +501,8 @@ impl SEFS {
}
/// Create a new SEFS
pub fn create(
device: Box<Storage>,
time_provider: &'static TimeProvider,
device: Box<dyn Storage>,
time_provider: &'static dyn TimeProvider,
) -> vfs::Result<Arc<Self>> {
let blocks = BLKBITS;
@ -683,7 +688,7 @@ impl vfs::FileSystem for SEFS {
Ok(())
}
fn root_inode(&self) -> Arc<vfs::INode> {
fn root_inode(&self) -> Arc<dyn vfs::INode> {
self.get_inode(BLKN_ROOT)
}

@ -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<F>(&self, begin: usize, end: usize, mut f: F) -> vfs::Result<usize>
where
F: FnMut(&Arc<Device>, &BlockRange, usize) -> vfs::Result<()>,
F: FnMut(&Arc<dyn Device>, &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<Arc<vfs::INode>>{
fn create2(
&self,
name: &str,
type_: vfs::FileType,
_mode: u32,
data: usize,
) -> vfs::Result<Arc<dyn vfs::INode>> {
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<INode>) -> vfs::Result<()> {
fn link(&self, name: &str, other: &Arc<dyn INode>) -> 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<INode>, new_name: &str) -> vfs::Result<()> {
fn move_(&self, old_name: &str, target: &Arc<dyn INode>, 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<Arc<vfs::INode>> {
fn find(&self, name: &str) -> vfs::Result<Arc<dyn vfs::INode>> {
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<vfs::FileSystem> {
fn fs(&self) -> Arc<dyn vfs::FileSystem> {
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<BTreeMap<INodeId, Weak<INodeImpl>>>,
/// device
device: Arc<Device>,
device: Arc<dyn Device>,
/// Pointer to self, used by INodes
self_ptr: Weak<SimpleFileSystem>,
/// device inode
@ -734,7 +739,7 @@ pub struct SimpleFileSystem {
impl SimpleFileSystem {
/// Load SFS from device
pub fn open(device: Arc<Device>) -> vfs::Result<Arc<Self>> {
pub fn open(device: Arc<dyn Device>) -> vfs::Result<Arc<Self>> {
let super_block = device.load_struct::<SuperBlock>(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<Device>, space: usize) -> vfs::Result<Arc<Self>> {
pub fn create(device: Arc<dyn Device>, space: usize) -> vfs::Result<Arc<Self>> {
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<vfs::INode> {
fn root_inode(&self) -> Arc<dyn vfs::INode> {
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?

@ -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 {

@ -2,14 +2,14 @@ use crate::vfs::{INode, Metadata, Result};
use alloc::{string::String, sync::Arc};
pub struct File {
inode: Arc<INode>,
inode: Arc<dyn INode>,
offset: usize,
readable: bool,
writable: bool,
}
impl File {
pub fn new(inode: Arc<INode>, readable: bool, writable: bool) -> Self {
pub fn new(inode: Arc<dyn INode>, readable: bool, writable: bool) -> Self {
File {
inode,
offset: 0,

@ -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<Arc<INode>>{
fn create(&self, name: &str, type_: FileType, mode: u32) -> Result<Arc<dyn INode>> {
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<Arc<INode>>{
fn create2(
&self,
name: &str,
type_: FileType,
mode: u32,
_data: usize,
) -> Result<Arc<dyn INode>> {
self.create(name, type_, mode)
}
/// Create a hard link `name` to `other`
fn link(&self, name: &str, other: &Arc<INode>) -> Result<()>;
fn link(&self, name: &str, other: &Arc<dyn INode>) -> 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<INode>, new_name: &str) -> Result<()>;
fn move_(&self, old_name: &str, target: &Arc<dyn INode>, new_name: &str) -> Result<()>;
/// Find the INode `name` in the directory
fn find(&self, name: &str) -> Result<Arc<INode>>;
fn find(&self, name: &str) -> Result<Arc<dyn INode>>;
/// Get the name of directory entry
fn get_entry(&self, id: usize) -> Result<String>;
@ -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<FileSystem>;
fn fs(&self) -> Arc<dyn FileSystem>;
/// 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<T: INode>(&self) -> Option<&T> {
self.as_any_ref().downcast_ref::<T>()
@ -88,12 +94,12 @@ impl INode {
}
/// Lookup path from current INode, and do not follow symlinks
pub fn lookup(&self, path: &str) -> Result<Arc<INode>> {
pub fn lookup(&self, path: &str) -> Result<Arc<dyn INode>> {
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<Arc<INode>> {
pub fn lookup_follow(&self, path: &str, mut follow_times: usize) -> Result<Arc<dyn INode>> {
if self.metadata()?.type_ != FileType::Dir {
return Err(FsError::NotDir);
}
@ -285,12 +291,12 @@ impl std::error::Error for FsError {}
pub type Result<T> = result::Result<T, FsError>;
/// 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<INode>;
fn root_inode(&self) -> Arc<dyn INode>;
/// Get the file system information
fn info(&self) -> FsInfo;

Loading…
Cancel
Save