refactor: use auto_inode in memfs,fat32

This commit is contained in:
Mark Poliakov 2021-11-11 22:19:21 +02:00
parent 94c2fc3c82
commit 7e04b11da8
17 changed files with 79 additions and 157 deletions

3
Cargo.lock generated
View File

@ -45,6 +45,8 @@ checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7"
name = "fat32"
version = "0.1.0"
dependencies = [
"fs-macros",
"libsys",
"vfs",
]
@ -113,6 +115,7 @@ dependencies = [
name = "memfs"
version = "0.1.0"
dependencies = [
"fs-macros",
"libsys",
"vfs",
]

View File

@ -7,3 +7,5 @@ edition = "2021"
[dependencies]
vfs = { path = "../vfs" }
fs-macros = { path = "../macros" }
libsys = { path = "../../libsys" }

View File

@ -1,4 +1,4 @@
use libcommon::{read_le16, read_le32};
use libsys::mem::{read_le16, read_le32};
#[derive(Debug)]
pub struct Bpb {

View File

@ -1,7 +1,11 @@
use crate::{Bpb, FileInode};
use alloc::{borrow::ToOwned, boxed::Box, string::String};
use error::Errno;
use libcommon::{read_le16, read_le32};
use libsys::{
error::Errno,
ioctl::IoctlCmd,
mem::{read_le16, read_le32},
stat::{OpenFlags, Stat},
};
use vfs::{BlockDevice, Vnode, VnodeImpl, VnodeKind, VnodeRef};
pub struct DirectoryInode {
@ -26,20 +30,8 @@ pub struct Dirent {
pub cluster: u32,
}
#[auto_inode]
impl VnodeImpl for DirectoryInode {
fn create(
&mut self,
_parent: VnodeRef,
_name: &str,
_kind: VnodeKind,
) -> Result<VnodeRef, Errno> {
todo!()
}
fn remove(&mut self, _parent: VnodeRef, _name: &str) -> Result<(), Errno> {
todo!()
}
fn lookup(&mut self, parent: VnodeRef, name: &str) -> Result<VnodeRef, Errno> {
let fs = parent.fs().unwrap();
let dirent = {
@ -72,30 +64,6 @@ impl VnodeImpl for DirectoryInode {
}
Ok(vnode)
}
fn open(&mut self, _node: VnodeRef) -> Result<usize, Errno> {
todo!()
}
fn close(&mut self, _node: VnodeRef) -> Result<(), Errno> {
todo!()
}
fn read(&mut self, _node: VnodeRef, _pos: usize, _data: &mut [u8]) -> Result<usize, Errno> {
todo!()
}
fn write(&mut self, _node: VnodeRef, _pos: usize, _data: &[u8]) -> Result<usize, Errno> {
todo!()
}
fn truncate(&mut self, _node: VnodeRef, _size: usize) -> Result<(), Errno> {
todo!()
}
fn size(&mut self, _node: VnodeRef) -> Result<usize, Errno> {
todo!()
}
}
impl Iterator for FatIterator<'_> {

View File

@ -1,5 +1,9 @@
use crate::Bpb;
use error::Errno;
use libsys::{
stat::{Stat, OpenFlags},
ioctl::IoctlCmd,
error::Errno
};
use vfs::{VnodeImpl, VnodeKind, VnodeRef};
pub struct FileInode {
@ -7,27 +11,15 @@ pub struct FileInode {
pub size: u32,
}
#[auto_inode]
impl VnodeImpl for FileInode {
fn create(&mut self, _at: VnodeRef, _name: &str, _kind: VnodeKind) -> Result<VnodeRef, Errno> {
panic!()
fn open(&mut self, _node: VnodeRef, flags: OpenFlags) -> Result<usize, Errno> {
if flags & OpenFlags::O_ACCESS != OpenFlags::O_RDONLY {
return Err(Errno::ReadOnly);
}
fn remove(&mut self, _parent: VnodeRef, _name: &str) -> Result<(), Errno> {
panic!()
}
fn lookup(&mut self, _parent: VnodeRef, _name: &str) -> Result<VnodeRef, Errno> {
panic!()
}
fn open(&mut self, _node: VnodeRef) -> Result<usize, Errno> {
Ok(0)
}
fn close(&mut self, _node: VnodeRef) -> Result<(), Errno> {
todo!()
}
fn read(&mut self, node: VnodeRef, pos: usize, data: &mut [u8]) -> Result<usize, Errno> {
let size = self.size as usize;
if pos >= size {
@ -60,16 +52,4 @@ impl VnodeImpl for FileInode {
Ok(off)
}
fn write(&mut self, _node: VnodeRef, _pos: usize, _data: &[u8]) -> Result<usize, Errno> {
todo!()
}
fn truncate(&mut self, _node: VnodeRef, _size: usize) -> Result<(), Errno> {
todo!()
}
fn size(&mut self, _node: VnodeRef) -> Result<usize, Errno> {
todo!()
}
}

View File

@ -4,13 +4,18 @@
#[macro_use]
extern crate std;
#[macro_use]
extern crate fs_macros;
extern crate alloc;
use alloc::{boxed::Box, rc::Rc};
use core::any::Any;
use core::cell::{Ref, RefCell};
use error::Errno;
use libcommon::read_le32;
use libsys::{
mem::read_le32,
error::Errno,
};
use vfs::{BlockDevice, Filesystem, Vnode, VnodeKind, VnodeRef};
pub mod dir;

View File

@ -7,6 +7,7 @@ edition = "2021"
[dependencies]
vfs = { path = "../vfs" }
fs-macros = { path = "../macros" }
libsys = { path = "../../libsys" }
[features]

View File

@ -1,12 +1,17 @@
use crate::{BlockAllocator, Bvec, FileInode};
use alloc::boxed::Box;
use libsys::error::Errno;
use vfs::{IoctlCmd, OpenFlags, Stat, Vnode, VnodeImpl, VnodeKind, VnodeRef};
use libsys::{
error::Errno,
stat::{OpenFlags, Stat},
ioctl::IoctlCmd
};
use vfs::{Vnode, VnodeImpl, VnodeKind, VnodeRef};
pub struct DirInode<A: BlockAllocator + Copy + 'static> {
alloc: A,
}
#[auto_inode]
impl<A: BlockAllocator + Copy + 'static> VnodeImpl for DirInode<A> {
fn create(
&mut self,
@ -30,44 +35,6 @@ impl<A: BlockAllocator + Copy + 'static> VnodeImpl for DirInode<A> {
fn remove(&mut self, _parent: VnodeRef, _name: &str) -> Result<(), Errno> {
Ok(())
}
fn open(&mut self, _node: VnodeRef, _flags: OpenFlags) -> Result<usize, Errno> {
todo!()
}
fn close(&mut self, _node: VnodeRef) -> Result<(), Errno> {
todo!()
}
fn read(&mut self, _node: VnodeRef, _pos: usize, _data: &mut [u8]) -> Result<usize, Errno> {
todo!()
}
fn write(&mut self, _node: VnodeRef, _pos: usize, _data: &[u8]) -> Result<usize, Errno> {
todo!()
}
fn truncate(&mut self, _node: VnodeRef, _size: usize) -> Result<(), Errno> {
todo!()
}
fn size(&mut self, _node: VnodeRef) -> Result<usize, Errno> {
todo!()
}
fn stat(&mut self, _node: VnodeRef, _stat: &mut Stat) -> Result<(), Errno> {
todo!();
}
fn ioctl(
&mut self,
node: VnodeRef,
cmd: IoctlCmd,
ptr: usize,
len: usize,
) -> Result<usize, Errno> {
todo!()
}
}
impl<A: BlockAllocator + Copy + 'static> DirInode<A> {

View File

@ -1,29 +1,17 @@
use crate::{BlockAllocator, Bvec};
use libsys::error::Errno;
use vfs::{OpenFlags, Stat, VnodeImpl, VnodeKind, VnodeRef, IoctlCmd};
use libsys::{
error::Errno,
stat::{OpenFlags, Stat},
ioctl::IoctlCmd
};
use vfs::{VnodeImpl, VnodeKind, VnodeRef};
pub struct FileInode<'a, A: BlockAllocator + Copy + 'static> {
data: Bvec<'a, A>,
}
#[auto_inode]
impl<'a, A: BlockAllocator + Copy + 'static> VnodeImpl for FileInode<'a, A> {
fn create(
&mut self,
_parent: VnodeRef,
_name: &str,
_kind: VnodeKind,
) -> Result<VnodeRef, Errno> {
panic!()
}
fn lookup(&mut self, _parent: VnodeRef, _name: &str) -> Result<VnodeRef, Errno> {
panic!()
}
fn remove(&mut self, _parent: VnodeRef, _name: &str) -> Result<(), Errno> {
panic!()
}
fn open(&mut self, _node: VnodeRef, _mode: OpenFlags) -> Result<usize, Errno> {
Ok(0)
}
@ -54,16 +42,6 @@ impl<'a, A: BlockAllocator + Copy + 'static> VnodeImpl for FileInode<'a, A> {
stat.mode = 0o755;
Ok(())
}
fn ioctl(
&mut self,
node: VnodeRef,
cmd: IoctlCmd,
ptr: usize,
len: usize,
) -> Result<usize, Errno> {
todo!()
}
}
impl<'a, A: BlockAllocator + Copy + 'static> FileInode<'a, A> {

View File

@ -11,14 +11,18 @@ extern crate alloc;
#[macro_use]
extern crate std;
#[macro_use]
extern crate fs_macros;
use alloc::{boxed::Box, rc::Rc};
use core::any::Any;
use core::cell::{Ref, RefCell};
use libsys::{
error::Errno,
path::{path_component_right, path_component_left}
path::{path_component_left, path_component_right},
stat::FileMode,
};
use vfs::{BlockDevice, FileMode, Filesystem, Vnode, VnodeKind, VnodeRef};
use vfs::{BlockDevice, Filesystem, Vnode, VnodeKind, VnodeRef};
mod block;
pub use block::{BlockAllocator, BlockRef};

View File

@ -1,5 +1,9 @@
use crate::{OpenFlags, Stat, VnodeImpl, VnodeKind, VnodeRef, IoctlCmd};
use libsys::error::Errno;
use crate::{VnodeImpl, VnodeKind, VnodeRef};
use libsys::{
error::Errno,
stat::{OpenFlags, Stat},
ioctl::IoctlCmd
};
/// Generic character device trait
pub trait CharDevice {

View File

@ -1,6 +1,7 @@
use crate::{FileMode, FileRef, OpenFlags, VnodeKind, VnodeRef};
use crate::{FileRef, VnodeKind, VnodeRef};
use libsys::{
error::Errno,
stat::{OpenFlags, FileMode},
path::{path_component_left, path_component_right},
};

View File

@ -12,8 +12,8 @@ extern crate fs_macros;
extern crate alloc;
pub use libsys::stat::{FileMode, OpenFlags, Stat};
pub use libsys::ioctl::IoctlCmd;
// pub use libsys::stat::{FileMode, OpenFlags, Stat};
// pub use libsys::ioctl::IoctlCmd;
mod block;
pub use block::BlockDevice;

View File

@ -1,8 +1,12 @@
use crate::{File, FileMode, FileRef, Filesystem, IoctlCmd, OpenFlags, Stat};
use crate::{File, FileRef, Filesystem};
use alloc::{borrow::ToOwned, boxed::Box, rc::Rc, string::String, vec::Vec};
use core::cell::{RefCell, RefMut};
use core::fmt;
use libsys::error::Errno;
use libsys::{
error::Errno,
ioctl::IoctlCmd,
stat::{FileMode, OpenFlags, Stat},
};
/// Convenience type alias for [Rc<Vnode>]
pub type VnodeRef = Rc<Vnode>;
@ -405,7 +409,7 @@ impl fmt::Debug for Vnode {
mod tests {
use super::*;
use libsys::{stat::OpenFlags, ioctl::IoctlCmd, stat::Stat};
use libsys::{ioctl::IoctlCmd, stat::OpenFlags, stat::Stat};
pub struct DummyInode;
#[auto_inode]
@ -447,10 +451,13 @@ mod tests {
root.set_data(Box::new(DummyInode {}));
let node = root.create("test", FileMode::default_dir(), VnodeKind::Directory).unwrap();
let node = root
.create("test", FileMode::default_dir(), VnodeKind::Directory)
.unwrap();
assert_eq!(
root.create("test", FileMode::default_dir(), VnodeKind::Directory).unwrap_err(),
root.create("test", FileMode::default_dir(), VnodeKind::Directory)
.unwrap_err(),
Errno::AlreadyExists
);

View File

@ -17,7 +17,7 @@ use tock_registers::{
register_bitfields, register_structs,
registers::{ReadOnly, ReadWrite, WriteOnly},
};
use vfs::{CharDevice, IoctlCmd};
use vfs::CharDevice;
register_bitfields! {
u32,

View File

@ -5,7 +5,8 @@ use crate::fs::{devfs, MemfsBlockAlloc};
use crate::mem;
use crate::proc::{elf, Process};
use memfs::Ramfs;
use vfs::{Filesystem, Ioctx, OpenFlags};
use libsys::stat::OpenFlags;
use vfs::{Filesystem, Ioctx};
/// Kernel init process function
#[inline(never)]

View File

@ -9,10 +9,11 @@ use core::time::Duration;
use libsys::{
abi,
error::Errno,
stat::{AT_EMPTY_PATH, AT_FDCWD},
ioctl::IoctlCmd,
stat::{FileMode, OpenFlags, Stat, AT_EMPTY_PATH, AT_FDCWD},
traits::{Read, Write},
};
use vfs::{FileMode, IoctlCmd, OpenFlags, Stat, VnodeRef};
use vfs::VnodeRef;
pub mod arg;
pub use arg::*;