Skip to content

Commit

Permalink
du: give -h output the same precision as GNU coreutils
Browse files Browse the repository at this point in the history
When printing the `du -h` output GNU coreutils does autoscale
the size, e.g.
```
$ truncate -s12M a
$ truncate -s8500 b
$ truncate -s133456345 c
$ truncate -s56990456345 d
$ du -h --apparent-size a b c d
12M	a
8,4K	b
128M	c
54G	d
```
Align our version to do the same by sharing the code with `ls`.

Closes: #6159
  • Loading branch information
mvo5 authored and cakebaker committed Apr 24, 2024
1 parent d07fb73 commit 61e0450
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/uu/du/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ chrono = { workspace = true }
# For the --exclude & --exclude-from options
glob = { workspace = true }
clap = { workspace = true }
uucore = { workspace = true }
uucore = { workspace = true, features = ["format"] }

[target.'cfg(target_os = "windows")'.dependencies]
windows-sys = { workspace = true, features = [
Expand Down
30 changes: 12 additions & 18 deletions src/uu/du/src/du.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@ const ABOUT: &str = help_about!("du.md");
const AFTER_HELP: &str = help_section!("after help", "du.md");
const USAGE: &str = help_usage!("du.md");

// TODO: Support Z & Y (currently limited by size of u64)
const UNITS: [(char, u32); 6] = [('E', 6), ('P', 5), ('T', 4), ('G', 3), ('M', 2), ('K', 1)];

struct TraversalOptions {
all: bool,
separate_dirs: bool,
Expand Down Expand Up @@ -117,7 +114,8 @@ enum Time {

#[derive(Clone)]
enum SizeFormat {
Human(u64),
HumanDecimal,
HumanBinary,
BlockSize(u64),
}

Expand Down Expand Up @@ -549,18 +547,14 @@ impl StatPrinter {
return size.to_string();
}
match self.size_format {
SizeFormat::Human(multiplier) => {
if size == 0 {
return "0".to_string();
}
for &(unit, power) in &UNITS {
let limit = multiplier.pow(power);
if size >= limit {
return format!("{:.1}{}", (size as f64) / (limit as f64), unit);
}
}
format!("{size}B")
}
SizeFormat::HumanDecimal => uucore::format::human::human_readable(
size,
uucore::format::human::SizeFormat::Decimal,
),
SizeFormat::HumanBinary => uucore::format::human::human_readable(
size,
uucore::format::human::SizeFormat::Binary,
),
SizeFormat::BlockSize(block_size) => div_ceil(size, block_size).to_string(),
}
}
Expand Down Expand Up @@ -688,9 +682,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
});

let size_format = if matches.get_flag(options::HUMAN_READABLE) {
SizeFormat::Human(1024)
SizeFormat::HumanBinary
} else if matches.get_flag(options::SI) {
SizeFormat::Human(1000)
SizeFormat::HumanDecimal
} else if matches.get_flag(options::BYTES) {
SizeFormat::BlockSize(1)
} else if matches.get_flag(options::BLOCK_SIZE_1K) {
Expand Down
28 changes: 28 additions & 0 deletions tests/by-util/test_du.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,34 @@ fn test_du_h_flag_empty_file() {
.stdout_only("0\tempty.txt\n");
}

#[test]
fn test_du_h_precision() {
let test_cases = [
(133456345, "128M"),
(12 * 1024 * 1024, "12M"),
(8500, "8.4K"),
];

for &(test_len, expected_output) in &test_cases {
let (at, mut ucmd) = at_and_ucmd!();

let fpath = at.plus("test.txt");
std::fs::File::create(&fpath)
.expect("cannot create test file")
.set_len(test_len)
.expect("cannot truncate test len to size");
ucmd.arg("-h")
.arg("--apparent-size")
.arg(&fpath)
.succeeds()
.stdout_only(format!(
"{}\t{}\n",
expected_output,
&fpath.to_string_lossy()
));
}
}

#[cfg(feature = "touch")]
#[test]
fn test_du_time() {
Expand Down

0 comments on commit 61e0450

Please sign in to comment.