Merge pull request #1487 from sylvestre/df-human

refactor and fix some `df` build and cosmetic issues
This commit is contained in:
Roy Ivy III 2020-05-03 15:15:14 -05:00 committed by GitHub
commit f12c27a57d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 24 deletions

1
Cargo.lock generated
View file

@ -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)",
]

View file

@ -2,19 +2,22 @@
name = "df"
version = "0.0.1"
authors = []
license = "MIT"
build = "../../common/mkmain.rs"
[lib]
name = "uu_df"
path = "src/df.rs"
[dependencies]
clap = "2.32.0"
libc = "0.2.42"
clap = "2.32"
libc = "0.2"
number_prefix = "0.2"
uucore = "0.0.1"
[target.'cfg(target_os = "windows")'.dependencies]
kernel32-sys = "0.2.2"
winapi = { version = "0.3", features = ["handleapi"] }
kernel32-sys = "0.2"
winapi = { version = "0.3", features = ["handleapi", "winerror"] }
[[bin]]
name = "df"

View file

@ -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, PrefixNames};
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,23 @@ fn filter_mount_list(vmi: Vec<MountInfo>, 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(),
// ref: [Binary prefix](https://en.wikipedia.org/wiki/Binary_prefix) @@ <https://archive.is/cnwmF>
// ref: [SI/metric prefix](https://en.wikipedia.org/wiki/Metric_prefix) @@ <https://archive.is/QIuLj>
1000 => match decimal_prefix(value as f64) {
Standalone(bytes) => bytes.to_string(),
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.symbol()),
},
_ => crash!(EXIT_ERR, "Internal error: Unknown base value {}", base),
}
}

View file

@ -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...