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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
|
/*
* Copyright (C) 2005-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#include <stdlib.h>
#include "DarwinStorageProvider.h"
#include "utils/RegExp.h"
#include "Util.h"
#include "guilib/LocalizeStrings.h"
#include <sys/mount.h>
#if defined(TARGET_DARWIN_OSX)
#include <DiskArbitration/DiskArbitration.h>
#include <IOKit/storage/IOCDMedia.h>
#include <IOKit/storage/IODVDMedia.h>
#include "platform/darwin/DarwinUtils.h"
#endif
#include "platform/darwin/osx/CocoaInterface.h"
std::vector<std::pair<std::string, std::string>> CDarwinStorageProvider::m_mountsToNotify;
std::vector<std::pair<std::string, std::string>> CDarwinStorageProvider::m_unmountsToNotify;
IStorageProvider* IStorageProvider::CreateInstance()
{
return new CDarwinStorageProvider();
}
CDarwinStorageProvider::CDarwinStorageProvider()
{
PumpDriveChangeEvents(NULL);
}
void CDarwinStorageProvider::GetLocalDrives(VECSOURCES &localDrives)
{
CMediaSource share;
// User home folder
#ifdef TARGET_DARWIN_IOS
share.strPath = "special://envhome/";
#else
share.strPath = getenv("HOME");
#endif
share.strName = g_localizeStrings.Get(21440);
share.m_ignore = true;
localDrives.push_back(share);
#if defined(TARGET_DARWIN_IOS)
// iOS Inbox folder
share.strPath = "special://envhome/Documents/Inbox";
share.strName = "Inbox";
share.m_ignore = true;
localDrives.push_back(share);
#endif
#if defined(TARGET_DARWIN_OSX)
// User desktop folder
share.strPath = getenv("HOME");
share.strPath += "/Desktop";
share.strName = "Desktop";
share.m_ignore = true;
localDrives.push_back(share);
// Volumes (all mounts are present here)
share.strPath = "/Volumes";
share.strName = "Volumes";
share.m_ignore = true;
localDrives.push_back(share);
// This will pick up all local non-removable disks including the Root Disk.
DASessionRef session = DASessionCreate(kCFAllocatorDefault);
if (session)
{
unsigned i, count = 0;
struct statfs *buf = NULL;
std::string mountpoint, devicepath;
count = getmntinfo(&buf, 0);
for (i=0; i<count; i++)
{
mountpoint = buf[i].f_mntonname;
devicepath = buf[i].f_mntfromname;
DADiskRef disk = DADiskCreateFromBSDName(kCFAllocatorDefault, session, devicepath.c_str());
if (disk)
{
CFDictionaryRef details = DADiskCopyDescription(disk);
if (details)
{
if (kCFBooleanFalse == CFDictionaryGetValue(details, kDADiskDescriptionMediaRemovableKey))
{
CMediaSource share;
share.strPath = mountpoint;
Cocoa_GetVolumeNameFromMountPoint(mountpoint.c_str(), share.strName);
share.m_ignore = true;
localDrives.push_back(share);
}
CFRelease(details);
}
CFRelease(disk);
}
}
CFRelease(session);
}
#endif
}
void CDarwinStorageProvider::GetRemovableDrives(VECSOURCES &removableDrives)
{
#if defined(TARGET_DARWIN_OSX)
DASessionRef session = DASessionCreate(kCFAllocatorDefault);
if (session)
{
unsigned i, count = 0;
struct statfs *buf = NULL;
std::string mountpoint, devicepath;
count = getmntinfo(&buf, 0);
for (i=0; i<count; i++)
{
mountpoint = buf[i].f_mntonname;
devicepath = buf[i].f_mntfromname;
DADiskRef disk = DADiskCreateFromBSDName(kCFAllocatorDefault, session, devicepath.c_str());
if (disk)
{
CFDictionaryRef details = DADiskCopyDescription(disk);
if (details)
{
if (kCFBooleanTrue == CFDictionaryGetValue(details, kDADiskDescriptionMediaRemovableKey))
{
CMediaSource share;
share.strPath = mountpoint;
share.m_iDriveType = CMediaSource::SOURCE_TYPE_REMOVABLE;
Cocoa_GetVolumeNameFromMountPoint(mountpoint.c_str(), share.strName);
share.m_ignore = true;
// detect if its a cd or dvd
// needs to be ejectable
if (kCFBooleanTrue == CFDictionaryGetValue(details, kDADiskDescriptionMediaEjectableKey))
{
CFStringRef mediaKind = (CFStringRef)CFDictionaryGetValue(details, kDADiskDescriptionMediaKindKey);
// and either cd or dvd kind of media in it
if (mediaKind != NULL &&
(CFStringCompare(mediaKind, CFSTR(kIOCDMediaClass), 0) == kCFCompareEqualTo ||
CFStringCompare(mediaKind, CFSTR(kIODVDMediaClass), 0) == kCFCompareEqualTo))
share.m_iDriveType = CMediaSource::SOURCE_TYPE_DVD;
}
removableDrives.push_back(share);
}
CFRelease(details);
}
CFRelease(disk);
}
}
CFRelease(session);
}
#endif
}
std::vector<std::string> CDarwinStorageProvider::GetDiskUsage()
{
std::vector<std::string> result;
char line[1024];
#ifdef TARGET_DARWIN_IOS
FILE* pipe = popen("df -ht hfs,apfs", "r");
#else
FILE* pipe = popen("df -HT ufs,cd9660,hfs,apfs,udf", "r");
#endif
if (pipe)
{
while (fgets(line, sizeof(line) - 1, pipe))
{
result.push_back(line);
}
pclose(pipe);
}
return result;
}
#if defined(TARGET_DARWIN_OSX)
namespace
{
class DAOperationContext
{
public:
explicit DAOperationContext(const std::string& mountpath);
~DAOperationContext();
DADiskRef GetDisk() const { return m_disk; }
void Reset();
bool WaitForCompletion(CFTimeInterval timeout);
void Completed(bool success);
static void CompletionCallback(DADiskRef disk, DADissenterRef dissenter, void* context);
private:
DAOperationContext() = delete;
static void RunloopPerformCallback(void* info) {}
CFRunLoopSourceContext m_runLoopSourceContext = { .perform = RunloopPerformCallback };
bool m_success;
bool m_completed;
const DASessionRef m_session;
const CFRunLoopRef m_runloop;
const CFRunLoopSourceRef m_runloopSource;
DADiskRef m_disk;
};
DAOperationContext::DAOperationContext(const std::string& mountpath)
: m_success(true),
m_completed(false),
m_session(DASessionCreate(kCFAllocatorDefault)),
m_runloop(CFRunLoopGetCurrent()), // not owner!
m_runloopSource(CFRunLoopSourceCreate(kCFAllocatorDefault, 0, &m_runLoopSourceContext))
{
if (m_session && m_runloop && m_runloopSource)
{
CFURLRef url = CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, (const UInt8 *)mountpath.c_str(), mountpath.size(), TRUE);
if (url)
{
m_disk = DADiskCreateFromVolumePath(kCFAllocatorDefault, m_session, url);
CFRelease(url);
}
DASessionScheduleWithRunLoop(m_session, m_runloop, kCFRunLoopDefaultMode);
CFRunLoopAddSource(m_runloop, m_runloopSource, kCFRunLoopDefaultMode);
}
}
DAOperationContext::~DAOperationContext()
{
if (m_session && m_runloop && m_runloopSource)
{
CFRunLoopRemoveSource(m_runloop, m_runloopSource, kCFRunLoopDefaultMode);
DASessionUnscheduleFromRunLoop(m_session, m_runloop, kCFRunLoopDefaultMode);
CFRunLoopSourceInvalidate(m_runloopSource);
}
if (m_disk)
CFRelease(m_disk);
if (m_runloopSource)
CFRelease(m_runloopSource);
if (m_session)
CFRelease(m_session);
}
bool DAOperationContext::WaitForCompletion(CFTimeInterval timeout)
{
while (!m_completed)
{
if (CFRunLoopRunInMode(kCFRunLoopDefaultMode, timeout, TRUE) == kCFRunLoopRunTimedOut)
break;
}
return m_success;
}
void DAOperationContext::Completed(bool success)
{
m_success = success;
m_completed = true;
CFRunLoopSourceSignal(m_runloopSource);
CFRunLoopWakeUp(m_runloop);
}
void DAOperationContext::Reset()
{
m_success = true;
m_completed = false;
}
void DAOperationContext::CompletionCallback(DADiskRef disk, DADissenterRef dissenter, void* context)
{
DAOperationContext* dacontext = static_cast<DAOperationContext*>(context);
bool success = true;
if (dissenter)
{
DAReturn status = DADissenterGetStatus(dissenter);
success = (status == kDAReturnSuccess || status == kDAReturnUnsupported);
}
dacontext->Completed(success);
}
} // unnamed namespace
#endif
bool CDarwinStorageProvider::Eject(const std::string& mountpath)
{
#if defined(TARGET_DARWIN_OSX)
if (mountpath.empty())
return false;
DAOperationContext ctx(mountpath);
DADiskRef disk = ctx.GetDisk();
if (!disk)
return false;
bool success = false;
CFDictionaryRef details = DADiskCopyDescription(disk);
if (details)
{
// Does the device need to be unmounted first?
if (CFDictionaryGetValueIfPresent(details, kDADiskDescriptionVolumePathKey, NULL))
{
DADiskUnmount(disk, kDADiskUnmountOptionDefault, DAOperationContext::CompletionCallback, &ctx);
success = ctx.WaitForCompletion(30.0); // timeout after 30 secs
}
if (success)
{
ctx.Reset();
DADiskEject(disk, kDADiskEjectOptionDefault, DAOperationContext::CompletionCallback, &ctx);
success = ctx.WaitForCompletion(30.0); // timeout after 30 secs
}
CFRelease(details);
}
return success;
#else
return false;
#endif
}
bool CDarwinStorageProvider::PumpDriveChangeEvents(IStorageEventsCallback *callback)
{
// Note: If we find a way to only notify kodi user initiated mounts/unmounts we
// could do this here, but currently we can't distinguish this and popups
// for system initiated mounts/unmounts (like done by Time Machine automatic
// backups) are very confusing and annoying.
bool bChanged = !m_mountsToNotify.empty() || !m_unmountsToNotify.empty();
if (bChanged)
{
m_mountsToNotify.clear();
m_unmountsToNotify.clear();
}
return bChanged;
}
void CDarwinStorageProvider::VolumeMountNotification(const char* label, const char* mountpoint)
{
if (label && mountpoint)
m_mountsToNotify.emplace_back(std::make_pair(label, mountpoint));
}
void CDarwinStorageProvider::VolumeUnmountNotification(const char* label, const char* mountpoint)
{
if (label && mountpoint)
m_unmountsToNotify.emplace_back(std::make_pair(label, mountpoint));
}
|