Skip to content

Commit

Permalink
Update directory size and remove Kernel level function call
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Hsiao authored and Nathan Hsiao committed Aug 17, 2024
1 parent aac805c commit d82d7a1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 28 deletions.
20 changes: 11 additions & 9 deletions applications/ls/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub fn main(args: Vec<String>) -> isize {
};

if size_option && matches.free.is_empty() {
print_children(&curr_wd, true);
print_children(&curr_wd, size_option);
return 0;
}

Expand All @@ -58,12 +58,7 @@ pub fn main(args: Vec<String>) -> isize {
// Navigate to the path specified by first argument
match path.get(&curr_wd) {
Some(FileOrDir::Dir(dir)) => {
if size_option {
print_children(&dir, true);
}
else {
print_children(&dir, false);
}
print_children(&dir, size_option);
0
}
Some(FileOrDir::File(file)) => {
Expand All @@ -84,8 +79,15 @@ fn print_children(dir: &DirRef, print_size: bool) {
for child in child_list.iter() {
let child_path = dir.lock().get(child).expect("Failed to get child path");
if print_size {
let size = FileOrDir::get_file_size(&child_path);
writeln!(child_string, " {} {}", size, child).expect("Failed to write child_string");
match &child_path {
FileOrDir::File(file_ref) => {
let file = file_ref.lock();
writeln!(child_string, " {} {}", file.len(), child).expect("Failed to write child_string");
},
FileOrDir::Dir(_) => {
writeln!(child_string, " -- {}", child).expect("Failed to write child_string");
},
};
} else {
writeln!(child_string, "{}", child).expect("Failed to write child_string");
}
Expand Down
19 changes: 0 additions & 19 deletions kernel/fs_node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,6 @@ use alloc::sync::{Arc, Weak};
use memory::MappedPages;
use io::{ByteReader, ByteWriter, KnownLength};

/// A value to represent the space on the disk (stored in meta-information) for directores
macro_rules! DIRECTORY_DISK_SPACE {
() => {
4096
};
}


/// A reference to any type that implements the [`File`] trait,
/// which can only represent a File (not a Directory).
pub type FileRef = Arc<Mutex<dyn File + Send>>;
Expand Down Expand Up @@ -197,15 +189,4 @@ impl FileOrDir {
FileOrDir::Dir(_) => true,
}
}

/// Returns the size of the file in bytes, or directory disk space.
pub fn get_file_size(file_or_dir: &FileOrDir) -> usize {
match file_or_dir {
FileOrDir::File(file_ref) => {
let file = file_ref.lock();
file.len()
},
FileOrDir::Dir(_) => DIRECTORY_DISK_SPACE!(),
}
}
}

0 comments on commit d82d7a1

Please sign in to comment.