APFS Clones and cp -c: A Fast Copy Can Still Be a Full Copy
The second copy finishes before your hand leaves Return. That speed is suggestive, but it is not proof that APFS created a clone. On macOS, cp -c can produce an ordinary full copy instead—and still exit successfully.
That fallback is not an obscure implementation detail. It is documented in Apple's cp(1) manual: -c asks for clonefile(2), but if the source and destination are on different filesystems, or the destination filesystem does not support cloning, cp falls back to copyfile(2) so the copy can still succeed.
For a script, that difference matters. A successful command answers “do I have a usable destination file?” It does not answer “did I avoid duplicating the data blocks?”
Try the copy, but do not over-read the result
Use a scratch directory and substitute your own filenames. Do not run an experiment against the only copy of important data.
cd /path/to/scratch-directory cp -c source.bin clone.bin stat -f '%N inode=%i links=%l logical=%z blocks=%b' source.bin clone.bin cmp -s source.bin clone.bin && printf 'contents match\n' df -h . We ran the equivalent commands on macOS with a temporary copy of a small local file, then removed the temporary files. I am deliberately not supplying the resulting inode numbers or free-space figures as sample output: those values describe one machine at one moment, not a result you should reproduce.
The commands have four different jobs. cp requests the copy. stat describes directory-entry and per-file metadata. cmp compares the current contents. df reports capacity for the filesystem containing the scratch directory.
None of them, alone or in combination here, proves that two directory entries share APFS data blocks.
A clone is not a hard link
This is where the stat output can be useful without becoming a clone detector.
A hard link is another directory entry for the same inode. The two paths therefore have the same inode number, and the inode's link count reflects the additional name. Removing one name does not remove the file while another hard link remains.
An APFS clone is a separate file with its own inode. Apple's clonefile(2) manual describes it as sharing data blocks with the source while maintaining its own attributes and extended attributes, subject to documented ownership, mode-bit, and ACL rules. Subsequent writes to either file are private to the file being modified through copy-on-write.
That means a normal clone will commonly show different inode numbers and link counts of one. Unfortunately, an ordinary independent copy shows the same pattern. Different inode numbers rule out a hard-link relationship; they do not distinguish a clone from a full copy.
The blocks= values need similar restraint. They are per-file stat fields, not a report of unique physical storage after accounting for every block shared with other files. Adding values from multiple files can therefore be misleading when clones are involved.
Likewise, cmp returning success only establishes that the bytes currently match. A clone and a newly completed full copy should both pass that check immediately after creation.
When cp -c is allowed to become a full copy
Two fallback cases are explicit in the manual:
- The paths resolve to different filesystems.
- The destination filesystem does not support cloning.
The second case is easy to encounter in a heterogeneous Mac workflow. An APFS startup volume, an external disk with another format, a network mount, and a disk image should not be assumed to offer identical file-system operations. The visible Finder copy dialog does not change those underlying constraints.
Even same-volume placement is not a reason to skip the distinction. The documented behavior of cp -c is an optimization with a correctness-preserving fallback, not a guarantee that a clone occurred. If your cleanup calculation, benchmark, or storage budget depends on block sharing, exit status zero is the wrong assertion.
Use clonefile(2) when the distinction is contractual
Apple's system-call interface is stricter than the convenience command. A direct clonefile(2) call fails with ENOTSUP when the underlying filesystem does not support the operation and EXDEV when source and destination are on different filesystems. It also requires that the destination not already exist, returning EEXIST otherwise.
For software that must make a cloning decision rather than merely make a copy, those errors are useful. They let the program choose and disclose its fallback instead of silently changing its storage behavior.
The same manual documents a way to test volume support before relying on the operation: use getattrlist(2) to request ATTR_VOL_CAPABILITIES and check VOL_CAP_INT_CLONE. That is a capability check, not a replacement for handling errors from the actual operation.
There is another useful failure scenario in the documentation. Because cloning may avoid allocating new storage for the initial data blocks, a later overwrite can return ENOSPC when copy-on-write needs space. The instant first copy did not repeal capacity limits; it deferred some allocation until the files diverged.
For directory trees, follow the manual's more specific advice rather than extrapolating from a single file: it strongly discourages cloning directory hierarchies with clonefile(2) and points callers to copyfile(3) instead.
Deleting one path has a narrow meaning
Suppose the clone operation did succeed. Deleting source.bin may free very little while clone.bin still references the shared blocks. After either file changes, some blocks are no longer shared, so the eventual accounting changes too.
If cp -c fell back to a full copy, the two files were independent data copies from the start. Deleting one still does not tell you how much free space will appear across the whole volume: snapshots, other files, concurrent activity, and the filesystem's own accounting all sit outside this small experiment.
A df measurement before and after one deletion is therefore an observation, not a controlled benchmark. Keep other writes quiet, compare the same mount point, and avoid converting one sample into a universal “clone size.”
Clones also do not provide an independent backup. They live in the same storage failure domain, and copy-on-write is not a protection against losing the underlying device.
The practical rule I would put in a script comment is short: use cp -c when cloning is a welcome optimization; use the system API and handle its errors when cloning is a requirement. Do not turn matching contents, different inodes, or a fast return into evidence the commands did not collect.
Sources checked: Apple's macOS cp(1), clonefile(2), and stat(1) manual pages.
Published by the DiskBee (磁盘小蜜蜂) team. We build DiskBee, an FSKit-based macOS storage tool, at https://www.ntfs.pro/. The commands and API behavior discussed here are built into macOS and do not require our software.
原文:https://www.a8q9.cn/en/article/apfs-clones-and-cp-c-a-fast-copy-can-still-be-a-full-copy-2cd49d.html