1

On Windows I am able to enumerate removable drives using FindFirstVolume, FindNextVolume, GetVolumePathNamesForVolumeName and the GetDriveType functions. How can I achieve this on Linux and macOS?

4
  • On Linux you can parse /proc/mounts. Sep 19, 2022 at 11:24
  • @SamVarshavchik I quickly checked, but I am not sure what indicates that it is removable: /dev/sdc1 /media/robert/KIOXIA vfat rw,nosuid,nodev,relatime,uid=1000,gid=1000,fmask=0022,dmask=0022,codepage=437,iocharset=iso8859-1,shortname=mixed,showexec,utf8,flush,errors=remount-ro 0 0
    – robert
    Sep 19, 2022 at 11:28
  • On macOS, diskutil list for the command line. Not sure what you'd need to do that programmatically (other than fork and execvp a child process and slurp up the output).
    – Eljay
    Sep 19, 2022 at 11:31
  • You are not going to find many Linux installations that use FAT. Any fat or vfat volume is a safe bet for being a mounted volume. There are other, more complicated ways to determine this, but this is the easiest way. Another telltale sign is the /media mount point. Sep 19, 2022 at 11:32

1 Answer 1

2

On macOS you need to work with IOKit: https://developer.apple.com/documentation/iokit

It can looks like the following code:

#include <cstdlib>
#include <iostream>

#include <mach/mach_port.h>

#include <IOKit/IOKitLib.h>
#include <IOKit/IOTypes.h>
#include <IOKit/IOKitKeys.h>
#include <IOKit/IOCFPlugIn.h>
#include <IOKit/usb/IOUSBLib.h>


std::string get_descriptor_idx(IOUSBDeviceInterface **dev, UInt8 idx);

int main(int argc, char *argv[]) {
  
  /*
   * Get the IO registry
   */
  auto entry = IORegistryGetRootEntry(kIOMasterPortDefault);
  if (entry == 0)
    return EXIT_FAILURE;
  
  io_iterator_t iter{};
  auto kret = IORegistryEntryCreateIterator(entry, kIOUSBPlane, kIORegistryIterateRecursively, &iter);
  if (kret != KERN_SUCCESS || iter == 0)
    return EXIT_FAILURE;
  
  io_service_t  service {};
  
  std::cout << std::endl;
  
  while ((service = IOIteratorNext(iter))) {
    
    IOCFPlugInInterface  **plug  = nullptr;
    IOUSBDeviceInterface **dev   = nullptr;
    io_string_t            path;
    SInt32                 score = 0;
    IOReturn               ioret;
    
    kret = IOCreatePlugInInterfaceForService(service, kIOUSBDeviceUserClientTypeID, kIOCFPlugInInterfaceID, &plug, &score);
    IOObjectRelease(service);
    if (kret != KERN_SUCCESS || plug == nullptr) {
      continue;
    }
    
    /*
     * USB
     */
    ioret = (*plug)->QueryInterface(plug, CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID),
                                    static_cast<LPVOID *>((void *) &dev));
    (*plug)->Release(plug);
    if (ioret != kIOReturnSuccess || dev == nullptr) {
      continue;
    }
    
    if (IORegistryEntryGetPath(service, kIOServicePlane, path) != KERN_SUCCESS) {
      (*dev)->Release(dev);
      continue;
    }
    
    std::cout << "USB Path: " << path << std::endl;
    UInt8   si;
    UInt16  u16v;
  
    if ((*dev)->GetDeviceVendor(dev, &u16v) == kIOReturnSuccess)
      std::cout << "VID: "<< u16v << std::endl;
  
    if ((*dev)->GetDeviceProduct(dev, &u16v) == kIOReturnSuccess)
      std::cout << "PID: "<< u16v << std::endl;
  
    if ((*dev)->USBGetManufacturerStringIndex(dev, &si) == kIOReturnSuccess) {
      std::cout << "Manufacturer: " << get_descriptor_idx(dev, si) << std::endl;
    }
  
    if ((*dev)->USBGetProductStringIndex(dev, &si) == kIOReturnSuccess) {
      std::cout << "Product: " << get_descriptor_idx(dev, si) << std::endl;
    }
    
    (*dev)->Release(dev);
    
    std::cout << std::endl;
  }
  
  IOObjectRelease(iter);
  
  return 0;
}


/* a quick version */
std::string get_descriptor_idx(IOUSBDeviceInterface **dev, UInt8 idx)
{
  IOUSBDevRequest request;
  IOReturn        ioret;
  char            buffer[4086] = { 0 };
  CFStringRef     cfstr;
  CFIndex         len;
  
  request.bmRequestType = USBmakebmRequestType(kUSBIn, kUSBStandard, kUSBDevice);
  request.bRequest      = kUSBRqGetDescriptor;
  request.wValue        = (kUSBStringDesc << 8) | idx;
  request.wIndex        = 0x409;
  request.wLength       = sizeof(buffer);
  request.pData         = buffer;
  
  ioret = (*dev)->DeviceRequest(dev, &request);
  if (ioret != kIOReturnSuccess)
    return "n/a";
  
  if (request.wLenDone <= 2)
    return "n/a";
  
  cfstr   = CFStringCreateWithBytes(nullptr, (const UInt8 *)buffer+2, request.wLenDone-2, kCFStringEncodingUTF16LE, 0);
  len     = CFStringGetMaximumSizeForEncoding(CFStringGetLength(cfstr), kCFStringEncodingUTF8) + 1;
  if (len < 0) {
    CFRelease(cfstr);
    return "n/a";
  }
  
  std::string str; str.resize(len);
  
  CFStringGetCString(cfstr, str.data(), len, kCFStringEncodingUTF8);
  
  CFRelease(cfstr);
  return str;
}

In my case the result ıs:

USB Path: IOService:/AppleARMPE/arm-io/xxxx/USB2.1 Hub@01100000
VID: 1507
PID: 1552
Manufacturer: GenesysLogic
Product: USB2.1 USB

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.