0

我在获取文件的完整 url 时遇到了问题FSCopyURLForVolume。我正在使用此问题中的代码从文件 URL 确定 AFP 共享,但它没有给我完整的 URL。例如:

使用如下路径:/Volume/server/html/index.html

我得到的只是基础安装的 url:nfs://real_server_name/vol

叶目录和文件名被省略,完整路径在文件信息中可用,因此必须有一种方法来获取此信息。

编辑:

经过更多的挖掘之后,我似乎想使用kFSCatInfoParentDirIDkFSCatInfoNodeID获取父节点和节点(文件)ID,但我不确定如何将其变成有用的东西。

4

2 回答 2

2

因为 FSPathMakeRef、FSGetCatalogInfo 和 FSCopyURLForVolume 已从 Mac OS X 10.8 中弃用,所以我对获取 Mac OS X 安装卷上的 UNC 网络路径的代码进行了现代化改造。

    NSError *error=nil; //Error 
    NSURL *volumePath=nil; //result of UNC network mounting path 

    NSString* testPath =@"/Volumes/SCAMBIO/testreport.exe"; //File path to test 

    NSURL *testUrl = [NSURL fileURLWithPath:testPath]; //Create a NSURL from file path
    NSString* mountPath = [testPath stringByDeletingLastPathComponent]; //Get only mounted volume part i.e. /Volumes/SCAMBIO
    NSString* pathComponents = [testPath substringFromIndex:[mountPath length]]; //Get the rest of the path starting after the mounted path i.e. /testereport.exe
   [testUrl getResourceValue:&volumePath forKey:NSURLVolumeURLForRemountingKey error:&error]; //Get real UNC network mounted path i.e. smb://....

    NSLog(@"Path: %@%@", volumePath,pathComponents); //Write result to debug console

结果是在我的情况下,路径:smb://FRANCESCO@192.168.69.44/SCMBIO/testreport.exe

您需要指定网络映射卷。

再见。

于 2013-10-30T19:00:33.303 回答
0

Apple Dev Forums上提出了解决此问题的方法,这是我想出的最终功能:

- (NSURL *)volumeMountPathFromPath:(NSString *)path{
    NSString *mountPath = nil;
    NSString *testPath = [path copy];
    while(![testPath isEqualToString:@"/"]){
        NSURL *testUrl = [NSURL fileURLWithPath:testPath];
        NSNumber *isVolumeKey;
        [testUrl getResourceValue:&isVolumeKey forKey:NSURLIsVolumeKey error:nil];
        if([isVolumeKey boolValue]){
            mountPath = testPath;
            break;
        }        
        testPath = [testPath stringByDeletingLastPathComponent];
    }

    if(mountPath == nil){
        return nil;
    }

    NSString *pathCompointents = [path substringFromIndex:[mountPath length]];

    FSRef pathRef;
    FSPathMakeRef((UInt8*)[path fileSystemRepresentation], &pathRef, NULL);
    FSCatalogInfo catalogInfo;
    OSErr osErr = FSGetCatalogInfo(&pathRef, kFSCatInfoVolume|kFSCatInfoParentDirID,
                                   &catalogInfo, NULL, NULL, NULL);
    FSVolumeRefNum volumeRefNum = 0;
    if(osErr == noErr){
        volumeRefNum = catalogInfo.volume;
    }

    CFURLRef serverLocation;
    OSStatus result = FSCopyURLForVolume(volumeRefNum, &serverLocation);
    if(result == noErr){
        NSString *fullUrl = [NSString stringWithFormat:@"%@%@", 
                             CFURLGetString(serverLocation), pathCompointents];        
        return [NSURL URLWithString:fullUrl];
    }else{
        NSLog(@"Error getting the mount path: %i", result);
    }
    return nil;
}
于 2012-03-10T02:48:04.217 回答