Get type information from volume path on MacOS

When I get a volume path like /Volumes/somedrive, I need to check if it's a USB drive, a CD/DVD drive or a network share. Is there a way to get this information by C/C++ or Objective-C code? Thanks!

Replies

Some of this information can be obtained from the CFURL/NSURL APIs. Look for the APIs having to do with resource properties, like CFURLCopyResourcePropertyForKey() and the volume property keys.


For other information, you may need to use the Disk Arbitration API.

Thanks for your reply. After browsing apple's documentation, I found it's hard to differentiate these three types of volumes. Does the kCFURLVolumeIsEjectableKey mean it's a CD/DVD drive? And kCFURLVolumeIsRemovableKey means it's a USB drive? I still cannot find a way to check if the drive is a network share, like AFP or SMB. Could you please share some snippet? Thanks!

It would be a network share if it's not local: kCFURLVolumeIsLocalKey/NSURLVolumeIsLocalKey.


A question you need to ask yourself is whether USB vs. CD/DVD is really the distinction you want. It might be more appropriate to actually base code logic and/or UI on removable or not, ejectable or not, internal or not, etc. Consider, for example, tape drives or other kinds of drives that don't fall into either of your categories.


If you really need to know the media type or bus type, that would be where Disk Arbitration would be better. You would create a DADisk reference using DADiskCreateFromVolumePath(). Then you can get its properties using DADiskCopyDescription(). In the returned dictionary, should be a value under the key kDADiskDescriptionMediaKindKey.