Source

SOExtendedAttributes /

Filename Size Date modified Message
Documentation
SOExtendedAttributes.xcodeproj
UnitTests
10 B
269 B
1.5 KB
8.2 KB
12.7 KB
3.4 KB
639 B
286 B
579 B

SOExtendedAttributes

SOExtendedAttributes is a category on NSURL providing methods for manipulating extended attributes on a file system object.

The implementation uses listxattr(2), getxattr(2), setxattr(2), and removexattr(2).

Introspection

- (BOOL) hasExtendedAttributeWithName:(NSString *)name;

Accessing attributes individually

- (id) valueOfExtendedAttributeWithName:(NSString *)name error:(NSError * __autoreleasing *)outError;
- (BOOL) setExtendedAttributeValue:(id)value forName:(NSString *)name error:(NSError * __autoreleasing *)outError;

Accessing attributes in batch

- (NSDictionary *) extendedAttributesWithError:(NSError * __autoreleasing *)outError;
- (BOOL) setExtendedAttributes:(NSDictionary *)attributes error:(NSError * __autoreleasing *)outError;

Removing an extended attribute

- (BOOL) removeExtendedAttributeWithName:(NSString *)name error:(NSError * __autoreleasing *)outError;

Use with iCloud Backup

iCloud (as of iOS 5.0.1) honors the extended attribute @"com.apple.MobileMeBackup" as a flag to exclude a file system item from iCloud backup. This category defines the constant iCloudDoNotBackupAttributeName to work with this iCloud behavior.

Constants

iCloudDoNotBackupAttributeName = @"com.apple.MobileMeBackup"

E.g. To determine if a file system item is marked to be excluded from iCloud backup:

NSURL fileURL = ...;
BOOL isExcluded = [fileURL hasExtendedAttributeWithName:iCloudDoNotBackupAttribute];

Extended Attribute Values

An extended attribute value can be any Foundation object that can be serialized into a property list:

  • NSData
  • NSString
  • NSNumber
  • NSArray
  • NSDictionary
  • NSDate

Known Issues, Stuff & Bother

Maximum Value Size

The maximum size of an extended attribute value? I don't know. The Internet says anywhere from 3802 bytes to 128 KB. So the standard disclaimers apply: Your mileage may vary. Void where prohibited. Do not operate heavy machinery.

Refs: Google Search - ArsTechnica 10.4 Review - ArsTechnica 10.6 Review - MacFuse Release Notes

Non-file URLs

An NSInternalInconsistencyException is thrown if these methods are invoked on a non-file URL.

Resolving Symbolic links

These methods act on the explicitly given URL. If that URL points to a symbolic link, you'll be manipulating extended attributes on the symlink, not its original file. Use -URLByResolvingSymlinksInPath to get a URL pointing to the original file system item.

ARC

If your project is not using ARC, you will need to set a per-file build flag in Xcode on NSURL+SOExtendedAttributes.m:

-fobc-arc

Installation

For either Mac OS X or iOS projects, add these files:

NSURL+SOExtendedAttributes.h
NSURL+SOExtendedAttributes.m

Compatibility

SOExtendedAttributes is compatible with Mac OS X 10.6+ and iOS 5. The clang compiler is required. The source file NSURL+SOExtendedAttributes.m must be compiled with ARC enabled.

For an alternate Cocoa implementation compatible with Mac OS X 10.4 and greater, see UKXattrMetadataStore.