Skip to content

Fix file_copy(overwrite = TRUE) EPERM when destination is owned by another user#502

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/fix-file-copy-overwrite-permission
Draft

Fix file_copy(overwrite = TRUE) EPERM when destination is owned by another user#502
Copilot wants to merge 2 commits intomainfrom
copilot/fix-file-copy-overwrite-permission

Conversation

Copy link

Copilot AI commented Mar 3, 2026

file_copy(overwrite = TRUE) fails with [EPERM] and corrupts the destination when the file is owned by a different user — even if the current user has write permission via group membership. The underlying uv_fs_copyfile calls fchmod() internally to mirror the source's permissions, and fchmod() requires ownership, not just write access.

Changes

  • src/file.cc: Before calling uv_fs_copyfile when overwrite = TRUE, unlink the destination first. The resulting fresh file is owned by the current user, so fchmod() succeeds. ENOENT from the unlink is silently ignored; other errors are propagated.
  • tests/testthat/test-copy.R: Add test asserting file_copy(overwrite = TRUE) correctly replaces an existing file's content.
  • NEWS.md: Document the fix.

This matches the behavior of cp and mirrors the existing approach in link_copy.

Original prompt

This section details on the original issue you should resolve

<issue_title>file_copy with overwrite = TRUE permission problem</issue_title>
<issue_description>I ran into strange behavior with file_copy.

When another user with the same group create file (with rw-rw-rw- permissions) then

library(fs)

file_info("tmp-tst-copy.txt")$permissions # rw-rw-rw-
file_access("tmp-tst-copy.txt", "write") # TRUE

file_copy("tmp-tst-origin.txt", "tmp-tst-copy.txt", overwrite = TRUE)
Error: [EPERM] Failed to copy 'tmp-tst-origin.txt' to 'tmp-tst-copy.txt': operation not permitted

But actually file is removed (so runing command second time works as the destination file does not exists).

There is no problem with file_delete("tmp-tst-copy.txt").
There is no problem with system command cp tmp-tst-origin.txt tmp-tst-copy.txt

Session Info
> sessioninfo::session_info()
─ Session info ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.5.0 (2025-04-11)
 os       Red Hat Enterprise Linux 8.10 (Ootpa)
 system   x86_64, linux-gnu
 ui       RStudio
 language (EN)
 collate  en_US.UTF-8
 ctype    en_US.UTF-8
 tz       Europe/Warsaw
 date     2025-12-02
 rstudio  2025.05.0+496.pro5 Mariposa Orchid (server)
 pandoc   NA
 quarto   1.6.42 @ /usr/lib/rstudio-server/bin/quarto/bin/quartoPackages ─────────────────────────────────────────────
 package     * version date (UTC) lib source
 cli           3.6.5   2025-04-23 [1] RSPM (R 4.5.0)
 crayon        1.5.3   2024-06-20 [1] RSPM (R 4.5.0)
 fs          * 1.6.6   2025-04-12 [1] RSPM (R 4.5.0)
 glue          1.8.0   2024-09-30 [1] RSPM (R 4.5.0)
 lifecycle     1.0.4   2023-11-07 [1] RSPM (R 4.5.0)
 magrittr      2.0.3   2022-03-30 [1] RSPM (R 4.5.0)
 pillar        1.10.2  2025-04-05 [1] RSPM (R 4.5.0)
 pkgconfig     2.0.3   2019-09-22 [1] RSPM (R 4.5.0)
 pkgload       1.4.0   2024-06-28 [1] RSPM (R 4.5.0)
 rlang         1.1.6   2025-04-11 [1] RSPM (R 4.5.0)
 rstudioapi    0.17.1  2024-10-22 [1] RSPM (R 4.5.0)
 sessioninfo   1.2.3   2025-02-05 [1] RSPM (R 4.5.0)
 tibble        3.2.1   2023-03-20 [1] RSPM (R 4.5.0)
 utf8          1.2.5   2025-05-01 [1] RSPM (R 4.5.0)
 vctrs         0.6.5   2023-12-01 [1] RSPM (R 4.5.0)

 [1] /opt/R/libraries/4.5.0_2025-05
 [2] /opt/R/4.5.0/lib/R/library
 * ── Packages attached to the search path.

───────────────────────────────────────────────────

</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…y other users

When overwrite=TRUE, uv_fs_copyfile internally calls fchmod() which requires
file ownership. Unlink the destination first so that fchmod() succeeds on the
freshly-created file owned by the current user.

Co-authored-by: jeroen <216319+jeroen@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix file_copy permission problem with overwrite Fix file_copy(overwrite = TRUE) EPERM when destination is owned by another user Mar 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

file_copy with overwrite = TRUE permission problem

2 participants