mirror of
https://github.com/uutils/coreutils
synced 2024-11-16 09:48:03 +00:00
Merge pull request #3309 from cakebaker/fix_use_percentage_calculation
df: fix calculation of Use% column
This commit is contained in:
commit
52b2d2ac1c
2 changed files with 15 additions and 20 deletions
|
@ -149,24 +149,28 @@ impl From<Filesystem> for Row {
|
|||
ffree,
|
||||
..
|
||||
} = fs.usage;
|
||||
let bused = blocks - bfree;
|
||||
Self {
|
||||
file: fs.file,
|
||||
fs_device: dev_name,
|
||||
fs_type,
|
||||
fs_mount: mount_dir,
|
||||
bytes: blocksize * blocks,
|
||||
bytes_used: blocksize * (blocks - bfree),
|
||||
bytes_used: blocksize * bused,
|
||||
bytes_avail: blocksize * bavail,
|
||||
bytes_usage: if blocks == 0 {
|
||||
None
|
||||
} else {
|
||||
Some(((blocks - bfree) as f64) / blocks as f64)
|
||||
// We use "(bused + bavail)" instead of "blocks" because on some filesystems (e.g.
|
||||
// ext4) "blocks" also includes reserved blocks we ignore for the usage calculation.
|
||||
// https://www.gnu.org/software/coreutils/faq/coreutils-faq.html#df-Size-and-Used-and-Available-do-not-add-up
|
||||
Some((bused as f64) / (bused + bavail) as f64)
|
||||
},
|
||||
#[cfg(target_os = "macos")]
|
||||
bytes_capacity: if bavail == 0 {
|
||||
None
|
||||
} else {
|
||||
Some(bavail as f64 / ((blocks - bfree + bavail) as f64))
|
||||
Some(bavail as f64 / ((bused + bavail) as f64))
|
||||
},
|
||||
inodes: files,
|
||||
inodes_used: files - ffree,
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
// spell-checker:ignore udev
|
||||
// spell-checker:ignore udev pcent
|
||||
use crate::common::util::*;
|
||||
|
||||
#[test]
|
||||
|
@ -139,33 +139,24 @@ fn test_total() {
|
|||
|
||||
#[test]
|
||||
fn test_use_percentage() {
|
||||
// Example output:
|
||||
//
|
||||
// Filesystem 1K-blocks Used Available Use% Mounted on
|
||||
// udev 3858016 0 3858016 0% /dev
|
||||
// ...
|
||||
// /dev/loop14 63488 63488 0 100% /snap/core20/1361
|
||||
let output = new_ucmd!().succeeds().stdout_move_str();
|
||||
let output = new_ucmd!()
|
||||
.args(&["--output=used,avail,pcent"])
|
||||
.succeeds()
|
||||
.stdout_move_str();
|
||||
|
||||
// Skip the header line.
|
||||
let lines: Vec<&str> = output.lines().skip(1).collect();
|
||||
|
||||
for line in lines {
|
||||
let mut iter = line.split_whitespace();
|
||||
iter.next();
|
||||
let reported_size = iter.next().unwrap().parse::<f64>().unwrap();
|
||||
let reported_used = iter.next().unwrap().parse::<f64>().unwrap();
|
||||
// Skip "Available" column
|
||||
iter.next();
|
||||
if cfg!(target_os = "macos") {
|
||||
// Skip "Capacity" column
|
||||
iter.next();
|
||||
}
|
||||
let reported_avail = iter.next().unwrap().parse::<f64>().unwrap();
|
||||
let reported_percentage = iter.next().unwrap();
|
||||
let reported_percentage = reported_percentage[..reported_percentage.len() - 1]
|
||||
.parse::<u8>()
|
||||
.unwrap();
|
||||
let computed_percentage = (100.0 * (reported_used / reported_size)).ceil() as u8;
|
||||
let computed_percentage =
|
||||
(100.0 * (reported_used / (reported_used + reported_avail))).ceil() as u8;
|
||||
|
||||
assert_eq!(computed_percentage, reported_percentage);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue