Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cp: fail when trying to copy to read only file on mac #5261

Merged
merged 5 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
19 changes: 17 additions & 2 deletions src/uu/cp/src/platform/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,23 @@
{
// clonefile(2) fails if the destination exists. Remove it and try again. Do not
// bother to check if removal worked because we're going to try to clone again.
let _ = fs::remove_file(dest);
error = pfn(src.as_ptr(), dst.as_ptr(), 0);
// first lets make sure the dest file is not read only
match fs::metadata(dest) {
Ok(md) => {
if md.permissions().readonly() {
return Err(format!(
"failed to clone {source:?} from {dest:?}: permission denied"
PGIII marked this conversation as resolved.
Show resolved Hide resolved
)
.into());
} else {
let _ = fs::remove_file(dest);
error = pfn(src.as_ptr(), dst.as_ptr(), 0);
}
}
Err(e) => {
return Err(format!("failed to clone {source:?} from {dest:?}: {e}").into());
PGIII marked this conversation as resolved.
Show resolved Hide resolved
}

Check warning on line 81 in src/uu/cp/src/platform/macos.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/cp/src/platform/macos.rs#L79-L81

Added lines #L79 - L81 were not covered by tests
}
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions tests/by-util/test_cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3243,3 +3243,19 @@ fn test_cp_only_source_no_target() {
panic!("Failure: stderr was \n{stderr_str}");
}
}

#[test]
fn test_cp_dest_no_permissions() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;

at.touch("valid.txt");
at.touch("invalid_perms.txt");
at.set_readonly("invalid_perms.txt");

ts.ucmd()
.args(&["valid.txt", "invalid_perms.txt"])
.fails()
.stderr_contains("invalid_perms.txt")
.stderr_contains("denied");
}
Loading