summaryrefslogtreecommitdiffstats
path: root/macosx/HBDriveDetector.m
blob: 2b57237d9feef90550dde9429b60b7647513c1ca (plain)
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
228
229
230
231
232
233
234
235
236
237
238
239
240
/**
 * HBDriveDetector.m
 * 8/17/2007
 * 
 * This file is part of the HandBrake source code.
 * Homepage: <http://handbrake.m0k.org/>.
 * It may be used under the terms of the GNU General Public License.
 */

#include <IOKit/IOKitLib.h>
#include <IOKit/storage/IOMedia.h>
#include <IOKit/storage/IODVDMedia.h>

#import "HBDVDDetector.h"


@interface HBDVDDetector (Private)

- (NSString *)bsdNameForPath;
- (BOOL)pathHasVideoTS;
- (BOOL)deviceIsDVD;
- (io_service_t)getIOKitServiceForBSDName;
- (BOOL)isDVDService: (io_service_t)service;
- (BOOL)isWholeMediaService: (io_service_t)service;

@end


@implementation HBDVDDetector

+ (HBDVDDetector *)detectorForPath: (NSString *)aPath
{
    return [[[self alloc] initWithPath:aPath] autorelease];
}


- (HBDVDDetector *)initWithPath: (NSString *)aPath
{
    NSAssert(aPath, @"nil string passed to drive detector.");
	if( self = [super init] )	
	{
        path = [aPath retain];
        bsdName = nil;
	}
    return self;
}


- (void)dealloc
{
    [path release];
    path = nil;
    [bsdName release];
    bsdName = nil;
    [super dealloc];
}


- (BOOL)isVideoDVD
{
    if( !bsdName )
    {
        bsdName = [[self bsdNameForPath] retain];
    }
    return ( [self pathHasVideoTS] && [self deviceIsDVD] );
}


- (NSString *)devicePath
{
    if( !bsdName )
    {
        bsdName = [[self bsdNameForPath] retain];
    }
    return [NSString stringWithFormat:@"/dev/%@", bsdName];
}

@end


@implementation HBDVDDetector (Private)

- (NSString *)bsdNameForPath
{
    OSStatus err;
	FSRef ref;
	err = FSPathMakeRef( (const UInt8 *) [path fileSystemRepresentation],
                         &ref, NULL );	
	if( err != noErr )
    {
        return nil;
    }

    // Get the volume reference number.
    FSCatalogInfo catalogInfo;
    err = FSGetCatalogInfo( &ref, kFSCatInfoVolume, &catalogInfo, NULL, NULL,
                            NULL);
    if( err != noErr )
    {
        return nil;
    }
    FSVolumeRefNum volRefNum = catalogInfo.volume;

	// Mow let's get the device name
	GetVolParmsInfoBuffer volumeParms;
	HParamBlockRec pb;
	pb.ioParam.ioNamePtr = NULL;
	pb.ioParam.ioVRefNum = volRefNum;
	pb.ioParam.ioBuffer = (Ptr) &volumeParms;
	pb.ioParam.ioReqCount = sizeof( volumeParms );
	err = PBHGetVolParmsSync( &pb );
    if( err != noErr )
    {
        return nil;
    }

	// 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.
    return [NSString stringWithCString:(char *)volumeParms.vMDeviceID];
}


- (BOOL)pathHasVideoTS
{
    // Check one level under the path
    if( [[NSFileManager defaultManager] fileExistsAtPath:
        [path stringByAppendingPathComponent:@"VIDEO_TS"]] )
    {
        return YES;
    }

    // Now check above the path
    return [[path pathComponents] containsObject:@"VIDEO_TS"];
}


- (BOOL)deviceIsDVD
{
    io_service_t service = [self getIOKitServiceForBSDName];
    if( service == IO_OBJECT_NULL )
    {
        return NO;
    }
    BOOL result = [self isDVDService:service];
    IOObjectRelease(service);
    return result;
}


- (io_service_t)getIOKitServiceForBSDName
{
    CFMutableDictionaryRef  matchingDict;
    matchingDict = IOBSDNameMatching( kIOMasterPortDefault, 0, [bsdName cString] );
    if( matchingDict == NULL )
    {
        return IO_OBJECT_NULL;
    }
	
    // Fetch the object with the matching BSD node name. There should only be
    // one match, so IOServiceGetMatchingService is used instead of
    // IOServiceGetMatchingServices to simplify the code.
    return IOServiceGetMatchingService( kIOMasterPortDefault, matchingDict );    
}


- (BOOL)isDVDService: (io_service_t)service
{
    // Find the IOMedia object that represents the entire (whole) media that the
    // volume is on. 
    //
    // If the volume is on partitioned media, the whole media object will be a
    // parent of the volume's media object. If the media is not partitioned, the
    // volume's media object will be the whole media object.
    // 
    // The whole media object is indicated in the IORegistry by the presence of
    // a property with the key "Whole" and value "Yes".

    // Create an iterator across all parents of the service object passed in.
    kern_return_t  kernResult;
    io_iterator_t  iter;
    kernResult = IORegistryEntryCreateIterator( service,
                                                kIOServicePlane,
                                                kIORegistryIterateRecursively | kIORegistryIterateParents,
                                                &iter );
    if( kernResult != KERN_SUCCESS )
    {
        return NO;
    }
    if( iter == IO_OBJECT_NULL )
    {
        return NO;
    }


    // A reference on the initial service object is released in the do-while loop below,
    // so add a reference to balance.
    IOObjectRetain( service );

    BOOL isDVD = NO;
    do
    {
        isDVD = ( [self isWholeMediaService:service] &&
                  IOObjectConformsTo(service, kIODVDMediaClass) );
        IOObjectRelease(service);
    } while( !isDVD && (service = IOIteratorNext(iter)) );
    IOObjectRelease( iter );

    return isDVD;
}


- (BOOL)isWholeMediaService: (io_service_t)service
{
    //
    // Determine if the object passed in represents an IOMedia (or subclass) object.
    // If it does, test the "Whole" property.
    //
    
    Boolean isWholeMedia = NO;
    
    if( IOObjectConformsTo(service, kIOMediaClass) )
    {
        CFTypeRef wholeMedia;
        wholeMedia = IORegistryEntryCreateCFProperty( service, 
                                                      CFSTR(kIOMediaWholeKey), 
                                                      kCFAllocatorDefault, 
                                                      0);
        if( !wholeMedia )
        {
            return NO;
        }
        isWholeMedia = CFBooleanGetValue( (CFBooleanRef)wholeMedia );
        CFRelease(wholeMedia);
    }

    return isWholeMedia;
}


@end