2012-03-10 76 views
1

我遇到一個問題,獲取文件的完整url與FSCopyURLForVolume。我正在使用這個問題的代碼Determine AFP share from a file URL,但它沒有給我完整的網址。例如:完整的URL從FSCopyURLForVolume

有了這樣的路徑:/Volume/server/html/index.html

所有我得到的回覆是URL到基座安裝架:nfs://real_server_name/vol

葉目錄和文件名都不放過,全路徑是可用文件信息,所以必須有一種方法來獲取這些信息。

編輯:

後一些更多的挖是好像我要使用kFSCatInfoParentDirIDkFSCatInfoNodeID獲取父和節點(文件)的ID,但我不知道如何把它變成有用的東西。

+0

'FSCopyURLForVolume'實際上是給你卷的完整URL。如果您需要該捲上的某個項目的URL,則需要自行構建該項目,最好使用NSURL的方法或CFURL的功能。 – 2012-03-10 16:01:59

回答

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

你爲什麼使用'stringWithFormat:'?您可以創建一個相對於另一個URL的URL。 – 2012-03-10 16:00:00

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 [email protected]"/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://[email protected]/SCAMBIO/testreport.exe

您需要指定網絡映射的卷。

ciao。