@ohos-ports/mountutils 0.0.0-stage → 2.0.8-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,557 @@
1
+ /*
2
+ * Copyright 2017 resin.io
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ // Adapted from https://support.microsoft.com/en-us/kb/165721
18
+
19
+ #ifndef WIN32_LEAN_AND_MEAN
20
+ #define WIN32_LEAN_AND_MEAN
21
+ #endif
22
+
23
+ #include <windows.h>
24
+ #include <winioctl.h>
25
+ #include <tchar.h>
26
+ #include <stdio.h>
27
+ #include <cfgmgr32.h>
28
+ #include <setupapi.h>
29
+ #include "../mountutils.hpp"
30
+
31
+ HANDLE CreateVolumeHandleFromDevicePath(LPCTSTR devicePath, DWORD flags) {
32
+ return CreateFile(devicePath,
33
+ flags,
34
+ FILE_SHARE_READ | FILE_SHARE_WRITE,
35
+ NULL,
36
+ OPEN_EXISTING,
37
+ 0,
38
+ NULL);
39
+ }
40
+
41
+ HANDLE CreateVolumeHandleFromDriveLetter(TCHAR driveLetter, DWORD flags) {
42
+ TCHAR devicePath[8];
43
+ sprintf_s(devicePath, "\\\\.\\%c:", driveLetter);
44
+ return CreateVolumeHandleFromDevicePath(devicePath, flags);
45
+ }
46
+
47
+ ULONG GetDeviceNumberFromVolumeHandle(HANDLE volume) {
48
+ STORAGE_DEVICE_NUMBER storageDeviceNumber;
49
+ DWORD bytesReturned;
50
+
51
+ BOOL result = DeviceIoControl(volume,
52
+ IOCTL_STORAGE_GET_DEVICE_NUMBER,
53
+ NULL, 0,
54
+ &storageDeviceNumber,
55
+ sizeof(storageDeviceNumber),
56
+ &bytesReturned,
57
+ NULL);
58
+
59
+ if (!result) {
60
+ return 0;
61
+ }
62
+
63
+ return storageDeviceNumber.DeviceNumber;
64
+ }
65
+
66
+ BOOL IsDriveFixed(TCHAR driveLetter) {
67
+ TCHAR rootName[5];
68
+ sprintf_s(rootName, "%c:\\", driveLetter);
69
+ return GetDriveType(rootName) == DRIVE_FIXED;
70
+ }
71
+
72
+ BOOL LockVolume(HANDLE volume) {
73
+ DWORD bytesReturned;
74
+
75
+ for (size_t tries = 0; tries < 20; tries++) {
76
+ if (DeviceIoControl(volume,
77
+ FSCTL_LOCK_VOLUME,
78
+ NULL, 0,
79
+ NULL, 0,
80
+ &bytesReturned,
81
+ NULL)) {
82
+ return TRUE;
83
+ }
84
+
85
+ Sleep(500);
86
+ }
87
+
88
+ return FALSE;
89
+ }
90
+
91
+ // Adapted from https://www.codeproject.com/articles/13839/how-to-prepare-a-usb-drive-for-safe-removal
92
+ // which is licensed under "The Code Project Open License (CPOL) 1.02"
93
+ // https://www.codeproject.com/info/cpol10.aspx
94
+ DEVINST GetDeviceInstanceFromDeviceNumber(ULONG deviceNumber) {
95
+ const GUID* guid = reinterpret_cast<const GUID *>(&GUID_DEVINTERFACE_DISK);
96
+
97
+ // Get device interface info set handle for all devices attached to system
98
+ DWORD deviceInformationFlags = DIGCF_PRESENT | DIGCF_DEVICEINTERFACE;
99
+ HDEVINFO deviceInformation = SetupDiGetClassDevs(guid,
100
+ NULL, NULL,
101
+ deviceInformationFlags);
102
+
103
+ if (deviceInformation == INVALID_HANDLE_VALUE) {
104
+ return 0;
105
+ }
106
+
107
+ DWORD memberIndex = 0;
108
+ BYTE buffer[1024];
109
+
110
+ PSP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetailData =
111
+ (PSP_DEVICE_INTERFACE_DETAIL_DATA)buffer;
112
+
113
+ SP_DEVINFO_DATA deviceInformationData;
114
+ DWORD requiredSize;
115
+
116
+ SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
117
+ deviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
118
+
119
+ while (true) {
120
+ if (!SetupDiEnumDeviceInterfaces(deviceInformation,
121
+ NULL,
122
+ guid,
123
+ memberIndex,
124
+ &deviceInterfaceData)) {
125
+ break;
126
+ }
127
+
128
+ requiredSize = 0;
129
+ SetupDiGetDeviceInterfaceDetail(deviceInformation,
130
+ &deviceInterfaceData,
131
+ NULL, 0,
132
+ &requiredSize, NULL);
133
+
134
+ if (requiredSize == 0 || requiredSize > sizeof(buffer)) {
135
+ memberIndex++;
136
+ continue;
137
+ }
138
+
139
+ deviceInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
140
+
141
+ ZeroMemory((PVOID)&deviceInformationData, sizeof(SP_DEVINFO_DATA));
142
+ deviceInformationData.cbSize = sizeof(SP_DEVINFO_DATA);
143
+
144
+ BOOL result = SetupDiGetDeviceInterfaceDetail(deviceInformation,
145
+ &deviceInterfaceData,
146
+ deviceInterfaceDetailData,
147
+ sizeof(buffer),
148
+ &requiredSize,
149
+ &deviceInformationData);
150
+
151
+ if (!result) {
152
+ memberIndex++;
153
+ continue;
154
+ }
155
+
156
+ LPCTSTR devicePath = deviceInterfaceDetailData->DevicePath;
157
+ HANDLE driveHandle = CreateVolumeHandleFromDevicePath(devicePath, 0);
158
+
159
+ if (driveHandle == INVALID_HANDLE_VALUE) {
160
+ memberIndex++;
161
+ continue;
162
+ }
163
+
164
+ ULONG currentDriveDeviceNumber =
165
+ GetDeviceNumberFromVolumeHandle(driveHandle);
166
+
167
+ CloseHandle(driveHandle);
168
+
169
+ if (!currentDriveDeviceNumber) {
170
+ memberIndex++;
171
+ continue;
172
+ }
173
+
174
+ if (deviceNumber == currentDriveDeviceNumber) {
175
+ SetupDiDestroyDeviceInfoList(deviceInformation);
176
+ return deviceInformationData.DevInst;
177
+ }
178
+
179
+ memberIndex++;
180
+ }
181
+
182
+ SetupDiDestroyDeviceInfoList(deviceInformation);
183
+
184
+ return 0;
185
+ }
186
+
187
+ BOOL UnlockVolume(HANDLE volume) {
188
+ DWORD bytesReturned;
189
+
190
+ return DeviceIoControl(volume,
191
+ FSCTL_UNLOCK_VOLUME,
192
+ NULL, 0,
193
+ NULL, 0,
194
+ &bytesReturned,
195
+ NULL);
196
+ }
197
+
198
+ BOOL DismountVolume(HANDLE volume) {
199
+ DWORD bytesReturned;
200
+
201
+ return DeviceIoControl(volume,
202
+ FSCTL_DISMOUNT_VOLUME,
203
+ NULL, 0,
204
+ NULL, 0,
205
+ &bytesReturned,
206
+ NULL);
207
+ }
208
+
209
+ BOOL IsVolumeMounted(HANDLE volume) {
210
+ DWORD bytesReturned;
211
+
212
+ return DeviceIoControl(volume,
213
+ FSCTL_IS_VOLUME_MOUNTED,
214
+ NULL, 0,
215
+ NULL, 0,
216
+ &bytesReturned,
217
+ NULL);
218
+ }
219
+
220
+ BOOL EjectRemovableVolume(HANDLE volume) {
221
+ DWORD bytesReturned;
222
+ PREVENT_MEDIA_REMOVAL buffer;
223
+ buffer.PreventMediaRemoval = FALSE;
224
+ BOOL result = DeviceIoControl(volume,
225
+ IOCTL_STORAGE_MEDIA_REMOVAL,
226
+ &buffer, sizeof(PREVENT_MEDIA_REMOVAL),
227
+ NULL, 0,
228
+ &bytesReturned,
229
+ NULL);
230
+
231
+ if (!result) {
232
+ MountUtilsLog("Couldn't prevent media removal");
233
+ return FALSE;
234
+ }
235
+
236
+ for (size_t tries = 0; tries < 5; tries++) {
237
+ if (tries != 0) {
238
+ MountUtilsLog("Retrying ejection");
239
+ Sleep(500);
240
+ }
241
+ const BOOL result = DeviceIoControl(volume,
242
+ IOCTL_STORAGE_EJECT_MEDIA,
243
+ NULL, 0,
244
+ NULL, 0,
245
+ &bytesReturned,
246
+ NULL);
247
+ if (result) {
248
+ MountUtilsLog("Volume ejected");
249
+ return TRUE;
250
+ }
251
+ }
252
+
253
+ return FALSE;
254
+ }
255
+
256
+ MOUNTUTILS_RESULT EjectFixedDriveByDeviceNumber(ULONG deviceNumber) {
257
+ DEVINST deviceInstance = GetDeviceInstanceFromDeviceNumber(deviceNumber);
258
+ if (!deviceInstance) {
259
+ MountUtilsLog("Couldn't get instance from device number");
260
+ return MOUNTUTILS_ERROR_GENERAL;
261
+ }
262
+
263
+ CONFIGRET status;
264
+ PNP_VETO_TYPE vetoType = PNP_VetoTypeUnknown;
265
+ char vetoName[MAX_PATH];
266
+
267
+ // It's often seen that the removal fails on the first
268
+ // attempt but works on the second attempt.
269
+ // See https://www.codeproject.com/articles/13839/how-to-prepare-a-usb-drive-for-safe-removal
270
+ for (size_t tries = 0; tries < 3; tries++) {
271
+ if (tries != 0) {
272
+ MountUtilsLog("Retrying");
273
+ Sleep(500);
274
+ }
275
+ MountUtilsLog("Ejecting device instance");
276
+ status = CM_Request_Device_Eject(deviceInstance,
277
+ &vetoType,
278
+ vetoName,
279
+ MAX_PATH,
280
+ 0);
281
+
282
+ if (status == CR_SUCCESS) {
283
+ MountUtilsLog("Ejected device instance successfully");
284
+ return MOUNTUTILS_SUCCESS;
285
+ }
286
+
287
+ MountUtilsLog("Ejecting was vetoed");
288
+
289
+ // We use this as an indicator that the device driver
290
+ // is not setting the `SurpriseRemovalOK` capability.
291
+ // See https://msdn.microsoft.com/en-us/library/windows/hardware/ff539722(v=vs.85).aspx
292
+ if (status == CR_REMOVE_VETOED &&
293
+ vetoType == PNP_VetoIllegalDeviceRequest) {
294
+ MountUtilsLog("Removing subtree");
295
+ status = CM_Query_And_Remove_SubTree(deviceInstance,
296
+ &vetoType,
297
+ vetoName,
298
+ MAX_PATH,
299
+
300
+ // We have to add the `CM_REMOVE_NO_RESTART` flag because
301
+ // otherwise the just-removed device may be immediately
302
+ // redetected, which might happen on XP and Vista.
303
+ // See https://www.codeproject.com/articles/13839/how-to-prepare-a-usb-drive-for-safe-removal
304
+
305
+ CM_REMOVE_NO_RESTART);
306
+
307
+ if (status == CR_ACCESS_DENIED) {
308
+ return MOUNTUTILS_ERROR_ACCESS_DENIED;
309
+ }
310
+
311
+ if (status == CR_SUCCESS) {
312
+ return MOUNTUTILS_SUCCESS;
313
+ }
314
+
315
+ MountUtilsLog("Couldn't eject device instance");
316
+ return MOUNTUTILS_ERROR_GENERAL;
317
+ }
318
+ }
319
+
320
+ return MOUNTUTILS_ERROR_GENERAL;
321
+ }
322
+
323
+ MOUNTUTILS_RESULT EjectDriveLetter(TCHAR driveLetter) {
324
+ DWORD volumeFlags = GENERIC_READ | GENERIC_WRITE;
325
+ HANDLE volumeHandle = CreateVolumeHandleFromDriveLetter(driveLetter,
326
+ volumeFlags);
327
+
328
+ MountUtilsLog("Creating volume handle");
329
+
330
+ if (volumeHandle == INVALID_HANDLE_VALUE) {
331
+ MountUtilsLog("Couldn't create volume handle");
332
+ return MOUNTUTILS_ERROR_INVALID_DRIVE;
333
+ }
334
+
335
+ // Don't proceed if the volume is not mounted
336
+ if (!IsVolumeMounted(volumeHandle)) {
337
+ MountUtilsLog("Volume is not mounted");
338
+
339
+ if (!CloseHandle(volumeHandle)) {
340
+ MountUtilsLog("Couldn't close volume handle");
341
+ return MOUNTUTILS_ERROR_GENERAL;
342
+ }
343
+
344
+ return MOUNTUTILS_SUCCESS;
345
+ }
346
+
347
+ if (IsDriveFixed(driveLetter)) {
348
+ MountUtilsLog("Drive is fixed");
349
+
350
+ ULONG deviceNumber = GetDeviceNumberFromVolumeHandle(volumeHandle);
351
+ if (!deviceNumber) {
352
+ MountUtilsLog("Couldn't get device number from volume handle");
353
+ CloseHandle(volumeHandle);
354
+ return MOUNTUTILS_ERROR_GENERAL;
355
+ }
356
+
357
+ if (!CloseHandle(volumeHandle)) {
358
+ MountUtilsLog("Couldn't close volume handle");
359
+ return MOUNTUTILS_ERROR_GENERAL;
360
+ }
361
+
362
+ MountUtilsLog("Ejecting fixed drive");
363
+ return EjectFixedDriveByDeviceNumber(deviceNumber);
364
+ }
365
+
366
+ MountUtilsLog("Locking volume");
367
+
368
+ if (!LockVolume(volumeHandle)) {
369
+ MountUtilsLog("Couldn't lock volume");
370
+ CloseHandle(volumeHandle);
371
+ return MOUNTUTILS_ERROR_GENERAL;
372
+ }
373
+
374
+ MountUtilsLog("Dismounting volume");
375
+
376
+ if (!DismountVolume(volumeHandle)) {
377
+ MountUtilsLog("Couldn't dismount volume");
378
+ CloseHandle(volumeHandle);
379
+ return MOUNTUTILS_ERROR_GENERAL;
380
+ }
381
+
382
+ MountUtilsLog("Ejecting volume");
383
+
384
+ if (!EjectRemovableVolume(volumeHandle)) {
385
+ MountUtilsLog("Couldn't eject volume");
386
+ CloseHandle(volumeHandle);
387
+ return MOUNTUTILS_ERROR_GENERAL;
388
+ }
389
+
390
+ MountUtilsLog("Unlocking volume");
391
+
392
+ if (!UnlockVolume(volumeHandle)) {
393
+ MountUtilsLog("Couldn't unlock volume");
394
+ CloseHandle(volumeHandle);
395
+ return MOUNTUTILS_ERROR_GENERAL;
396
+ }
397
+
398
+ MountUtilsLog("Closing volume handle");
399
+
400
+ if (!CloseHandle(volumeHandle)) {
401
+ MountUtilsLog("Couldn't close volume handle");
402
+ return MOUNTUTILS_ERROR_GENERAL;
403
+ }
404
+
405
+ return MOUNTUTILS_SUCCESS;
406
+ }
407
+
408
+ BOOL IsDriveEjectable(TCHAR driveLetter) {
409
+ TCHAR devicePath[8];
410
+ sprintf_s(devicePath, "%c:\\", driveLetter);
411
+
412
+ MountUtilsLog("Checking whether drive is ejectable: "
413
+ + std::string(1, driveLetter));
414
+
415
+ switch (GetDriveType(devicePath)) {
416
+ case DRIVE_NO_ROOT_DIR:
417
+ MountUtilsLog("The drive doesn't exist");
418
+ return FALSE;
419
+ case DRIVE_REMOVABLE:
420
+ MountUtilsLog("The drive is removable");
421
+ return TRUE;
422
+ case DRIVE_FIXED:
423
+ MountUtilsLog("The drive is fixed");
424
+ return TRUE;
425
+ case DRIVE_REMOTE:
426
+ MountUtilsLog("The drive is remote");
427
+ return FALSE;
428
+ case DRIVE_CDROM:
429
+ MountUtilsLog("The drive is a CDROM");
430
+ return FALSE;
431
+ case DRIVE_RAMDISK:
432
+ MountUtilsLog("The drive is a RAM disk");
433
+ return FALSE;
434
+ default:
435
+ MountUtilsLog("The drive type is unknown");
436
+ return FALSE;
437
+ }
438
+ }
439
+
440
+ MOUNTUTILS_RESULT Eject(ULONG deviceNumber) {
441
+ DWORD logicalDrivesMask = GetLogicalDrives();
442
+ TCHAR currentDriveLetter = 'A';
443
+
444
+ if (logicalDrivesMask == 0) {
445
+ MountUtilsLog("Couldn't get logical drives");
446
+ return MOUNTUTILS_ERROR_GENERAL;
447
+ }
448
+
449
+ while (logicalDrivesMask) {
450
+ if (logicalDrivesMask & 1 && IsDriveEjectable(currentDriveLetter)) {
451
+ MountUtilsLog("Opening drive letter handle: "
452
+ + std::string(1, currentDriveLetter));
453
+
454
+ HANDLE driveHandle =
455
+ CreateVolumeHandleFromDriveLetter(currentDriveLetter, 0);
456
+
457
+ if (driveHandle == INVALID_HANDLE_VALUE) {
458
+ MountUtilsLog("Couldn't open drive letter handle");
459
+ return MOUNTUTILS_ERROR_GENERAL;
460
+ }
461
+
462
+ ULONG currentDeviceNumber = GetDeviceNumberFromVolumeHandle(driveHandle);
463
+
464
+ MountUtilsLog("Closing drive letter handle");
465
+
466
+ if (!CloseHandle(driveHandle)) {
467
+ MountUtilsLog("Couldn't close drive letter handle");
468
+ return MOUNTUTILS_ERROR_GENERAL;
469
+ }
470
+
471
+ if (currentDeviceNumber == deviceNumber) {
472
+ MountUtilsLog("Drive letter device matches");
473
+ MOUNTUTILS_RESULT result;
474
+
475
+ // Retry ejecting 3 times, since I've seen that in some systems
476
+ // the filesystem is ejected, but the drive letter remains assigned,
477
+ // which gets fixed if you retry again.
478
+ for (size_t times = 0; times < 3; times++) {
479
+ if (times != 0) {
480
+ MountUtilsLog("Retrying");
481
+ Sleep(500);
482
+ }
483
+ MountUtilsLog("Ejecting drive letter");
484
+ result = EjectDriveLetter(currentDriveLetter);
485
+
486
+ // Abort the loop if we couldn't open a handle on the drive letter
487
+ // after previous attempts worked, since this means the drive was
488
+ // completely ejected, and that we don't have to keep retrying.
489
+ if (times > 0 && result == MOUNTUTILS_ERROR_INVALID_DRIVE) {
490
+ MountUtilsLog("Drive letter has already been ejected");
491
+ break;
492
+ }
493
+
494
+ if (result != MOUNTUTILS_SUCCESS) {
495
+ MountUtilsLog("Couldn't eject drive letter");
496
+ return result;
497
+ }
498
+ }
499
+ }
500
+
501
+ MountUtilsLog("Continuing with the next available letter");
502
+ }
503
+
504
+ currentDriveLetter++;
505
+ logicalDrivesMask >>= 1;
506
+ }
507
+
508
+ return MOUNTUTILS_SUCCESS;
509
+ }
510
+
511
+ // From http://stackoverflow.com/a/12923949
512
+ MOUNTUTILS_RESULT stringToInteger(char *string, int *out) {
513
+ if (string[0] == '\0' || isspace((unsigned char) string[0])) {
514
+ return MOUNTUTILS_ERROR_GENERAL;
515
+ }
516
+
517
+ char *end;
518
+ errno = 0;
519
+ int result = strtol(string, &end, 10);
520
+
521
+ if (result > INT_MAX || result < INT_MIN ||
522
+ (errno == ERANGE && result == LONG_MAX) ||
523
+ (errno == ERANGE && result == LONG_MIN)) {
524
+ return MOUNTUTILS_ERROR_GENERAL;
525
+ }
526
+
527
+ if (*end != '\0') {
528
+ return MOUNTUTILS_ERROR_GENERAL;
529
+ }
530
+
531
+ *out = result;
532
+
533
+ return MOUNTUTILS_SUCCESS;
534
+ }
535
+
536
+ MOUNTUTILS_RESULT unmount_disk(const char *device) {
537
+ int deviceId;
538
+ char prefix[18];
539
+
540
+ MountUtilsLog(std::string(device));
541
+
542
+ int result = sscanf(device, "%17s%i", prefix, &deviceId);
543
+
544
+ // Return value of `sscanf` is the number of receiving arguments
545
+ // successfully assigned; and `0` or `EOF` in case of failure
546
+ if (result != 2 || result == EOF) {
547
+ return MOUNTUTILS_ERROR_INVALID_DRIVE;
548
+ }
549
+
550
+ return Eject(deviceId);
551
+ }
552
+
553
+ // FIXME: This is just a stub copy of `UnmountDisk()`,
554
+ // and needs implementation!
555
+ MOUNTUTILS_RESULT eject_disk(const char *device) {
556
+ return unmount_disk(device);
557
+ }
@@ -0,0 +1,74 @@
1
+ /*
2
+ * Copyright 2017 resin.io
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ #include <nan.h>
17
+ #include "mountutils.hpp"
18
+
19
+ class EjectWorker : public Nan::AsyncWorker {
20
+ public:
21
+ EjectWorker(Nan::Callback *callback, std::string device)
22
+ : Nan::AsyncWorker(callback) {
23
+ device_path = device;
24
+ }
25
+
26
+ ~EjectWorker() {}
27
+
28
+ void Execute() {
29
+ MOUNTUTILS_RESULT result = eject_disk(device_path.c_str());
30
+
31
+ MountUtilsLog("Eject complete");
32
+
33
+ if (result != MOUNTUTILS_SUCCESS) {
34
+ switch (result) {
35
+ case MOUNTUTILS_ERROR_ACCESS_DENIED:
36
+ SetErrorMessage("Eject failed, access denied");
37
+ break;
38
+ case MOUNTUTILS_ERROR_INVALID_DRIVE:
39
+ SetErrorMessage("Eject failed, invalid drive");
40
+ break;
41
+ default:
42
+ SetErrorMessage("Eject failed");
43
+ break;
44
+ }
45
+ }
46
+ }
47
+
48
+ void HandleOKCallback() {
49
+ Nan::HandleScope scope;
50
+ v8::Local<v8::Value> argv[] = { Nan::Null() };
51
+ callback->Call(1, argv, async_resource);
52
+ }
53
+
54
+ private:
55
+ std::string device_path;
56
+ };
57
+
58
+ NAN_METHOD(eject) {
59
+ if (!info[1]->IsFunction()) {
60
+ return Nan::ThrowError("Callback must be a function");
61
+ }
62
+
63
+ if (!info[0]->IsString()) {
64
+ return Nan::ThrowError("Device must be a string");
65
+ }
66
+
67
+ Nan::Utf8String device(Nan::To<v8::String>(info[0]).ToLocalChecked());
68
+ std::string device_path(*device);
69
+ Nan::Callback *callback = new Nan::Callback(info[1].As<v8::Function>());
70
+
71
+ Nan::AsyncQueueWorker(new EjectWorker(callback, device_path));
72
+
73
+ info.GetReturnValue().SetUndefined();
74
+ }
@@ -0,0 +1,74 @@
1
+ /*
2
+ * Copyright 2017 resin.io
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ #include <nan.h>
17
+ #include "mountutils.hpp"
18
+
19
+ class UnmountWorker : public Nan::AsyncWorker {
20
+ public:
21
+ UnmountWorker(Nan::Callback *callback, std::string device)
22
+ : Nan::AsyncWorker(callback) {
23
+ device_path = device;
24
+ }
25
+
26
+ ~UnmountWorker() {}
27
+
28
+ void Execute() {
29
+ MOUNTUTILS_RESULT result = unmount_disk(device_path.c_str());
30
+
31
+ MountUtilsLog("Unmount complete");
32
+
33
+ if (result != MOUNTUTILS_SUCCESS) {
34
+ switch (result) {
35
+ case MOUNTUTILS_ERROR_ACCESS_DENIED:
36
+ SetErrorMessage("Unmount failed, access denied");
37
+ break;
38
+ case MOUNTUTILS_ERROR_INVALID_DRIVE:
39
+ SetErrorMessage("Unmount failed, invalid drive");
40
+ break;
41
+ default:
42
+ SetErrorMessage("Unmount failed");
43
+ break;
44
+ }
45
+ }
46
+ }
47
+
48
+ void HandleOKCallback() {
49
+ Nan::HandleScope scope;
50
+ v8::Local<v8::Value> argv[] = { Nan::Null() };
51
+ callback->Call(1, argv, async_resource);
52
+ }
53
+
54
+ private:
55
+ std::string device_path;
56
+ };
57
+
58
+ NAN_METHOD(unmountDisk) {
59
+ if (!info[1]->IsFunction()) {
60
+ return Nan::ThrowError("Callback must be a function");
61
+ }
62
+
63
+ if (!info[0]->IsString()) {
64
+ return Nan::ThrowError("Device must be a string");
65
+ }
66
+
67
+ Nan::Utf8String device(Nan::To<v8::String>(info[0]).ToLocalChecked());
68
+ std::string device_path(*device);
69
+ Nan::Callback *callback = new Nan::Callback(info[1].As<v8::Function>());
70
+
71
+ Nan::AsyncQueueWorker(new UnmountWorker(callback, device_path));
72
+
73
+ info.GetReturnValue().SetUndefined();
74
+ }