Skip to content

File.Copy: Clone file when possible on MacOS #77835

Closed

Description

For File.Copy on macOS we're currently doing the following:

return fcopyfile(inFd, outFd, NULL, COPYFILE_ALL) == 0 ? 0 : -1;

If we instead use copyfile() we can use the COPYFILE_CLONE option (with the existing COPYFILE_ALL obviously, which I think the clone option may include all of the flags of anyway), which allows the file to be instantly copied on supported filesystems, and falls back to exactly the same implementation when it's not available (note - this behaves identically (except with symlinks (ie. this copies the symlink itself I think) and when the destination exists, which we can special case), it just allows APFS to immediately set the data location to the existing file, and when it's updated it will be create a copy since APFS is a copy-on-write filesystem). This is obviously much faster (I tested it on a hard drive with a 30GB file and it copied instantly).

We can implement that in this file:

public static void CopyFile(string sourceFullPath, string destFullPath, bool overwrite)

by splitting the OSX-like platforms code, like we have with the code that sets the creation date.

We should be able to implement it with effectively the same behaviour as whatever the current behaviour is with relative ease. Note: apps like finder clone files in this way.

I'd like to implement this in a PR if approved (ideally by .NET 8).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions