From deb796f3fe9c81d38c616b77aecf4720e805d1ce Mon Sep 17 00:00:00 2001 From: Andrus Suvalau Date: Fri, 30 Jan 2026 14:07:49 +0100 Subject: [PATCH 1/4] du: deduplicate Stat::new call --- src/uu/du/src/du.rs | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/src/uu/du/src/du.rs b/src/uu/du/src/du.rs index a70a0269ce1..36dd25b4f74 100644 --- a/src/uu/du/src/du.rs +++ b/src/uu/du/src/du.rs @@ -1107,20 +1107,21 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { #[cfg(not(all(unix, not(target_os = "redox"))))] let use_safe_traversal = false; + // Pre-populate seen_inodes with the starting directory to detect cycles + let stat = Stat::new(&path, None, &traversal_options); + if let Ok(stat) = stat.as_ref() { + if let Some(inode) = stat.inode { + if !traversal_options.count_links && seen_inodes.contains(&inode) { + continue 'loop_file; + } + seen_inodes.insert(inode); + } + } + if use_safe_traversal { // Use safe traversal (Unix except Redox, when not using -L) #[cfg(all(unix, not(target_os = "redox")))] { - // Pre-populate seen_inodes with the starting directory to detect cycles - if let Ok(stat) = Stat::new(&path, None, &traversal_options) { - if let Some(inode) = stat.inode { - if !traversal_options.count_links && seen_inodes.contains(&inode) { - continue 'loop_file; - } - seen_inodes.insert(inode); - } - } - match safe_du( &path, &traversal_options, @@ -1148,13 +1149,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { } } else { // Use regular traversal (non-Linux or when -L is used) - if let Ok(stat) = Stat::new(&path, None, &traversal_options) { - if let Some(inode) = stat.inode { - if !traversal_options.count_links && seen_inodes.contains(&inode) { - continue 'loop_file; - } - seen_inodes.insert(inode); - } + if let Ok(stat) = stat { let stat = du_regular( stat, &traversal_options, From 4748ca9a7ee1fb7ac788766d3f3e6cc640012901 Mon Sep 17 00:00:00 2001 From: Andrus Suvalau Date: Fri, 30 Jan 2026 15:35:24 +0100 Subject: [PATCH 2/4] du: pass Stat::new result into the safe_du func --- src/uu/du/src/du.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/uu/du/src/du.rs b/src/uu/du/src/du.rs index 36dd25b4f74..542f1eab345 100644 --- a/src/uu/du/src/du.rs +++ b/src/uu/du/src/du.rs @@ -303,6 +303,7 @@ fn safe_du( seen_inodes: &mut HashSet, print_tx: &mpsc::Sender>, parent_fd: Option<&DirFd>, + my_stat: Option>, ) -> Result>>> { // Get initial stat for this path - use DirFd if available to avoid path length issues let mut my_stat = if let Some(parent_fd) = parent_fd { @@ -354,7 +355,12 @@ fn safe_du( } } else { // This is the initial directory - try regular Stat::new first, then fallback to DirFd - match Stat::new(path, None, options) { + let my_stat = match my_stat { + Some(s) => s, + None => Stat::new(path, None, options) + }; + + match my_stat { Ok(s) => s, Err(_e) => { // Try using our new DirFd method for the root directory @@ -530,6 +536,7 @@ fn safe_du( seen_inodes, print_tx, Some(&dir_fd), + None, )?; if !options.separate_dirs { @@ -1129,6 +1136,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { &mut seen_inodes, &print_tx, None, + Some(stat), ) { Ok(stat) => { print_tx From 2eabad7b912729b91c45b96089f44bea5fee1424 Mon Sep 17 00:00:00 2001 From: Andrus Suvalau Date: Fri, 30 Jan 2026 15:44:21 +0100 Subject: [PATCH 3/4] du: apply stylistic changes --- src/uu/du/src/du.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/uu/du/src/du.rs b/src/uu/du/src/du.rs index 542f1eab345..2bb9c5e9a3d 100644 --- a/src/uu/du/src/du.rs +++ b/src/uu/du/src/du.rs @@ -357,7 +357,7 @@ fn safe_du( // This is the initial directory - try regular Stat::new first, then fallback to DirFd let my_stat = match my_stat { Some(s) => s, - None => Stat::new(path, None, options) + None => Stat::new(path, None, options), }; match my_stat { From 50f105977ab7474508f2721e4f328135cc3abeec Mon Sep 17 00:00:00 2001 From: Andrus Suvalau Date: Sat, 31 Jan 2026 11:48:57 +0100 Subject: [PATCH 4/4] du: rename the my_stat parameter for clarity --- src/uu/du/src/du.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/uu/du/src/du.rs b/src/uu/du/src/du.rs index 2bb9c5e9a3d..d8bfd4446a1 100644 --- a/src/uu/du/src/du.rs +++ b/src/uu/du/src/du.rs @@ -303,7 +303,7 @@ fn safe_du( seen_inodes: &mut HashSet, print_tx: &mpsc::Sender>, parent_fd: Option<&DirFd>, - my_stat: Option>, + initial_stat: Option>, ) -> Result>>> { // Get initial stat for this path - use DirFd if available to avoid path length issues let mut my_stat = if let Some(parent_fd) = parent_fd { @@ -355,12 +355,12 @@ fn safe_du( } } else { // This is the initial directory - try regular Stat::new first, then fallback to DirFd - let my_stat = match my_stat { + let initial_stat = match initial_stat { Some(s) => s, None => Stat::new(path, None, options), }; - match my_stat { + match initial_stat { Ok(s) => s, Err(_e) => { // Try using our new DirFd method for the root directory