SIGN IN SIGN UP

use uv_fs_copyfile for cp (#59662)

This PR changes `cp`, and other uses of the internal function
`Base.sendfile(srcpath, destpath)`, to use the `uv_fs_copyfile`
function.

This should be faster and more reliable, hopefully — on many
filesystems, `uv_fs_copyfile` can just create a copy-on-write link.
Hopefully, this should fix longstanding problems with `cp` of large
files: fixes #56537 (macos), fixes #39868 (linux), fixes #30723.

In particular, I noticed two problems with our previous `sendfile`
implementation, which called a lower-level `sendfile` function on the
file descriptors that in turn called `uv_fs_sendfile`. First,
`uv_fs_sendfile` takes a `Csize_t` argument for the number of bytes,
which is clearly too small on 32-bit systems. Second, it assumes that
the return value of `uv_fs_sendfile` is the number of bytes written, but
the return value is a `Cint`, which is clearly too small for the number
of bytes in a large file (> 2GiB). The PR therefore fixed the
`uv_fs_sendfile` call to pass at most `typemax(Cssize_t)` (which is the
maximum value on a 32-bit Unix system, since the underlying libc
`sendfile` returns an `ssize_t`) in a single call (writing larger files
in chunks), and second to only use the return value as an error code —
if `uv_fs_sendfile` succeeds, it looks like we can assume that it wrote
the requested number of bytes.

In principle, we could delete this lower-level `Base.sendfile` method
completely, since it is undocumented and we don't use it. I couldn't
find any external packages that use it either. But I left it in for now,
to be conservative.

I'm still seeing a test failure with this PR related to file mode: on my
macos system, it seems to be ignoring the `umask` for the permissions of
the copied file? I'm not sure why — libuv's `copyfile` implementation on
Unix should call [`open` with the mode of the source
file](https://github.com/libuv/libuv/blob/553bb9858f46eaf2ad877c9de4bb49059820ec71/src/unix/fs.c#L1270),
which should mask out the `umask`, no? See also #27295.

cc @vtjnash, who suggested using `uv_fs_copyfile` in
https://github.com/JuliaLang/julia/pull/27295#pullrequestreview-123846212.

---------

Co-authored-by: Kristoffer Carlsson <kcarlsson89@gmail.com>
S
Steven G. Johnson committed
602b6423a138de92e30c882aca910c155b345393
Parent: ae6562c
Committed by GitHub <noreply@github.com> on 4/25/2026, 8:40:17 AM