Skip to content

Commit e37d2f2

Browse files
authored
Merge pull request #6878 from sylvestre/du
du: fix the display with --inodes
2 parents 1abac33 + 1b2778b commit e37d2f2

File tree

2 files changed

+66
-6
lines changed

2 files changed

+66
-6
lines changed

src/uu/du/src/du.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -346,14 +346,21 @@ fn du(
346346
}
347347

348348
if let Some(inode) = this_stat.inode {
349-
if seen_inodes.contains(&inode) {
350-
if options.count_links {
349+
// Check if the inode has been seen before and if we should skip it
350+
if seen_inodes.contains(&inode)
351+
&& (!options.count_links || !options.all)
352+
{
353+
// If `count_links` is enabled and `all` is not, increment the inode count
354+
if options.count_links && !options.all {
351355
my_stat.inodes += 1;
352356
}
357+
// Skip further processing for this inode
353358
continue;
354359
}
360+
// Mark this inode as seen
355361
seen_inodes.insert(inode);
356362
}
363+
357364
if this_stat.is_dir {
358365
if options.one_file_system {
359366
if let (Some(this_inode), Some(my_inode)) =
@@ -550,9 +557,6 @@ impl StatPrinter {
550557
}
551558

552559
fn convert_size(&self, size: u64) -> String {
553-
if self.inodes {
554-
return size.to_string();
555-
}
556560
match self.size_format {
557561
SizeFormat::HumanDecimal => uucore::format::human::human_readable(
558562
size,
@@ -562,7 +566,14 @@ impl StatPrinter {
562566
size,
563567
uucore::format::human::SizeFormat::Binary,
564568
),
565-
SizeFormat::BlockSize(block_size) => size.div_ceil(block_size).to_string(),
569+
SizeFormat::BlockSize(block_size) => {
570+
if self.inodes {
571+
// we ignore block size (-B) with --inodes
572+
size.to_string()
573+
} else {
574+
size.div_ceil(block_size).to_string()
575+
}
576+
}
566577
}
567578
}
568579

tests/by-util/test_du.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,33 @@ fn test_du_inodes_with_count_links() {
546546
}
547547
}
548548

549+
#[cfg(not(target_os = "android"))]
550+
#[test]
551+
fn test_du_inodes_with_count_links_all() {
552+
let ts = TestScenario::new(util_name!());
553+
let at = &ts.fixtures;
554+
555+
at.mkdir("d");
556+
at.mkdir("d/d");
557+
at.touch("d/f");
558+
at.hard_link("d/f", "d/h");
559+
560+
let result = ts.ucmd().arg("--inodes").arg("-al").arg("d").succeeds();
561+
result.no_stderr();
562+
563+
let mut result_seq: Vec<String> = result
564+
.stdout_str()
565+
.split('\n')
566+
.filter(|x| !x.is_empty())
567+
.map(|x| x.parse().unwrap())
568+
.collect();
569+
result_seq.sort_unstable();
570+
#[cfg(windows)]
571+
assert_eq!(result_seq, ["1\td\\d", "1\td\\f", "1\td\\h", "4\td"]);
572+
#[cfg(not(windows))]
573+
assert_eq!(result_seq, ["1\td/d", "1\td/f", "1\td/h", "4\td"]);
574+
}
575+
549576
#[test]
550577
fn test_du_h_flag_empty_file() {
551578
new_ucmd!()
@@ -1171,3 +1198,25 @@ fn test_invalid_time_style() {
11711198
.succeeds()
11721199
.stdout_does_not_contain("du: invalid argument 'banana' for 'time style'");
11731200
}
1201+
1202+
#[test]
1203+
fn test_human_size() {
1204+
use std::fs::File;
1205+
1206+
let ts = TestScenario::new(util_name!());
1207+
let at = &ts.fixtures;
1208+
let dir = at.plus_as_string("d");
1209+
at.mkdir(&dir);
1210+
1211+
for i in 1..=1023 {
1212+
let file_path = format!("{dir}/file{i}");
1213+
File::create(&file_path).expect("Failed to create file");
1214+
}
1215+
1216+
ts.ucmd()
1217+
.arg("--inodes")
1218+
.arg("-h")
1219+
.arg(&dir)
1220+
.succeeds()
1221+
.stdout_contains(format!("1.0K {dir}"));
1222+
}

0 commit comments

Comments
 (0)