Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enumerate volumes on Mac OS X?

I am not very proficient in Mac OS X programming, but I am working on a Qt application which needs info about the storage devices. Basically a list of hard drives and USB thumb drives. The end result should be like a vector which contains the following info for each device:

string: Label
string: Mount point
string: Device description (aka friendly name)
uint64: Size
bool: Is removable?

I've been doing it on Windows and the following post Get information about disk drives result on windows7 - 32 bit system have been of a great help. However, although I am very proficient in C/C++ I am not really good in Mac OS X programming, Cocoa and/or Objective-C, so any help would be much appreciated.

like image 274
Amy Avatar asked Aug 23 '10 06:08

Amy


People also ask

How do I list all volumes on a Mac?

You can use this view for basic operations on volumes. This is the default view when you open Disk Utility for the first time. In Disk Utility on your Mac, choose View > Show Only Volumes.

How do I see mounted volumes on Mac?

If you still don't see anything, make sure Finder in selected, then click on Go , then Go to folder . In the window that's dropped down, type in /Volumes/ and hit Enter. You should now be in a Finder folder called Volumes that contains links to all of your connected drives and mounted disk images.

How do you add volumes on a Mac?

In the Disk Utility app on your Mac, select an existing APFS volume in the sidebar, then click the Add Volume button in the toolbar. If Disk Utility isn't open, click the Launchpad icon in the Dock, type Disk Utility in the Search field, then click the Disk Utility icon . Enter a name for the new APFS volume.


2 Answers

This should get you most of what you're looking for:

NSWorkspace   *ws = [NSWorkspace sharedWorkspace];
NSArray     *vols = [ws mountedLocalVolumePaths];
NSFileManager *fm = [NSFileManager defaultManager];

for (NSString *path in vols) 
{
    NSDictionary* fsAttributes;
    NSString *description, *type, *name;
    BOOL removable, writable, unmountable, res;
    NSNumber *size;

    res = [ws getFileSystemInfoForPath:path 
                           isRemovable:&removable 
                            isWritable:&writable 
                         isUnmountable:&unmountable
                           description:&description
                                  type:&type];
    if (!res) continue;
    fsAttributes = [fm fileSystemAttributesAtPath:path];
    name         = [fm displayNameAtPath:path];
    size         = [fsAttributes objectForKey:NSFileSystemSize];

    NSLog(@"path=%@\nname=%@\nremovable=%d\nwritable=%d\nunmountable=%d\n"
           "description=%@\ntype=%@, size=%@\n\n",
          path, name, removable, writable, unmountable, description, type, size);
}
like image 113
Georg Fritzsche Avatar answered Oct 05 '22 23:10

Georg Fritzsche


Well, back in the day we used FSGetVolumeInfo. As for removability, that would be FSGetVolumeParms using vMExtendedAttributes & 1<< bIsRemovable. (Actually, I don't recall that particular API. There was something called Driver Gestalt, but it's gone now.)

I suppose there's a shiny Objective-C interface, but if nobody else replies, at least there's the C way.

like image 39
Potatoswatter Avatar answered Oct 05 '22 23:10

Potatoswatter