Reputation: 201
I would like to find the mount point of a volume for a given NSString path.
Though I'm a beginner in Cocoa and objective-C, I'm trying to do this "elegantly", ie. using one of the provided class, rather than making an external shell call or listing mounted filesystems and finding which one the path belongs to.
I did find NSWorkspace and getFileSystemInfoForPath, but it does not mention the mount point.
Can anybody help ?
thanks
Upvotes: 6
Views: 3708
Reputation: 255
I've run by this a month after it has been asked, but anyway: in Python standard library there's os.path.ismount()
function, which detects if a path is a mount point. From its description it does it so:
The function checks whether
path
‘s parent,path/..
, is on a different device thanpath
, or whetherpath/..
andpath
point to the same i-node on the same device — this should detect mount points for all Unix and POSIX variants.
Upvotes: 1
Reputation: 3198
This should go something along those lines:
+ (NSString*)volumeNameForPath:(NSString *)inPath
{
HFSUniStr255 volumeName;
FSRef volumeFSRef;
unsigned int volumeIndex = 1;
while (FSGetVolumeInfo(kFSInvalidVolumeRefNum, volumeIndex++, NULL, kFSVolInfoNone, NULL, &volumeName, &volumeFSRef) == noErr) {
NSURL *url = [(NSURL *)CFURLCreateFromFSRef(NULL, &volumeFSRef) autorelease];
NSString *path = [url path];
if ([inPath hasPrefix:path]) {
return [NSString stringWithCharacters:volumeName.unicode length:volumeName.length]
}
}
return nil;
}
Upvotes: 4