Skip to content

Commit

Permalink
Add size option (-s) to the ls command (#1102)
Browse files Browse the repository at this point in the history
* Lists the size in bytes of each file when `ls -s` is invoked
* Prints '--' to indicate directory sizes are not calculated yet

Co-authored-by: Nathan Hsiao <nnh12@DESKTOP-EQ10QSJ>
  • Loading branch information
nnh12 and Nathan Hsiao authored Aug 20, 2024
1 parent ffb5e8b commit 8a1d149
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
27 changes: 22 additions & 5 deletions applications/ls/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use path::Path;
pub fn main(args: Vec<String>) -> 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,
Expand All @@ -34,13 +35,16 @@ pub fn main(args: Vec<String>) -> 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;
}

Expand All @@ -49,7 +53,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)) => {
print_children(&dir);
print_children(&dir, size_option);
0
}
Some(FileOrDir::File(file)) => {
Expand All @@ -63,12 +67,25 @@ pub fn main(args: Vec<String>) -> 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);
}
Expand All @@ -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.";
If no arguments are provided, it lists the contents of the current directory.";
1 change: 0 additions & 1 deletion kernel/fs_node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Mutex<dyn File + Send>>;
Expand Down

0 comments on commit 8a1d149

Please sign in to comment.