Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

df: switch from u64 to u128 to handle fs with large inodes nr #6071

Merged
merged 3 commits into from
Mar 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 106 additions & 15 deletions src/uu/df/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@
bytes_capacity: Option<f64>,

/// Total number of inodes in the filesystem.
inodes: u64,
inodes: u128,

/// Number of used inodes.
inodes_used: u64,
inodes_used: u128,

/// Number of free inodes.
inodes_free: u64,
inodes_free: u128,

/// Percentage of inodes that are used, given as a float between 0 and 1.
///
Expand Down Expand Up @@ -178,9 +178,9 @@
} else {
Some(bavail as f64 / ((bused + bavail) as f64))
},
inodes: files,
inodes_used: fused,
inodes_free: ffree,
inodes: files as u128,
inodes_used: fused as u128,
inodes_free: ffree as u128,
inodes_usage: if files == 0 {
None
} else {
Expand Down Expand Up @@ -235,9 +235,9 @@
/// Get a string giving the scaled version of the input number.
///
/// The scaling factor is defined in the `options` field.
fn scaled_inodes(&self, size: u64) -> String {
fn scaled_inodes(&self, size: u128) -> String {
if let Some(h) = self.options.human_readable {
to_magnitude_and_suffix(size.into(), SuffixType::HumanReadable(h))
to_magnitude_and_suffix(size, SuffixType::HumanReadable(h))

Check warning on line 240 in src/uu/df/src/table.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/df/src/table.rs#L240

Added line #L240 was not covered by tests
} else {
size.to_string()
}
Expand Down Expand Up @@ -395,12 +395,6 @@
let values = fmt.get_values();
total += row;

for (i, value) in values.iter().enumerate() {
if UnicodeWidthStr::width(value.as_str()) > widths[i] {
widths[i] = UnicodeWidthStr::width(value.as_str());
}
}

rows.push(values);
}
}
Expand All @@ -410,6 +404,16 @@
rows.push(total_row.get_values());
}

// extend the column widths (in chars) for long values in rows
// do it here, after total row was added to the list of rows
for row in &rows {
sylvestre marked this conversation as resolved.
Show resolved Hide resolved
for (i, value) in row.iter().enumerate() {
if UnicodeWidthStr::width(value.as_str()) > widths[i] {
widths[i] = UnicodeWidthStr::width(value.as_str());
}
}
}

Self {
rows,
widths,
Expand Down Expand Up @@ -466,9 +470,11 @@
#[cfg(test)]
mod tests {

use std::vec;

use crate::blocks::HumanReadable;
use crate::columns::Column;
use crate::table::{Header, HeaderMode, Row, RowFormatter};
use crate::table::{Header, HeaderMode, Row, RowFormatter, Table};
use crate::{BlockSize, Options};

const COLUMNS_WITH_FS_TYPE: [Column; 7] = [
Expand Down Expand Up @@ -848,4 +854,89 @@

assert_eq!(row.inodes_used, 0);
}

#[test]
fn test_table_column_width_computation_include_total_row() {
let d1 = 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: 99999999999,
ffree: 999999,
},
};

let filesystems = vec![d1.clone(), d1];

let mut options = Options {
show_total: true,
columns: vec![
Column::Source,
Column::Itotal,
Column::Iused,
Column::Iavail,
],
..Default::default()
};

let table_w_total = Table::new(&options, filesystems.clone());
assert_eq!(
table_w_total.to_string(),
"Filesystem Inodes IUsed IFree\n\
none 99999999999 99999000000 999999\n\
none 99999999999 99999000000 999999\n\
total 199999999998 199998000000 1999998"
);

options.show_total = false;

let table_w_o_total = Table::new(&options, filesystems);
assert_eq!(
table_w_o_total.to_string(),
"Filesystem Inodes IUsed IFree\n\
none 99999999999 99999000000 999999\n\
none 99999999999 99999000000 999999"
);
}

#[test]
fn test_row_accumulation_u64_overflow() {
let total = u64::MAX as u128;
let used1 = 3000u128;
let used2 = 50000u128;

let mut row1 = Row {
inodes: total,
inodes_used: used1,
inodes_free: total - used1,
..Default::default()
};

let row2 = Row {
inodes: total,
inodes_used: used2,
inodes_free: total - used2,
..Default::default()
};

row1 += row2;

assert_eq!(row1.inodes, total * 2);
assert_eq!(row1.inodes_used, used1 + used2);
assert_eq!(row1.inodes_free, total * 2 - used1 - used2);
}
}
Loading