vfs: readdir and finddir operations in FsNode

This commit is contained in:
TheOddGarlic 2022-08-03 14:23:58 +03:00
parent c63fbabc42
commit 9462350de7

View file

@ -21,11 +21,18 @@ use spin::Lazy;
use crate::handle::Handle;
use self::errors::FsError;
use FsResult as Result;
pub type FsOpenOperation = fn(/* TODO: flags */) -> Result<Handle, FsError>;
pub type FsCloseOperation = fn(fd: Handle) -> Result<(), FsError>;
pub type FsReadOperation = fn(fd: Handle, offset: u32, size: u32) -> Result<Box<[u8]>, FsError>;
pub type FsWriteOperation = fn(fd: Handle, offset: u32, buffer: Box<[u8]>) -> Result<(), FsError>;
pub type FsResult<T> = core::result::Result<T, FsError>;
pub type FsOpenOperation = fn(node: &FsNode /* TODO: flags */) -> Result<Handle>;
pub type FsCloseOperation = fn(node: &FsNode) -> Result<()>;
pub type FsReadOperation = fn(node: &FsNode, offset: u32, size: u32)
-> Result<Box<[u8]>>;
pub type FsWriteOperation = fn(node: &FsNode, offset: u32, buffer: Box<[u8]>)
-> Result<()>;
pub type FsReaddirOperation = fn(node: &FsNode, index: u32) -> Result<DirectoryEntry>;
pub type FsFinddirOperation = fn(node: &FsNode, name: &str) -> Result<FsNode>;
/// A VFS node, that can either be a file or a directory.
pub struct FsNode {
@ -41,40 +48,57 @@ pub struct FsNode {
close: Option<FsCloseOperation>,
read: Option<FsReadOperation>,
write: Option<FsWriteOperation>,
readdir: Option<FsReaddirOperation>,
finddir: Option<FsFinddirOperation>,
// todo: permissions mask
// todo: owning user/group
// todo: readdir, finddir fn pointers
}
impl FsNode {
// TODO: make this take flags
fn open(&self) -> Result<Handle, FsError> {
fn open(&self) -> Result<Handle> {
if let Some(open) = self.open {
open()
open(self)
} else {
Err(FsError::UnsupportedOperation)
}
}
fn close(&self, fd: Handle) -> Result<(), FsError> {
fn close(&self) -> Result<()> {
if let Some(close) = self.close {
close(fd)
close(self)
} else {
Err(FsError::UnsupportedOperation)
}
}
fn read(&self, fd: Handle, offset: u32, size: u32) -> Result<Box<[u8]>, FsError> {
fn read(&self, offset: u32, size: u32) -> Result<Box<[u8]>> {
if let Some(read) = self.read {
read(fd, offset, size)
read(self, offset, size)
} else {
Err(FsError::UnsupportedOperation)
}
}
fn write(&self, fd: Handle, offset: u32, buffer: Box<[u8]>) -> Result<(), FsError> {
fn write(&self, offset: u32, buffer: Box<[u8]>) -> Result<()> {
if let Some(write) = self.write {
write(fd, offset, buffer)
write(self, offset, buffer)
} else {
Err(FsError::UnsupportedOperation)
}
}
fn readdir(&self, index: u32) -> Result<DirectoryEntry> {
if let Some(readdir) = self.readdir {
readdir(self, index)
} else {
Err(FsError::UnsupportedOperation)
}
}
fn finddir(&self, name: &str) -> Result<FsNode> {
if let Some(finddir) = self.finddir {
finddir(self, name)
} else {
Err(FsError::UnsupportedOperation)
}
@ -96,6 +120,11 @@ bitflags! {
}
}
pub struct DirectoryEntry {
name: String,
inode: u32,
}
pub static FILE_SYSTEM: Lazy<spin::Mutex<Synced<Ext2<Size1024, Vec<u8>>>>> =
Lazy::new(|| spin::Mutex::new(load_fs()));