1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-30 12:37:49 +00:00

rmdir: use modern filename display

This commit is contained in:
Jan Verbeek 2021-08-29 20:18:52 +02:00
parent 0e1f8f7b34
commit 03b2d40154

View file

@ -14,6 +14,7 @@ use clap::{crate_version, App, Arg};
use std::fs::{read_dir, remove_dir}; use std::fs::{read_dir, remove_dir};
use std::io; use std::io;
use std::path::Path; use std::path::Path;
use uucore::display::Quotable;
use uucore::error::{set_exit_code, strip_errno, UResult}; use uucore::error::{set_exit_code, strip_errno, UResult};
use uucore::util_name; use uucore::util_name;
@ -77,27 +78,23 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
Ok(path.metadata()?.file_type().is_dir()) Ok(path.metadata()?.file_type().is_dir())
} }
let path = path.as_os_str().as_bytes(); let bytes = path.as_os_str().as_bytes();
if error.raw_os_error() == Some(libc::ENOTDIR) && path.ends_with(b"/") { if error.raw_os_error() == Some(libc::ENOTDIR) && bytes.ends_with(b"/") {
// Strip the trailing slash or .symlink_metadata() will follow the symlink // Strip the trailing slash or .symlink_metadata() will follow the symlink
let path: &Path = OsStr::from_bytes(&path[..path.len() - 1]).as_ref(); let no_slash: &Path = OsStr::from_bytes(&bytes[..bytes.len() - 1]).as_ref();
if is_symlink(path).unwrap_or(false) if is_symlink(no_slash).unwrap_or(false)
&& points_to_directory(path).unwrap_or(true) && points_to_directory(no_slash).unwrap_or(true)
{ {
show_error!( show_error!(
"failed to remove '{}/': Symbolic link not followed", "failed to remove {}: Symbolic link not followed",
path.display() path.quote()
); );
continue; continue;
} }
} }
} }
show_error!( show_error!("failed to remove {}: {}", path.quote(), strip_errno(&error));
"failed to remove '{}': {}",
path.display(),
strip_errno(&error)
);
} }
} }
@ -125,7 +122,7 @@ fn remove(mut path: &Path, opts: Opts) -> Result<(), Error<'_>> {
fn remove_single(path: &Path, opts: Opts) -> Result<(), Error<'_>> { fn remove_single(path: &Path, opts: Opts) -> Result<(), Error<'_>> {
if opts.verbose { if opts.verbose {
println!("{}: removing directory, '{}'", util_name(), path.display()); println!("{}: removing directory, {}", util_name(), path.quote());
} }
remove_dir(path).map_err(|error| Error { error, path }) remove_dir(path).map_err(|error| Error { error, path })
} }