10.6 compatibility: Fix some new compiler warnings, and work around apparent regressions in NSTask and -stringByStandardizingPath.
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 for %@",err,targetPath);
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( ! CheckOSErr( 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
235 Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
237 Redistribution and use in source and binary forms, with or without modification, are permitted
238 provided that the following conditions are met:
240 * Redistributions of source code must retain the above copyright notice, this list of conditions
241 and the following disclaimer.
242 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
243 and the following disclaimer in the documentation and/or other materials provided with the
246 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
247 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
248 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
249 BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
250 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
251 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
252 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
253 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.