Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/uu/cp/src/copydir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,14 @@ fn copy_direntry(
// TODO What other kinds of errors, if any, should
// cause us to continue walking the directory?
match err {
CpError::IoErrContext(e, _) if e.kind() == io::ErrorKind::PermissionDenied => {
CpError::IoErrContext(e, _)
if e.kind() == io::ErrorKind::PermissionDenied
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is starting to be a bit complex to understand, maybe move this into a function

&& !source_is_symlink
&& matches!(
fs::File::open(&entry.source_relative),
Err(read_err) if read_err.kind() == io::ErrorKind::PermissionDenied
) =>
{
show!(uio_error!(
e,
"{}",
Expand All @@ -336,6 +343,11 @@ fn copy_direntry(
),
));
}
CpError::IoErrContext(e, context)
if e.kind() == io::ErrorKind::PermissionDenied =>
{
show!(CpError::IoErrContext(e, context));
}
e => return Err(e),
}
}
Expand Down
25 changes: 25 additions & 0 deletions tests/by-util/test_cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3700,6 +3700,31 @@
assert_metadata_eq!(metadata1, metadata2);
}

/// Test for preserving destination-side context when recursive copy fails due to unwritable

Check failure on line 3703 in tests/by-util/test_cp.rs

View workflow job for this annotation

GitHub Actions / Style/spelling (ubuntu-latest, feat_os_unix)

ERROR: `cspell`: Unknown word 'unwritable' (file:'tests/by-util/test_cp.rs', line:3703)
/// destination file.
#[cfg(all(
not(windows),
not(target_os = "android"),
not(target_os = "freebsd"),
not(target_os = "openbsd")
))]
#[test]
fn test_copy_dir_permission_denied_on_destination_reports_destination() {
let (at, mut ucmd) = at_and_ucmd!();
at.mkdir("src");
at.touch("src/foo");
at.set_mode("src/foo", 0o440);

at.mkdir_all("dst/src");
at.touch("dst/src/foo");
at.set_mode("dst/src/foo", 0o440);

ucmd.args(&["-R", "src", "dst"])
.fails_with_code(1)
.stderr_contains("cp: 'src/foo' -> 'dst/src/foo':")
.stderr_contains("Permission denied");
}

/// Test that copying file to itself with backup fails.
#[test]
fn test_same_file_backup() {
Expand Down
Loading