diff --git a/src/sys/fs/dir.rs b/src/sys/fs/dir.rs index 0dc59fdab..9781e0359 100644 --- a/src/sys/fs/dir.rs +++ b/src/sys/fs/dir.rs @@ -35,6 +35,13 @@ impl Dir { root } + pub fn parent(&self) -> Option { + match &self.parent { + Some(dir) => Some(*dir.clone()), + None => None, + } + } + pub fn is_root(&self) -> bool { self.parent.is_none() } diff --git a/src/sys/fs/read_dir.rs b/src/sys/fs/read_dir.rs index c65ba2ceb..8704d95e6 100644 --- a/src/sys/fs/read_dir.rs +++ b/src/sys/fs/read_dir.rs @@ -2,6 +2,7 @@ use super::dir_entry::DirEntry; use super::block::LinkedBlock; use super::dir::Dir; use super::FileType; +use crate::sys; use alloc::string::String; use core::convert::From; @@ -13,6 +14,7 @@ pub struct ReadDir { pub block: LinkedBlock, pub block_offset: usize, block_index: usize, + meta_index: usize, // 0: self, 1: parent, 2: children } impl From for ReadDir { @@ -22,6 +24,7 @@ impl From for ReadDir { block: LinkedBlock::read(dir.addr()), block_offset: 0, block_index: 0, + meta_index: 0, } } } @@ -71,6 +74,27 @@ impl Iterator for ReadDir { type Item = DirEntry; fn next(&mut self) -> Option { + if self.meta_index == 0 { + self.meta_index += 1; + let entry_kind = FileType::Dir; + let entry_addr = self.dir.addr(); + let entry_size = self.dir.size() as u32; + let entry_time = sys::clock::realtime() as u64; + let entry_name = "."; + let dir = self.dir.clone(); + return Some(DirEntry::new(dir, entry_kind, entry_addr, entry_size, entry_time, &entry_name)); + } + if self.meta_index == 1 { + self.meta_index += 1; + if let Some(dir) = self.dir.parent() { + let entry_kind = FileType::Dir; + let entry_addr = dir.addr(); + let entry_size = dir.size() as u32; + let entry_time = sys::clock::realtime() as u64; + let entry_name = ".."; + return Some(DirEntry::new(dir, entry_kind, entry_addr, entry_size, entry_time, &entry_name)); + } + } loop { loop { let offset = self.block_offset; // Backup cursor position @@ -121,7 +145,6 @@ impl Iterator for ReadDir { None => break, } } - None } }