Skip to content
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

Add size option (-s) in the ls command #1102

Merged
merged 3 commits into from
Aug 20, 2024
Merged
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
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
Loading