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)", "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)", "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)", "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)", "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)", "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
] ]

View file

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

View file

@ -12,6 +12,8 @@
extern crate clap; extern crate clap;
extern crate libc; extern crate libc;
extern crate number_prefix;
#[macro_use] #[macro_use]
extern crate uucore; extern crate uucore;
@ -27,6 +29,7 @@ use kernel32::{
GetVolumeInformationW, GetVolumePathNamesForVolumeNameW, QueryDosDeviceW, GetVolumeInformationW, GetVolumePathNamesForVolumeNameW, QueryDosDeviceW,
}; };
use number_prefix::{binary_prefix, decimal_prefix, Prefixed, Standalone, PrefixNames};
use std::cell::Cell; use std::cell::Cell;
use std::collections::HashMap; use std::collections::HashMap;
use std::collections::HashSet; 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."; or all file systems by default.";
static EXIT_OK: i32 = 0; static EXIT_OK: i32 = 0;
#[cfg(any(target_os = "freebsd", target_os = "macos", target_os = "windows"))]
static EXIT_ERR: i32 = 1; static EXIT_ERR: i32 = 1;
#[cfg(windows)] #[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. /// 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. /// Note: It returns `value` if `base` isn't positive.
fn human_readable(value: u64, base: i64) -> String { fn human_readable(value: u64, base: i64) -> String {
#![allow(non_snake_case)] match base {
if base <= 0 { d if d < 0 => value.to_string(),
return value.to_string();
} // ref: [Binary prefix](https://en.wikipedia.org/wiki/Binary_prefix) @@ <https://archive.is/cnwmF>
let KB: u64 = base as u64; // ref: [SI/metric prefix](https://en.wikipedia.org/wiki/Metric_prefix) @@ <https://archive.is/QIuLj>
let MB: u64 = KB * base as u64;
let GB: u64 = MB * base as u64; 1000 => match decimal_prefix(value as f64) {
let TB: u64 = GB * base as u64; Standalone(bytes) => bytes.to_string(),
if value >= TB { Prefixed(prefix, bytes) => format!("{:.1}{}", bytes, prefix.symbol()),
format!("{:.1}T", (value as f64) / (TB as f64)) },
} else if value >= GB {
format!("{:.1}G", (value as f64) / (GB as f64)) 1024 => match binary_prefix(value as f64) {
} else if value >= MB { Standalone(bytes) => bytes.to_string(),
format!("{:.1}M", (value as f64) / (MB as f64)) Prefixed(prefix, bytes) => format!("{:.1}{}", bytes, prefix.symbol()),
} else if value >= KB { },
format!("{:.1}K", (value as f64) / (KB as f64))
} else { _ => crash!(EXIT_ERR, "Internal error: Unknown base value {}", base),
format!("{}B", value)
} }
} }

View file

@ -1,5 +1,12 @@
use common::util::*; 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] #[test]
fn test_df_compatible() { fn test_df_compatible() {
let (_, mut ucmd) = at_and_ucmd!(); let (_, mut ucmd) = at_and_ucmd!();
@ -13,4 +20,12 @@ fn test_df_compatible_type() {
let result = ucmd.arg("-aT").run(); let result = ucmd.arg("-aT").run();
assert!(result.success); 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...