FRADatabaseConfiguration.m revision 721bb987c406979bcfe705fa1ca8d54497d40fcb
0N/A/*
665N/A * The contents of this file are subject to the terms of the Common Development and
0N/A * Distribution License (the License). You may not use this file except in compliance with the
0N/A * License.
0N/A *
0N/A * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
0N/A * specific language governing permission and limitations under the License.
0N/A *
0N/A * When distributing Covered Software, include this CDDL Header Notice in each file and include
0N/A * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
0N/A * Header, with the fields enclosed by brackets [] replaced by your own identifying
0N/A * information: "Portions copyright [year] [name of copyright owner]".
0N/A *
0N/A * Copyright 2016 ForgeRock AS.
0N/A */
0N/A
0N/A#import "FRADatabaseConfiguration.h"
0N/A#import "FRAError.h"
0N/A
0N/A/*!
0N/A * Limited configuration at the moment for the SQLite database.
0N/A *
0N/A * Uses the iOS file system to locate the default storage location for the
0N/A * SQLite database.
0N/A */
0N/A@implementation FRADatabaseConfiguration
0N/A/*!
0N/A * Specifically uses the <Library folder>/Database/database.sqlite location for
0N/A * storage of non-user data which is expected to be backed up by iTunes.
0N/A */
0N/A- (NSString *)getDatabasePathWithError:(NSError *__autoreleasing *)error {
0N/A NSURL *library = [self systemLibraryPathWithError:error];
0N/A if (library == nil) {
0N/A return nil;
0N/A }
0N/A
0N/A NSString *databaseFolder = [[library path] stringByAppendingPathComponent:@"Database"];
0N/A
0N/A // Create folder if needed.
0N/A if (![FRADatabaseConfiguration parentFoldersFor:databaseFolder error:error]) {
0N/A return nil;
0N/A }
0N/A
0N/A // Create the Database/database.sql file path
0N/A NSString *databaseFile = [databaseFolder stringByAppendingPathComponent:@"database"];
0N/A return [databaseFile stringByAppendingPathExtension:@"sqlite"];
0N/A}
0N/A
0N/A+ (BOOL)parentFoldersFor:(NSString *)folder error:(NSError *__autoreleasing *)error {
0N/A NSFileManager* manager = [NSFileManager defaultManager];
0N/A // Creating the folder if required
0N/A if (![manager fileExistsAtPath:folder]) {
0N/A return [manager createDirectoryAtPath:folder withIntermediateDirectories:YES attributes:nil error:error];
0N/A NSLog(@"Created folder: %@", folder);
0N/A }
0N/A return YES;
0N/A}
0N/A
0N/A/*!
0N/A * The operating system path that is recommended for storage of files that the user
0N/A * should not have direct access to.
0N/A *
665N/A * @see table 1_1 for more details:
665N/A * https://developer.apple.com/library/mac/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html#//apple_ref/doc/uid/TP40010672-CH2-SW1
665N/A *
665N/A * @param error Reference to an error if any.
0N/A * @return nil if the system folder could not be located, otherwise the path of the folder.
0N/A */
0N/A- (NSURL *)systemLibraryPathWithError:(NSError *__autoreleasing *)error {
0N/A NSFileManager* manager = [NSFileManager defaultManager];
0N/A NSArray<NSURL*> *possibleURLs = [manager URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask];
0N/A // Guaranteed to be present by OS
0N/A if ([possibleURLs count] == 0) {
0N/A [FRAError createErrorForFilePath:@"NSLibraryDirectory" withReason:@"Could not locate system folder /Library" withError:error];
0N/A return nil;
0N/A }
0N/A return [possibleURLs objectAtIndex:0];
0N/A}
0N/A
0N/A@end