1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-27 11:07:44 +00:00

df: Fix calculation of Use% in "total" row

Change formula from: "Used/Size * 100" to "Used/(Used + Avail) * 100".
This formula also works if "Used" and "Avail" do not add up to "Size",
which is the case if there are reserved disk blocks.
This commit is contained in:
Daniel Hofstetter 2022-04-01 08:25:30 +02:00
parent bed7dc5a29
commit bf69c63e09
2 changed files with 8 additions and 4 deletions

View file

@ -102,6 +102,7 @@ impl AddAssign for Row {
fn add_assign(&mut self, rhs: Self) { fn add_assign(&mut self, rhs: Self) {
let bytes = self.bytes + rhs.bytes; let bytes = self.bytes + rhs.bytes;
let bytes_used = self.bytes_used + rhs.bytes_used; let bytes_used = self.bytes_used + rhs.bytes_used;
let bytes_avail = self.bytes_avail + rhs.bytes_avail;
let inodes = self.inodes + rhs.inodes; let inodes = self.inodes + rhs.inodes;
let inodes_used = self.inodes_used + rhs.inodes_used; let inodes_used = self.inodes_used + rhs.inodes_used;
*self = Self { *self = Self {
@ -111,11 +112,14 @@ impl AddAssign for Row {
fs_mount: "-".into(), fs_mount: "-".into(),
bytes, bytes,
bytes_used, bytes_used,
bytes_avail: self.bytes_avail + rhs.bytes_avail, bytes_avail,
bytes_usage: if bytes == 0 { bytes_usage: if bytes == 0 {
None None
} else { } else {
Some(bytes_used as f64 / bytes as f64) // We use "(bytes_used + bytes_avail)" instead of "bytes" because on some filesystems (e.g.
// ext4) "bytes" 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(bytes_used as f64 / (bytes_used + bytes_avail) as f64)
}, },
// TODO Figure out how to compute this. // TODO Figure out how to compute this.
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
@ -164,7 +168,7 @@ impl From<Filesystem> for Row {
// We use "(bused + bavail)" instead of "blocks" because on some filesystems (e.g. // 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. // 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 // 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) Some(bused as f64 / (bused + bavail) as f64)
}, },
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
bytes_capacity: if bavail == 0 { bytes_capacity: if bavail == 0 {

View file

@ -140,7 +140,7 @@ fn test_total() {
#[test] #[test]
fn test_use_percentage() { fn test_use_percentage() {
let output = new_ucmd!() let output = new_ucmd!()
.args(&["--output=used,avail,pcent"]) .args(&["--total", "--output=used,avail,pcent"])
.succeeds() .succeeds()
.stdout_move_str(); .stdout_move_str();