XATTR_NAME_WITH_FLAGS(3) Library Functions Manual XATTR_NAME_WITH_FLAGS(3)

xattr_preserve_for_intent, xattr_name_with_flags, xattr_name_without_flags, xattr_flags_from_name, xattr_intent_with_flagsobtain properties related to extended attributes, for use in copying

Standard C Library (libc, -lc)

#include <xattr_flags.h>

int
xattr_preserve_for_intent(const char *, xattr_operation_intent_t);

char *
xattr_name_with_flags(const char *, xattr_flags_t);

char *
xattr_name_without_flags(const char *);

xattr_flags_t
xattr_flags_from_name(const char *);

int
xattr_intent_with_flags(xattr_operation_intent_t, xattr_flags_t);

These functions are used in conjunction with copying extended attributes from one file to another. Various types of copying (an "intent") check flags to determine which is allowed or not.

The () function returns an extended attribute name with the appropriate flags encoded as a string; the () undoes this, giving the name of the extended attribute without the flags encoding. The slight inverse of that is (), which will return the flags encoded in a name.

The values returned by () and () are allocated using malloc(3), and should be released by the caller, using free(3).

These functions also have an internal table of pre-defined names, maintained by the operating system.

The function () will return 0 if the flags argument indicates it should not be preserved for the given intent, or 1 if it should.

The function () combines the functions above, and will return zero if the named extended attribute should be preserved during a copy for the given intent.

The type xattr_operation_intent_t is an integral type, which is used to indicate what the intent for the operation is. The following intent values are defined:

Indicates that the intent is to simply copy from the source to the destination. E.g., with cp. Most extended attributes should generally be preserved in this case.
Indicates that intent is to perform a save (perhaps as in a "safe save"). This differs from a copy in that the content may be changing; the destination may be over-writing or replacing the source, and some extended attributes should not be preserved during this process.
Indicates that the intent is to share, or export, the object. For example, saving as an attachment in an email message, or placing in a public folder. Sensitive information should probably not be preserved in this case.
Indicates that the intent is to sync the object to a service like iCloud Drive.
Indicates that the intent is to backup the object to a service like Time Machine.

Various flags are defined by the type xattr_flags_t; the currently-defined values for this are:

This indicates that the extended attribute should not be exported, or shared. This is used with XATTR_OPERATION_INTENT_SHARE.
This indicates that the extended attribute is tied to the contents of the file (or vice versa), such that it should be re-created when the contents are changed. A checksum, for example, should not be copied, and would thus be marked with this flag.
This indicates that the extended attribute should never be copied from a source object to a destination, no matter what the given intent is.
This indicates that the extended attribute should be copied when the file is synced on services like iCloud Drive. Sync services may enforce additional restrictions on the acceptable size and number of extended attributes. (Note that when used with XATTR_FLAG_CONTENT_DEPENDENT, this may generate conflicts when the file is synced.)
This indicates that the extended attribute should only be copied during backup on services like Time Machine.
This indicates that the extended attribute should only be copied during save/backup.

The following example is a simple function that, given an extended attribute name and an operation intent, will return whether or not the extended attribute should be copied. (This essentially does what xattr_preserve_for_intent() does.)

int
ShouldCopyEA(const char *eaName, xattr_operation_intent_t intent)
{
	xattr_flags_t flags = xattr_flags_from_name(eaName);
	return xattr_intent_with_flags(intent, flags);
}

These functions first appeared in Mac OS in 2013.

December 19, 2023 macOS 15.0