
/*
	NSFileManager: Resolve an alias
	Original Source: <http://cocoa.karelia.com/Foundation_Categories/NSFileManager__Reso.m>
	(See copyright notice at <http://cocoa.karelia.com>)
*/

// From app kit release notes, but modified so it returns nil if not resolved.

- (NSString*) pathResolved:(NSString*) inPath
{
	CFStringRef resolvedPath = nil;
	CFURLRef	url = CFURLCreateWithFileSystemPath(NULL /*allocator*/, (CFStringRef)inPath, kCFURLPOSIXPathStyle, NO /*isDirectory*/);
	if (url != NULL) {
		FSRef fsRef;
		if (CFURLGetFSRef(url, &fsRef)) {
			Boolean targetIsFolder, wasAliased;
			if (FSResolveAliasFile (&fsRef, true /*resolveAliasChains*/, &targetIsFolder, &wasAliased) == noErr && wasAliased) {
				CFURLRef resolvedurl = CFURLCreateFromFSRef(NULL /*allocator*/, &fsRef);
				if (resolvedurl != NULL) {
					resolvedPath = CFURLCopyFileSystemPath(resolvedurl, kCFURLPOSIXPathStyle);
					CFRelease(resolvedurl);
				}
			}
		}
		CFRelease(url);
	}
	return (NSString *)resolvedPath;
}

- (BOOL) isAliasPath:(NSString *)inPath
{
	return [self pathResolved:inPath] != nil;
}

- (NSString*) resolveAliasPath:(NSString*) inPath
{
	NSString *resolved = [self pathResolved:inPath];
	return (nil != resolved) ? resolved : inPath;
}