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