Skip to content

Commit 8a1d149

Browse files
nnh12Nathan Hsiao
and
Nathan Hsiao
authored
Add size option (-s) to the ls command (#1102)
* 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>
1 parent ffb5e8b commit 8a1d149

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

applications/ls/src/lib.rs

+22-5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use path::Path;
1919
pub fn main(args: Vec<String>) -> isize {
2020
let mut opts = Options::new();
2121
opts.optflag("h", "help", "print this help menu");
22+
opts.optflag("s", "size", "print the size of each file in directory");
2223

2324
let matches = match opts.parse(args) {
2425
Ok(m) => m,
@@ -34,13 +35,16 @@ pub fn main(args: Vec<String>) -> isize {
3435
return 0;
3536
}
3637

38+
let size_option = matches.opt_present("s");
39+
3740
let Ok(curr_wd) = task::with_current_task(|t| t.get_env().lock().working_dir.clone()) else {
3841
println!("failed to get current task");
3942
return -1;
4043
};
44+
4145
// print children of working directory if no child is specified
4246
if matches.free.is_empty() {
43-
print_children(&curr_wd);
47+
print_children(&curr_wd, size_option);
4448
return 0;
4549
}
4650

@@ -49,7 +53,7 @@ pub fn main(args: Vec<String>) -> isize {
4953
// Navigate to the path specified by first argument
5054
match path.get(&curr_wd) {
5155
Some(FileOrDir::Dir(dir)) => {
52-
print_children(&dir);
56+
print_children(&dir, size_option);
5357
0
5458
}
5559
Some(FileOrDir::File(file)) => {
@@ -63,12 +67,25 @@ pub fn main(args: Vec<String>) -> isize {
6367
}
6468
}
6569

66-
fn print_children(dir: &DirRef) {
70+
fn print_children(dir: &DirRef, print_size: bool) {
6771
let mut child_string = String::new();
6872
let mut child_list = dir.lock().list();
6973
child_list.reverse();
7074
for child in child_list.iter() {
71-
writeln!(child_string, "{child}").expect("Failed to write child_string");
75+
let child_path = dir.lock().get(child).expect("Failed to get child path");
76+
if print_size {
77+
match &child_path {
78+
FileOrDir::File(file_ref) => {
79+
let file = file_ref.lock();
80+
writeln!(child_string, " {} {}", file.len(), child).expect("Failed to write child_string");
81+
},
82+
FileOrDir::Dir(_) => {
83+
writeln!(child_string, " -- {}", child).expect("Failed to write child_string");
84+
},
85+
};
86+
} else {
87+
writeln!(child_string, "{}", child).expect("Failed to write child_string");
88+
}
7289
}
7390
println!("{}", child_string);
7491
}
@@ -80,4 +97,4 @@ fn print_usage(opts: Options) {
8097

8198
const USAGE: &str = "Usage: ls [DIR | FILE]
8299
List the contents of the given directory or info about the given file.
83-
If no arguments are provided, it lists the contents of the current directory.";
100+
If no arguments are provided, it lists the contents of the current directory.";

kernel/fs_node/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ use alloc::sync::{Arc, Weak};
2424
use memory::MappedPages;
2525
use io::{ByteReader, ByteWriter, KnownLength};
2626

27-
2827
/// A reference to any type that implements the [`File`] trait,
2928
/// which can only represent a File (not a Directory).
3029
pub type FileRef = Arc<Mutex<dyn File + Send>>;

0 commit comments

Comments
 (0)