-
Notifications
You must be signed in to change notification settings - Fork 15
/
SLVolume.m
314 lines (277 loc) · 8.92 KB
/
SLVolume.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
305
306
307
308
309
310
311
312
313
314
//
// SLVolume.m
// Semulov
//
// Created by Kevin Wojniak on 11/5/06.
// Copyright 2006 - 2014 Kevin Wojniak. All rights reserved.
//
#import "SLVolume.h"
#import <DiskArbitration/DiskArbitration.h>
#import <IOKit/storage/IOStorageDeviceCharacteristics.h>
#import <sys/mount.h>
#import "SLDiskImageManager.h"
@interface SLVolume ()
- (instancetype)initWithStatfs:(struct statfs *)statfs diskImageManager:(SLDiskImageManager *)diskImageManager;
@end
@implementation SLVolume
{
NSString *_path;
NSString *_name;
NSImage *_image;
BOOL _local;
BOOL _root;
NSURL *_hostURL;
BOOL _internal;
NSString *_imagePath;
SLVolumeType _type;
NSString *_diskID;
}
+ (NSArray *)allVolumesWithDiskManager:(SLDiskImageManager *)diskImageManager
{
NSMutableArray *volumes = [NSMutableArray array];
int count = getfsstat(NULL, 0, MNT_NOWAIT);
if (count > 0) {
struct statfs *buf = calloc((size_t)count, sizeof(struct statfs));
if (buf) {
if (getfsstat(buf, count * (int)sizeof(struct statfs), MNT_NOWAIT) > 0) {
for (int i = 0; i < count; i++) {
SLVolume *vol = [[SLVolume alloc] initWithStatfs:&buf[i] diskImageManager:diskImageManager];
if (vol == nil) {
continue;
}
if ([vol.path isEqualToString:@"/Volumes/MobileBackups"]) {
// Time Machine temp backups volume, ignore.
continue;
}
[volumes addObject:vol];
}
}
free(buf);
}
}
[volumes sortUsingSelector:@selector(compare:)];
return volumes;
}
+ (NSURL *)volumeURL:(NSURL *)url
{
NSURL *hostURL = nil;
(void)[url getResourceValue:&hostURL forKey:NSURLVolumeURLForRemountingKey error:nil];
return hostURL;
}
- (instancetype)initWithStatfs:(struct statfs *)statfs diskImageManager:(SLDiskImageManager *)diskImageManager
{
self = [super init];
if (self != nil)
{
NSString *fileSystemType = [NSString stringWithCString:statfs->f_fstypename encoding:NSUTF8StringEncoding];
NSString *path = [NSString stringWithUTF8String:statfs->f_mntonname];
//NSLog(@"%@: %@", [path lastPathComponent], fileSystemType);
if (!([path isEqualToString:@"/"] || [path hasPrefix:@"/Volumes"]))
{
return nil;
}
if ([fileSystemType isEqualToString:@"vmhgfs"]) {
// Ignore VMware Shared Folders internal mounted volume
return nil;
}
if ((statfs->f_flags & MNT_LOCAL) == MNT_LOCAL)
_local = YES;
if ((statfs->f_flags & MNT_ROOTFS) == MNT_ROOTFS)
_root = YES;
_path = [path copy];
_name = [[[[NSFileManager alloc] init] displayNameAtPath:path] copy];
if (strlen(statfs->f_mntfromname) > 0) {
_diskID = [NSString stringWithUTF8String:statfs->f_mntfromname].lastPathComponent;
}
if ([self isLocal] == NO)
{
_type = SLVolumeNetwork;
NSURL *hostURL = [[self class] volumeURL:[NSURL fileURLWithPath:[self path]]];
if (hostURL)
{
_hostURL = [hostURL copy];
NSString *urlScheme = [_hostURL scheme];
if ([urlScheme isEqualToString:@"ftp"]) {
_type = SLVolumeFTP;
} else if ([urlScheme isEqualToString:@"afp"] || [urlScheme isEqualToString:@"smb"]) {
// keep as SLVolumeNetwork
} else if ([urlScheme isEqualToString:@"file"]) {
// probably file:///Volumes/MobileBackups/ (mtmfs)
} else {
if ([fileSystemType isEqualToString:@"webdav"]) {
_type = SLVolumeWebDAV;
} else {
NSLog(@"unknown URL: %@ (%@)", _hostURL, fileSystemType);
}
}
}
}
else
{
if ([self isRoot])
_type = SLVolumeRoot;
else if ([self isiPod])
_type = SLVolumeiPod;
else
_type = SLVolumeDrive;
NSString *imgPath = [diskImageManager diskImageForVolume:[self path]];
if (imgPath) {
_imagePath = [imgPath copy];
_type = SLVolumeDiskImage;
}
DASessionRef session = DASessionCreate(kCFAllocatorDefault);
if (session)
{
DADiskRef disk = DADiskCreateFromBSDName(kCFAllocatorDefault, session, statfs->f_mntfromname);
if (disk)
{
CFDictionaryRef desc = DADiskCopyDescription(disk);
if (desc)
{
CFBooleanRef isInternal, isEjectable;
CFStringRef deviceModel;
CFStringRef mediaKind;
if (!CFDictionaryGetValueIfPresent(desc, kDADiskDescriptionDeviceInternalKey, (void *)&isInternal)) {
isInternal = kCFBooleanFalse;
}
if (!CFDictionaryGetValueIfPresent(desc, kDADiskDescriptionMediaEjectableKey, (void *)&isEjectable)) {
isEjectable = kCFBooleanFalse;
}
if (!CFDictionaryGetValueIfPresent(desc, kDADiskDescriptionDeviceModelKey, (void *)&deviceModel)) {
deviceModel = NULL;
}
if (!CFDictionaryGetValueIfPresent(desc, kDADiskDescriptionMediaKindKey, (void *)&mediaKind)) {
mediaKind = NULL;
}
// 2nd check for disk images..
if (([self type] != SLVolumeDiskImage) && ([(__bridge NSString *)deviceModel isEqualToString:@"Disk Image"]))
{
_type = SLVolumeDiskImage;
}
else if ([self type] == SLVolumeDrive) // if we haven't been identified by anything else yet, check for being a hd?
{
if ((isInternal == kCFBooleanTrue) && (isEjectable == kCFBooleanFalse))
{
_internal = YES;
}
// (will be overridden later if we're a DVD or CD)
_type = SLVolumeHardDrive;
}
if ([self type] == SLVolumeDiskImage) {
DADiskRef parentDisk = DADiskCopyWholeDisk(disk);
if (parentDisk) {
io_service_t ioMedia = DADiskCopyIOMedia(parentDisk);
if (ioMedia) {
CFTypeRef props = IORegistryEntrySearchCFProperty(ioMedia, kIOServicePlane, CFSTR(kIOPropertyProtocolCharacteristicsKey), kCFAllocatorDefault, kIORegistryIterateRecursively | kIORegistryIterateParents);
if (props) {
//NSLog(@"%@", props);
if (CFGetTypeID(props) == CFDictionaryGetTypeID()) {
CFTypeRef location = CFDictionaryGetValue(props, CFSTR(kIOPropertyPhysicalInterconnectLocationKey));
if (location && CFGetTypeID(location) == CFStringGetTypeID()) {
if (CFEqual(location, CFSTR(kIOPropertyInterconnectRAMKey))) {
_type = SLVolumeRAMDisk;
}
}
}
CFRelease(props);
}
IOObjectRelease(ioMedia);
}
CFRelease(parentDisk);
}
}
if (mediaKind != NULL) {
// could be IODVDMedia, IOCDMedia, IOBDMedia, etc.
if ([(__bridge NSString *)mediaKind rangeOfString:@"DVD"].location != NSNotFound) {
_type = SLVolumeDVD;
} else if ([(__bridge NSString *)mediaKind rangeOfString:@"CD"].location != NSNotFound) {
_type = SLVolumeCD;
} else if ([(__bridge NSString *)mediaKind rangeOfString:@"BD"].location != NSNotFound) {
_type = SLVolumeBluray;
}
}
CFRelease(desc);
}
CFRelease(disk);
}
CFRelease(session);
}
}
}
return self;
}
- (id)copyWithZone:(NSZone *)zone
{
SLVolume *copy = [[SLVolume allocWithZone:zone] init];
copy->_path = [_path copyWithZone:zone];
copy->_name = [_name copyWithZone:zone];
copy->_image = [_image copyWithZone:zone];
copy->_hostURL = [_hostURL copyWithZone:zone];
copy->_imagePath = [_imagePath copyWithZone:zone];
copy->_local = _local;
copy->_root = _root;
copy->_internal = _internal;
copy->_type = _type;
copy->_diskID = [_diskID copyWithZone:zone];
return copy;
}
- (NSString *)path
{
return _path;
}
- (NSString *)name
{
return _name;
}
- (NSImage *)image
{
if (!_image) {
_image = [[[NSWorkspace sharedWorkspace] iconForFile:_path] copy];
}
return _image;
}
- (BOOL)isLocal
{
return _local;
}
- (BOOL)isRoot
{
return _root;
}
- (SLVolumeType)type
{
return _type;
}
- (NSURL *)hostURL
{
return _hostURL;
}
- (BOOL)isInternalHardDrive
{
return _internal;
}
- (NSString *)diskImagePath
{
return _imagePath;
}
- (BOOL)isiPod
{
BOOL isDir;
NSFileManager *fm = [[NSFileManager alloc] init];
return ([fm fileExistsAtPath:[[self path] stringByAppendingPathComponent:@"iPod_Control"] isDirectory:&isDir] && isDir);
}
- (NSComparisonResult)compare:(SLVolume *)b
{
if ([self type] == [b type])
return [[self name] caseInsensitiveCompare:[b name]];
if ([self type] < [b type])
return NSOrderedAscending;
else if ([self type] > [b type])
return NSOrderedDescending;
return NSOrderedSame;
}
- (NSString *)diskID
{
return _diskID;
}
@end