Skip to content

Commit

Permalink
Add dot dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
vinc committed Nov 25, 2023
1 parent bd4672c commit d8d0f9d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/sys/fs/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ impl Dir {
root
}

pub fn parent(&self) -> Option<Self> {
match &self.parent {
Some(dir) => Some(*dir.clone()),
None => None,
}
}

pub fn is_root(&self) -> bool {
self.parent.is_none()
}
Expand Down
25 changes: 24 additions & 1 deletion src/sys/fs/read_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Dir> for ReadDir {
Expand All @@ -22,6 +24,7 @@ impl From<Dir> for ReadDir {
block: LinkedBlock::read(dir.addr()),
block_offset: 0,
block_index: 0,
meta_index: 0,
}
}
}
Expand Down Expand Up @@ -71,6 +74,27 @@ impl Iterator for ReadDir {
type Item = DirEntry;

fn next(&mut self) -> Option<DirEntry> {
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
Expand Down Expand Up @@ -121,7 +145,6 @@ impl Iterator for ReadDir {
None => break,
}
}

None
}
}

0 comments on commit d8d0f9d

Please sign in to comment.