From 9462350de7e13727921e08c213b65b7b90316525 Mon Sep 17 00:00:00 2001 From: TheOddGarlic Date: Wed, 3 Aug 2022 14:23:58 +0300 Subject: [PATCH] vfs: readdir and finddir operations in FsNode --- ableos/src/filesystem/mod.rs | 55 +++++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 13 deletions(-) diff --git a/ableos/src/filesystem/mod.rs b/ableos/src/filesystem/mod.rs index dfec8916..e32084d7 100644 --- a/ableos/src/filesystem/mod.rs +++ b/ableos/src/filesystem/mod.rs @@ -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; -pub type FsCloseOperation = fn(fd: Handle) -> Result<(), FsError>; -pub type FsReadOperation = fn(fd: Handle, offset: u32, size: u32) -> Result, FsError>; -pub type FsWriteOperation = fn(fd: Handle, offset: u32, buffer: Box<[u8]>) -> Result<(), FsError>; +pub type FsResult = core::result::Result; + +pub type FsOpenOperation = fn(node: &FsNode /* TODO: flags */) -> Result; +pub type FsCloseOperation = fn(node: &FsNode) -> Result<()>; +pub type FsReadOperation = fn(node: &FsNode, offset: u32, size: u32) + -> Result>; +pub type FsWriteOperation = fn(node: &FsNode, offset: u32, buffer: Box<[u8]>) + -> Result<()>; +pub type FsReaddirOperation = fn(node: &FsNode, index: u32) -> Result; +pub type FsFinddirOperation = fn(node: &FsNode, name: &str) -> Result; /// A VFS node, that can either be a file or a directory. pub struct FsNode { @@ -41,40 +48,57 @@ pub struct FsNode { close: Option, read: Option, write: Option, + readdir: Option, + finddir: Option, // todo: permissions mask // todo: owning user/group - // todo: readdir, finddir fn pointers } impl FsNode { // TODO: make this take flags - fn open(&self) -> Result { + fn open(&self) -> Result { 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, FsError> { + fn read(&self, offset: u32, size: u32) -> Result> { 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 { + if let Some(readdir) = self.readdir { + readdir(self, index) + } else { + Err(FsError::UnsupportedOperation) + } + } + + fn finddir(&self, name: &str) -> Result { + 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>>>> = Lazy::new(|| spin::Mutex::new(load_fs()));