-
Notifications
You must be signed in to change notification settings - Fork 5
/
Arbitration.m
174 lines (132 loc) · 5.92 KB
/
Arbitration.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
/*
* The MIT License (MIT)
*
* Application: NTFS OS X
* Copyright (c) 2015 Jeevanandam M. (jeeva@myjeeva.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
//
// Arbitration.m
// NTFS-OSX
//
// Created by Jeevanandam M. on 6/5/15.
// Copyright (c) 2015 myjeeva.com. All rights reserved.
//
#import "Arbitration.h"
#import "Disk.h"
DASessionRef session;
DASessionRef approvalSession;
NSMutableSet *ntfsDisks;
void RegisterDA(void) {
// Disk Arbitration Session
session = DASessionCreate(kCFAllocatorDefault);
if (!session) {
[NSException raise:NSGenericException format:@"Unable to create Disk Arbitration session."];
return;
}
LogDebug(@"Disk Arbitration Session created");
ntfsDisks = [NSMutableSet new];
// Matching Conditions
CFMutableDictionaryRef match = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
// Device matching criteria
// 1. Of-course it shouldn't be internal device since
CFDictionaryAddValue(match, kDADiskDescriptionDeviceInternalKey, kCFBooleanFalse);
// Volume matching criteria
// It should statisfy following
CFDictionaryAddValue(match, kDADiskDescriptionVolumeKindKey, (__bridge CFStringRef)DADiskDescriptionVolumeKindValue);
CFDictionaryAddValue(match, kDADiskDescriptionVolumeMountableKey, kCFBooleanTrue);
CFDictionaryAddValue(match, kDADiskDescriptionVolumeNetworkKey, kCFBooleanFalse);
//CFDictionaryAddValue(match, kDADiskDescriptionDeviceProtocolKey, CFSTR(kIOPropertyPhysicalInterconnectTypeUSB));
DASessionScheduleWithRunLoop(session, CFRunLoopGetMain(), kCFRunLoopCommonModes);
// Registring callbacks
DARegisterDiskAppearedCallback(session, match, DiskAppearedCallback, (__bridge void *)AppName);
DARegisterDiskDisappearedCallback(session, match, DiskDisappearedCallback, (__bridge void *)AppName);
DARegisterDiskDescriptionChangedCallback(session, match, NULL, DiskDescriptionChangedCallback, (__bridge void *)AppName);
// Disk Arbitration Approval Session
approvalSession = DAApprovalSessionCreate(kCFAllocatorDefault);
if (!approvalSession) {
LogDebug(@"Unable to create Disk Arbitration approval session.");
return;
}
LogDebug(@"Disk Arbitration Approval Session created");
DAApprovalSessionScheduleWithRunLoop(approvalSession, CFRunLoopGetMain(), kCFRunLoopCommonModes);
// Same match condition for Approval session too
DARegisterDiskMountApprovalCallback(approvalSession, match, DiskMountApprovalCallback, (__bridge void *)AppName);
Release(match);
}
void UnregisterDA(void) {
// DA Session
if (session) {
DAUnregisterCallback(session, DiskAppearedCallback, (__bridge void *)AppName);
DAUnregisterCallback(session, DiskDisappearedCallback, (__bridge void *)AppName);
DASessionUnscheduleFromRunLoop(session, CFRunLoopGetMain(), kCFRunLoopCommonModes);
Release(session);
LogDebug(@"Disk Arbitration Session destoryed");
}
// DA Approval Session
if (approvalSession) {
DAUnregisterApprovalCallback(approvalSession, DiskMountApprovalCallback, (__bridge void *)AppName);
DAApprovalSessionUnscheduleFromRunLoop(approvalSession, CFRunLoopGetMain(), kCFRunLoopCommonModes);
Release(approvalSession);
LogDebug(@"Disk Arbitration Approval Session destoryed");
}
[ntfsDisks removeAllObjects];
ntfsDisks = nil;
}
BOOL Validate(DADiskRef diskRef) {
if (DADiskGetBSDName(diskRef) == NULL) {
[NSException raise:NSInternalInconsistencyException format:@"NTFS Disk without BSDName"];
}
return TRUE;
}
void DiskAppearedCallback(DADiskRef diskRef, void *context) {
LogDebug(@"DiskAppearedCallback called: %s", DADiskGetBSDName(diskRef));
if (Validate(diskRef)) {
Disk *disk = [[Disk alloc] initWithDADiskRef:diskRef];
LogDebug(@"Name: %@ \tUUID: %@", disk.volumeName, disk.volumeUUID);
[[NSNotificationCenter defaultCenter] postNotificationName:NTFSDiskAppearedNotification object:disk];
}
}
void DiskDisappearedCallback(DADiskRef diskRef, void *context) {
LogDebug(@"DiskDisappearedCallback called: %s", DADiskGetBSDName(diskRef));
if (Validate(diskRef)) {
Disk *disk = [Disk getDiskForDARef:diskRef];
LogDebug(@"Name: %@ \tUUID: %@", disk.volumeName, disk.volumeUUID);
[[NSNotificationCenter defaultCenter] postNotificationName:NTFSDiskDisappearedNotification object:disk];
}
}
void DiskDescriptionChangedCallback(DADiskRef diskRef, CFArrayRef keys, void *context) {
LogDebug(@"DiskDescriptionChangedCallback called: %s", DADiskGetBSDName(diskRef));
Disk *disk = [Disk getDiskForDARef:diskRef];
if (disk) {
CFDictionaryRef newDesc = DADiskCopyDescription(diskRef);
disk.desc = newDesc;
LogDebug(@"Updated Disk Description: %@", disk.desc);
Release(newDesc);
}
}
DADissenterRef DiskMountApprovalCallback(DADiskRef diskRef, void *context) {
LogDebug(@"DiskMountApprovalCallback called: %s", DADiskGetBSDName(diskRef));
if (Validate(diskRef)) {
Disk *disk = [[Disk alloc] initWithDADiskRef:diskRef];
LogDebug(@"Name: %@ \tUUID: %@", disk.volumeName, disk.volumeUUID);
}
return NULL;
}