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

uucore: remove is_symlink function

Since Rust 1.58 there is `is_symlink` method for `Path`
This commit is contained in:
Niyaz Nigmatullin 2022-09-23 20:32:09 +03:00
parent 84a741fe91
commit 90babe2775
6 changed files with 20 additions and 33 deletions

View file

@ -14,7 +14,6 @@ use std::path::Path;
use uucore::display::Quotable;
use uucore::error::{ExitCode, UResult, USimpleError, UUsageError};
use uucore::fs::display_permissions_unix;
use uucore::fs::is_symlink;
use uucore::libc::mode_t;
#[cfg(not(windows))]
use uucore::mode;
@ -195,7 +194,7 @@ impl Chmoder {
let filename = &filename[..];
let file = Path::new(filename);
if !file.exists() {
if is_symlink(file) {
if file.is_symlink() {
println!(
"failed to change mode of {} from 0000 (---------) to 0000 (---------)",
filename.quote()
@ -237,10 +236,10 @@ impl Chmoder {
fn walk_dir(&self, file_path: &Path) -> UResult<()> {
let mut r = self.chmod_file(file_path);
if !is_symlink(file_path) && file_path.is_dir() {
if !file_path.is_symlink() && file_path.is_dir() {
for dir_entry in file_path.read_dir()? {
let path = dir_entry?.path();
if !is_symlink(&path) {
if !path.is_symlink() {
r = self.walk_dir(path.as_path());
}
}
@ -262,7 +261,7 @@ impl Chmoder {
let fperm = match fs::metadata(file) {
Ok(meta) => meta.mode() & 0o7777,
Err(err) => {
if is_symlink(file) {
if file.is_symlink() {
if self.verbose {
println!(
"neither symbolic link {} nor referent has been changed",

View file

@ -47,7 +47,7 @@ use std::str::FromStr;
use std::string::ToString;
use uucore::backup_control::{self, BackupMode};
use uucore::error::{set_exit_code, UClapError, UError, UResult, UUsageError};
use uucore::fs::{canonicalize, is_symlink, MissingHandling, ResolveMode};
use uucore::fs::{canonicalize, MissingHandling, ResolveMode};
use walkdir::WalkDir;
quick_error! {
@ -1141,7 +1141,7 @@ fn copy_directory(
};
let local_to_target = target.join(&local_to_root_parent);
if is_symlink(p.path()) && !options.dereference {
if p.path().is_symlink() && !options.dereference {
copy_link(&path, &local_to_target, symlinked_files)?;
} else if path.is_dir() && !local_to_target.exists() {
if target.is_file() {
@ -1164,7 +1164,7 @@ fn copy_directory(
) {
Ok(_) => Ok(()),
Err(err) => {
if is_symlink(source) {
if source.is_symlink() {
// silent the error with a symlink
// In case we do --archive, we might copy the symlink
// before the file itself
@ -1219,7 +1219,7 @@ fn copy_attribute(source: &Path, dest: &Path, attribute: &Attribute) -> CopyResu
// permissions of a symbolic link. In that case, we just
// do nothing, since every symbolic link has the same
// permissions.
if !is_symlink(dest) {
if !dest.is_symlink() {
fs::set_permissions(dest, source_metadata.permissions()).context(context)?;
// FIXME: Implement this for windows as well
#[cfg(feature = "feat_acl")]
@ -1254,7 +1254,7 @@ fn copy_attribute(source: &Path, dest: &Path, attribute: &Attribute) -> CopyResu
Attribute::Timestamps => {
let atime = FileTime::from_last_access_time(&source_metadata);
let mtime = FileTime::from_last_modification_time(&source_metadata);
if is_symlink(dest) {
if dest.is_symlink() {
filetime::set_symlink_file_times(dest, atime, mtime)?;
} else {
filetime::set_file_times(dest, atime, mtime)?;
@ -1397,7 +1397,7 @@ fn copy_file(
}
// Fail if dest is a dangling symlink or a symlink this program created previously
if is_symlink(dest) {
if dest.is_symlink() {
if FileInformation::from_path(dest, false)
.map(|info| symlinked_files.contains(&info))
.unwrap_or(false)
@ -1539,7 +1539,7 @@ fn copy_file(
};
// TODO: implement something similar to gnu's lchown
if !is_symlink(&dest) {
if !dest.is_symlink() {
// Here, to match GNU semantics, we quietly ignore an error
// if a user does not have the correct ownership to modify
// the permissions of a file.
@ -1648,7 +1648,7 @@ fn copy_link(
} else {
// we always need to remove the file to be able to create a symlink,
// even if it is writeable.
if is_symlink(dest) || dest.is_file() {
if dest.is_symlink() || dest.is_file() {
fs::remove_file(dest)?;
}
dest.into()

View file

@ -14,7 +14,7 @@ use clap::{crate_version, Arg, Command};
use uucore::display::Quotable;
use uucore::error::{FromIo, UError, UResult};
use uucore::format_usage;
use uucore::fs::{is_symlink, make_path_relative_to, paths_refer_to_same_file};
use uucore::fs::{make_path_relative_to, paths_refer_to_same_file};
use std::borrow::Cow;
use std::error::Error;
@ -318,7 +318,7 @@ fn link_files_in_dir(files: &[PathBuf], target_dir: &Path, settings: &Settings)
if settings.no_dereference && matches!(settings.overwrite, OverwriteMode::Force) {
// In that case, we don't want to do link resolution
// We need to clean the target
if is_symlink(target_dir) {
if target_dir.is_symlink() {
if target_dir.is_file() {
if let Err(e) = fs::remove_file(target_dir) {
show_error!("Could not update {}: {}", target_dir.quote(), e);
@ -387,7 +387,7 @@ fn link(src: &Path, dst: &Path, settings: &Settings) -> UResult<()> {
src.into()
};
if is_symlink(dst) || dst.exists() {
if dst.is_symlink() || dst.exists() {
backup_path = match settings.backup {
BackupMode::NoBackup => None,
BackupMode::SimpleBackup => Some(simple_backup_path(dst, &settings.suffix)),
@ -415,7 +415,7 @@ fn link(src: &Path, dst: &Path, settings: &Settings) -> UResult<()> {
// In case of error, don't do anything
}
OverwriteMode::Force => {
if !is_symlink(dst) && paths_refer_to_same_file(src, dst, true) {
if !dst.is_symlink() && paths_refer_to_same_file(src, dst, true) {
return Err(LnError::SameFile(src.to_owned(), dst.to_owned()).into());
}
if fs::remove_file(dst).is_ok() {};
@ -427,7 +427,7 @@ fn link(src: &Path, dst: &Path, settings: &Settings) -> UResult<()> {
if settings.symbolic {
symlink(&source, dst)?;
} else {
let p = if settings.logical && is_symlink(&source) {
let p = if settings.logical && source.is_symlink() {
// if we want to have an hard link,
// source is a symlink and -L is passed
// we want to resolve the symlink to create the hardlink

View file

@ -19,8 +19,6 @@ use std::path::Path;
use uucore::display::Quotable;
use uucore::error::{set_exit_code, strip_errno, UResult};
#[cfg(unix)]
use uucore::fs::is_symlink;
use uucore::{format_usage, util_name};
static ABOUT: &str = "Remove the DIRECTORY(ies), if they are empty.";
@ -78,7 +76,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
if error.raw_os_error() == Some(libc::ENOTDIR) && bytes.ends_with(b"/") {
// Strip the trailing slash or .symlink_metadata() will follow the symlink
let no_slash: &Path = OsStr::from_bytes(&bytes[..bytes.len() - 1]).as_ref();
if is_symlink(no_slash) && points_to_directory(no_slash).unwrap_or(true) {
if no_slash.is_symlink() && points_to_directory(no_slash).unwrap_or(true) {
show_error!(
"failed to remove {}: Symbolic link not followed",
path.quote()

View file

@ -235,16 +235,6 @@ pub fn normalize_path(path: &Path) -> PathBuf {
ret
}
/// Decide whether the given path is a symbolic link.
///
/// This function is essentially a backport of the
/// [`std::path::Path::is_symlink`] function that exists in Rust
/// version 1.58 and greater. This can be removed when the minimum
/// supported version of Rust is 1.58.
pub fn is_symlink<P: AsRef<Path>>(path: P) -> bool {
fs::symlink_metadata(path).map_or(false, |m| m.file_type().is_symlink())
}
fn resolve_symlink<P: AsRef<Path>>(path: P) -> IOResult<Option<PathBuf>> {
let result = if fs::symlink_metadata(&path)?.file_type().is_symlink() {
Some(fs::read_link(&path)?)

View file

@ -10,7 +10,7 @@ use crate::error::strip_errno;
use crate::error::UResult;
use crate::error::USimpleError;
pub use crate::features::entries;
use crate::fs::{is_symlink, resolve_relative_path};
use crate::fs::resolve_relative_path;
use crate::show_error;
use clap::Arg;
use clap::ArgMatches;
@ -274,7 +274,7 @@ impl ChownExecutor {
let root = root.as_ref();
// walkdir always dereferences the root directory, so we have to check it ourselves
if self.traverse_symlinks == TraverseSymlinks::None && is_symlink(root) {
if self.traverse_symlinks == TraverseSymlinks::None && root.is_symlink() {
return 0;
}