Skip to content

Commit b5f9578

Browse files
committed
the /dev/null at source case has same issue
1 parent 5d5b430 commit b5f9578

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

src/uu/install/src/install.rs

+10-13
Original file line numberDiff line numberDiff line change
@@ -736,18 +736,6 @@ fn perform_backup(to: &Path, b: &Behavior) -> UResult<Option<PathBuf>> {
736736
}
737737
}
738738

739-
fn copy_file_source_not_dev_null(from: &Path, to: &Path) -> io::Result<()> {
740-
let dest_meta_res = fs::symlink_metadata(to);
741-
if let Ok(dest_meta) = dest_meta_res {
742-
if dest_meta.is_symlink() {
743-
// fs::copy fails if destination is a invalid symlink
744-
// so lets just remove all symlinks at destination before copy
745-
fs::remove_file(to)?;
746-
}
747-
}
748-
fs::copy(from, to).map(|_| ())
749-
}
750-
751739
/// Copy a file from one path to another.
752740
///
753741
/// # Parameters
@@ -760,6 +748,15 @@ fn copy_file_source_not_dev_null(from: &Path, to: &Path) -> io::Result<()> {
760748
/// Returns an empty Result or an error in case of failure.
761749
///
762750
fn copy_file(from: &Path, to: &Path) -> UResult<()> {
751+
let dest_meta_res = fs::symlink_metadata(to);
752+
if let Ok(dest_meta) = dest_meta_res {
753+
if dest_meta.is_symlink() {
754+
// fs::copy fails if destination is a invalid symlink
755+
// so lets just remove all symlinks at destination before copy
756+
fs::remove_file(to)?;
757+
}
758+
}
759+
763760
if from.as_os_str() == "/dev/null" {
764761
/* workaround a limitation of fs::copy
765762
* https://github.com/rust-lang/rust/issues/79390
@@ -770,7 +767,7 @@ fn copy_file(from: &Path, to: &Path) -> UResult<()> {
770767
);
771768
}
772769
} else {
773-
let result = copy_file_source_not_dev_null(from, to);
770+
let result = fs::copy(from, to);
774771
if let Err(err) = result {
775772
return Err(
776773
InstallError::InstallFailed(from.to_path_buf(), to.to_path_buf(), err).into(),

tests/by-util/test_install.rs

+31
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,37 @@ fn test_install_on_invalid_link_at_destination() {
733733
.no_stdout();
734734
}
735735

736+
#[cfg(all(unix, feature = "chmod"))]
737+
#[test]
738+
// FixME: Freebsd fails on 'No such file or directory'
739+
#[cfg(not(target_os = "freebsd"))]
740+
fn test_install_on_invalid_link_at_destination_and_dev_null_at_source() {
741+
let scene = TestScenario::new(util_name!());
742+
743+
let at = &scene.fixtures;
744+
at.mkdir("src");
745+
at.mkdir("dest");
746+
let src_dir = at.plus("src");
747+
let dst_dir = at.plus("dest");
748+
749+
at.touch("test.sh");
750+
at.symlink_file(
751+
"/opt/FakeDestination",
752+
&dst_dir.join("test.sh").to_string_lossy(),
753+
);
754+
scene.ccmd("chmod").arg("+x").arg("test.sh").succeeds();
755+
at.symlink_file("test.sh", &src_dir.join("test.sh").to_string_lossy());
756+
757+
scene
758+
.ucmd()
759+
.current_dir(&src_dir)
760+
.arg("/dev/null")
761+
.arg(dst_dir.join("test.sh"))
762+
.succeeds()
763+
.no_stderr()
764+
.no_stdout();
765+
}
766+
736767
#[test]
737768
#[cfg(not(windows))]
738769
fn test_install_and_strip_with_invalid_program() {

0 commit comments

Comments
 (0)