Skip to content

Commit

Permalink
Merge pull request #6588 from matrixhead/part-symlink
Browse files Browse the repository at this point in the history
cp: gnu test case `part-symlink` compatibility
  • Loading branch information
sylvestre authored Jul 26, 2024
2 parents 2c92020 + 36237a2 commit a55d9b3
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 33 deletions.
65 changes: 32 additions & 33 deletions src/uu/cp/src/cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1669,34 +1669,33 @@ fn handle_existing_dest(
backup_dest(dest, &backup_path, is_dest_removed)?;
}
}
match options.overwrite {
// FIXME: print that the file was removed if --verbose is enabled
OverwriteMode::Clobber(ClobberMode::Force) => {
if !is_dest_removed
&& (is_symlink_loop(dest) || fs::metadata(dest)?.permissions().readonly())
{
if !is_dest_removed {
match options.overwrite {
// FIXME: print that the file was removed if --verbose is enabled
OverwriteMode::Clobber(ClobberMode::Force) => {
if is_symlink_loop(dest) || fs::metadata(dest)?.permissions().readonly() {
fs::remove_file(dest)?;
}
}
OverwriteMode::Clobber(ClobberMode::RemoveDestination) => {
fs::remove_file(dest)?;
}
}
OverwriteMode::Clobber(ClobberMode::RemoveDestination) => {
fs::remove_file(dest)?;
}
OverwriteMode::Clobber(ClobberMode::Standard) => {
// Consider the following files:
//
// * `src/f` - a regular file
// * `src/link` - a hard link to `src/f`
// * `dest/src/f` - a different regular file
//
// In this scenario, if we do `cp -a src/ dest/`, it is
// possible that the order of traversal causes `src/link`
// to get copied first (to `dest/src/link`). In that case,
// in order to make sure `dest/src/link` is a hard link to
// `dest/src/f` and `dest/src/f` has the contents of
// `src/f`, we delete the existing file to allow the hard
// linking.

if options.preserve_hard_links()
OverwriteMode::Clobber(ClobberMode::Standard) => {
// Consider the following files:
//
// * `src/f` - a regular file
// * `src/link` - a hard link to `src/f`
// * `dest/src/f` - a different regular file
//
// In this scenario, if we do `cp -a src/ dest/`, it is
// possible that the order of traversal causes `src/link`
// to get copied first (to `dest/src/link`). In that case,
// in order to make sure `dest/src/link` is a hard link to
// `dest/src/f` and `dest/src/f` has the contents of
// `src/f`, we delete the existing file to allow the hard
// linking.

if options.preserve_hard_links()
// only try to remove dest file only if the current source
// is hardlink to a file that is already copied
&& copied_files.contains_key(
Expand All @@ -1705,14 +1704,13 @@ fn handle_existing_dest(
options.dereference(source_in_command_line),
)
.context(format!("cannot stat {}", source.quote()))?,
)
&& !is_dest_removed
{
fs::remove_file(dest)?;
) {
fs::remove_file(dest)?;
}
}
}
_ => (),
};
_ => (),
};
}

Ok(())
}
Expand Down Expand Up @@ -2044,6 +2042,7 @@ fn copy_file(
options.overwrite,
OverwriteMode::Clobber(ClobberMode::RemoveDestination)
)
&& options.backup == BackupMode::NoBackup
{
fs::remove_file(dest)?;
}
Expand Down
20 changes: 20 additions & 0 deletions tests/by-util/test_cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5696,3 +5696,23 @@ fn test_cp_parents_absolute_path() {
let res = format!("dest{}/a/b/f", at.root_dir_resolved());
at.file_exists(res);
}

// make sure that cp backup dest symlink before removing it.
#[test]
fn test_cp_with_options_backup_and_rem_when_dest_is_symlink() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.write("file", "xyz");
at.mkdir("inner_dir");
at.write("inner_dir/inner_file", "abc");
at.relative_symlink_file("inner_file", "inner_dir/sl");
scene
.ucmd()
.args(&["-b", "--rem", "file", "inner_dir/sl"])
.succeeds();
assert!(at.file_exists("inner_dir/inner_file"));
assert_eq!(at.read("inner_dir/inner_file"), "abc");
assert!(at.symlink_exists("inner_dir/sl~"));
assert!(!at.symlink_exists("inner_dir/sl"));
assert_eq!(at.read("inner_dir/sl"), "xyz");
}

0 comments on commit a55d9b3

Please sign in to comment.