开发者

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

开发者 https://www.devze.com 2022-12-16 01:40 出处:网络
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,

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 menti开发者_开发知识库on the mount point.

Can anybody help ?

thanks


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消