Skip to content
Draft
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
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
* `dir_exists()` follows relative symlinks in non-current directories
(@heavywatal, #395).

* `file_copy(overwrite = TRUE)` no longer fails with `[EPERM]` when the
destination file is owned by a different user but the current user has write
permission via group membership (#487).

# fs 1.6.6

* No changes.
Expand Down
14 changes: 14 additions & 0 deletions src/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,20 @@ fs_copyfile_(SEXP path_sxp, SEXP new_path_sxp, SEXP overwrite_sxp) {
uv_fs_t req;
const char* p = CHAR(STRING_ELT(path_sxp, i));
const char* n = CHAR(STRING_ELT(new_path_sxp, i));

// When overwriting, unlink the destination first so that fchmod() does not
// fail with EPERM when the destination is owned by a different user (but
// the current user has write permission via group membership).
if (overwrite) {
uv_fs_t unlink_req;
uv_fs_unlink(uv_default_loop(), &unlink_req, n, NULL);
// Ignore ENOENT (file didn't exist); propagate other errors.
if (unlink_req.result < 0 && unlink_req.result != UV_ENOENT) {
stop_for_error(unlink_req, "Failed to remove '%s'", n);
}
uv_fs_req_cleanup(&unlink_req);
}

uv_fs_copyfile(
uv_default_loop(),
&req,
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-copy.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ describe("file_copy", {
expect_equal(readLines("bar2"), readLines("bar"))
})
})
it("can overwrite an existing file", {
with_dir_tree(list("foo" = "original", "bar" = "new content"), {
file_copy("bar", "foo", overwrite = TRUE)
expect_equal(readLines("foo"), "new content")
})
})
it("errors on missing input", {
expect_error(file_copy(NA, "foo2"), class = "invalid_argument")
expect_error(file_copy("foo", NA), class = "invalid_argument")
Expand Down