I found the information attached to IODVDServices using ioreg -r -c IODVDServices
(ioreg).
The corresponding registry entry has (grand)children of which one is of type IODVDMedia when no CD is in the drive and of type IOCDMedia when an audio disc is in the drive (including the device name when a disc is inserted).
These children can be accessed with IORegistryEntryCreateIterator()
and tested for being a certain class with IOObjectConformsTo()
:
static io_object_t get_media(io_object_t storageDevice)
{
io_iterator_t childrenIterator;
io_object_t nextChild;
io_object_t mediaObject = 0;
IORegistryEntryCreateIterator(storageDevice,
kIOServicePlane, kIORegistryIterateRecursively,
&childrenIterator);
while ((nextChild = IOIteratorNext(childrenIterator))) {
if (IOObjectConformsTo(nextChild, kIOCDMediaClass))
mediaObject = nextChild;
else
IOObjectRelease(nextChild);
}
IOObjectRelease(childrenIterator);
return mediaObject;
}
I pushed a complete solution to gist.
This tries to find a mounted media attached to a specific drive of IOCDBlockStorageDeviceClass. Finding the device name of that media works the same as specified in the question and the CDROMSample provided by Apple.
EDIT:
You can also just take any BSD name in one of the (recursive) children with IORegistryEntrySearchCFProperty
. There shouldn't be other children with device names and this saves you the additional iteration.
deviceFilePathAsCFString = IORegistryEntrySearchCFProperty(storageDevice,
kIOServicePlane, CFSTR(kIOBSDNameKey),
kCFAllocatorDefault, kIORegistryIterateRecursively);
This is used in the implementation for libdiscid.