How to check that a file really is a faithful copy

You’ve got two files, which are claimed to be identical. How can you check whether they really are?

In macOS, and several other operating systems, on HFS+ and APFS in particular, files not only have their regular data content, but extended attributes too. Mainstay command tools such as cmp will check through every byte of their data, but completely ignore those extended attributes (xattrs).

It wasn’t really so much of a problem in the past: although there have always been ways of copying files which also ignore xattrs, if you use the Finder and other higher-level ways of copying files, no matter where they went, so long as they were on a ‘safe’ file system such as HFS+, their xattrs went quietly along with them.

This has changed now that most of us make extensive use of remote storage, the cloud, and specifically iCloud. Although I haven’t yet found it documented anywhere, iCloud Drive will silently censor most xattrs when used to move files between computers, even if they’re both Macs, running the same version of macOS, and with local HFS+ or APFS storage. On the other hand, Time Machine and all the better backup systems do preserve xattrs.

So how can you check whether a copy of a file in your iCloud Drive, or in a backup, is exactly the same, with the same xattrs?

The only answer that I have been able to find is “with difficulty”, although I’d love to hear of something reliable and simple. This was the subject of a question on StackExchange, and the best answer seems to be to hack it using rsync, which is frankly bizarre.

Turn to the macOS FileManager, though, and you might think that therein lies the solution: FileManager.contentsEqual(atPath: String, andPath: String) looks as if it might do the job. Only its documentation doesn’t mention xattrs, so I made off to its source code, where it is clearly stated that no account is taken of xattrs.

You can do this by eye in my GUI tool xattred, but it currently doesn’t do the comparison for you, and is useless for even making spot checks on a sample of allegedly identical files. It’s a feature which I am adding to xattred, but a basic command tool similar to cmp is what is needed first.

So here it is: the first version of cmpxat. It runs in cmp‘s most basic mode – just give it the paths to the two files like so
cmpxat file1 file2
If it returns no output, then they are identical with respect to their xattrs. If there are any differences between their xattrs, it will tell you in what way they are different, such as one file missing one or more, or their contents not being identical.

cmpxat is available from here: cmpxat1
and from Downloads above. It should be compatible with El Capitan, Sierra, High Sierra, and Mojave.