In the first article of this pair, I explained a previously little-known system of extended attribute (xattr) flags introduced in 2013, which allows macOS to determine whether to preserve or strip individual xattrs from files (or folders) when they are copied. This article looks at how this can be implemented in practice, and provides a demonstration app, SaveTheMetadata, which will preserve almost all xattrs when they’re passed through iCloud Drive.
In macOS, functions for working with xattr flags are provided as part of the Standard C Library libc
, and documented in man xattr_name_with_flags
, for instance. These are mainly concerned with discovering the copying intent for a given xattr, and aren’t suitable for manipulating xattrs from a language such as Swift. Given the essential simplicity of the xattr flag system, the manipulation of xattrs directly seems a better option.
Currently, I access xattrs based on a Swift wrapper originally posted by Martin R on StackOverflow. Neither that nor existing functions in macOS appear to give direct access to changing the name of an existing xattr: if you know better, please tell! Therefore the only means that I can see of achieving this is to create a new xattr named with the original name and xattr flag(s) appended, and the same contents as the original, then deleting that original:
// for a xattr named item
if item.count < 126 {
let theNewName = item + "#S"
let theData = try self.extendedAttribute(forName: item)
try self.setExtendedAttribute(data: theData, forName: theNewName)
try self.removeExtendedAttribute(forName: item) }
to add the xattr flag S to a xattr without any existing flags, for instance. This gets more complex when trying to deal with xattrs with existing flags, though, where removal of the original doesn’t appear to work.
It’s relatively complex to add xattr flags to existing xattrs using my free tool xattred, as there isn’t an easily-accessible feature yet to rename a xattr. The best way to do this is to select the xattr, then click on the Edit button. In the editor drop-down, append the # and flag(s) in the name at the top, then click Save. This leaves both the original xattr and the new one with its flagged name, so you then select the original and click on Cut to remove it.
I have built a demo app, SaveTheMetadata, which does this automatically to all xattrs of files which are dropped onto it. All xattrs which don’t currently have flags, apart from com.apple.quarantine
and com.apple.security*
, are rewritten with #S appended to their names, provided the name doesn’t then exceed the 127 character limit.
SaveTheMetadata version 1.0b2 is available from here: STM10b2
but I’m not putting it into other pages here just yet, as it needs more extensive testing, and has limited utility at present, as I’ll explain.
The snag with these methods is that, while they preserve xattrs robustly, the renamed xattrs aren’t recognised by most apps which use them. This is because the apps aren’t expecting the #S xattr flag, so don’t drop the flag from the name, thus don’t recognise the xattr. This is neatly demonstrated with one app which relies heavily on xattrs: Skim, the PDF reader and annotation tool.
I started by adding annotations to a test PDF using Skim 1.5.2, as shown.
The resulting xattrs, as shown here in xattred, consist of five added for the Skim annotations, two left from the document’s creation using PDF Expert, and four ‘standard’ xattrs from macOS. I then passed that file through iCloud Drive to another Mac running Mojave 10.14.5, which stripped all the xattrs apart from two of the standard ones.
This removed all the annotations which I had added in Skim, and is a well-known limitation of that app.
Returning to the original annotated file on the first Mac, I next ran it through SaveTheMetadata, which added the S xattr flag to all the xattrs apart from com.apple.lastuseddate
, which already had the PS flags present.
Passing that file through iCloud Drive to the other Mac resulted in preservation of all the custom xattrs, but com.apple.FinderInfo
and com.apple.metadata:_kMDItemUserTags
were still stripped, presumably because of overriding rules which ignored the S xattr flag.
Thus the S flag preserved all the xattrs containing Skim’s annotations, but when that file was opened in Skim, it was unable to read them because it doesn’t strip the xattr flags from their names when reading xattrs. And that is the problem with this technique: although the xattrs are, in general, preserved, because most apps don’t expect to have to handle xattr flags appended to their names, the preserved xattrs aren’t used – after all that.
The solution therefore is for all apps which access xattrs by name to drop any xattr flags from the names before using them. This demonstrates the cost of this elegant kludge. As this isn’t handled transparently in calls such as getxattr()
which access xattrs, every app is left to its own devices to handle xattr flags appended to xattr names, which is inefficient and encourages inconsistencies between apps.
One immediate solution would be to provide the inverse of SaveTheMetadata, to strip xattr flags after transit through iCloud Drive. However, this would be a kludge of a kludge, and possibly not workable in practice. For apps like Skim, though, there is at last a clear way ahead for them to preserve xattrs across iCloud, provided that they handle xattr flags transparently.
I look forward to your comments and discussion.