-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNSFileManager+DirectoryLocations.m
119 lines (87 loc) · 3.28 KB
/
NSFileManager+DirectoryLocations.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//
// NSFileManager+DirectoryLocations.m
// Property Manager
//
// Created by Samuel Williams on 24/05/12.
// Copyright (c) 2012 Orion Transfer Ltd. All rights reserved.
//
#import "NSFileManager+DirectoryLocations.h"
@implementation NSFileManager (DirectoryLocations)
- (NSArray *)listDirectories:(NSSearchPathDirectory)searchPathDirectory inDomain:(NSSearchPathDomainMask)domainMask subdirectory:(NSString *)subdirectory {
// Search for the path
NSArray * paths = NSSearchPathForDirectoriesInDomains(searchPathDirectory, domainMask, YES);
if (subdirectory == nil) {
return paths;
}
NSMutableArray * result = [NSMutableArray arrayWithCapacity:paths.count];
for (NSString * path in paths) {
[result addObject:[path stringByAppendingPathComponent:subdirectory]];
}
return result;
}
- (NSArray *)findExistingDirectories:(NSSearchPathDirectory)searchPathDirectory inDomain:(NSSearchPathDomainMask)domainMask subdirectory:(NSString *)subdirectory {
NSArray * paths = [self listDirectories:searchPathDirectory inDomain:domainMask subdirectory:subdirectory];
NSMutableArray * result = [NSMutableArray arrayWithCapacity:paths.count];
for (NSString * path in paths) {
if ([self fileExistsAtPath:path]) {
[result addObject:path];
}
}
return result;
}
- (NSString *)findOrCreateDirectory:(NSSearchPathDirectory)searchPathDirectory inDomain:(NSSearchPathDomainMask)domainMask subdirectory:(NSString *)subdirectory error:(NSError **)error
{
NSArray * paths = [self listDirectories:searchPathDirectory inDomain:domainMask subdirectory:subdirectory];
if (paths.count == 0) {
return nil;
}
// Normally only need the first path
NSString * resolvedPath = paths[0];
BOOL success = [self createDirectoryAtPath:resolvedPath withIntermediateDirectories:YES attributes:nil error:error];
if (success) {
return resolvedPath;
} else {
return nil;
}
}
- (NSString *)applicationSupportDirectory {
NSString *executableName = [NSBundle mainBundle].infoDictionary[@"CFBundleExecutable"];
NSError *error = nil;
NSString *result = [self findOrCreateDirectory:NSApplicationSupportDirectory inDomain:NSUserDomainMask subdirectory:executableName error:&error];
if (error) {
NSLog(@"Unable to find or create application support directory: %@", error);
}
return result;
}
- (NSArray *)findExistingPaths:(NSArray*)paths withSubdirectory:(NSString*)subdirectory {
NSMutableArray * subpaths = [NSMutableArray array];
for (NSString * path in paths) {
NSString *fullPath = [path stringByAppendingPathComponent:subdirectory];
if ([self fileExistsAtPath:fullPath]) {
[subpaths addObject:fullPath];
}
}
return subpaths;
}
- (NSArray *)listAllSubdirectories:(NSArray*)paths {
NSMutableArray * subdirectories = [NSMutableArray array];
NSError * error = nil;
for (NSString * path in paths) {
NSArray * names = [self contentsOfDirectoryAtPath:path error:&error];
if (error) {
NSLog(@"Error while listing subdirectories in path %@: %@", path, error);
error = nil;
continue;
}
for (NSString * name in names) {
NSString * fullPath = [path stringByAppendingPathComponent:name];
BOOL directory = NO;
[self fileExistsAtPath:fullPath isDirectory:&directory];
if (directory) {
[subdirectories addObject:fullPath];
}
}
}
return subdirectories;
}
@end