| 1 |
// |
| 2 |
// NSFileManager+DirectoryLocations.m |
| 3 |
// |
| 4 |
// Created by Matt Gallagher on 06 May 2010 |
| 5 |
// |
| 6 |
// This software is provided 'as-is', without any express or implied |
| 7 |
// warranty. In no event will the authors be held liable for any damages |
| 8 |
// arising from the use of this software. Permission is granted to anyone to |
| 9 |
// use this software for any purpose, including commercial applications, and to |
| 10 |
// alter it and redistribute it freely, subject to the following restrictions: |
| 11 |
// |
| 12 |
// 1. The origin of this software must not be misrepresented; you must not |
| 13 |
// claim that you wrote the original software. If you use this software |
| 14 |
// in a product, an acknowledgment in the product documentation would be |
| 15 |
// appreciated but is not required. |
| 16 |
// 2. Altered source versions must be plainly marked as such, and must not be |
| 17 |
// misrepresented as being the original software. |
| 18 |
// 3. This notice may not be removed or altered from any source |
| 19 |
// distribution. |
| 20 |
// |
| 21 |
|
| 22 |
#import "NSFileManager+DirectoryLocations.h" |
| 23 |
|
| 24 |
enum |
| 25 |
{ |
| 26 |
DirectoryLocationErrorNoPathFound, |
| 27 |
DirectoryLocationErrorFileExistsAtLocation |
| 28 |
}; |
| 29 |
|
| 30 |
NSString * const DirectoryLocationDomain = @"DirectoryLocationDomain"; |
| 31 |
|
| 32 |
@implementation NSFileManager (DirectoryLocations) |
| 33 |
|
| 34 |
// |
| 35 |
// findOrCreateDirectory:inDomain:appendPathComponent:error: |
| 36 |
// |
| 37 |
// Method to tie together the steps of: |
| 38 |
// 1) Locate a standard directory by search path and domain mask |
| 39 |
// 2) Select the first path in the results |
| 40 |
// 3) Append a subdirectory to that path |
| 41 |
// 4) Create the directory and intermediate directories if needed |
| 42 |
// 5) Handle errors by emitting a proper NSError object |
| 43 |
// |
| 44 |
// Parameters: |
| 45 |
// searchPathDirectory - the search path passed to NSSearchPathForDirectoriesInDomains |
| 46 |
// domainMask - the domain mask passed to NSSearchPathForDirectoriesInDomains |
| 47 |
// appendComponent - the subdirectory appended |
| 48 |
// errorOut - any error from file operations |
| 49 |
// |
| 50 |
// returns the path to the directory (if path found and exists), nil otherwise |
| 51 |
// |
| 52 |
- (NSString *)findOrCreateDirectory:(NSSearchPathDirectory)searchPathDirectory |
| 53 |
inDomain:(NSSearchPathDomainMask)domainMask |
| 54 |
appendPathComponent:(NSString *)appendComponent |
| 55 |
error:(NSError **)errorOut |
| 56 |
{ |
| 57 |
// |
| 58 |
// Search for the path |
| 59 |
// |
| 60 |
NSArray* paths = NSSearchPathForDirectoriesInDomains( |
| 61 |
searchPathDirectory, |
| 62 |
domainMask, |
| 63 |
YES); |
| 64 |
if ([paths count] == 0) |
| 65 |
{ |
| 66 |
if (errorOut) |
| 67 |
{ |
| 68 |
NSDictionary *userInfo = |
| 69 |
[NSDictionary dictionaryWithObjectsAndKeys: |
| 70 |
NSLocalizedStringFromTable( |
| 71 |
@"No path found for directory in domain.", |
| 72 |
@"Errors", |
| 73 |
nil), |
| 74 |
NSLocalizedDescriptionKey, |
| 75 |
[NSNumber numberWithInteger:searchPathDirectory], |
| 76 |
@"NSSearchPathDirectory", |
| 77 |
[NSNumber numberWithInteger:domainMask], |
| 78 |
@"NSSearchPathDomainMask", |
| 79 |
nil]; |
| 80 |
*errorOut = |
| 81 |
[NSError |
| 82 |
errorWithDomain:DirectoryLocationDomain |
| 83 |
code:DirectoryLocationErrorNoPathFound |
| 84 |
userInfo:userInfo]; |
| 85 |
} |
| 86 |
return nil; |
| 87 |
} |
| 88 |
|
| 89 |
// |
| 90 |
// Normally only need the first path returned |
| 91 |
// |
| 92 |
NSString *resolvedPath = [paths objectAtIndex:0]; |
| 93 |
|
| 94 |
// |
| 95 |
// Append the extra path component |
| 96 |
// |
| 97 |
if (appendComponent) |
| 98 |
{ |
| 99 |
resolvedPath = [resolvedPath |
| 100 |
stringByAppendingPathComponent:appendComponent]; |
| 101 |
} |
| 102 |
|
| 103 |
// |
| 104 |
// Create the path if it doesn't exist |
| 105 |
// |
| 106 |
NSError *error = nil; |
| 107 |
BOOL success = [self |
| 108 |
createDirectoryAtPath:resolvedPath |
| 109 |
withIntermediateDirectories:YES |
| 110 |
attributes:nil |
| 111 |
error:&error]; |
| 112 |
if (!success) |
| 113 |
{ |
| 114 |
if (errorOut) |
| 115 |
{ |
| 116 |
*errorOut = error; |
| 117 |
} |
| 118 |
return nil; |
| 119 |
} |
| 120 |
|
| 121 |
// |
| 122 |
// If we've made it this far, we have a success |
| 123 |
// |
| 124 |
if (errorOut) |
| 125 |
{ |
| 126 |
*errorOut = nil; |
| 127 |
} |
| 128 |
return resolvedPath; |
| 129 |
} |
| 130 |
|
| 131 |
// |
| 132 |
// applicationSupportDirectory |
| 133 |
// |
| 134 |
// Returns the path to the applicationSupportDirectory (creating it if it doesn't |
| 135 |
// exist). |
| 136 |
// |
| 137 |
- (NSString *)applicationSupportDirectory |
| 138 |
{ |
| 139 |
NSString *executableName = |
| 140 |
[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleExecutable"]; |
| 141 |
NSError *error; |
| 142 |
NSString *result = |
| 143 |
[self |
| 144 |
findOrCreateDirectory:NSApplicationSupportDirectory |
| 145 |
inDomain:NSUserDomainMask |
| 146 |
appendPathComponent:executableName |
| 147 |
error:&error]; |
| 148 |
if (!result) |
| 149 |
{ |
| 150 |
NSLog(@"Unable to find or create application support directory:\n%@", error); |
| 151 |
} |
| 152 |
return result; |
| 153 |
} |
| 154 |
|
| 155 |
@end |