Is it possible to clone data into existing files?

macOS has the clonefile*() calls to create a new file that's a clone of an existing file, but is it possible to clone only parts of an existing file into a different existing file? Linux (FICLONERANGE) and Windows (FSCTL_DUPLICATE_EXTENTS_TO_FILE) both provide this functionality.

I previously filed FB12737014 with this request.

Without wanting to set expectations about whether this feature could be provided or not, please note that on macOS such an operation would only be possible from a particular file offset in the source file to the same file offset in the destination file. (This is due to some low-level invariants of how our encryption scheme works.)

Thanks for the reply, that's interesting. I don't expect that would be a problem, 99+% of our use of this would be to copy an entire existing file into another existing file.

I’m not sure I follow, if that’s 99+% of your use case then in what way is clonefile() not suitable for you? If the problem is that you’d like to overwrite an existing file then you can always clone to a temporary location and then rename() the result to the final destination, similar to a safe save.

The issue is that we're running Windows applications, and so need to implement the FSCTL_DUPLICATE_EXTENTS_TO_FILE ioctl. Since file creation and the copy operation are separate, an application could create the destination file, possibly modify its attributes/add extended attributes/open additional handles to the file, and then clone the contents in.

I don't think it would be possible to implement that with full fidelity on top of clonefile()--we would need to get the paths to the source/destination FD (not ideal when we already have open FDs), clonefile() to a temp location, set attributes to match as much as possible, then rename the file to the destination path. Fixing up open handles to the original destination file to point to the new one would also be difficult at best.

Is it possible to clone data into existing files?
 
 
Q