Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cocoa objective-c for os x : get volume mount point from path

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

like image 749
Michael C Avatar asked Jan 09 '10 10:01

Michael C


2 Answers

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;
}
like image 110
Pierre Bernard Avatar answered Oct 01 '22 02:10

Pierre Bernard


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 than path, or whether path/.. and path point to the same i-node on the same device — this should detect mount points for all Unix and POSIX variants.

like image 28
Mikhail Edoshin Avatar answered Oct 01 '22 01:10

Mikhail Edoshin