From e21b34a3e76f3638e4f3fb871536ee4b22f93bb7 Mon Sep 17 00:00:00 2001 From: TheOddGarlic Date: Thu, 4 Aug 2022 13:31:29 +0300 Subject: [PATCH] Ext2: read_dir implementation --- ableos/src/filesystem/errors.rs | 5 +++-- ableos/src/filesystem/ext2.rs | 13 +++++++++++-- ableos/src/filesystem/mod.rs | 9 +++++++++ 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/ableos/src/filesystem/errors.rs b/ableos/src/filesystem/errors.rs index 4e54c84c..23864691 100644 --- a/ableos/src/filesystem/errors.rs +++ b/ableos/src/filesystem/errors.rs @@ -5,10 +5,11 @@ */ pub enum FsError { + EndOfFile, InodeNotFound, InvalidDevice, IsDirectory, - EndOfFile, + NotADirectory, UnsupportedOperation, } @@ -28,7 +29,7 @@ impl Into for ext2::error::Error { by_inodes: _, } => todo!(), ext2::error::Error::InodeNotFound { inode: _ } => FsError::InodeNotFound, - ext2::error::Error::NotADirectory { inode: _, name: _ } => todo!(), + ext2::error::Error::NotADirectory { inode: _, name: _ } => FsError::NotADirectory, ext2::error::Error::NotAbsolute { name: _ } => todo!(), ext2::error::Error::NotFound { name: _ } => todo!(), } diff --git a/ableos/src/filesystem/ext2.rs b/ableos/src/filesystem/ext2.rs index adcc0ebc..0d351da2 100644 --- a/ableos/src/filesystem/ext2.rs +++ b/ableos/src/filesystem/ext2.rs @@ -57,8 +57,17 @@ where todo!() } - fn read_dir(&self, _node: &FsNode, _index: usize) -> Result { - todo!() + fn read_dir(&self, node: &FsNode, index: usize) -> Result { + let inode = self + .fs + .inode_nth(node.inode as usize) + .ok_or_else(|| FsError::InodeNotFound)?; + let mut dir = inode.directory().ok_or_else(|| FsError::NotADirectory)?; + let entry = dir.nth(index) + .ok_or_else(|| FsError::InodeNotFound)? + .map_err(|e| e.into())?; + + Ok(DirectoryEntry::new(entry.file_name_string(), entry.inode)) } fn find_dir(&self, _node: &FsNode, _name: &str) -> Result { diff --git a/ableos/src/filesystem/mod.rs b/ableos/src/filesystem/mod.rs index 8793191c..dabf90d5 100644 --- a/ableos/src/filesystem/mod.rs +++ b/ableos/src/filesystem/mod.rs @@ -156,3 +156,12 @@ pub struct DirectoryEntry { name: String, inode: usize, } + +impl DirectoryEntry { + fn new(name: String, inode: usize) -> Self { + Self { + name, + inode, + } + } +}