f044151721e4f6495713022eefd6c6d716b30fa1
[reactos.git] / dll / shellext / shellbtrfs / devices.cpp
1 /* Copyright (c) Mark Harmstone 2016-17
2 *
3 * This file is part of WinBtrfs.
4 *
5 * WinBtrfs is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public Licence as published by
7 * the Free Software Foundation, either version 3 of the Licence, or
8 * (at your option) any later version.
9 *
10 * WinBtrfs is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public Licence for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public Licence
16 * along with WinBtrfs. If not, see <http://www.gnu.org/licenses/>. */
17
18 #define ISOLATION_AWARE_ENABLED 1
19 #define STRSAFE_NO_DEPRECATE
20
21 #include "shellext.h"
22 #include "devices.h"
23 #include "resource.h"
24 #include "balance.h"
25 #include <stddef.h>
26 #include <uxtheme.h>
27 #include <setupapi.h>
28 #include <strsafe.h>
29 #include <mountmgr.h>
30 #ifndef __REACTOS__
31 #include <algorithm>
32 #include "../btrfs.h"
33 #include "mountmgr.h"
34 #else
35 #include <ntddstor.h>
36 #include <ndk/rtlfuncs.h>
37 #include <ndk/obfuncs.h>
38 #include "btrfs.h"
39 #include "mountmgr_local.h"
40 #endif
41
42 DEFINE_GUID(GUID_DEVINTERFACE_HIDDEN_VOLUME, 0x7f108a28L, 0x9833, 0x4b3b, 0xb7, 0x80, 0x2c, 0x6b, 0x5f, 0xa5, 0xc0, 0x62);
43
44 static wstring get_mountdev_name(const nt_handle& h ) {
45 NTSTATUS Status;
46 IO_STATUS_BLOCK iosb;
47 MOUNTDEV_NAME mdn, *mdn2;
48 wstring name;
49
50 Status = NtDeviceIoControlFile(h, nullptr, nullptr, nullptr, &iosb, IOCTL_MOUNTDEV_QUERY_DEVICE_NAME,
51 nullptr, 0, &mdn, sizeof(MOUNTDEV_NAME));
52 if (!NT_SUCCESS(Status) && Status != STATUS_BUFFER_OVERFLOW)
53 return L"";
54
55 size_t mdnsize = offsetof(MOUNTDEV_NAME, Name[0]) + mdn.NameLength;
56
57 mdn2 = (MOUNTDEV_NAME*)malloc(mdnsize);
58
59 Status = NtDeviceIoControlFile(h, nullptr, nullptr, nullptr, &iosb, IOCTL_MOUNTDEV_QUERY_DEVICE_NAME,
60 nullptr, 0, mdn2, (ULONG)mdnsize);
61 if (!NT_SUCCESS(Status)) {
62 free(mdn2);
63 return L"";
64 }
65
66 name = wstring(mdn2->Name, mdn2->NameLength / sizeof(WCHAR));
67
68 free(mdn2);
69
70 return name;
71 }
72
73 static void find_devices(HWND hwnd, const GUID* guid, const mountmgr& mm, vector<device>& device_list) {
74 HDEVINFO h;
75
76 static const wstring dosdevices = L"\\DosDevices\\";
77
78 h = SetupDiGetClassDevs(guid, nullptr, 0, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);
79
80 if (h != INVALID_HANDLE_VALUE) {
81 DWORD index = 0;
82 SP_DEVICE_INTERFACE_DATA did;
83
84 did.cbSize = sizeof(did);
85
86 if (!SetupDiEnumDeviceInterfaces(h, nullptr, guid, index, &did))
87 return;
88
89 do {
90 SP_DEVINFO_DATA dd;
91 SP_DEVICE_INTERFACE_DETAIL_DATA_W* detail;
92 DWORD size;
93
94 dd.cbSize = sizeof(dd);
95
96 SetupDiGetDeviceInterfaceDetailW(h, &did, nullptr, 0, &size, nullptr);
97
98 detail = (SP_DEVICE_INTERFACE_DETAIL_DATA_W*)malloc(size);
99 memset(detail, 0, size);
100
101 detail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_W);
102
103 if (SetupDiGetDeviceInterfaceDetailW(h, &did, detail, size, &size, &dd)) {
104 NTSTATUS Status;
105 nt_handle file;
106 device dev;
107 STORAGE_DEVICE_NUMBER sdn;
108 IO_STATUS_BLOCK iosb;
109 UNICODE_STRING path;
110 OBJECT_ATTRIBUTES attr;
111 GET_LENGTH_INFORMATION gli;
112 ULONG i;
113 uint8_t sb[4096];
114
115 path.Buffer = detail->DevicePath;
116 path.Length = path.MaximumLength = (uint16_t)(wcslen(detail->DevicePath) * sizeof(WCHAR));
117
118 if (path.Length > 4 * sizeof(WCHAR) && path.Buffer[0] == '\\' && path.Buffer[1] == '\\' && path.Buffer[2] == '?' && path.Buffer[3] == '\\')
119 path.Buffer[1] = '?';
120
121 InitializeObjectAttributes(&attr, &path, 0, nullptr, nullptr);
122
123 Status = NtOpenFile(&file, FILE_GENERIC_READ, &attr, &iosb, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, FILE_SYNCHRONOUS_IO_ALERT);
124
125 if (!NT_SUCCESS(Status)) {
126 free(detail);
127 index++;
128 continue;
129 }
130
131 dev.pnp_name = detail->DevicePath;
132
133 Status = NtDeviceIoControlFile(file, nullptr, nullptr, nullptr, &iosb, IOCTL_DISK_GET_LENGTH_INFO, nullptr, 0, &gli, sizeof(GET_LENGTH_INFORMATION));
134 if (!NT_SUCCESS(Status)) {
135 free(detail);
136 index++;
137 continue;
138 }
139
140 dev.size = gli.Length.QuadPart;
141
142 Status = NtDeviceIoControlFile(file, nullptr, nullptr, nullptr, &iosb, IOCTL_STORAGE_GET_DEVICE_NUMBER, nullptr, 0, &sdn, sizeof(STORAGE_DEVICE_NUMBER));
143 if (!NT_SUCCESS(Status)) {
144 dev.disk_num = 0xffffffff;
145 dev.part_num = 0xffffffff;
146 } else {
147 dev.disk_num = sdn.DeviceNumber;
148 dev.part_num = sdn.PartitionNumber;
149 }
150
151 dev.friendly_name = L"";
152 dev.drive = L"";
153 dev.fstype = L"";
154 dev.has_parts = false;
155 dev.ignore = false;
156 dev.multi_device = false;
157
158 dev.is_disk = RtlCompareMemory(guid, &GUID_DEVINTERFACE_DISK, sizeof(GUID)) == sizeof(GUID);
159
160 if (dev.is_disk) {
161 STORAGE_PROPERTY_QUERY spq;
162 STORAGE_DEVICE_DESCRIPTOR sdd, *sdd2;
163 ULONG dlisize;
164 DRIVE_LAYOUT_INFORMATION_EX* dli;
165
166 spq.PropertyId = StorageDeviceProperty;
167 spq.QueryType = PropertyStandardQuery;
168 spq.AdditionalParameters[0] = 0;
169
170 Status = NtDeviceIoControlFile(file, nullptr, nullptr, nullptr, &iosb, IOCTL_STORAGE_QUERY_PROPERTY,
171 &spq, sizeof(STORAGE_PROPERTY_QUERY), &sdd, sizeof(STORAGE_DEVICE_DESCRIPTOR));
172
173 if (NT_SUCCESS(Status) || Status == STATUS_BUFFER_OVERFLOW) {
174 sdd2 = (STORAGE_DEVICE_DESCRIPTOR*)malloc(sdd.Size);
175
176 Status = NtDeviceIoControlFile(file, nullptr, nullptr, nullptr, &iosb, IOCTL_STORAGE_QUERY_PROPERTY,
177 &spq, sizeof(STORAGE_PROPERTY_QUERY), sdd2, sdd.Size);
178 if (NT_SUCCESS(Status)) {
179 string desc2;
180
181 desc2 = "";
182
183 if (sdd2->VendorIdOffset != 0) {
184 desc2 += (char*)((uint8_t*)sdd2 + sdd2->VendorIdOffset);
185
186 while (desc2.length() > 0 && desc2[desc2.length() - 1] == ' ')
187 desc2 = desc2.substr(0, desc2.length() - 1);
188 }
189
190 if (sdd2->ProductIdOffset != 0) {
191 if (sdd2->VendorIdOffset != 0 && desc2.length() != 0 && desc2[desc2.length() - 1] != ' ')
192 desc2 += " ";
193
194 desc2 += (char*)((uint8_t*)sdd2 + sdd2->ProductIdOffset);
195
196 while (desc2.length() > 0 && desc2[desc2.length() - 1] == ' ')
197 desc2 = desc2.substr(0, desc2.length() - 1);
198 }
199
200 if (sdd2->VendorIdOffset != 0 || sdd2->ProductIdOffset != 0) {
201 ULONG ss;
202
203 ss = MultiByteToWideChar(CP_OEMCP, MB_PRECOMPOSED, desc2.c_str(), -1, nullptr, 0);
204
205 if (ss > 0) {
206 WCHAR* desc3 = (WCHAR*)malloc(ss * sizeof(WCHAR));
207
208 if (MultiByteToWideChar(CP_OEMCP, MB_PRECOMPOSED, desc2.c_str(), -1, desc3, (int)(ss * sizeof(WCHAR))))
209 dev.friendly_name = desc3;
210
211 free(desc3);
212 }
213 }
214 }
215
216 free(sdd2);
217 }
218
219 dlisize = 0;
220 dli = nullptr;
221
222 do {
223 dlisize += 1024;
224
225 if (dli)
226 free(dli);
227
228 dli = (DRIVE_LAYOUT_INFORMATION_EX*)malloc(dlisize);
229
230 Status = NtDeviceIoControlFile(file, nullptr, nullptr, nullptr, &iosb, IOCTL_DISK_GET_DRIVE_LAYOUT_EX,
231 nullptr, 0, dli, dlisize);
232 } while (Status == STATUS_BUFFER_TOO_SMALL);
233
234 if (NT_SUCCESS(Status) && dli->PartitionCount > 0)
235 dev.has_parts = true;
236
237 free(dli);
238 } else {
239 try {
240 std::vector<mountmgr_point> v;
241 v = mm.query_points(L"", L"", wstring_view(path.Buffer, path.Length / sizeof(WCHAR)));
242
243 for (size_t i = 0; i < v.size(); ++i)
244 {
245 const mountmgr_point& p = v[i];
246 if (p.symlink.length() == 14 && p.symlink.substr(0, dosdevices.length()) == dosdevices && p.symlink[13] == ':') {
247 WCHAR dr[3];
248
249 dr[0] = p.symlink[12];
250 dr[1] = ':';
251 dr[2] = 0;
252
253 dev.drive = dr;
254 break;
255 }
256 }
257 } catch (...) { // don't fail entirely if mountmgr refuses to co-operate
258 }
259 }
260
261 if (!dev.is_disk || !dev.has_parts) {
262 i = 0;
263 while (fs_ident[i].name) {
264 if (i == 0 || fs_ident[i].kboff != fs_ident[i-1].kboff) {
265 LARGE_INTEGER off;
266
267 off.QuadPart = fs_ident[i].kboff * 1024;
268 Status = NtReadFile(file, nullptr, nullptr, nullptr, &iosb, sb, sizeof(sb), &off, nullptr);
269 }
270
271 if (NT_SUCCESS(Status)) {
272 if (RtlCompareMemory(sb + fs_ident[i].sboff, fs_ident[i].magic, fs_ident[i].magiclen) == fs_ident[i].magiclen) {
273 dev.fstype = fs_ident[i].name;
274
275 if (dev.fstype == L"Btrfs") {
276 superblock* bsb = (superblock*)sb;
277
278 RtlCopyMemory(&dev.fs_uuid, &bsb->uuid, sizeof(BTRFS_UUID));
279 RtlCopyMemory(&dev.dev_uuid, &bsb->dev_item.device_uuid, sizeof(BTRFS_UUID));
280 }
281
282 break;
283 }
284 }
285
286 i++;
287 }
288
289 if (dev.fstype == L"Btrfs" && RtlCompareMemory(guid, &GUID_DEVINTERFACE_DISK, sizeof(GUID)) != sizeof(GUID)) {
290 wstring name;
291 wstring pref = L"\\Device\\Btrfs{";
292
293 name = get_mountdev_name(file);
294
295 if (name.length() > pref.length() && RtlCompareMemory(name.c_str(), pref.c_str(), pref.length() * sizeof(WCHAR)) == pref.length() * sizeof(WCHAR))
296 dev.ignore = true;
297 }
298 }
299
300 device_list.push_back(dev);
301 }
302
303 free(detail);
304
305 index++;
306 } while (SetupDiEnumDeviceInterfaces(h, nullptr, guid, index, &did));
307
308 SetupDiDestroyDeviceInfoList(h);
309 } else
310 throw last_error(GetLastError());
311 }
312
313 #ifndef __REACTOS__ // Disabled because building with our <algorithm> seems complex right now...
314 static bool sort_devices(device i, device j) {
315 if (i.disk_num < j.disk_num)
316 return true;
317
318 if (i.disk_num == j.disk_num && i.part_num < j.part_num)
319 return true;
320
321 return false;
322 }
323 #endif
324
325 void BtrfsDeviceAdd::populate_device_tree(HWND tree) {
326 HWND hwnd = GetParent(tree);
327 unsigned int i;
328 ULONG last_disk_num = 0xffffffff;
329 HTREEITEM diskitem;
330 NTSTATUS Status;
331 OBJECT_ATTRIBUTES attr;
332 UNICODE_STRING us;
333 IO_STATUS_BLOCK iosb;
334 btrfs_filesystem* bfs = nullptr;
335
336 static WCHAR btrfs[] = L"\\Btrfs";
337
338 device_list.clear();
339
340 {
341 mountmgr mm;
342
343 {
344 nt_handle btrfsh;
345
346 us.Length = us.MaximumLength = (uint16_t)(wcslen(btrfs) * sizeof(WCHAR));
347 us.Buffer = btrfs;
348
349 InitializeObjectAttributes(&attr, &us, 0, nullptr, nullptr);
350
351 Status = NtOpenFile(&btrfsh, SYNCHRONIZE | FILE_READ_ATTRIBUTES, &attr, &iosb,
352 FILE_SHARE_READ | FILE_SHARE_WRITE, FILE_SYNCHRONOUS_IO_ALERT);
353 if (NT_SUCCESS(Status)) {
354 ULONG bfssize = 0;
355
356 do {
357 bfssize += 1024;
358
359 if (bfs) free(bfs);
360 bfs = (btrfs_filesystem*)malloc(bfssize);
361
362 Status = NtDeviceIoControlFile(btrfsh, nullptr, nullptr, nullptr, &iosb, IOCTL_BTRFS_QUERY_FILESYSTEMS, nullptr, 0, bfs, bfssize);
363 if (!NT_SUCCESS(Status) && Status != STATUS_BUFFER_OVERFLOW) {
364 free(bfs);
365 bfs = nullptr;
366 break;
367 }
368 } while (Status == STATUS_BUFFER_OVERFLOW);
369
370 if (bfs && bfs->num_devices == 0) { // no mounted filesystems found
371 free(bfs);
372 bfs = nullptr;
373 }
374 }
375 }
376
377 find_devices(hwnd, &GUID_DEVINTERFACE_DISK, mm, device_list);
378 find_devices(hwnd, &GUID_DEVINTERFACE_VOLUME, mm, device_list);
379 find_devices(hwnd, &GUID_DEVINTERFACE_HIDDEN_VOLUME, mm, device_list);
380 }
381
382 #ifndef __REACTOS__ // Disabled because building with our <algorithm> seems complex right now...
383 sort(device_list.begin(), device_list.end(), sort_devices);
384 #endif
385
386 for (i = 0; i < device_list.size(); i++) {
387 if (!device_list[i].ignore) {
388 TVINSERTSTRUCTW tis;
389 HTREEITEM item;
390 wstring name, size;
391
392 if (device_list[i].disk_num != 0xffffffff && device_list[i].disk_num == last_disk_num)
393 tis.hParent = diskitem;
394 else
395 tis.hParent = TVI_ROOT;
396
397 tis.hInsertAfter = TVI_LAST;
398 tis.itemex.mask = TVIF_TEXT | TVIF_STATE | TVIF_PARAM;
399 tis.itemex.state = TVIS_EXPANDED;
400 tis.itemex.stateMask = TVIS_EXPANDED;
401
402 if (device_list[i].disk_num != 0xffffffff) {
403 wstring t;
404
405 if (!load_string(module, device_list[i].part_num != 0 ? IDS_PARTITION : IDS_DISK_NUM, t))
406 throw last_error(GetLastError());
407
408 wstring_sprintf(name, t, device_list[i].part_num != 0 ? device_list[i].part_num : device_list[i].disk_num);
409 } else
410 name = device_list[i].pnp_name;
411
412 // match child Btrfs devices to their parent
413 if (bfs && device_list[i].drive == L"" && device_list[i].fstype == L"Btrfs") {
414 btrfs_filesystem* bfs2 = bfs;
415
416 while (true) {
417 if (RtlCompareMemory(&bfs2->uuid, &device_list[i].fs_uuid, sizeof(BTRFS_UUID)) == sizeof(BTRFS_UUID)) {
418 ULONG j, k;
419 btrfs_filesystem_device* dev;
420
421 for (j = 0; j < bfs2->num_devices; j++) {
422 if (j == 0)
423 dev = &bfs2->device;
424 else
425 dev = (btrfs_filesystem_device*)((uint8_t*)dev + offsetof(btrfs_filesystem_device, name[0]) + dev->name_length);
426
427 if (RtlCompareMemory(&device_list[i].dev_uuid, &device_list[i].dev_uuid, sizeof(BTRFS_UUID)) == sizeof(BTRFS_UUID)) {
428 for (k = 0; k < device_list.size(); k++) {
429 if (k != i && device_list[k].fstype == L"Btrfs" && device_list[k].drive != L"" &&
430 RtlCompareMemory(&device_list[k].fs_uuid, &device_list[i].fs_uuid, sizeof(BTRFS_UUID)) == sizeof(BTRFS_UUID)) {
431 device_list[i].drive = device_list[k].drive;
432 break;
433 }
434 }
435
436 device_list[i].multi_device = bfs2->num_devices > 1;
437
438 break;
439 }
440 }
441
442 break;
443 }
444
445 if (bfs2->next_entry != 0)
446 bfs2 = (btrfs_filesystem*)((uint8_t*)bfs2 + bfs2->next_entry);
447 else
448 break;
449 }
450 }
451
452 name += L" (";
453
454 if (device_list[i].friendly_name != L"") {
455 name += device_list[i].friendly_name;
456 name += L", ";
457 }
458
459 if (device_list[i].drive != L"") {
460 name += device_list[i].drive;
461 name += L", ";
462 }
463
464 if (device_list[i].fstype != L"") {
465 name += device_list[i].fstype;
466 name += L", ";
467 }
468
469 format_size(device_list[i].size, size, false);
470 name += size;
471
472 name += L")";
473
474 tis.itemex.pszText = (WCHAR*)name.c_str();
475 tis.itemex.cchTextMax = (int)name.length();
476 tis.itemex.lParam = (LPARAM)&device_list[i];
477
478 item = (HTREEITEM)SendMessageW(tree, TVM_INSERTITEMW, 0, (LPARAM)&tis);
479 if (!item)
480 throw string_error(IDS_TVM_INSERTITEM_FAILED);
481
482 if (device_list[i].part_num == 0) {
483 diskitem = item;
484 last_disk_num = device_list[i].disk_num;
485 }
486 }
487 }
488 }
489
490 void BtrfsDeviceAdd::AddDevice(HWND hwndDlg) {
491 wstring mess, title;
492 NTSTATUS Status;
493 UNICODE_STRING vn;
494 OBJECT_ATTRIBUTES attr;
495 IO_STATUS_BLOCK iosb;
496
497 if (!sel) {
498 EndDialog(hwndDlg, 0);
499 return;
500 }
501
502 if (sel->fstype != L"") {
503 wstring s;
504
505 if (!load_string(module, IDS_ADD_DEVICE_CONFIRMATION_FS, s))
506 throw last_error(GetLastError());
507
508 wstring_sprintf(mess, s, sel->fstype.c_str());
509 } else {
510 if (!load_string(module, IDS_ADD_DEVICE_CONFIRMATION, mess))
511 throw last_error(GetLastError());
512 }
513
514 if (!load_string(module, IDS_CONFIRMATION_TITLE, title))
515 throw last_error(GetLastError());
516
517 if (MessageBoxW(hwndDlg, mess.c_str(), title.c_str(), MB_YESNO) != IDYES)
518 return;
519
520 win_handle h = CreateFileW(cmdline, FILE_TRAVERSE | FILE_READ_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr,
521 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, nullptr);
522
523 if (h == INVALID_HANDLE_VALUE)
524 throw last_error(GetLastError());
525
526 {
527 nt_handle h2;
528
529 vn.Length = vn.MaximumLength = (uint16_t)(sel->pnp_name.length() * sizeof(WCHAR));
530 vn.Buffer = (WCHAR*)sel->pnp_name.c_str();
531
532 InitializeObjectAttributes(&attr, &vn, OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, nullptr, nullptr);
533
534 Status = NtOpenFile(&h2, FILE_GENERIC_READ | FILE_GENERIC_WRITE, &attr, &iosb, FILE_SHARE_READ, FILE_SYNCHRONOUS_IO_ALERT);
535 if (!NT_SUCCESS(Status))
536 throw ntstatus_error(Status);
537
538 if (!sel->is_disk) {
539 Status = NtFsControlFile(h2, nullptr, nullptr, nullptr, &iosb, FSCTL_LOCK_VOLUME, nullptr, 0, nullptr, 0);
540 if (!NT_SUCCESS(Status))
541 throw string_error(IDS_LOCK_FAILED, Status);
542 }
543
544 Status = NtFsControlFile(h, nullptr, nullptr, nullptr, &iosb, FSCTL_BTRFS_ADD_DEVICE, &h2, sizeof(HANDLE), nullptr, 0);
545 if (!NT_SUCCESS(Status))
546 throw ntstatus_error(Status);
547
548 if (!sel->is_disk) {
549 Status = NtFsControlFile(h2, nullptr, nullptr, nullptr, &iosb, FSCTL_DISMOUNT_VOLUME, nullptr, 0, nullptr, 0);
550 if (!NT_SUCCESS(Status))
551 throw ntstatus_error(Status);
552
553 Status = NtFsControlFile(h2, nullptr, nullptr, nullptr, &iosb, FSCTL_UNLOCK_VOLUME, nullptr, 0, nullptr, 0);
554 if (!NT_SUCCESS(Status))
555 throw ntstatus_error(Status);
556 }
557 }
558
559 EndDialog(hwndDlg, 0);
560 }
561
562 INT_PTR CALLBACK BtrfsDeviceAdd::DeviceAddDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) {
563 try {
564 switch (uMsg) {
565 case WM_INITDIALOG:
566 {
567 EnableThemeDialogTexture(hwndDlg, ETDT_ENABLETAB);
568 populate_device_tree(GetDlgItem(hwndDlg, IDC_DEVICE_TREE));
569 EnableWindow(GetDlgItem(hwndDlg, IDOK), false);
570 break;
571 }
572
573 case WM_COMMAND:
574 switch (HIWORD(wParam)) {
575 case BN_CLICKED:
576 switch (LOWORD(wParam)) {
577 case IDOK:
578 AddDevice(hwndDlg);
579 return true;
580
581 case IDCANCEL:
582 EndDialog(hwndDlg, 0);
583 return true;
584 }
585 break;
586 }
587 break;
588
589 case WM_NOTIFY:
590 switch (((LPNMHDR)lParam)->code) {
591 case TVN_SELCHANGEDW:
592 {
593 NMTREEVIEWW* nmtv = (NMTREEVIEWW*)lParam;
594 TVITEMW tvi;
595 bool enable = false;
596
597 RtlZeroMemory(&tvi, sizeof(TVITEMW));
598 tvi.hItem = nmtv->itemNew.hItem;
599 tvi.mask = TVIF_PARAM | TVIF_HANDLE;
600
601 if (SendMessageW(GetDlgItem(hwndDlg, IDC_DEVICE_TREE), TVM_GETITEMW, 0, (LPARAM)&tvi))
602 sel = tvi.lParam == 0 ? nullptr : (device*)tvi.lParam;
603 else
604 sel = nullptr;
605
606 if (sel)
607 enable = (!sel->is_disk || !sel->has_parts) && !sel->multi_device;
608
609 EnableWindow(GetDlgItem(hwndDlg, IDOK), enable);
610 break;
611 }
612 }
613 break;
614 }
615 } catch (const exception& e) {
616 error_message(hwndDlg, e.what());
617 }
618
619 return false;
620 }
621
622 static INT_PTR CALLBACK stub_DeviceAddDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) {
623 BtrfsDeviceAdd* bda;
624
625 if (uMsg == WM_INITDIALOG) {
626 SetWindowLongPtr(hwndDlg, GWLP_USERDATA, (LONG_PTR)lParam);
627 bda = (BtrfsDeviceAdd*)lParam;
628 } else {
629 bda = (BtrfsDeviceAdd*)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
630 }
631
632 if (bda)
633 return bda->DeviceAddDlgProc(hwndDlg, uMsg, wParam, lParam);
634 else
635 return false;
636 }
637
638 void BtrfsDeviceAdd::ShowDialog() {
639 DialogBoxParamW(module, MAKEINTRESOURCEW(IDD_DEVICE_ADD), hwnd, stub_DeviceAddDlgProc, (LPARAM)this);
640 }
641
642 BtrfsDeviceAdd::BtrfsDeviceAdd(HINSTANCE hinst, HWND hwnd, WCHAR* cmdline) {
643 this->hinst = hinst;
644 this->hwnd = hwnd;
645 this->cmdline = cmdline;
646
647 sel = nullptr;
648 }
649
650 void BtrfsDeviceResize::do_resize(HWND hwndDlg) {
651 NTSTATUS Status;
652 IO_STATUS_BLOCK iosb;
653 btrfs_resize br;
654
655 {
656 win_handle h = CreateFileW(fn.c_str(), FILE_TRAVERSE | FILE_READ_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr,
657 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, nullptr);
658
659 if (h == INVALID_HANDLE_VALUE)
660 throw last_error(GetLastError());
661
662 br.device = dev_id;
663 br.size = new_size;
664
665 Status = NtFsControlFile(h, nullptr, nullptr, nullptr, &iosb, FSCTL_BTRFS_RESIZE, &br, sizeof(btrfs_resize), nullptr, 0);
666
667 if (Status != STATUS_MORE_PROCESSING_REQUIRED && !NT_SUCCESS(Status))
668 throw ntstatus_error(Status);
669 }
670
671 if (Status != STATUS_MORE_PROCESSING_REQUIRED) {
672 wstring s, t, u;
673
674 load_string(module, IDS_RESIZE_SUCCESSFUL, s);
675 format_size(new_size, u, true);
676 wstring_sprintf(t, s, dev_id, u.c_str());
677 MessageBoxW(hwndDlg, t.c_str(), L"", MB_OK);
678
679 EndDialog(hwndDlg, 0);
680 } else {
681 HWND par;
682
683 par = GetParent(hwndDlg);
684 EndDialog(hwndDlg, 0);
685
686 BtrfsBalance bb(fn, false, true);
687 bb.ShowBalance(par);
688 }
689 }
690
691 INT_PTR CALLBACK BtrfsDeviceResize::DeviceResizeDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) {
692 try {
693 switch (uMsg) {
694 case WM_INITDIALOG:
695 {
696 win_handle h;
697 WCHAR s[255];
698 wstring t, u;
699
700 EnableThemeDialogTexture(hwndDlg, ETDT_ENABLETAB);
701
702 GetDlgItemTextW(hwndDlg, IDC_RESIZE_DEVICE_ID, s, sizeof(s) / sizeof(WCHAR));
703 wstring_sprintf(t, s, dev_id);
704 SetDlgItemTextW(hwndDlg, IDC_RESIZE_DEVICE_ID, t.c_str());
705
706 h = CreateFileW(fn.c_str(), FILE_TRAVERSE | FILE_READ_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr,
707 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, nullptr);
708
709 if (h != INVALID_HANDLE_VALUE) {
710 NTSTATUS Status;
711 IO_STATUS_BLOCK iosb;
712 btrfs_device *devices, *bd;
713 ULONG devsize;
714 bool found = false;
715 HWND slider;
716
717 devsize = 1024;
718 devices = (btrfs_device*)malloc(devsize);
719
720 while (true) {
721 Status = NtFsControlFile(h, nullptr, nullptr, nullptr, &iosb, FSCTL_BTRFS_GET_DEVICES, nullptr, 0, devices, devsize);
722 if (Status == STATUS_BUFFER_OVERFLOW) {
723 devsize += 1024;
724
725 free(devices);
726 devices = (btrfs_device*)malloc(devsize);
727 } else
728 break;
729 }
730
731 if (!NT_SUCCESS(Status)) {
732 free(devices);
733 return false;
734 }
735
736 bd = devices;
737
738 while (true) {
739 if (bd->dev_id == dev_id) {
740 memcpy(&dev_info, bd, sizeof(btrfs_device));
741 found = true;
742 break;
743 }
744
745 if (bd->next_entry > 0)
746 bd = (btrfs_device*)((uint8_t*)bd + bd->next_entry);
747 else
748 break;
749 }
750
751 if (!found) {
752 free(devices);
753 return false;
754 }
755
756 free(devices);
757
758 GetDlgItemTextW(hwndDlg, IDC_RESIZE_CURSIZE, s, sizeof(s) / sizeof(WCHAR));
759 format_size(dev_info.size, u, true);
760 wstring_sprintf(t, s, u.c_str());
761 SetDlgItemTextW(hwndDlg, IDC_RESIZE_CURSIZE, t.c_str());
762
763 new_size = dev_info.size;
764
765 GetDlgItemTextW(hwndDlg, IDC_RESIZE_NEWSIZE, new_size_text, sizeof(new_size_text) / sizeof(WCHAR));
766 wstring_sprintf(t, new_size_text, u.c_str());
767 SetDlgItemTextW(hwndDlg, IDC_RESIZE_NEWSIZE, t.c_str());
768
769 slider = GetDlgItem(hwndDlg, IDC_RESIZE_SLIDER);
770 SendMessageW(slider, TBM_SETRANGEMIN, false, 0);
771 SendMessageW(slider, TBM_SETRANGEMAX, false, (LPARAM)(dev_info.max_size / 1048576));
772 SendMessageW(slider, TBM_SETPOS, true, (LPARAM)(new_size / 1048576));
773 } else
774 return false;
775
776 break;
777 }
778
779 case WM_COMMAND:
780 switch (HIWORD(wParam)) {
781 case BN_CLICKED:
782 switch (LOWORD(wParam)) {
783 case IDOK:
784 do_resize(hwndDlg);
785 return true;
786
787 case IDCANCEL:
788 EndDialog(hwndDlg, 0);
789 return true;
790 }
791 break;
792 }
793 break;
794
795 case WM_HSCROLL:
796 {
797 wstring t, u;
798
799 new_size = UInt32x32To64(SendMessageW(GetDlgItem(hwndDlg, IDC_RESIZE_SLIDER), TBM_GETPOS, 0, 0), 1048576);
800
801 format_size(new_size, u, true);
802 wstring_sprintf(t, new_size_text, u.c_str());
803 SetDlgItemTextW(hwndDlg, IDC_RESIZE_NEWSIZE, t.c_str());
804
805 EnableWindow(GetDlgItem(hwndDlg, IDOK), new_size > 0 ? true : false);
806
807 break;
808 }
809 }
810 } catch (const exception& e) {
811 error_message(hwndDlg, e.what());
812 }
813
814 return false;
815 }
816
817 static INT_PTR CALLBACK stub_DeviceResizeDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) {
818 BtrfsDeviceResize* bdr;
819
820 if (uMsg == WM_INITDIALOG) {
821 SetWindowLongPtr(hwndDlg, GWLP_USERDATA, (LONG_PTR)lParam);
822 bdr = (BtrfsDeviceResize*)lParam;
823 } else
824 bdr = (BtrfsDeviceResize*)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
825
826 if (bdr)
827 return bdr->DeviceResizeDlgProc(hwndDlg, uMsg, wParam, lParam);
828 else
829 return false;
830 }
831
832 void BtrfsDeviceResize::ShowDialog(HWND hwnd, const wstring& fn, uint64_t dev_id) {
833 this->dev_id = dev_id;
834 this->fn = fn;
835
836 DialogBoxParamW(module, MAKEINTRESOURCEW(IDD_RESIZE), hwnd, stub_DeviceResizeDlgProc, (LPARAM)this);
837 }
838
839 #ifdef __cplusplus
840 extern "C" {
841 #endif
842
843 void CALLBACK AddDeviceW(HWND hwnd, HINSTANCE hinst, LPWSTR lpszCmdLine, int nCmdShow) {
844 try {
845 win_handle token;
846 TOKEN_PRIVILEGES tp;
847 LUID luid;
848
849 set_dpi_aware();
850
851 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &token))
852 throw last_error(GetLastError());
853
854 if (!LookupPrivilegeValueW(nullptr, L"SeManageVolumePrivilege", &luid))
855 throw last_error(GetLastError());
856
857 tp.PrivilegeCount = 1;
858 tp.Privileges[0].Luid = luid;
859 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
860
861 if (!AdjustTokenPrivileges(token, false, &tp, sizeof(TOKEN_PRIVILEGES), nullptr, nullptr))
862 throw last_error(GetLastError());
863
864 BtrfsDeviceAdd bda(hinst, hwnd, lpszCmdLine);
865 bda.ShowDialog();
866 } catch (const exception& e) {
867 error_message(hwnd, e.what());
868 }
869 }
870
871 void CALLBACK RemoveDeviceW(HWND hwnd, HINSTANCE hinst, LPWSTR lpszCmdLine, int nCmdShow) {
872 try {
873 WCHAR *s, *vol, *dev;
874 uint64_t devid;
875 win_handle h, token;
876 TOKEN_PRIVILEGES tp;
877 LUID luid;
878 NTSTATUS Status;
879 IO_STATUS_BLOCK iosb;
880
881 set_dpi_aware();
882
883 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &token))
884 throw last_error(GetLastError());
885
886 if (!LookupPrivilegeValueW(nullptr, L"SeManageVolumePrivilege", &luid))
887 throw last_error(GetLastError());
888
889 tp.PrivilegeCount = 1;
890 tp.Privileges[0].Luid = luid;
891 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
892
893 if (!AdjustTokenPrivileges(token, false, &tp, sizeof(TOKEN_PRIVILEGES), nullptr, nullptr))
894 throw last_error(GetLastError());
895
896 s = wcsstr(lpszCmdLine, L"|");
897 if (!s)
898 return;
899
900 s[0] = 0;
901
902 vol = lpszCmdLine;
903 dev = &s[1];
904
905 devid = _wtoi(dev);
906 if (devid == 0)
907 return;
908
909 h = CreateFileW(vol, FILE_TRAVERSE | FILE_READ_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr,
910 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, nullptr);
911
912 if (h == INVALID_HANDLE_VALUE)
913 throw last_error(GetLastError());
914
915 Status = NtFsControlFile(h, nullptr, nullptr, nullptr, &iosb, FSCTL_BTRFS_REMOVE_DEVICE, &devid, sizeof(uint64_t), nullptr, 0);
916 if (!NT_SUCCESS(Status)) {
917 if (Status == STATUS_CANNOT_DELETE)
918 throw string_error(IDS_CANNOT_REMOVE_RAID);
919 else
920 throw ntstatus_error(Status);
921
922 return;
923 }
924
925 BtrfsBalance bb(vol, true);
926 bb.ShowBalance(hwnd);
927 } catch (const exception& e) {
928 error_message(hwnd, e.what());
929 }
930 }
931
932 void CALLBACK ResizeDeviceW(HWND hwnd, HINSTANCE hinst, LPWSTR lpszCmdLine, int nCmdShow) {
933 try {
934 WCHAR *s, *vol, *dev;
935 uint64_t devid;
936 win_handle token;
937 TOKEN_PRIVILEGES tp;
938 LUID luid;
939
940 set_dpi_aware();
941
942 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &token))
943 throw last_error(GetLastError());
944
945 if (!LookupPrivilegeValueW(nullptr, L"SeManageVolumePrivilege", &luid))
946 throw last_error(GetLastError());
947
948 tp.PrivilegeCount = 1;
949 tp.Privileges[0].Luid = luid;
950 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
951
952 if (!AdjustTokenPrivileges(token, false, &tp, sizeof(TOKEN_PRIVILEGES), nullptr, nullptr))
953 throw last_error(GetLastError());
954
955 s = wcsstr(lpszCmdLine, L"|");
956 if (!s)
957 return;
958
959 s[0] = 0;
960
961 vol = lpszCmdLine;
962 dev = &s[1];
963
964 devid = _wtoi(dev);
965 if (devid == 0)
966 return;
967
968 BtrfsDeviceResize bdr;
969 bdr.ShowDialog(hwnd, vol, devid);
970 } catch (const exception& e) {
971 error_message(hwnd, e.what());
972 }
973 }
974
975 #ifdef __cplusplus
976 }
977 #endif