Re: Another filesystem mystery
Re: Another filesystem mystery
- Subject: Re: Another filesystem mystery
- From: email@hidden
- Date: Wed, 27 Feb 2002 16:32:26 -0800
Well, some followup to my question already went over the list, but I
thought it might be useful for somebody to see the code I ended up
with. It's a combination of code from several replies, together with my
own innovations. :-> It seems to work nicely for me, although I'll
admit I haven't tested cases where aliases point onto unmounted volumes,
etc.
If any sort of failure occurs, it falls back on
-stringByResolvingSymlinksInPath's result. This is important, as
otherwise this method would return nil for any path that did not
actually exist on the filesystem. It's an interesting question what
this method will do with a path that does not exist, but that starts
with a path that *does* exist but contains an alias. I believe it will
return the full path without resolving the alias in the middle, but I
haven't tried it. Beyond a certain point, I can't bring myself to
care. :->
I look forward to the promised fix for these problems in Foundation!
@interface NSString (CocoaExtra)
- (NSString *)stringByResolvingSymlinksAndAliasesInPath;
@end
@implementation NSString (CocoaExtra)
- (NSString *)stringByResolvingSymlinksAndAliasesInPath
{
NSString *path = [self stringByResolvingSymlinksInPath];
NSString *outString = nil;
CFURLRef url = NULL;
Boolean success = false;
FSRef aliasRef;
OSErr anErr = noErr;
Boolean targetIsFolder;
Boolean wasAliased;
url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
(CFStringRef)path,
kCFURLPOSIXPathStyle,
false);
if (url == NULL)
return path;
success = CFURLGetFSRef(url, &aliasRef);
CFRelease(url);
if (success == false)
return path;
anErr = FSResolveAliasFileWithMountFlags(&aliasRef, true,
&targetIsFolder,
&wasAliased, kARMMountVol | kARMNoUI | kARMMultVols | kARMSearch);
if (anErr != noErr)
return path;
url = CFURLCreateFromFSRef(kCFAllocatorDefault, &aliasRef);
if (url == NULL)
return path;
outString = (NSString *)CFURLCopyFileSystemPath(url,
kCFURLPOSIXPathStyle);
CFRelease(url);
return (outString ? [outString autorelease] : path);
}
@end
I hope this is useful to somebody...
Ben Haller
Stick Software
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.