NSFileManager category

Each method in the NSFileManager category corresponds to a method in the NSFileManager class, but provides a secure version of the functionality. For more information about the functionality and usage, see NSFileManager in developer.apple.com.

NOTE: The category methods return an NSError object. The methods set the properties on the object as described in NSError objects that secure Objective-C methods return.

The following table shows each added method and its corresponding method in NSFileManager.

Table 1. NSFileManager category methods

Method in category

Corresponding method in NSFileManager

- (BOOL)createSecureFileAtPath:

(NSString *)path

contents:(NSData *)contents

attributes:(NSDictionary *)attributes

error:(

NSError *__autoreleasing *)error;

- (BOOL)createFileAtPath:

(NSString *)path

contents:(NSData *)contents

attributes:(NSDictionary *)attributes;

- (BOOL)moveSecureFileAtPath:

(NSString *)srcPath

toPath:(NSString *)dstPath

error:(

NSError *__autoreleasing *)error;

- (BOOL)moveItemAtPath:

(NSString *)srcPath

toPath:(NSString *)dstPath

error:(NSError **)error;

- (BOOL)moveSecureFileAtURL:

(NSURL *)srcURL

toURL:(NSURL *)dstURL

error:(

NSError *__autoreleasing *)error;

- (BOOL)moveItemAtURL:

(NSURL *)srcURL

toURL:(NSURL *)dstURL

error:(NSError **)error;

- (BOOL)copySecureFileAtPath:

(NSString *)srcPath

toPath:(NSString *)dstPath

error:(

NSError *__autoreleasing *)error;

- (BOOL)copyItemAtPath:

(NSString *)srcPath

toPath:(NSString *)dstPath

error:(NSError **)error;

- (BOOL)copySecureFileAtURL:

(NSURL *)srcURL

toURL:(NSURL *)dstURL

error:(

NSError *__autoreleasing *)error;

- (BOOL)copyItemAtURL:

(NSURL *)srcURL

toURL:(NSURL *)dstURL

error:(NSError **)error;

- (NSData *)secureContentsAtPath:

(NSString *)path

error:(

NSError *__autoreleasing *)error;

- (NSData *)contentsAtPath:

(NSString *)path;

- (BOOL)secureContentsEqualAtPath:

(NSString *)path1

andPath:(NSString *)path2

error:(

NSError *__autoreleasing *)error;

- (BOOL)contentsEqualAtPath:

(NSString *)path1

andPath:(NSString *)path2;

- (NSDictionary *)

attributesOfSecureFileAtPath:

(NSString *)path

error:(

NSError *__autoreleasing *)error;

- (NSDictionary *)

attributesOfItemAtPath:

(NSString *)path

error:(NSError **)error;

Example:

The following example shows how to move a secure file to a new location. Specifically, the example:

1. Creates a secure file.
2. Writes the contents of the unsecured file /etc/group into the secure file.
3. Moves the secure file to a new location using the NSFileManager+ACSecureData category methods.
NOTE: For brevity, the example does not include error handling.
- (void)NSFileManagerCategoryExample
{
	NSError *error;
 
	// Read the contents of /etc/group.
	NSData *etcGroupData = [NSData dataWithContentsOfFile:@"/etc/group"];
 
	// Create a secure file with the contents of /etc/group. 
	NSString *secureFileName = @"/tmp/secureFile";
	[[NSFileManager defaultManager] createSecureFileAtPath:secureFileName 
							contents:etcGroupData attributes:nil];
 
	// Move the newly created secure file to a new location.
	// First, create the source and destination file URLs. 
	NSString *anotherSecureFileName = @"/tmp/anotherSecureFile";
	NSURL *sourceURL = [NSURL fileURLWithPath:secureFileName];
	NSURL *destinationURL = [NSURL fileURLWithPath:anotherSecureFileName];
 
	// Move the secure file.
	[[NSFileManager defaultManager] moveSecureFileAtURL:sourceURL 
							toURL:destinationURL error:&error];
 
	// Note: The following line incorrectly moves a secure file. 
	// Mixing regular and secure file I/O on the same file can result
	// in corrupted data.
	// DO NOT USE.
	// [[NSFileManager defaultManager] moveItemAtPath:sourceURL toPath:destinationURL 
                                                                   error:&error];
}