cp: fix cp -f f loop when loop is a symlink loop

Fix: tests/cp/thru-dangling.sh
This commit is contained in:
Sylvestre Ledru 2023-05-02 23:22:13 +02:00
parent 6547bec2ef
commit 832fd2d2c6
2 changed files with 18 additions and 3 deletions

View file

@ -38,7 +38,8 @@ use uucore::backup_control::{self, BackupMode};
use uucore::display::Quotable;
use uucore::error::{set_exit_code, UClapError, UError, UResult, UUsageError};
use uucore::fs::{
canonicalize, paths_refer_to_same_file, FileInformation, MissingHandling, ResolveMode,
canonicalize, is_symlink_loop, paths_refer_to_same_file, FileInformation, MissingHandling,
ResolveMode,
};
use uucore::{crash, format_usage, help_about, help_usage, prompt_yes, show_error, show_warning};
@ -1388,11 +1389,10 @@ fn handle_existing_dest(
backup_dest(dest, &backup_path)?;
}
}
match options.overwrite {
// FIXME: print that the file was removed if --verbose is enabled
OverwriteMode::Clobber(ClobberMode::Force) => {
if fs::metadata(dest)?.permissions().readonly() {
if is_symlink_loop(dest) || fs::metadata(dest)?.permissions().readonly() {
fs::remove_file(dest)?;
}
}
@ -1498,6 +1498,7 @@ fn copy_file(
options.overwrite,
OverwriteMode::Clobber(ClobberMode::RemoveDestination)
)
&& !is_symlink_loop(dest)
&& std::env::var_os("POSIXLY_CORRECT").is_none()
{
return Err(Error::Error(format!(

View file

@ -2309,6 +2309,20 @@ fn test_remove_destination_symbolic_link_loop() {
assert!(at.file_exists("loop"));
}
#[test]
#[cfg(not(windows))]
fn test_cp_symbolic_link_loop() {
let (at, mut ucmd) = at_and_ucmd!();
at.symlink_file("loop", "loop");
at.plus("loop");
at.touch("f");
ucmd.args(&["-f", "f", "loop"])
.succeeds()
.no_stdout()
.no_stderr();
assert!(at.file_exists("loop"));
}
/// Test that copying a directory to itself is disallowed.
#[test]
fn test_copy_directory_to_itself_disallowed() {