Skip to content
This repository has been archived by the owner on Mar 15, 2024. It is now read-only.

Commit

Permalink
feat(ext2): Celled<Ext2> implements FileSystem
Browse files Browse the repository at this point in the history
  • Loading branch information
RatCornu committed Jan 7, 2024
1 parent 21928c9 commit 8f4ae17
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
10 changes: 10 additions & 0 deletions src/fs/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use alloc::string::String;
use core::fmt;
use core::fmt::Display;

use crate::file::Type;

/// Enumeration of possible errors encountered with [`FileSystem`](super::FileSystem)s' manipulation.
#[allow(clippy::module_name_repetitions)]
#[derive(Debug)]
Expand All @@ -25,6 +27,11 @@ pub enum FsError<E: core::error::Error> {

/// Indicates that this error is coming from the filesystem's implementation.
Implementation(E),

/// Tried to assign a wrong type to a file.
///
/// `WrongFileType(expected, given)`
WrongFileType(Type, Type),
}

impl<E: core::error::Error> Display for FsError<E> {
Expand All @@ -37,6 +44,9 @@ impl<E: core::error::Error> Display for FsError<E> {
Self::NoEnt(filename) => write!(formatter, "No entry: \"{filename}\" is an symbolic link pointing at an empty string"),
Self::NotFound(filename) => write!(formatter, "Not found: \"{filename}\" has not been found"),
Self::Implementation(err) => write!(formatter, "{err}"),
Self::WrongFileType(expected, given) => {
write!(formatter, "Wrong File Type: {expected:?} file type expected, {given:?} given")
},
}
}
}
Expand Down
7 changes: 0 additions & 7 deletions src/fs/ext2/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use core::error;
use core::fmt::{self, Display};

use super::superblock::EXT2_SIGNATURE;
use crate::file::Type;

/// Enumeration of possible errors encountered with Ext2's manipulation.
#[allow(clippy::module_name_repetitions)]
Expand Down Expand Up @@ -59,9 +58,6 @@ pub enum Ext2Error {

/// Tried to access a byte which is out of bounds.
OutOfBounds(i128),

/// Tried to assign a wrong type to a file.
WrongFileType(Type, Type),
}

impl Display for Ext2Error {
Expand Down Expand Up @@ -105,9 +101,6 @@ impl Display for Ext2Error {
Self::OutOfBounds(byte) => {
write!(formatter, "Out of Bounds: tried to access the {byte}th byte which is out of bounds")
},
Self::WrongFileType(expected, given) => {
write!(formatter, "Wrong File Type: {expected:?} file type expected, {given:?} given")
},
}
}
}
Expand Down
20 changes: 19 additions & 1 deletion src/fs/ext2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ use self::block::Block;
use self::block_group::BlockGroupDescriptor;
use self::error::Ext2Error;
use self::file::{Directory, Regular, SymbolicLink};
use self::inode::Inode;
use self::inode::{Inode, ROOT_DIRECTORY_INODE};
use self::superblock::{Superblock, SUPERBLOCK_START_BYTE};
use super::FileSystem;
use crate::dev::celled::Celled;
use crate::dev::sector::Address;
use crate::dev::Device;
Expand Down Expand Up @@ -214,6 +215,23 @@ impl<Dev: Device<u8, Ext2Error>> Celled<Ext2<Dev>> {
}
}

impl<Dev: Device<u8, Ext2Error>> FileSystem<Directory<Dev>> for Celled<Ext2<Dev>> {
#[inline]
fn root(&self) -> Result<Directory<Dev>, Error<<Directory<Dev> as crate::file::Directory>::Error>> {
self.file(ROOT_DIRECTORY_INODE).and_then(|root| match root {
TypeWithFile::Directory(root_dir) => Ok(root_dir),
TypeWithFile::Regular(_) | TypeWithFile::SymbolicLink(_) | TypeWithFile::Other(_) => {
Err(Error::Fs(FsError::WrongFileType(Type::Directory, root.into())))
},
})
}

#[inline]
fn double_slash_root(&self) -> Result<Directory<Dev>, Error<<Directory<Dev> as crate::file::Directory>::Error>> {
self.root()
}
}

#[cfg(test)]
mod test {
use core::cell::RefCell;
Expand Down
16 changes: 12 additions & 4 deletions src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,22 @@ pub const PATH_MAX: usize = 4_096;
/// A filesystem.
pub trait FileSystem<Dir: Directory> {
/// Returns the root directory of the filesystem.
fn root(&self) -> Dir;
///
/// # Errors
///
/// Returns a [`DevError`](crate::dev::error::DevError) if the device could not be read.
fn root(&self) -> Result<Dir, Error<Dir::Error>>;

/// Returns the double slash root directory of the filesystem.
///
/// If you do not have any idea of what this is, you are probably looking for [`root`](trait.FileSystem.html#tymethod.root).
///
/// See [`Component::DoubleSlashRootDir`] and [`Path`] for more information.
fn double_slash_root(&self) -> Dir;
///
/// # Errors
///
/// Returns a [`DevError`](crate::dev::error::DevError) if the device could not be read.
fn double_slash_root(&self) -> Result<Dir, Error<Dir::Error>>;

/// Performs a pathname resolution as described in [this POSIX definition](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13).
///
Expand Down Expand Up @@ -96,14 +104,14 @@ pub trait FileSystem<Dir: Directory> {
match comp {
Component::RootDir => {
if pos == Position::First || pos == Position::Only {
current_dir = fs.root();
current_dir = fs.root()?;
} else {
unreachable!("The root directory cannot be encountered during the pathname resolution");
}
},
Component::DoubleSlashRootDir => {
if pos == Position::First || pos == Position::Only {
current_dir = fs.double_slash_root();
current_dir = fs.double_slash_root()?;
} else {
unreachable!("The double slash root directory cannot be encountered during the pathname resolution");
}
Expand Down

0 comments on commit 8f4ae17

Please sign in to comment.