Initial checkin.
5 // Copyright 2008 Jens Alfke. All rights reserved.
12 @implementation FileAlias
15 - (id) initWithFilePath: (NSString*)targetPath
16 relativeToPath: (NSString*)fromPath
17 error: (NSError**)error
19 NSParameterAssert(targetPath);
23 FSRef fromRef, targetRef;
24 err = PathToFSRef(targetPath, &targetRef);
27 err = PathToFSRef(fromPath,&fromRef);
29 err = FSNewAlias(&fromRef,&targetRef,&_alias);
31 err = FSNewAlias(NULL,&targetRef,&_alias);
35 if( ! CheckOSErr(err,error) ) {
36 Warn(@"FileAlias init failed with OSStatus %i",err);
44 - (id) initWithFilePath: (NSString*)path
45 error: (NSError**)error
47 return [self initWithFilePath: path relativeToPath: nil error: error];
54 DisposeHandle((Handle)_alias);
59 - (void)encodeWithCoder:(NSCoder *)coder
61 NSParameterAssert([coder allowsKeyedCoding]);
62 NSKeyedArchiver *arch = (NSKeyedArchiver*)coder;
64 [arch encodeBytes: (const uint8_t *) *_alias
65 length: GetHandleSize((Handle)_alias)
66 forKey: @"aliasHandle"];
70 - (id)initWithCoder:(NSCoder *)decoder
72 NSParameterAssert([decoder allowsKeyedCoding]);
73 NSKeyedUnarchiver *arch = (NSKeyedUnarchiver*)decoder;
79 const void *bytes = [arch decodeBytesForKey:@"aliasHandle" returnedLength: &length];
81 PtrToHand(bytes,&handle,length);
86 _alias = (AliasHandle) handle;
92 - (NSString*) description
94 return [NSString stringWithFormat: @"%@['%@']", [self class],[self originalFilename]];
98 - (NSString*) originalPath
100 CFStringRef path = NULL;
101 OSStatus err = FSCopyAliasInfo(_alias,NULL,NULL,&path,NULL,NULL);
105 return [(id)path autorelease];
108 - (NSString*) originalFilename
110 HFSUniStr255 targetName;
111 OSStatus err = FSCopyAliasInfo(_alias,&targetName,NULL,NULL,NULL,NULL);
115 return [(id)FSCreateStringFromHFSUniStr(NULL,&targetName) autorelease];
118 - (NSString*) originalVolumeName
120 HFSUniStr255 volName;
121 OSStatus err = FSCopyAliasInfo(_alias,NULL,&volName,NULL,NULL,NULL);
125 return [(id)FSCreateStringFromHFSUniStr(NULL,&volName) autorelease];
130 HFSUniStr255 targetName,volName;
132 FSAliasInfoBitmap whichInfo = 0;
134 OSStatus err = FSCopyAliasInfo(_alias,&targetName,&volName,&path,&whichInfo,&info);
136 NSLog(@"FSCopyAliasInfo returned error %i",err);
139 NSString *str = (id)FSCreateStringFromHFSUniStr(NULL,&targetName);
140 NSLog(@"Target name = '%@'",str);
142 str = (id)FSCreateStringFromHFSUniStr(NULL,&volName);
143 NSLog(@"Volume name = '%@'",str);
145 NSLog(@"Path = %@",path);
146 if( path ) CFRelease(path);
147 NSLog(@"Info bitmap = %08X", whichInfo);
152 #pragma mark RESOLVING:
155 - (NSString*) filePathRelativeToPath: (NSString*)fromPath error: (NSError**)error
157 FSRef fromRef, targetRef, *fromRefPtr;
159 if( ! CheckOSErr( PathToFSRef(fromPath,&fromRef), error ) )
161 fromRefPtr = &fromRef;
167 NSString *targetPath;
168 if( CheckOSErr( FSResolveAlias(fromRefPtr,_alias,&targetRef,&wasChanged), error)
169 && CheckOSErr( FSRefToPath(&targetRef,&targetPath), error ) )
172 NSLog(@"%@: Couldn't resolve alias!",self);
178 - (NSString*) filePath: (NSError**)error
180 return [self filePathRelativeToPath: nil error: error];
184 - (NSArray*) findMatchesRelativeToPath: (NSString*)fromPath
185 withRules: (unsigned)rules
186 error: (NSError**)error
188 FSRef fromRef, *fromRefPtr;
190 if( ! CheckOSErr( PathToFSRef(fromPath,&fromRef), error ) )
192 fromRefPtr = &fromRef;
199 FSRef matches[count];
201 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
202 if( ! FSMatchAliasBulk(fromRefPtr, rules, _alias, &count, matches, &wasChanged, NULL, NULL), error ) {
203 NSLog(@"%@: FSMatchAliasBulk failed!",self);
207 if( ! CheckOSErr( FSMatchAlias(fromRefPtr,rules,_alias,&count,matches,&wasChanged,NULL,NULL), error) ) {
208 NSLog(@"%@: FSMatchAlias failed!",self);
213 NSMutableArray *paths = [NSMutableArray arrayWithCapacity: count];
214 for( short i=0; i<count; i++ ) {
216 if( FSRefToPath(&matches[i],&path) == noErr )
217 [paths addObject: path];
223 - (NSArray*) findMatches: (NSError**)error
225 return [self findMatchesRelativeToPath: nil
226 withRules: kARMMultVols | kARMSearch | kARMSearchMore | kARMTryFileIDFirst