|
6 | 6 | // spell-checker:ignore (path) eacces inacc |
7 | 7 |
|
8 | 8 | use clap::{builder::ValueParser, crate_version, parser::ValueSource, Arg, ArgAction, Command}; |
| 9 | +#[cfg(not(windows))] |
9 | 10 | use regex::bytes::Regex; |
10 | 11 | use std::collections::VecDeque; |
11 | 12 | use std::ffi::{OsStr, OsString}; |
12 | 13 | use std::fs::{self, File, Metadata}; |
13 | 14 | use std::io::ErrorKind; |
14 | 15 | use std::ops::BitOr; |
| 16 | +#[cfg(not(windows))] |
15 | 17 | use std::os::unix::ffi::OsStrExt; |
| 18 | + |
16 | 19 | use std::path::{Path, PathBuf}; |
17 | 20 | use uucore::display::Quotable; |
18 | 21 | use uucore::error::{UResult, USimpleError, UUsageError}; |
@@ -296,28 +299,31 @@ pub fn remove(files: &[&OsStr], options: &Options) -> bool { |
296 | 299 | had_err = match file.symlink_metadata() { |
297 | 300 | Ok(metadata) => { |
298 | 301 | #[cfg(not(windows))] |
299 | | - let re = Regex::new(r"/\.{1,2}\/*$").unwrap(); |
300 | | - if re.is_match(filename.as_bytes()) { |
301 | | - show_error!( |
302 | | - "refusing to remove '.' or '..' directory: skipping '{:}'", |
303 | | - clean_trailing_slashes(filename).display() |
304 | | - ); |
305 | | - true |
306 | | - } else if metadata.is_dir() { |
307 | | - handle_dir(file, options) |
308 | | - } else if is_symlink_dir(&metadata) { |
309 | | - remove_dir(file, options) |
310 | | - } else { |
311 | | - remove_file(file, options) |
| 302 | + { |
| 303 | + let re = Regex::new(r"/\.{1,2}\/*$").unwrap(); |
| 304 | + if re.is_match(filename.as_bytes()) { |
| 305 | + show_error!( |
| 306 | + "refusing to remove '.' or '..' directory: skipping '{:}'", |
| 307 | + clean_trailing_slashes(filename).display() |
| 308 | + ); |
| 309 | + true |
| 310 | + } else if metadata.is_dir() { |
| 311 | + handle_dir(file, options) |
| 312 | + } else if is_symlink_dir(&metadata) { |
| 313 | + remove_dir(file, options) |
| 314 | + } else { |
| 315 | + remove_file(file, options) |
| 316 | + } |
312 | 317 | } |
313 | | - |
314 | 318 | #[cfg(windows)] |
315 | | - if metadata.is_dir() { |
316 | | - handle_dir(file, options) |
317 | | - } else if is_symlink_dir(&metadata) { |
318 | | - remove_dir(file, options) |
319 | | - } else { |
320 | | - remove_file(file, options) |
| 319 | + { |
| 320 | + if metadata.is_dir() { |
| 321 | + handle_dir(file, options) |
| 322 | + } else if is_symlink_dir(&metadata) { |
| 323 | + remove_dir(file, options) |
| 324 | + } else { |
| 325 | + remove_file(file, options) |
| 326 | + } |
321 | 327 | } |
322 | 328 | } |
323 | 329 | Err(_e) => { |
@@ -606,6 +612,7 @@ fn handle_writable_directory(path: &Path, options: &Options, metadata: &Metadata |
606 | 612 | } |
607 | 613 | } |
608 | 614 |
|
| 615 | +#[cfg(not(windows))] |
609 | 616 | /// Removes trailing slashes, for example 'd/../////' yield 'd/../' required to fix rm-r4 GNU test |
610 | 617 | fn clean_trailing_slashes(path: &OsStr) -> &Path { |
611 | 618 | let path_bytes = path.as_bytes(); |
|
0 commit comments