Skip to content

Commit e9ca618

Browse files
committed
refactor(uu/rm): extract device skip logic into reusable function
Extract the logic for checking and skipping directories on different devices into a new `should_skip_different_device_with_dev` function. This refactoring improves code organization by consolidating the device check, error handling, and preserve-root logic into a single, reusable Unix-specific function, while providing a no-op for non-Unix platforms. The change maintains existing behavior but enhances maintainability.
1 parent a10928d commit e9ca618

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed

src/uu/rm/src/platform/linux.rs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ use uucore::show_error;
2020
use uucore::translate;
2121

2222
use super::super::{
23-
InteractiveMode, Options, RmError, is_dir_empty, is_readable_metadata, prompt_descend,
24-
prompt_dir, prompt_file, remove_file, show_permission_denied_error, show_removal_error,
25-
verbose_removed_directory, verbose_removed_file,
23+
InteractiveMode, Options, is_dir_empty, is_readable_metadata, prompt_descend, prompt_dir,
24+
prompt_file, remove_file, should_skip_different_device_with_dev, show_permission_denied_error,
25+
show_removal_error, verbose_removed_directory, verbose_removed_file,
2626
};
2727

2828
/// Whether the given file or directory is readable.
@@ -314,22 +314,16 @@ pub fn safe_remove_dir_recursive_impl(
314314
let is_dir = (entry_stat.st_mode & libc::S_IFMT) == libc::S_IFDIR;
315315

316316
if is_dir {
317-
if check_device {
318-
if let Some(parent_dev_id) = parent_dev_id {
319-
if entry_stat.st_dev != parent_dev_id {
320-
show_error!(
321-
"{}",
322-
RmError::SkippingDirectoryOnDifferentDevice(
323-
entry_path.as_os_str().to_os_string()
324-
)
325-
);
326-
if options.preserve_root_all {
327-
show_error!("{}", RmError::PreserveRootAllInEffect);
328-
}
329-
error = true;
330-
continue;
331-
}
332-
}
317+
if check_device
318+
&& should_skip_different_device_with_dev(
319+
parent_dev_id,
320+
entry_stat.st_dev,
321+
options,
322+
&entry_path,
323+
)
324+
{
325+
error = true;
326+
continue;
333327
}
334328
// Ask user if they want to descend into this directory
335329
if options.interactive == InteractiveMode::Always

src/uu/rm/src/rm.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -615,9 +615,29 @@ fn should_skip_different_device(
615615
metadata: &Metadata,
616616
options: &Options,
617617
path: &Path,
618+
) -> bool {
619+
should_skip_different_device_with_dev(parent_dev_id, metadata.dev(), options, path)
620+
}
621+
622+
#[cfg(not(unix))]
623+
fn should_skip_different_device(
624+
_parent_dev_id: Option<u64>,
625+
_metadata: &Metadata,
626+
_options: &Options,
627+
_path: &Path,
628+
) -> bool {
629+
false
630+
}
631+
632+
#[cfg(unix)]
633+
fn should_skip_different_device_with_dev(
634+
parent_dev_id: Option<u64>,
635+
dev_id: u64,
636+
options: &Options,
637+
path: &Path,
618638
) -> bool {
619639
if let Some(parent_dev_id) = parent_dev_id {
620-
if metadata.dev() != parent_dev_id {
640+
if dev_id != parent_dev_id {
621641
show_error!(
622642
"{}",
623643
RmError::SkippingDirectoryOnDifferentDevice(path.as_os_str().to_os_string())
@@ -632,9 +652,9 @@ fn should_skip_different_device(
632652
}
633653

634654
#[cfg(not(unix))]
635-
fn should_skip_different_device(
655+
fn should_skip_different_device_with_dev(
636656
_parent_dev_id: Option<u64>,
637-
_metadata: &Metadata,
657+
_dev_id: u64,
638658
_options: &Options,
639659
_path: &Path,
640660
) -> bool {

0 commit comments

Comments
 (0)