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

Conversation

nnh12
Copy link
Contributor

@nnh12 nnh12 commented Aug 14, 2024

This pull request is to add the ls -s option in the Theseus operating environment. The ls -s is a standard linux command. It shows the sizes of files and directories in blocks, which is useful for getting a quick overview of file sizes. Below are some sample test cases:

/extra_files: ls -s
    --         wasm
    --         test_files
    1159    README.md
/extra_files: ls test_files -s
    --         zork_dat
    --         text
/extra_files/test_files/text: ls -s
    422     the_rise_of_skywalker.txt
    492     the_phanton_menace.txt
    477     the_last_jedi.txt
    499     the_force_awakens.txt
    486     the_empire_strikes_back.txt
    482     revenge_of_the_sith.txt
    462     return_of_the_jedi.txt
    490     attack_of_the_clones.txt
    503     a_new_hope.txt

Default ls command still works:

/extra_files/test_files: ls 
zork_dat
text

* Lists the size of each file (in bytes) when `ls -s` is invoked
Copy link
Member

@kevinaboos kevinaboos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! I left a few comments.

My top-level comment is that the default output of size should be in units of bytes, not in disk sectors. Kindly make that change and address my other comments, and then I can merge it in. Thanks!

applications/ls/src/lib.rs Outdated Show resolved Hide resolved
@@ -49,7 +58,12 @@ 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);
if size_option {
print_children(&dir, true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment here, no need to duplicate code, just pass in size_option directly as an argument.

kernel/fs_node/src/lib.rs Outdated Show resolved Hide resolved
@@ -190,4 +197,15 @@ 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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is redundant, see the existing len() method for files (part of the KnownLength trait). See my other comment about handling sizes for directories.

* Add '--' option in place for directory size
* Remove remove redundant Kernel function and perform operation
  in `ls` appliciation instead
@nnh12
Copy link
Contributor Author

nnh12 commented Aug 17, 2024

I have created the PR to the requested changes
The file size is returned from the ls -s in bytes (since it calls the len() function from KnownLength
The directory size is returned as -- as you suggested

Copy link
Member

@kevinaboos kevinaboos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks! i left only one small comment about code duplication that you missed.

For future reference, kindly don't force push commits to your PR branch, as it removes all of my review comments (notice that they're all gone now). This makes it hard to track whether all of the comments have been addressed.

Comment on lines 45 to 48
if size_option && matches.free.is_empty() {
print_children(&curr_wd, size_option);
return 0;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this duplicated code block is unnecessary, you can remove it as it's redundant with the block below it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it I will remove that

@nnh12
Copy link
Contributor Author

nnh12 commented Aug 19, 2024

Got it thank you!
Just to confirm, so should I just make a new branch and open a new PR addressing the feedback?

@kevinaboos
Copy link
Member

Got it thank you! Just to confirm, so should I just make a new branch and open a new branch addressing the feedback?

Nope, all you have to do is make another change, commit it to this same branch, and push it. Github will automatically show the new commit in this PR.

@nnh12
Copy link
Contributor Author

nnh12 commented Aug 19, 2024

Got it thanks - I won't force push

@nnh12
Copy link
Contributor Author

nnh12 commented Aug 19, 2024

I just removed the duplicated code block- thanks

Copy link
Member

@kevinaboos kevinaboos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, looks great!

@kevinaboos kevinaboos merged commit 8a1d149 into theseus-os:theseus_main Aug 20, 2024
3 checks passed
github-actions bot pushed a commit that referenced this pull request Aug 20, 2024
* 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> 8a1d149
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants