Skip to content

Add dot dirs #551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}