Skip to content

Commit

Permalink
improve
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvestre committed Dec 28, 2024
1 parent 577e2bf commit 9fc1a68
Showing 1 changed file with 76 additions and 6 deletions.
82 changes: 76 additions & 6 deletions src/uu/rm/src/rm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,11 +364,12 @@ fn get_device_id(_p: &Path) -> Option<u64> {

/// Checks if the given path is on the same device as its parent.
/// Returns false if the `one_fs` option is enabled and devices differ.

Check failure on line 366 in src/uu/rm/src/rm.rs

View workflow job for this annotation

GitHub Actions / Style and Lint (ubuntu-24.04, unix)

ERROR: `cargo clippy`: empty line after doc comment (file:'src/uu/rm/src/rm.rs', line:366)
fn check_one_fs(path: &Path, options: &Options) -> bool {
/*fn check_one_fs(path: &Path, options: &Options) -> bool {
if !options.one_fs && options.preserve_root != PreserveRoot::YesAll {
return true;
}

println!("checking {}", path.display());
println!("parent: {:?}", fs::canonicalize(path).ok().and_then(|p| p.parent().map(Path::to_path_buf)));
// as we can get relative path, we need to canonicalize
// and manage potential errors
let parent_device = fs::canonicalize(path)
Expand All @@ -377,6 +378,75 @@ fn check_one_fs(path: &Path, options: &Options) -> bool {
.as_deref()
.and_then(get_device_id);
let current_device = get_device_id(path);
println!("parent_device: {:?}, current_device: {:?}", parent_device, current_device);
if parent_device != current_device {
show_error!(
"skipping {}, since it's on a different device",
path.quote()
);
if options.preserve_root == PreserveRoot::YesAll {
show_error!("and --preserve-root=all is in effect");
}
return false;
}
true
}*/
/*
fn check_one_fs(path: &Path, options: &Options) -> bool {
if !options.one_fs && options.preserve_root != PreserveRoot::YesAll {
return true;
}
let parent_device = path.parent().and_then(get_device_id);
let current_device = get_device_id(path);
println!("parent_device: {:?}, current_device: {:?}", parent_device, current_device);
if parent_device != current_device {
show_error!(
"skipping {}, since it's on a different device",
path.quote()
);
if options.preserve_root == PreserveRoot::YesAll {
show_error!("and --preserve-root=all is in effect");
}
return false;
}
true
}
*/

fn check_one_fs(path: &Path, options: &Options) -> bool {
if !options.one_fs && options.preserve_root != PreserveRoot::YesAll {
return true;
}

// Attempt to canonicalize the path
let path_canon = match path.canonicalize() {
Ok(p) => p,
Err(_) => {
// If we can't canonicalize, fallback to original
// or handle the error differently
path.to_path_buf()
}
};

let parent_path = path_canon
.parent()
.map(|p| p.to_path_buf())
.unwrap_or_else(|| path_canon.clone());

let parent_device = get_device_id(&parent_path);
let current_device = get_device_id(&path_canon);

println!(
"parent_device: {:?}, current_device: {:?}",
parent_device, current_device
);

if parent_device != current_device {
show_error!(
"skipping {}, since it's on a different device",
Expand All @@ -395,10 +465,6 @@ fn check_one_fs(path: &Path, options: &Options) -> bool {
fn handle_dir(path: &Path, options: &Options) -> bool {
let mut had_err = false;

if !check_one_fs(path, options) {
return true;
}

let path = clean_trailing_slashes(path);
if path_is_current_or_parent_directory(path) {
show_error!(
Expand All @@ -408,6 +474,10 @@ fn handle_dir(path: &Path, options: &Options) -> bool {
return true;
}

if !check_one_fs(path, options) {
return true;
}

let is_root = path.has_root() && path.parent().is_none();
if options.recursive && (!is_root || options.preserve_root == PreserveRoot::No) {
if options.interactive != InteractiveMode::Always && !options.verbose {
Expand Down

0 comments on commit 9fc1a68

Please sign in to comment.