Skip to content

Commit 3226212

Browse files
committed
install: fixes issue uutils#7795
1 parent 18db15e commit 3226212

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/uu/install/src/install.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use filetime::{FileTime, set_file_times};
1313
use std::fmt::Debug;
1414
use std::fs::File;
1515
use std::fs::{self, metadata};
16-
use std::path::{MAIN_SEPARATOR, Path, PathBuf};
16+
use std::path::{self, Path, PathBuf, MAIN_SEPARATOR};
1717
use std::process;
1818
use thiserror::Error;
1919
use uucore::backup_control::{self, BackupMode};
@@ -101,6 +101,9 @@ enum InstallError {
101101

102102
#[error("cannot overwrite directory {} with non-directory {}", .0.quote(), .1.quote())]
103103
OverrideDirectoryFailed(PathBuf, PathBuf),
104+
105+
#[error("'{0}' and '{1}' are the same file")]
106+
SameFile(PathBuf, PathBuf),
104107
}
105108

106109
impl UError for InstallError {
@@ -751,6 +754,10 @@ fn copy_normal_file(from: &Path, to: &Path) -> UResult<()> {
751754
/// Returns an empty Result or an error in case of failure.
752755
///
753756
fn copy_file(from: &Path, to: &Path) -> UResult<()> {
757+
if path::absolute(from)? == path::absolute(to)? {
758+
return Err(InstallError::SameFile(from.to_path_buf(), to.to_path_buf()).into());
759+
}
760+
754761
if to.is_dir() && !from.is_dir() {
755762
return Err(InstallError::OverrideDirectoryFailed(
756763
to.to_path_buf().clone(),

tests/by-util/test_install.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,3 +1778,15 @@ fn test_install_failing_copy_file_to_target_contain_subdir_with_same_name() {
17781778
.fails()
17791779
.stderr_contains("cannot overwrite directory");
17801780
}
1781+
1782+
#[test]
1783+
fn test_install_same_file() {
1784+
let (at, mut ucmd) = at_and_ucmd!();
1785+
let file = "file";
1786+
1787+
at.touch(file);
1788+
ucmd.arg(file)
1789+
.arg(".")
1790+
.fails()
1791+
.stderr_contains("'file' and './file' are the same file");
1792+
}

0 commit comments

Comments
 (0)