-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNSFileManager+ESBExtensions.m
121 lines (96 loc) · 3.62 KB
/
NSFileManager+ESBExtensions.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
120
121
//
// NSFoundation+ESBExtension.m
// ESBFoundation
//
// Created by Eric Blair on Wed Sep 17 2003.
// Copyright (c) 2003 __MyCompanyName__. All rights reserved.
//
#import "NSFileManager+ESBExtensions.h"
@implementation NSFileManager (ESBExtensions)
- (BOOL)directoryExistsAtPath:(NSString *)path
{
return [self directoryExistsAtPath:path traverseLink:NO];
}
- (BOOL)directoryExistsAtPath:(NSString *)path
traverseLink:(BOOL)traverseLink
{
NSDictionary * attributes;
attributes = [self fileAttributesAtPath:path traverseLink:traverseLink];
return [[attributes objectForKey:NSFileType] isEqualToString:NSFileTypeDirectory];
}
- (void)createPathToFile:(NSString *)path
attributes:(NSDictionary *)attributes
{
NSArray * pathComponents;
unsigned int dirCount;
NSString * finalDirectory;
pathComponents = [path pathComponents];
dirCount = [pathComponents count] - 1;
// Short-circuit if the final directory already exists
finalDirectory =
[NSString pathWithComponents:
[pathComponents subarrayWithRange:NSMakeRange(0, dirCount)]];
[self createPath:finalDirectory attributes:attributes];
}
- (void)createPath:(NSString *)path
attributes:(NSDictionary *)attributes
{
NSArray * pathComponents;
unsigned int dirIndex, dirCount;
unsigned int startingIndex;
pathComponents = [path pathComponents];
dirCount = [pathComponents count];
// Short-circuit if the final directory already exists
path =
[NSString pathWithComponents:
[pathComponents subarrayWithRange:NSMakeRange(0, dirCount)]];
if ([self directoryExistsAtPath:path traverseLink:YES])
return;
startingIndex = 0;
for (dirIndex = startingIndex; dirIndex < dirCount; dirIndex++) {
NSString *partialPath;
BOOL fileExists;
partialPath =
[NSString pathWithComponents:
[pathComponents subarrayWithRange:NSMakeRange(0, dirIndex + 1)]];
// Don't use the 'fileExistsAtPath:isDirectory:' version since it doesn't traverse symlinks
fileExists = [self fileExistsAtPath:partialPath];
if (!fileExists) {
if (![self createDirectoryAtPath:partialPath attributes:attributes])
[NSException raise:NSGenericException
format:@"Unable to create a directory at path: %@", partialPath];
} else {
NSDictionary *attributes;
attributes = [self fileAttributesAtPath:partialPath traverseLink:YES];
if (![[attributes objectForKey:NSFileType] isEqualToString: NSFileTypeDirectory]) {
[NSException raise:NSGenericException
format:@"Unable to write to path \"%@\" because \"%@\" is not a directory",
path, partialPath];
}
}
}
}
- (NSString *)getApplicationSupportFolder
{
NSArray * domains =
NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
NSUserDomainMask,
YES);
NSString * baseDir = [domains objectAtIndex:0];
NSString * allSupportDir =
[baseDir stringByAppendingPathComponent:@"Application Support"];
NSString * appSupportDir =
[allSupportDir stringByAppendingPathComponent:
[self displayNameAtPath:[[NSBundle mainBundle] bundlePath]]];
[self createPath:appSupportDir attributes:nil];
return [[appSupportDir copy] autorelease];
}
- (NSString *)getApplicationSupportSubpath:(NSString *)subPath
{
NSString * appSupportDir = [self getApplicationSupportFolder];
NSString * appSupportSubdir =
[appSupportDir stringByAppendingPathComponent:subPath];
[self createPath:appSupportSubdir attributes:nil];
return [[appSupportSubdir copy] autorelease];
}
@end