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

df: fix block size header for multiples of 1024 #3191

Merged
merged 2 commits into from
Mar 7, 2022

Conversation

jfinkels
Copy link
Collaborator

This pull request corrects the column header printed by df when the --block-size argument has a value that is a multiple of 1024. After this commit, the header looks like "1K" or "4M" or "117G", etc., depending on the particular value of the block size. For example:

$ df --block-size=1024 | head -n1
Filesystem                1K-blocks     Used Available Use% Mounted on
$ df --block-size=2048 | head -n1
Filesystem                2K-blocks     Used Available Use% Mounted on
$ df --block-size=3072 | head -n1
Filesystem                3K-blocks     Used Available Use% Mounted on
$ df --block-size=4096 | head -n1
Filesystem                4K-blocks     Used Available Use% Mounted on

This matches the behavior of GNU df.

This pull request does not correct the header when the block size is not divisible by 1024. It sets it to "1kB-blocks", which is wrong in all cases except when block size is set to 1000. (Previously, the header was always set to "1k-blocks", which is never a correct column header for GNU df. I figured that changing it to be correct in one case is better than leaving it incorrect in all cases. 🤷 ) A future pull request can fix that.

Comment on lines +155 to +184
// TODO We have not yet implemented this behavior, but when we do,
// uncomment this test.

// #[test]
// fn test_to_magnitude_and_suffix_not_powers_of_1024() {
// assert_eq!(to_magnitude_and_suffix(1).unwrap(), "1B");
// assert_eq!(to_magnitude_and_suffix(999).unwrap(), "999B");

// assert_eq!(to_magnitude_and_suffix(1000).unwrap(), "1kB");
// assert_eq!(to_magnitude_and_suffix(1001).unwrap(), "1.1kB");
// assert_eq!(to_magnitude_and_suffix(1023).unwrap(), "1.1kB");
// assert_eq!(to_magnitude_and_suffix(1025).unwrap(), "1.1kB");
// assert_eq!(to_magnitude_and_suffix(999_000).unwrap(), "999kB");

// assert_eq!(to_magnitude_and_suffix(999_001).unwrap(), "1MB");
// assert_eq!(to_magnitude_and_suffix(999_999).unwrap(), "1MB");
// assert_eq!(to_magnitude_and_suffix(1_000_000).unwrap(), "1MB");
// assert_eq!(to_magnitude_and_suffix(1_000_001).unwrap(), "1.1MB");
// assert_eq!(to_magnitude_and_suffix(1_100_000).unwrap(), "1.1MB");
// assert_eq!(to_magnitude_and_suffix(1_100_001).unwrap(), "1.2MB");
// assert_eq!(to_magnitude_and_suffix(1_900_000).unwrap(), "1.9MB");
// assert_eq!(to_magnitude_and_suffix(1_900_001).unwrap(), "2MB");
// assert_eq!(to_magnitude_and_suffix(9_900_000).unwrap(), "9.9MB");
// assert_eq!(to_magnitude_and_suffix(9_900_001).unwrap(), "10MB");
// assert_eq!(to_magnitude_and_suffix(999_000_000).unwrap(), "999MB");

// assert_eq!(to_magnitude_and_suffix(999_000_001).unwrap(), "1GB");
// assert_eq!(to_magnitude_and_suffix(1_000_000_000).unwrap(), "1GB");
// // etc.
// }
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I left some test cases here for the future

@jfinkels jfinkels force-pushed the df-block-size-heading branch 2 times, most recently from ce2b876 to 5e7e05d Compare March 2, 2022 03:42
jfinkels added 2 commits March 5, 2022 11:36
Correct the column header printed by `df` when the `--block-size`
argument has a value that is a multiple of 1024. After this commit,
the header looks like "1K" or "4M" or "117G", etc., depending on the
particular value of the block size. For example:

    $ df --block-size=1024 | head -n1
    Filesystem                1K-blocks     Used Available Use% Mounted on
    $ df --block-size=2048 | head -n1
    Filesystem                2K-blocks     Used Available Use% Mounted on
    $ df --block-size=3072 | head -n1
    Filesystem                3K-blocks     Used Available Use% Mounted on
    $ df --block-size=4096 | head -n1
    Filesystem                4K-blocks     Used Available Use% Mounted on
@jfinkels jfinkels force-pushed the df-block-size-heading branch from 5e7e05d to 5cf7139 Compare March 5, 2022 16:48
@sylvestre sylvestre merged commit 76cb746 into uutils:main Mar 7, 2022
@jfinkels jfinkels deleted the df-block-size-heading branch March 12, 2022 18:36
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