Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: dde81ed01d
Fetching contributors…

Cannot retrieve contributors at this time

file 227 lines (194 sloc) 7.532 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
//
// NSWorkspace+TypeOfVolumeAtPath.m
// MovieTheatre
//
// Created by Uli Kusterer on 12.06.05.
// Copyright 2005 M. Uli Kusterer.
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
// distribution.
//

#import "NSWorkspace+TypeOfVolumeAtPath.h"
#include <CoreFoundation/CoreFoundation.h>
#include <CoreServices/CoreServices.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/storage/IOMedia.h>
#include <IOKit/storage/IOCDMedia.h>
#include <IOKit/storage/IODVDMedia.h>
#include <IOKit/storage/IOStorageDeviceCharacteristics.h>
#include <IOKit/usb/IOUSBLib.h>
#import "NSString+CarbonUtilities.h"



NSString* IsWholeMedia(io_service_t service)
{
    //
    // Determine if the object passed in represents an IOMedia (or subclass) object.
    // If it does, retrieve the "Whole" property.
    // If this is the whole media object, find out if it is a CD, DVD, or something else.
    // If it isn't the whole media object, iterate across its parents in the IORegistry
    // until the whole media object is found.
    //
    // Note that media types other than CD and DVD are not distinguished by class name
    // but are generic IOMedia objects.
    //
    
    NSString* mediaType = nil;
    Boolean isWholeMedia = false;
    io_name_t className;
    kern_return_t kernResult;

    if( IOObjectConformsTo(service, kIOMediaClass) )
{
        CFTypeRef wholeMedia;
        
        // Find out whether it's a whole media:
        wholeMedia = IORegistryEntryCreateCFProperty(service,
                                                     CFSTR(kIOMediaWholeKey),
                                                     kCFAllocatorDefault,
                                                     0);
                                                    
        if( NULL == wholeMedia )
{
            printf("Could not retrieve Whole property\n");
        }
        else
{
            isWholeMedia = CFBooleanGetValue(wholeMedia);
            CFRelease(wholeMedia);
        }
    }
            
    if( isWholeMedia )
    {
        kernResult = IOObjectGetClass(service, className);
        mediaType = [NSString stringWithUTF8String: className];
    }

    return mediaType;
}

NSString* FindWholeMedia(io_service_t service)
{
    kern_return_t kernResult;
    io_iterator_t iter;
    NSString* mediaType = nil;
    
    // Create an iterator across all parents of the service object passed in.
    kernResult = IORegistryEntryCreateIterator(service,
                                               kIOServicePlane,
                                               kIORegistryIterateRecursively | kIORegistryIterateParents,
                                               &iter);
    
    if (KERN_SUCCESS != kernResult)
{
        printf("IORegistryEntryCreateIterator returned %d\n", kernResult);
    }
    else if (IO_OBJECT_NULL == iter)
{
        printf("IORegistryEntryCreateIterator returned a NULL iterator\n");
    }
    else
{
        // A reference on the initial service object is released in the do-while loop below,
        // so add a reference to balance
        IOObjectRetain(service);
        
        do
{
            mediaType = IsWholeMedia(service);
            IOObjectRelease(service);
        } while ((service = IOIteratorNext(iter)) && !mediaType);
                
        IOObjectRelease(iter);
    }
    
    return mediaType;
}


@implementation NSWorkspace (TypeOfVolumeAtPath)

-(NSArray*) mountedMediaOfType: (NSString*)mediaType
{
    NSEnumerator* enny = [[self mountedRemovableMedia] objectEnumerator];
    NSString* path = nil;
    NSMutableArray* result = [NSMutableArray array];
    
    while( (path = [enny nextObject]) )
    {
        if( [[self typeOfVolumeAtPath: path] isEqualToString: mediaType] )
            [result addObject: path];
    }
    
    return result;
}

-(NSString*) typeOfVolumeAtPath: (NSString*)path
{
    OSStatus err;
    FSRef fileRef;
    FSCatalogInfo catInfo;
    GetVolParmsInfoBuffer volumeParms;
    kern_return_t kernResult;
    
    if( ![path getFSRef: &fileRef] )
        NSLog( @"Couldn't get FSRef" );
    
    err = FSGetCatalogInfo( &fileRef, kFSCatInfoVolume, &catInfo, NULL, NULL, NULL );
    if( err != noErr )
        NSLog( @"FSGetCatalogInfo returned %ld", err );

    // A version 4 GetVolParmsInfoBuffer contains the BSD node name in the vMDeviceID field.
    // It is actually a char * value. This is mentioned in the header CoreServices/CarbonCore/Files.h.
err = FSGetVolumeParms( catInfo.volume, &volumeParms, sizeof(volumeParms) );
    if( err != noErr )
        NSLog( @"FSGetVolumeParms returned %ld", err );
    
    char *bsdName = (char *) volumeParms.vMDeviceID;
    CFMutableDictionaryRef matchingDict;
    io_iterator_t iter;
    io_service_t service;
    NSString* mediaType = nil;
io_name_t theName;
    
    static mach_port_t gMasterPort = 0;
    if( gMasterPort == 0 )
    {
        kernResult = IOMasterPort(MACH_PORT_NULL, &gMasterPort);
        if (KERN_SUCCESS != kernResult)
            NSLog( @"IOMasterPort returned %d\n", kernResult);
    }
    
NSLog(@"volume path & BSD name: %@ %s",path,bsdName);
    matchingDict = IOBSDNameMatching( gMasterPort, 0, bsdName );
    if (NULL == matchingDict)
{
        printf("IOBSDNameMatching returned a NULL dictionary.\n");
    }
    else {
        // Return an iterator across all objects with the matching BSD node name. Note that there
        // should only be one match!
        kernResult = IOServiceGetMatchingServices(gMasterPort, matchingDict, &iter);
    
        if (KERN_SUCCESS != kernResult)
{
            printf("IOServiceGetMatchingServices returned %d\n", kernResult);
        }
        else if (IO_OBJECT_NULL == iter)
{
            printf("IOServiceGetMatchingServices returned a NULL iterator\n");
        }
        else
{
            service = IOIteratorNext(iter);
            
            // Release this now because we only expect the iterator to contain
            // a single io_service_t.
            IOObjectRelease(iter);
            
            if (IO_OBJECT_NULL == service)
{
                printf("IOIteratorNext returned NULL\n");
            }
            else
{
                mediaType = FindWholeMedia(service);
NSMutableDictionary* propDict = nil;
IORegistryEntryCreateCFProperties( service, (CFMutableDictionaryRef*) &propDict, kCFAllocatorDefault, 0 );
[propDict autorelease];
NSLog( @"%@",propDict );
                IOObjectRelease(service);
            }
        }
    }
    
    return mediaType;
}

@end
Something went wrong with that request. Please try again.