Skip to content

Commit

Permalink
Merge pull request uutils#5381 from howard0su/df_wsl
Browse files Browse the repository at this point in the history
Fix overflow error on WSL. Default to 0 when the values are non-sense.
  • Loading branch information
sylvestre committed Oct 10, 2023
2 parents 89daff4 + 95a1a08 commit f3f82c1
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions src/uu/df/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
// spell-checker:ignore tmpfs Pcent Itotal Iused Iavail Ipcent
// spell-checker:ignore tmpfs Pcent Itotal Iused Iavail Ipcent nosuid nodev
//! The filesystem usage data table.
//!
//! A table ([`Table`]) comprises a header row ([`Header`]) and a
Expand Down Expand Up @@ -152,8 +152,10 @@ impl From<Filesystem> for Row {
ffree,
..
} = fs.usage;
let bused = blocks - bfree;
let fused = files - ffree;

// On Windows WSL, files can be less than ffree. Protect such cases via saturating_sub.
let bused = blocks.saturating_sub(bfree);
let fused = files.saturating_sub(ffree);
Self {
file: fs.file,
fs_device: dev_name,
Expand Down Expand Up @@ -815,4 +817,35 @@ mod tests {
assert_eq!(get_formatted_values(1000, 1000, 0), vec!("1", "1", "0"));
assert_eq!(get_formatted_values(1001, 1000, 1), vec!("2", "1", "1"));
}

#[test]
fn test_row_converter_with_invalid_numbers() {
// copy from wsl linux
let d = crate::Filesystem {
file: None,
mount_info: crate::MountInfo {
dev_id: "28".to_string(),
dev_name: "none".to_string(),
fs_type: "9p".to_string(),
mount_dir: "/usr/lib/wsl/drivers".to_string(),
mount_option: "ro,nosuid,nodev,noatime".to_string(),
mount_root: "/".to_string(),
remote: false,
dummy: false,
},
usage: crate::table::FsUsage {
blocksize: 4096,
blocks: 244029695,
bfree: 125085030,
bavail: 125085030,
bavail_top_bit_set: false,
files: 999,
ffree: 1000000,
},
};

let row = Row::from(d);

assert_eq!(row.inodes_used, 0);
}
}

0 comments on commit f3f82c1

Please sign in to comment.