how can I know the relationship between usb device node and volume on mac

白昼怎懂夜的黑 提交于 2021-02-10 08:58:20

问题


for example, i have a flash disk(KingStone Mass Storage), and only one partition , so when I plug it on mac. I'll see a Volume(it might be /Volumes/KingStone) was mounted automatically. we could see volume(/Volumes/Kingstone) is belong to the KingSton disk.

but now I pluged another disk, such as AData disk. and another volume was mounted. and how could I know which volume is belong to kingstone disk.(we could know which disk is kongston by VenderID).

now in code, we could know mounted volumes by call [[NSWorkspace sharedWorkspace] mountedRemovableMedia] OR [[NSFileManager defaultFileManager] mountedVolumeURLsInclud.....]

we could also know all usb device by using kIOUSBDeviceClassName with IOServiceMatching and IOServicesGetMatchingServices

even kIOMediaClassName with the two function we will know volume media,

we could determine every volume media belongs to which usb device by path.

but I don't know the mount point of volume media.

either something else useful.

sorry for my pool English.


回答1:


Another way.

Create a Matching Dictionary with kIOMediaClass

matchingDict = IOServiceMatching(kIOMediaClass);

if you only want to get removable storage volume set the dictionary with kIOMediaRemovableKey and kCFBooleanTrue

CFDictionarySetValue(matchingDict, CFSTR(kIOMediaRemovableKey), kCFBooleanTrue);

and getting matching service now,

IOServiceGetMatchingService(kIOMasterPortDefault, matchingDict, &iterator);

you can enumerate your device now.

while((removableMedia = IOteratorNext(iterator)))
{
    IORegistryEntryGetName(removableMedia, deviceName); 
    // and something else you can do   


    kr = IORegistryGetPath(removableMedia, kIOServicePlane, devicePath);
    // compare the path with path you get in device.
    // if one device's path is the substring of this media
    // we could simply think this media is belong to the device

    // you could get mount point by following code
    DASessionRef sessionRef = DASessionCreate(kCFAllocatorDefault);
    if (sessionRef) {
        DADiskRef diskRef - DADiskCreateFromIOMedia(kCFAllocatorDefault, sessionRef, removableMedia);
        if (diskRef) {
            CFDictionaryRef *diskProperty=DADisCopyDescription(diskRef);
            if (property) {
                NSURL *mountURL = [(NSDictionary*)property objectForKey:(NSString*)kDADiskDescriptionVolumePathKey];
                // mountURL or [mountURL path] is the mount point you want

                CFRelease(diskProperty);
            }
            CFRelease(diskRef);
        }
        CFRelease(sessionRef);
    }

    // don't forget to release
    IOObjectRelease(removableMedia);
}

and you could Observer mount/unmount event like below

[[[NSWorkSpace sharedWorkspace] notificationCenter] addObsever:self selector@selector(volumeMounted:) name:NSWorkspaceDidMountNotification object:nil];
[[[NSWorkSpace sharedWorkspace] notificationCenter] addObsever:self selector@selector(volumeUnmounted:) name:NSWorkspaceDidUnmountNotification object:nil];


来源:https://stackoverflow.com/questions/12951287/how-can-i-know-the-relationship-between-usb-device-node-and-volume-on-mac

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