15

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.

Community
  • 1
  • 1
Amy
  • 1,814
  • 2
  • 23
  • 38

3 Answers3

16

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);
}
Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
  • Thank you. This does exactly what I need :) I didn't find a way to display the information like the 'Friendly Name' on windows does, though I suppose there's a way to do it on a Mac (i thought it's the description field here, but it's a filesystem). If you know how to retrieve that info, please let me know... otherwise, it's awesome :) – Amy Aug 24 '10 at 15:24
  • @emi: Hm, i don't know. I can't even find anything like this using the I/O Registry Explorer (located in `/Developer/Applications/Utilities`). – Georg Fritzsche Aug 24 '10 at 16:57
  • 1
    This used to be the correct answer, but is now deprecated. See this question for a current answer: https://stackoverflow.com/questions/10852056/is-fsgetvolumeinfo-the-recommended-alternative-to-nsworkspaces-deprecated-mount?rq=1 – fzwo Aug 12 '19 at 18:30
4

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.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
4

Take a look at getmntinfo() (for an enumeration of mount points) and statfs() (for information about a known mount point.)

Jonathan Grynspan
  • 43,286
  • 8
  • 74
  • 104