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
|
/**
* HBDriveDetector.m
* 8/17/2007
*
* This file is part of the HandBrake source code.
* Homepage: <http://handbrake.fr/>.
* 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>
#include <IOKit/storage/IOBDMedia.h>
#include <sys/mount.h>
#import "HBDVDDetector.h"
@implementation HBDVDDetector
{
NSString *path;
NSString *bsdName;
}
- (instancetype)init
{
@throw nil;
}
+ (HBDVDDetector *)detectorForPath: (NSString *)aPath
{
return [[self alloc] initWithPath:aPath];
}
- (HBDVDDetector *)initWithPath: (NSString *)aPath
{
NSAssert(aPath, @"nil string passed to drive detector.");
if( self = [super init] )
{
path = aPath;
bsdName = nil;
}
return self;
}
- (BOOL)isVideoDVD
{
return ( [self pathHasVideoTS] && [self deviceIsDVD] );
}
- (BOOL)isVideoBluRay
{
return ( [self pathHasBDMV] && [self deviceIsBluRay] );
}
- (NSString *)devicePath
{
return [NSString stringWithFormat:@"/dev/%@", [self bsdName]];
}
#pragma mark - DVD
- (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
{
return [self deviceIs:kIODVDMediaClass];
}
#pragma mark - BluRay
- (BOOL)pathHasBDMV
{
// Check one level under the path
if( [[NSFileManager defaultManager] fileExistsAtPath:
[path stringByAppendingPathComponent:@"BDMV"]] )
{
return YES;
}
// Now check above the path
return [[path pathComponents] containsObject:@"BDMV"];
}
- (BOOL)deviceIsBluRay
{
return [self deviceIs:kIOBDMediaClass];
}
#pragma mark - Common
- (NSString *)bsdName
{
if ( bsdName )
{
return bsdName;
}
struct statfs s;
statfs(path.fileSystemRepresentation, &s);
bsdName = @(s.f_mntfromname);
if ([bsdName hasPrefix:@"/dev/"])
{
bsdName = [bsdName substringFromIndex:5];
}
return bsdName;
}
- (BOOL)deviceIs:(const io_name_t)class
{
io_service_t service = [self getIOKitServiceForBSDName];
if( service == IO_OBJECT_NULL )
{
return NO;
}
BOOL result = [self isService:service class:class];
IOObjectRelease(service);
return result;
}
- (io_service_t)getIOKitServiceForBSDName
{
CFMutableDictionaryRef matchingDict;
matchingDict = IOBSDNameMatching( kIOMasterPortDefault, 0, [[self bsdName] UTF8String] );
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)isService: (io_service_t)service class:(const io_name_t)class
{
// 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 conformsToClass = NO;
do
{
conformsToClass = ( [self isWholeMediaService:service] &&
IOObjectConformsTo(service, class) );
IOObjectRelease(service);
} while( !conformsToClass && (service = IOIteratorNext(iter)) );
IOObjectRelease( iter );
return conformsToClass;
}
- (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
|