External Publication
Visit Post

Idea: Add an option to set the mode to fs::copy

Rust Internals [Unofficial] March 14, 2026
Source

On Unixy systems, at the system call level, "filesystem cloning or kernel-space-only copying" is still an operation on a pair of file descriptors, not a pair of pathnames. On all supported Unixy systems except MacOS, fs::copy already just calls io::copy, and, skimming the code, I don't see a good reason why the MacOS implementation of fs::copy couldn't be pushed down into io::copy.

On Windows, the CopyFile family of functions really does operate on pathnames, not file handles. I wasn't able to find an equivalent that operates on file handles in a couple minutes skimming MSDN, but that doesn't mean there isn't one. However, CopyFile doesn't appear to allow you to control the ACL of the destination file, either, so the feature you want may not be possible to implement for Windows. (People who know more about low-level Windows programming than I do, please correct me if I'm wrong about anything in this paragraph.)

Therefore, I think the Right Thing would be to push the MacOS-specific fs::copy down into io::copy, make a documented guarantee that io::copy will use copy_file_range or equivalent primitive whenever possible, and also document in fs::copy that if you want to control the ownership, permissions, or other metadata of the destination file, create it yourself and use io::copy.

Discussion in the ATmosphere

Loading comments...