Determine AFP share from a file URL

一笑奈何 提交于 2019-12-12 10:00:06

问题


I writing an application to copy some files to a certain location. It allows the user to choose the destination, which is always on an AFP share. This is done with a NSOpenPanel. The URL that gets returned is: file://localhost/Volumes/Oliver%20Legg's%20Backup/.

What I want to accomplish is when the application is started, if the AFP share isn't mounted I want it to automatically mount it. What is the best way to do this?

The Get Info command lists the server as: afp://Power Mac G5._afpovertcp.local/Oliver%20Legg's%20Backup. How can I programatically get this URL from a file URL? I think if I could get that URL I could mount it using FSMountServerVolumeAsync. Is that the best (easiest, most abstracted) API to use?


回答1:


You need to use some lower-level File Manager routines to get this information, there's no way to do it with regular Cocoa calls. The URL is obtained using FSCopyURLForVolume() but you need to get a volume reference number for the mounted volume in order to use it:

#import <CoreServices/CoreServices.h>

//this is the path to the mounted network volume
NSString* pathToVolume = @"/Volumes/MountedNetworkVolume/";

//get the volume reference number
FSRef pathRef;
FSPathMakeRef((UInt8*)[pathToVolume fileSystemRepresentation],&pathRef,NULL);
FSCatalogInfo catalogInfo;
OSErr osErr = FSGetCatalogInfo(
                               &pathRef,
                               kFSCatInfoVolume,
                               &catalogInfo,
                               NULL,
                               NULL,
                               NULL
                               ) ;
FSVolumeRefNum volumeRefNum = 0;
if(osErr == noErr) 
    volumeRefNum = catalogInfo.volume;

//get the server URL for the volume
CFURLRef serverLocation;
OSStatus result = FSCopyURLForVolume (volumeRefNum,&serverLocation);
if(result == noErr)
    NSLog(@"The server location is: %@",serverLocation);
else
    NSLog(@"An error occurred: %i",result);
CFRelease(serverLocation);

FSMountServerVolumeAsync is definitely the correct way to mount a remote volume.



来源:https://stackoverflow.com/questions/2243531/determine-afp-share-from-a-file-url

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!