Merge pull request #3347 from cakebaker/fix_total_use_percentage

df: fix calculation of Use% in "total" row
This commit is contained in:
Sylvestre Ledru 2022-04-02 09:53:08 +02:00 committed by GitHub
commit ef8921044b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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) {
let bytes = self.bytes + rhs.bytes;
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_used = self.inodes_used + rhs.inodes_used;
*self = Self {
@ -111,11 +112,14 @@ impl AddAssign for Row {
fs_mount: "-".into(),
bytes,
bytes_used,
bytes_avail: self.bytes_avail + rhs.bytes_avail,
bytes_avail,
bytes_usage: if bytes == 0 {
None
} 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.
#[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.
// 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)
Some(bused as f64 / (bused + bavail) as f64)
},
#[cfg(target_os = "macos")]
bytes_capacity: if bavail == 0 {

View file

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