forked from jerrykrinock/CategoriesObjC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSData+FileAlias.h
102 lines (79 loc) · 2.83 KB
/
NSData+FileAlias.h
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
#import <Cocoa/Cocoa.h>
extern NSString* const NSDataFileAliasAliasRecord ;
extern NSString* const NSDataFileAliasPath ;
extern NSString* const NSDataFileAliasError ;
/*!
@brief A category on NSData for converting (sometimes "resolving")
AliasRecord datas to paths, and vice versa, creating AliasRecords
@details This category requires Mac OS X 10.5 or later. Test
code is provided below.
*/
@interface NSData (FileAlias)
/*!
@brief Returns a handle to the receiver's bytes.
*/
- (AliasHandle)aliasHandle ;
/*!
@brief Returns the data of an alias record, given a path.
@details Does not require that the file specified by path exists,
but at least its parent must exist.
If file does not exist, but its parent exists, returns a minimal
alias.
If file parent does not exist, will return nil.
This method may be non-deterministic. Try it twice on the same
path and you may get a few bits different. Or, you may not.
*/
+ (NSData*)aliasRecordFromPath:(NSString*)path ;
/*!
@brief Invokes pathFromAliasRecordWithTimeout:error_p
@details First, tries to resolve the alias and returns the resolved
path. If the file specified by the receiver does not
exist, extracts the path and returns it.
By convention, if the alias record specifies a directory,
the path returned by this method will NOT have a trailing slash.
@param timeout The timeout after which nil will be returned.
@param error_p Pointer which will, upon return, if an error
occurred and said pointer is not NULL, point to an NSError
describing said error.
*/
- (NSString*)pathFromAliasRecordWithTimeout:(NSTimeInterval)timeout
error_p:(NSError**)error_p ;
@end
/* TEST CODE
#import "NSData+FileAlias.h"
void TestPath(NSString* path) {
NSData* alias = [NSData aliasRecordFromPath:path] ;
NSString* recoveredPath = [alias pathFromAliasRecordWithTimeout:3.0
error_p:NULL] ;
if ([path isEqualToString:recoveredPath]) {
NSLog(@"Passed: %@", path) ;
}
else {
NSLog(@"Failed: %@", path) ;
NSLog(@" Got: %@", recoveredPath) ;
}
}
int main(int argc, const char *argv[]) {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init] ;
NSLog(@"*** Tests which should pass ***") ;
TestPath(@"/Users") ;
TestPath(@"/Users/Ptolemy") ;
TestPath(@"/Yousers") ;
TestPath(@"/Volumes/NoSuchVolume") ;
TestPath(@"/Users/NoSuchFileButParentExists") ;
TestPath([NSHomeDirectory() stringByAppendingPathComponent:@".DS_Store"]) ;
TestPath(@"/Applications/Safari.app/Contents/MacOS/Safari") ;
NSLog(@"\n") ;
NSLog(@"\n") ;
NSLog(@"*** Tests which should fail ***") ;
TestPath(@"/Users/") ;
TestPath(@"/Yousers/") ;
TestPath(@"/Yousers/Ptolemy") ;
TestPath(@"") ;
TestPath(@"/") ;
TestPath(@"/No/Such/File/And/Parent/Does/Not/Exist") ;
TestPath(@"NotEvenAPath") ;
[pool release] ;
return 0 ;
}
*/