Skip to content

Commit 76d14ed

Browse files
committed
du: fix the count with --inodes
1 parent fb1874b commit 76d14ed

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/uu/du/src/du.rs

Lines changed: 9 additions & 2 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)) =

tests/by-util/test_du.rs

Lines changed: 27 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!()

0 commit comments

Comments
 (0)