1

I'm brand new to Mac development/xcode. I'm trying to do what I feel should be extremely simple, but over a week of research has yielded no results.

I want to list the external usb drive available as a vector of strings.

I don't want their cryptic information like address serial or anything. I just want their paths IE: "D:/" or "Sandisk USB".

I accomplished this in Windows quite easily using the code found below, but finding out how to do this on Mac has me pulling my hair out.

The only thing I've found seems to be done for Objective C, - How to enumerate volumes on Mac OS X? but my project uses c++.

Can someone please provide a simple example or point me in the right direction.

struct ESDriveDescription
{
    std::string path;
    std::string label;

    ESDriveDescription() = default;
    ESDriveDescription(const std::string &path, const std::string &label)
        : path(path), label(label)
    {}
};



int ESFileUtils::getExternalStorageDevicePaths(vector<ESDriveDescription> &paths){
        // Letters in alphabet * 3 characters per drive path, + nul term + final nul
        // NOTE: constexpr not supported in vs2013
        static const DWORD DRIVE_BUFFER_SIZE = 26 * 4 + 1;
        static const DWORD VOLUME_LABEL_MAX = 32;

        const char* removableDriveNames[26] = { 0 };
        char allDrives[DRIVE_BUFFER_SIZE] = { 0 };
        int numRemovableDrives = 0;

        DWORD n = GetLogicalDriveStringsA(DRIVE_BUFFER_SIZE, allDrives);
        for (DWORD i = 0; i < n; i += 4) {
            const char* driveName = &allDrives[i];

            UINT type = GetDriveTypeA(driveName);
            if (type == DRIVE_REMOVABLE)
                removableDriveNames[numRemovableDrives++] = driveName;
        }

        char label[VOLUME_LABEL_MAX] = { 0 };
        for (int i = 0; i < numRemovableDrives; i++) {
            const char* driveName = removableDriveNames[i];
            GetVolumeInformationA(driveName, label, VOLUME_LABEL_MAX, 0, 0, 0, 0, 0);
            paths.emplace_back(driveName, label);
        }

        return numRemovableDrives;
    }
Remixt
  • 597
  • 6
  • 28
  • Have you looked at the Disk Arbitration framework? I don't have any experience with it, but it seems like it might be what you're looking for. [1] https://developer.apple.com/library/content/documentation/DriversKernelHardware/Conceptual/DiskArbitrationProgGuide/Introduction/Introduction.html [2] https://developer.apple.com/documentation/diskarbitration – Alex Jan 22 '18 at 17:51
  • 1
    You can implement the suggestion you found in Objective-C++ and expose a "pure" C++ class to the rest of your code. ("D:/" is not a path on OS X, btw.) – molbdnilo Jan 22 '18 at 17:58
  • @Alex I don't think that is what I need, it seems to use callback functions. I don't need to be updated when a device is connected, I simply need a list of currently connected devices. Thanks for looking. – Remixt Jan 22 '18 at 20:47
  • 1
    @molbdnilo I can't use objective C++ due to constraints in my project. – Remixt Jan 22 '18 at 20:48
  • @Remixt Gotcha, I’ve got a quick flight coming up but I’ll keep looking tonight. Update here if you find something! – Alex Jan 23 '18 at 01:50
  • 1
    If you can't use Objective-C++ (which means you can use `NSWorkspace`), the next most straightforward solution is to drill down to the POSIX API. See `man getfsstat()` for how to get the number of mounted volumes and iterate through them. – James Bucanek Jan 23 '18 at 04:34

0 Answers0