Skip to content

Commit

Permalink
cp: fix canonicalize usage issue, don't dereference when checking file
Browse files Browse the repository at this point in the history
to be the same, use FileInformation instead
  • Loading branch information
niyaznigmatullin committed Jul 7, 2022
1 parent 6e68272 commit f7b77f5
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/uu/cp/src/cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1245,15 +1245,15 @@ fn backup_dest(dest: &Path, backup_path: &Path) -> CopyResult<PathBuf> {
}

fn handle_existing_dest(source: &Path, dest: &Path, options: &Options) -> CopyResult<()> {
if paths_refer_to_same_file(source, dest)? {
if paths_refer_to_same_file(source, dest, options.dereference) {
return Err(format!("{}: same file", context_for(source, dest)).into());
}

options.overwrite.verify(dest)?;

let backup_path = backup_control::get_backup_path(options.backup, dest, &options.backup_suffix);
if let Some(backup_path) = backup_path {
if paths_refer_to_same_file(source, &backup_path)? {
if paths_refer_to_same_file(source, &backup_path, true) {
return Err(format!(
"backing up {} might destroy source; {} not copied",
dest.quote(),
Expand Down Expand Up @@ -1685,12 +1685,15 @@ pub fn localize_to_target(root: &Path, source: &Path, target: &Path) -> CopyResu
Ok(target.join(&local_to_root))
}

pub fn paths_refer_to_same_file(p1: &Path, p2: &Path) -> io::Result<bool> {
pub fn paths_refer_to_same_file(p1: &Path, p2: &Path, dereference: bool) -> bool {
// We have to take symlinks and relative paths into account.
let pathbuf1 = canonicalize(p1, MissingHandling::Normal, ResolveMode::Logical)?;
let pathbuf2 = canonicalize(p2, MissingHandling::Normal, ResolveMode::Logical)?;
let res1 = FileInformation::from_path(p1, dereference);
let res2 = FileInformation::from_path(p2, dereference);

Ok(pathbuf1 == pathbuf2)
match (res1, res2) {
(Ok(info1), Ok(info2)) => info1 == info2,
_ => false,
}
}

pub fn path_has_prefix(p1: &Path, p2: &Path) -> io::Result<bool> {
Expand Down

0 comments on commit f7b77f5

Please sign in to comment.