How to use Mac Finder to list all aliases in a folder

前端 未结 2 1850
忘掉有多难
忘掉有多难 2020-12-17 01:23

I downloaded a repository from Bitbucket.org as a zip file and unzipped it using iZip to my Mac. Xcode found many compile errors because the zip or unzip did not preserve a

2条回答
  •  一个人的身影
    2020-12-17 01:29

    Apple has conflated alias and symlink. I don't know how to do this at the command line other than using ls.

    ls -la | grep ">"
    

    The following Objective-C function would find the target.

    #include 
    
    NSURL *targetOfAlias(NSURL *url) {
        CFErrorRef *errorRef = NULL;
        CFDataRef bookmark = CFURLCreateBookmarkDataFromFile (NULL, (__bridge CFURLRef)url, errorRef);
        if (bookmark == nil) return nil;
        CFURLRef resolvedUrl = CFURLCreateByResolvingBookmarkData (NULL, bookmark, kCFBookmarkResolutionWithoutUIMask, NULL, NULL, NO, errorRef);
        return CFBridgingRelease(resolvedUrl);
    }
    
    NSString *getTarget(NSString *fPath) {
        NSString *resolvedPath = nil;
        // Use lstat to determine if the file is a symlink
        struct stat fileInfo;
        NSFileManager *fileManager = [NSFileManager new];
        if (lstat([fileManager fileSystemRepresentationWithPath:fPath], &fileInfo) < 0)
            return nil;
        if (S_ISLNK(fileInfo.st_mode)) {
            // Resolve the symlink component in the path
            NSError *error = nil;
            resolvedPath = [fileManager destinationOfSymbolicLinkAtPath:fPath error:&error];
            if (resolvedPath == nil) {
                NSAlert *alert = [NSAlert alertWithError:error];
                [alert runModal];
                return nil;
            }
            if ([resolvedPath isAbsolutePath])
                return resolvedPath;
            else
                return [[fPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:resolvedPath];
        }
    
        // Resolve alias
        NSURL *resolvedUrl = targetOfAlias([NSURL fileURLWithPath:fPath]);
        return [resolvedUrl path];
    }
    

提交回复
热议问题