-
Notifications
You must be signed in to change notification settings - Fork 0
/
BDDisk.m
304 lines (252 loc) · 6.96 KB
/
BDDisk.m
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//
// BDDisk.m
// ButteredDisk
//
// Created by Jeremy Knope on 10/17/09.
// Copyright 2009 Buttered Cat Software. All rights reserved.
//
#import "BDDisk.h"
//#import "FMNSFileManagerAdditions.h"
NSString * const BDDiskErrorDomain = @"BDDiskErrorDomain";
@interface BDDisk () {
BDDiskDidUnmountBlock _unmountCompletionBlock;
BDDiskDidMountBlock _mountCompletionBlock;
BDDiskDidEjectBlock _ejectCompletionBlock;
}
- (void)_mountDidFinishWithError:(NSError *)error;
- (void)_unmountDidFinishWithError:(NSError *)error;
- (void)_ejectDidFinishWithError:(NSError *)error;
@end
// DA callbacks
void bcDiskDidMount(DADiskRef disk, DADissenterRef dissenter, void *context)
{
NSError *error = nil;
if(dissenter != NULL)
{
int failureCode = DADissenterGetStatus(dissenter);
NSString *failureReason = CFBridgingRelease(DADissenterGetStatusString(dissenter));
NSDictionary *errorInfo = [NSDictionary dictionaryWithObject:failureReason forKey:NSLocalizedFailureReasonErrorKey];
error = [NSError errorWithDomain:BDDiskErrorDomain code:failureCode userInfo:errorInfo];
}
[(__bridge BDDisk *)context _mountDidFinishWithError:error];
}
void bcDiskDidUnmount(DADiskRef disk, DADissenterRef dissenter, void *context)
{
NSError *error = nil;
if(dissenter != NULL)
{
int failureCode = DADissenterGetStatus(dissenter);
NSString *failureReason = CFBridgingRelease(DADissenterGetStatusString(dissenter));
NSDictionary *errorInfo = [NSDictionary dictionaryWithObject:failureReason forKey:NSLocalizedFailureReasonErrorKey];
error = [NSError errorWithDomain:BDDiskErrorDomain code:failureCode userInfo:errorInfo];
}
[(__bridge BDDisk *)context _unmountDidFinishWithError:error];
}
void bcDiskDidEject(DADiskRef disk, DADissenterRef dissenter, void *context)
{
NSError *error = nil;
if(dissenter != NULL)
{
int failureCode = DADissenterGetStatus(dissenter);
NSString *failureReason = CFBridgingRelease(DADissenterGetStatusString(dissenter));
NSDictionary *errorInfo = [NSDictionary dictionaryWithObject:failureReason forKey:NSLocalizedFailureReasonErrorKey];
error = [NSError errorWithDomain:BDDiskErrorDomain code:failureCode userInfo:errorInfo];
}
[(__bridge BDDisk *)context _ejectDidFinishWithError:error];
}
#pragma mark -
@implementation BDDisk
+ (BDDisk *)diskWithRef:(DADiskRef)disk
{
return [[BDDisk alloc] initWithDiskRef:disk];
}
- (id)initWithDiskRef:(DADiskRef)disk
{
self = [super init];
if(self)
{
CFRetain(disk);
diskRef = disk;
}
return self;
}
- (void)dealloc
{
CFRelease(diskRef);
diskRef = nil;
info = nil;
_unmountCompletionBlock = nil;
}
#pragma mark -
- (NSUInteger)hash
{
if(![[self diskDescription] objectForKey:@"DAVolumeUUID"])
return [[self devicePath] hash];
return [[self volumeUUIDString] hash];
}
- (BOOL)isEqual:(id)anObject
{
return ([anObject hash] == [self hash]);
}
#pragma mark -
- (NSString *)description
{
return [NSString stringWithFormat:@"<BCDisk %p uuid=%@ device=%@ name=%@ fs=%@ mountPath=%@>", self, [self volumeUUIDString], [self devicePath], [self volumeName], [self filesystem], [self volumePath]];
}
- (NSDictionary *)diskDescription
{
if(!info)
{
info = (NSDictionary *)CFBridgingRelease(DADiskCopyDescription(diskRef));
}
return info;
}
- (NSString *)BSDName
{
return [[self diskDescription] objectForKey:@"DAMediaBSDName"];
}
- (NSString *)devicePath
{
return [NSString stringWithFormat:@"/dev/%@", [self BSDName]];
}
- (NSString *)volumeName
{
return [[self diskDescription] objectForKey:@"DAVolumeName"];
}
- (NSURL *)volumeURL
{
return [[self diskDescription] objectForKey:@"DAVolumePath"];
}
- (NSString *)volumePath
{
return [[self volumeURL] path];
}
- (NSString *)filesystem
{
return [[self diskDescription] objectForKey:@"DAVolumeKind"];
}
- (NSString *)volumeUUIDString
{
CFUUIDRef uuidRef = (__bridge CFUUIDRef)[[self diskDescription] objectForKey:@"DAVolumeUUID"];
if(!uuidRef)
return nil;
NSString *string = (NSString *)CFBridgingRelease(CFUUIDCreateString(kCFAllocatorDefault, uuidRef));
return string;
}
- (NSInteger)mediaSize
{
return [[[self diskDescription] objectForKey:@"DAMediaSize"] integerValue];
}
- (NSImage *)icon
{
if([self isMounted] && [self volumeURL])
{
NSImage *image = [[NSWorkspace sharedWorkspace] iconForFile:[self volumePath]];
if(image)
{
return image;
}
}
return [[NSImage alloc] initByReferencingFile:@"/System/Library/Extensions/IOStorageFamily.kext/Contents/Resources/Internal.icns"];
}
- (BOOL)isRemovable
{
return [[[self diskDescription] objectForKey:@"DAMediaRemovable"] boolValue];
}
- (BOOL)isDiskImage
{
return [[[self diskDescription] objectForKey:@"DADeviceModel"] isEqualToString:@"Disk Image"];
}
- (NSString *)mediaName
{
return [[self diskDescription] objectForKey:@"DAMediaName"];
}
- (NSString *)realDevicePath
{
return [NSString stringWithFormat:@"/dev/r%@", self.BSDName];
}
#pragma mark -
- (BOOL)_boolForDescriptionKey:(NSString *)key
{
if(![[self diskDescription] objectForKey:key])
return NO;
return [[[self diskDescription] objectForKey:key] boolValue];
}
- (BOOL)isWholeDisk
{
return [self _boolForDescriptionKey:@"DAMediaWhole"];
}
- (BOOL)isMountable
{
return [self _boolForDescriptionKey:@"DAVolumeMountable"];
}
- (BOOL)isNetwork
{
return [self _boolForDescriptionKey:@"DAVolumeNetwork"];
}
- (BOOL)isMounted
{
return ([self volumePath] != nil ? YES : NO);
}
- (BOOL)isCurrentSystem
{
return ([[self volumePath] isEqualToString:@"/"]) ? YES : NO;
}
#pragma mark -
- (void)_mountDidFinishWithError:(NSError *)error
{
if(_mountCompletionBlock)
{
info = nil; // make sure to get latest disk info, should have mount path now I hope
_mountCompletionBlock([self volumeURL], error);
_mountCompletionBlock = nil;
}
}
- (void)mountWithCompletionHandler:(BDDiskDidMountBlock)handler
{
if(_mountCompletionBlock)
{
_mountCompletionBlock = handler;
DADiskMount(diskRef, NULL, self.isWholeDisk ? kDADiskMountOptionWhole : kDADiskMountOptionDefault, bcDiskDidMount, (__bridge void*)self);
}
}
- (void)_unmountDidFinishWithError:(NSError *)error
{
if(_unmountCompletionBlock)
{
_unmountCompletionBlock(error);
_unmountCompletionBlock = nil;
}
}
- (void)unmountWithCompletionHandler:(BDDiskDidUnmountBlock)handler
{
[self unmountWithCompletionHandler:handler force:NO];
}
- (void)unmountWithCompletionHandler:(BDDiskDidUnmountBlock)handler force:(BOOL)force
{
if(!_unmountCompletionBlock)
{
DADiskUnmountOptions options = (self.isWholeDisk ? kDADiskUnmountOptionWhole : kDADiskUnmountOptionDefault) | (force ? kDADiskUnmountOptionForce : 0);
_unmountCompletionBlock = handler;
DADiskUnmount(diskRef, options, bcDiskDidUnmount, (__bridge void *)self);
}
}
- (void)_ejectDidFinishWithError:(NSError *)error
{
if(_ejectCompletionBlock)
{
_ejectCompletionBlock(error);
_ejectCompletionBlock = nil;
}
}
- (BOOL)ejectWithCompletionHandler:(BDDiskDidEjectBlock)handler
{
BOOL result = NO;
if(self.isRemovable)
{
DADiskEject(diskRef, kDADiskEjectOptionDefault, bcDiskDidEject, (__bridge void *)self);
result = YES;
}
return result;
}
@end