diff --git a/applications/ls/src/lib.rs b/applications/ls/src/lib.rs index dd6cb40bef..70448e8492 100644 --- a/applications/ls/src/lib.rs +++ b/applications/ls/src/lib.rs @@ -19,6 +19,7 @@ use path::Path; pub fn main(args: Vec) -> isize { let mut opts = Options::new(); opts.optflag("h", "help", "print this help menu"); + opts.optflag("s", "size", "print the size of each file in directory"); let matches = match opts.parse(args) { Ok(m) => m, @@ -34,13 +35,16 @@ pub fn main(args: Vec) -> isize { return 0; } + let size_option = matches.opt_present("s"); + let Ok(curr_wd) = task::with_current_task(|t| t.get_env().lock().working_dir.clone()) else { println!("failed to get current task"); return -1; }; + // print children of working directory if no child is specified if matches.free.is_empty() { - print_children(&curr_wd); + print_children(&curr_wd, size_option); return 0; } @@ -49,7 +53,7 @@ pub fn main(args: Vec) -> isize { // Navigate to the path specified by first argument match path.get(&curr_wd) { Some(FileOrDir::Dir(dir)) => { - print_children(&dir); + print_children(&dir, size_option); 0 } Some(FileOrDir::File(file)) => { @@ -63,12 +67,25 @@ pub fn main(args: Vec) -> isize { } } -fn print_children(dir: &DirRef) { +fn print_children(dir: &DirRef, print_size: bool) { let mut child_string = String::new(); let mut child_list = dir.lock().list(); child_list.reverse(); for child in child_list.iter() { - writeln!(child_string, "{child}").expect("Failed to write child_string"); + let child_path = dir.lock().get(child).expect("Failed to get child path"); + if print_size { + 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"); + } } println!("{}", child_string); } @@ -80,4 +97,4 @@ fn print_usage(opts: Options) { const USAGE: &str = "Usage: ls [DIR | FILE] List the contents of the given directory or info about the given file. -If no arguments are provided, it lists the contents of the current directory."; \ No newline at end of file +If no arguments are provided, it lists the contents of the current directory."; diff --git a/kernel/fs_node/src/lib.rs b/kernel/fs_node/src/lib.rs index 25f457ac66..71dbef633e 100644 --- a/kernel/fs_node/src/lib.rs +++ b/kernel/fs_node/src/lib.rs @@ -24,7 +24,6 @@ use alloc::sync::{Arc, Weak}; use memory::MappedPages; use io::{ByteReader, ByteWriter, KnownLength}; - /// A reference to any type that implements the [`File`] trait, /// which can only represent a File (not a Directory). pub type FileRef = Arc>;