APFS Folder Clones
After my experiments with APFS cloning, I made a Quick Action shortcut for Finder that’s much faster than Duplicate or “cp -c -R”, both of which clone files individually instead of the whole tree in one go.
The regular file copying APIs also give you folders full of file clones rather than a directory clone. His shortcut runs a Python script:
The clone is made with the macOS clonefile(2) syscall, invoked directly through ctypes. clonefile asks APFS to create a new inode that shares the source’s data extents — no bytes are copied up front, the new tree just points at the same disk blocks. The two trees diverge lazily: only blocks that are later modified in one side get their own physical storage (copy-on-write).
However, it’s not clear to me what the benefit is. Aside from somehow being faster, it sounds like you end up with the same structure. The clonefile(2) man page says:
If
srcnames a directory, the directory hierarchy is cloned as if each item was cloned individually. However, the use of clonefile(2) to clone directory hierarchies is strongly discouraged. Use copyfile(3) instead for copying directories.
I don’t think APFS really supports directory clones except at the snapshot level.
See also: Ask Different, Howard Oakely.
Previously:
5 Comments RSS · Twitter · Mastodon
So clonefile works like a live snapshot at the directory level. Too bad its use is "strongly discouraged". It sounds like it would be efficient.
More details as to why clonefile(2) for directories is discouraged: https://developer.apple.com/forums/thread/784446 (previous message got stripped of the link).
@Frizlab Thanks for that link. That explains the "strongly discouraged" part. If it didn't have the potential to crash the kernel, it sounds like it would be like backing your way into a deduplication feature. :-)