Fix crash under iOS 5.0.x due to attempt to use unavailable NSURLIsExcludedFromBackupKey symbol. fixes #1015

This commit is contained in:
Blake Watters
2012-10-29 10:47:13 -04:00
parent ea8666e2ec
commit 7fba450311
3 changed files with 54 additions and 12 deletions

View File

@@ -147,18 +147,8 @@ static RKManagedObjectStore *defaultStore = nil;
persistentStore = [self.persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nilOrConfigurationName URL:storeURL options:seedOptions error:error];
if (! persistentStore) return nil;
/**
Exclude the SQLite database from iCloud Backups to conform to the iCloud Data Storage Guidelines
See https://developer.apple.com/icloud/documentation/data-storage/
*/
#if __IPHONE_OS_VERSION_MIN_REQUIRED
BOOL success = [storeURL setResourceValue:@(YES) forKey:NSURLIsExcludedFromBackupKey error:error];
if (!success) {
RKLogError(@"Failed to exclude SQLite store at path '%@' from iCloud Backup: %@", storePath, *error);
return nil;
}
#endif
// Exclude the SQLite database from iCloud Backups to conform to the iCloud Data Storage Guidelines
RKSetExcludeFromBackupAttributeForItemAtPath(storePath);
return persistentStore;
}

View File

@@ -55,3 +55,15 @@ NSString *RKPathFromPatternWithObject(NSString *pathPattern, id object);
@return The expected MIME Type of the resource identified by the path or nil if unknown.
*/
NSString *RKMIMETypeFromPathExtension(NSString *path);
/**
Excludes the item at a given path from backup via iTunes and/or iCloud using the approaches detailed in "Apple Technical Q&A QA1719".
Excluding a path from backup can be necessary in order to conform to the iCloud Data Storage Guidelines. Please refer to the following links for more details:
1. [iCloud Data Storage Guidelines](https://developer.apple.com/icloud/documentation/data-storage/)
1. [Technical Q&A QA1719](http://developer.apple.com/library/ios/#qa/qa1719/_index.html)
@param path The path to the item that is to be excluded from backup.
*/
void RKSetExcludeFromBackupAttributeForItemAtPath(NSString *path);

View File

@@ -8,9 +8,12 @@
#if TARGET_OS_IPHONE
#import <MobileCoreServices/UTType.h>
#import <UIKit/UIDevice.h>
#else
#import <CoreServices/CoreServices.h>
#endif
#import <Availability.h>
#import <sys/xattr.h>
#import "RKPathUtilities.h"
#import "RKLog.h"
#import "RKPathMatcher.h"
@@ -119,3 +122,40 @@ NSString *RKMIMETypeFromPathExtension(NSString *path)
// Consult our internal dictionary of mappings if not found
return [RKDictionaryOfFileExtensionsToMIMETypes() valueForKey:pathExtension];
}
void RKSetExcludeFromBackupAttributeForItemAtPath(NSString *path)
{
NSCParameterAssert(path);
NSCAssert([[NSFileManager defaultManager] fileExistsAtPath:path], @"Cannot set Exclude from Backup attribute for non-existant item at path: '%@'", path);
#if __IPHONE_OS_VERSION_MIN_REQUIRED
NSError *error = nil;
NSURL *URL = [NSURL fileURLWithPath:path];
NSComparisonResult order = [[UIDevice currentDevice].systemVersion compare:@"5.1" options:NSNumericSearch];
if (order == NSOrderedSame || order == NSOrderedDescending) {
// On iOS >= 5.1, we can use the resource value API's. Note that we probe the iOS version number directly because the `setResourceValue:forKey:` symbol is defined in iOS 4.0 and greater, but performs no operation when invoked until iOS 5.1
BOOL success = [URL setResourceValue:@(YES) forKey:NSURLIsExcludedFromBackupKey error:&error];
if (!success) {
RKLogError(@"Failed to exclude item at path '%@' from Backup: %@", path, error);
}
} else {
order = [[UIDevice currentDevice].systemVersion compare:@"5.0.1" options: NSNumericSearch];
if (order == NSOrderedSame || order == NSOrderedDescending) {
// On iOS 5.0.1 we must use the extended attribute API's directly
const char* filePath = [[URL path] fileSystemRepresentation];
const char* attrName = "com.apple.MobileBackup";
u_int8_t attrValue = 1;
int result = setxattr(filePath, attrName, &attrValue, sizeof(attrValue), 0, 0);
if (result != 0) {
RKLogError(@"Failed to exclude item at path '%@' from Backup. setxattr returned result code %d", path, result);
}
} else {
RKLogWarning(@"Unable to exclude item from backup: resource value and extended attribute APIs are only available on iOS 5.0.1 and up");
}
}
#else
RKLogDebug(@"Not built for iOS -- excluding path from Backup is not possible.");
#endif
}