From def1bb2b8722f37a8976e01e7efe08cef68036ec Mon Sep 17 00:00:00 2001 From: Daniel Hofstetter Date: Sat, 13 Sep 2025 16:02:19 +0200 Subject: [PATCH 1/3] du: simplify signature of Stat::new_from_dirfd --- src/uu/du/src/du.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/uu/du/src/du.rs b/src/uu/du/src/du.rs index 9f58c89db..af3b4d29d 100644 --- a/src/uu/du/src/du.rs +++ b/src/uu/du/src/du.rs @@ -166,11 +166,7 @@ impl Stat { /// Create a Stat using safe traversal methods with `DirFd` for the root directory #[cfg(target_os = "linux")] - fn new_from_dirfd( - dir_fd: &DirFd, - full_path: &Path, - _options: &TraversalOptions, - ) -> std::io::Result { + fn new_from_dirfd(dir_fd: &DirFd, full_path: &Path) -> std::io::Result { // Get metadata for the directory itself using fstat let safe_metadata = dir_fd.metadata()?; @@ -361,7 +357,7 @@ fn safe_du( Err(_e) => { // Try using our new DirFd method for the root directory match DirFd::open(path) { - Ok(dir_fd) => match Stat::new_from_dirfd(&dir_fd, path, options) { + Ok(dir_fd) => match Stat::new_from_dirfd(&dir_fd, path) { Ok(s) => s, Err(e) => { let error = e.map_err_context( From d3af6fd529c162a47a6b6ef8a0d7ce7f7f8452e9 Mon Sep 17 00:00:00 2001 From: Daniel Hofstetter Date: Sat, 13 Sep 2025 16:07:09 +0200 Subject: [PATCH 2/3] du: remove condition with identical if/else blocks --- src/uu/du/src/du.rs | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/uu/du/src/du.rs b/src/uu/du/src/du.rs index af3b4d29d..82b73c7e1 100644 --- a/src/uu/du/src/du.rs +++ b/src/uu/du/src/du.rs @@ -719,18 +719,9 @@ fn du_regular( } } Err(e) => { - // Check if this is the "too many symlinks" error we want to catch - if e.kind() == std::io::ErrorKind::InvalidData - && e.to_string().contains("Too many levels") - { - print_tx.send(Err(e.map_err_context( + print_tx.send(Err(e.map_err_context( || translate!("du-error-cannot-access", "path" => entry_path.quote()), )))?; - } else { - print_tx.send(Err(e.map_err_context( - || translate!("du-error-cannot-access", "path" => entry_path.quote()), - )))?; - } } } } From e76afc9bd37873be2337778255a3af341f5f93cb Mon Sep 17 00:00:00 2001 From: Daniel Hofstetter Date: Sat, 13 Sep 2025 16:22:17 +0200 Subject: [PATCH 3/3] du: simplify two if/else constructs --- src/uu/du/src/du.rs | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/uu/du/src/du.rs b/src/uu/du/src/du.rs index 82b73c7e1..c893b06ca 100644 --- a/src/uu/du/src/du.rs +++ b/src/uu/du/src/du.rs @@ -442,23 +442,19 @@ fn safe_du( // Handle symlinks with -L option // For safe traversal with -L, we skip symlinks to directories entirely // and let the non-safe traversal handle them at the top level - let (entry_stat, is_dir) = if is_symlink && options.dereference == Deref::All { + if is_symlink && options.dereference == Deref::All { // Skip symlinks to directories when using safe traversal with -L // They will be handled by regular traversal continue; - } else { - let is_dir = (lstat.st_mode & S_IFMT) == S_IFDIR; - (lstat, is_dir) - }; + } - let file_info = if entry_stat.st_ino != 0 { - Some(FileInfo { - file_id: entry_stat.st_ino as u128, - dev_id: entry_stat.st_dev, - }) - } else { - None - }; + let is_dir = (lstat.st_mode & S_IFMT) == S_IFDIR; + let entry_stat = lstat; + + let file_info = (entry_stat.st_ino != 0).then_some(FileInfo { + file_id: entry_stat.st_ino as u128, + dev_id: entry_stat.st_dev, + }); // For safe traversal, we need to handle stats differently // We can't use std::fs::Metadata since that requires the full path