【问题标题】:Full URL from FSCopyURLForVolume来自 FSCopyURLForVolume 的完整 URL
【发布时间】:2013-10-31 04:13:21
【问题描述】:

我在获取带有FSCopyURLForVolume 的文件的完整网址时遇到问题。我正在使用此问题中的代码Determine AFP share from a file URL,但它没有给我完整的网址。例如:

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

我得到的只是基本安装的网址:nfs://real_server_name/vol

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

编辑:

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

【问题讨论】:

  • FSCopyURLForVolume 实际上是为您提供卷的完整 URL。如果您想要该卷上某个项目的 URL,则需要自己构建,最好使用 NSURL 的方法或 CFURL 的函数来实现此目的。

标签: objective-c cocoa core-services


【解决方案1】:

因为 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

您需要指定网络映射卷。

ciao.

【讨论】:

    【解决方案2】:

    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;
    }
    

    【讨论】:

    • 你为什么使用stringWithFormat:?您可以创建一个相对于另一个 URL 的 URL。
    猜你喜欢
    • 2011-10-18
    • 1970-01-01
    • 1970-01-01
    • 2022-09-23
    • 2015-10-14
    • 2013-09-10
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多