From 90de33d5d73753b83302c6be2133ed5fc983a94f Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Wed, 29 Apr 2020 09:50:13 +0200 Subject: [PATCH 1/5] maint/df: relax the dependencies --- src/uu/df/Cargo.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/uu/df/Cargo.toml b/src/uu/df/Cargo.toml index 5fc2e3502..5ed07c34d 100644 --- a/src/uu/df/Cargo.toml +++ b/src/uu/df/Cargo.toml @@ -8,12 +8,12 @@ name = "uu_df" path = "src/df.rs" [dependencies] -clap = "2.32.0" -libc = "0.2.42" +clap = "2.32" +libc = "0.2" uucore = "0.0.1" [target.'cfg(target_os = "windows")'.dependencies] -kernel32-sys = "0.2.2" +kernel32-sys = "0.2" winapi = { version = "0.3", features = ["handleapi"] } [[bin]] From fc83024ebeaa90f75004e1510386cb0c69a41134 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Wed, 29 Apr 2020 19:03:22 +0200 Subject: [PATCH 2/5] refactor(df): use number_prefix like 'ls' instead of doing the display by hand --- Cargo.lock | 1 + src/uu/df/Cargo.toml | 1 + src/uu/df/src/df.rs | 36 +++++++++++++++++------------------- tests/test_df.rs | 17 ++++++++++++++++- 4 files changed, 35 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0e201780b..a9da406ff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -415,6 +415,7 @@ dependencies = [ "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", + "number_prefix 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "uucore 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", ] diff --git a/src/uu/df/Cargo.toml b/src/uu/df/Cargo.toml index 5ed07c34d..107e932a6 100644 --- a/src/uu/df/Cargo.toml +++ b/src/uu/df/Cargo.toml @@ -10,6 +10,7 @@ path = "src/df.rs" [dependencies] clap = "2.32" libc = "0.2" +number_prefix = "0.2" uucore = "0.0.1" [target.'cfg(target_os = "windows")'.dependencies] diff --git a/src/uu/df/src/df.rs b/src/uu/df/src/df.rs index 764182e03..5762b4ac3 100644 --- a/src/uu/df/src/df.rs +++ b/src/uu/df/src/df.rs @@ -12,6 +12,8 @@ extern crate clap; extern crate libc; +extern crate number_prefix; + #[macro_use] extern crate uucore; @@ -27,6 +29,7 @@ use kernel32::{ GetVolumeInformationW, GetVolumePathNamesForVolumeNameW, QueryDosDeviceW, }; +use number_prefix::{binary_prefix, decimal_prefix, Prefixed, Standalone}; use std::cell::Cell; use std::collections::HashMap; use std::collections::HashSet; @@ -77,7 +80,6 @@ static ABOUT: &str = "Show information about the file system on which each FILE or all file systems by default."; static EXIT_OK: i32 = 0; -#[cfg(any(target_os = "freebsd", target_os = "macos", target_os = "windows"))] static EXIT_ERR: i32 = 1; #[cfg(windows)] @@ -711,24 +713,20 @@ fn filter_mount_list(vmi: Vec, paths: &[String], opt: &Options) -> Ve /// e.g. It returns 1G when value is 1 * 1024 * 1024 * 1024 and base is 1024. /// Note: It returns `value` if `base` isn't positive. fn human_readable(value: u64, base: i64) -> String { - #![allow(non_snake_case)] - if base <= 0 { - return value.to_string(); - } - let KB: u64 = base as u64; - let MB: u64 = KB * base as u64; - let GB: u64 = MB * base as u64; - let TB: u64 = GB * base as u64; - if value >= TB { - format!("{:.1}T", (value as f64) / (TB as f64)) - } else if value >= GB { - format!("{:.1}G", (value as f64) / (GB as f64)) - } else if value >= MB { - format!("{:.1}M", (value as f64) / (MB as f64)) - } else if value >= KB { - format!("{:.1}K", (value as f64) / (KB as f64)) - } else { - format!("{}B", value) + match base { + d if d < 0 => value.to_string(), + + 1000 => match decimal_prefix(value as f64) { + Standalone(bytes) => bytes.to_string(), + Prefixed(prefix, bytes) => format!("{:.1}{}", bytes, prefix).to_uppercase(), + }, + + 1024 => match binary_prefix(value as f64) { + Standalone(bytes) => bytes.to_string(), + Prefixed(prefix, bytes) => format!("{:.1}{}", bytes, prefix).to_uppercase(), + }, + + _ => crash!(EXIT_ERR, "Internal error: Unknown base value {}", base), } } diff --git a/tests/test_df.rs b/tests/test_df.rs index 03903049a..32bb0f481 100644 --- a/tests/test_df.rs +++ b/tests/test_df.rs @@ -1,5 +1,12 @@ use common::util::*; +#[test] +fn test_df_compatible_no_size_arg() { + let (_, mut ucmd) = at_and_ucmd!(); + let result = ucmd.arg("-a").run(); + assert!(result.success); +} + #[test] fn test_df_compatible() { let (_, mut ucmd) = at_and_ucmd!(); @@ -13,4 +20,12 @@ fn test_df_compatible_type() { let result = ucmd.arg("-aT").run(); assert!(result.success); } -// TODO + +#[test] +fn test_df_compatible_si() { + let (_, mut ucmd) = at_and_ucmd!(); + let result = ucmd.arg("-aH").run(); + assert!(result.success); +} + +// ToDO: more tests... From 091a356807c1133855aea95eb35d4ff9815ed7e1 Mon Sep 17 00:00:00 2001 From: Roy Ivy III Date: Sun, 3 May 2020 14:31:45 -0500 Subject: [PATCH 3/5] fix/df ~ correct number suffix display by using number_prefix `PrefixNames::symbol()` --- src/uu/df/src/df.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/uu/df/src/df.rs b/src/uu/df/src/df.rs index 5762b4ac3..62225749b 100644 --- a/src/uu/df/src/df.rs +++ b/src/uu/df/src/df.rs @@ -29,7 +29,7 @@ use kernel32::{ GetVolumeInformationW, GetVolumePathNamesForVolumeNameW, QueryDosDeviceW, }; -use number_prefix::{binary_prefix, decimal_prefix, Prefixed, Standalone}; +use number_prefix::{binary_prefix, decimal_prefix, Prefixed, Standalone, PrefixNames}; use std::cell::Cell; use std::collections::HashMap; use std::collections::HashSet; @@ -716,14 +716,17 @@ fn human_readable(value: u64, base: i64) -> String { match base { d if d < 0 => value.to_string(), + // ref: [Binary prefix](https://en.wikipedia.org/wiki/Binary_prefix) @@ + // ref: [SI/metric prefix](https://en.wikipedia.org/wiki/Metric_prefix) @@ + 1000 => match decimal_prefix(value as f64) { Standalone(bytes) => bytes.to_string(), - Prefixed(prefix, bytes) => format!("{:.1}{}", bytes, prefix).to_uppercase(), + Prefixed(prefix, bytes) => format!("{:.1}{}", bytes, prefix.symbol()), }, 1024 => match binary_prefix(value as f64) { Standalone(bytes) => bytes.to_string(), - Prefixed(prefix, bytes) => format!("{:.1}{}", bytes, prefix).to_uppercase(), + Prefixed(prefix, bytes) => format!("{:.1}{}", bytes, prefix.symbol()), }, _ => crash!(EXIT_ERR, "Internal error: Unknown base value {}", base), From 81d85f398bfd0397fca164d8f949c2d771e6fee7 Mon Sep 17 00:00:00 2001 From: Roy Ivy III Date: Sun, 3 May 2020 14:34:02 -0500 Subject: [PATCH 4/5] fix/df ~ missing windows imports --- src/uu/df/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/uu/df/Cargo.toml b/src/uu/df/Cargo.toml index 107e932a6..c949a91dc 100644 --- a/src/uu/df/Cargo.toml +++ b/src/uu/df/Cargo.toml @@ -15,7 +15,7 @@ uucore = "0.0.1" [target.'cfg(target_os = "windows")'.dependencies] kernel32-sys = "0.2" -winapi = { version = "0.3", features = ["handleapi"] } +winapi = { version = "0.3", features = ["handleapi", "winerror"] } [[bin]] name = "df" From 23b49101c0dd0598607174b5a3987b5a6f14a6e7 Mon Sep 17 00:00:00 2001 From: Roy Ivy III Date: Sun, 3 May 2020 14:45:09 -0500 Subject: [PATCH 5/5] fix/df ~ fix build script for sub-crate builds --- src/uu/df/Cargo.toml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/uu/df/Cargo.toml b/src/uu/df/Cargo.toml index c949a91dc..e77cb2650 100644 --- a/src/uu/df/Cargo.toml +++ b/src/uu/df/Cargo.toml @@ -2,6 +2,8 @@ name = "df" version = "0.0.1" authors = [] +license = "MIT" +build = "../../common/mkmain.rs" [lib] name = "uu_df"