[SHELLBTRFS] Upgrade to 1.5
[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 auto v = mm.query_points(L"", L"", wstring_view(path.Buffer, path.Length / sizeof(WCHAR)));
241
242 for (const auto& p : v) {
243 if (p.symlink.length() == 14 && p.symlink.substr(0, dosdevices.length()) == dosdevices && p.symlink[13] == ':') {
244 WCHAR dr[3];
245
246 dr[0] = p.symlink[12];
247 dr[1] = ':';
248 dr[2] = 0;
249
250 dev.drive = dr;
251 break;
252 }
253 }
254 } catch (...) { // don't fail entirely if mountmgr refuses to co-operate
255 }
256 }
257
258 if (!dev.is_disk || !dev.has_parts) {
259 i = 0;
260 while (fs_ident[i].name) {
261 if (i == 0 || fs_ident[i].kboff != fs_ident[i-1].kboff) {
262 LARGE_INTEGER off;
263
264 off.QuadPart = fs_ident[i].kboff * 1024;
265 Status = NtReadFile(file, nullptr, nullptr, nullptr, &iosb, sb, sizeof(sb), &off, nullptr);
266 }
267
268 if (NT_SUCCESS(Status)) {
269 if (RtlCompareMemory(sb + fs_ident[i].sboff, fs_ident[i].magic, fs_ident[i].magiclen) == fs_ident[i].magiclen) {
270 dev.fstype = fs_ident[i].name;
271
272 if (dev.fstype == L"Btrfs") {
273 superblock* bsb = (superblock*)sb;
274
275 RtlCopyMemory(&dev.fs_uuid, &bsb->uuid, sizeof(BTRFS_UUID));
276 RtlCopyMemory(&dev.dev_uuid, &bsb->dev_item.device_uuid, sizeof(BTRFS_UUID));
277 }
278
279 break;
280 }
281 }
282
283 i++;
284 }
285
286 if (dev.fstype == L"Btrfs" && RtlCompareMemory(guid, &GUID_DEVINTERFACE_DISK, sizeof(GUID)) != sizeof(GUID)) {
287 wstring name;
288 wstring pref = L"\\Device\\Btrfs{";
289
290 name = get_mountdev_name(file);
291
292 if (name.length() > pref.length() && RtlCompareMemory(name.c_str(), pref.c_str(), pref.length() * sizeof(WCHAR)) == pref.length() * sizeof(WCHAR))
293 dev.ignore = true;
294 }
295 }
296
297 device_list.push_back(dev);
298 }
299
300 free(detail);
301
302 index++;
303 } while (SetupDiEnumDeviceInterfaces(h, nullptr, guid, index, &did));
304
305 SetupDiDestroyDeviceInfoList(h);
306 } else
307 throw last_error(GetLastError());
308 }
309
310 #ifndef __REACTOS__ // Disabled because building with our <algorithm> seems complex right now...
311 static bool sort_devices(device i, device j) {
312 if (i.disk_num < j.disk_num)
313 return true;
314
315 if (i.disk_num == j.disk_num && i.part_num < j.part_num)
316 return true;
317
318 return false;
319 }
320 #endif
321
322 void BtrfsDeviceAdd::populate_device_tree(HWND tree) {
323 HWND hwnd = GetParent(tree);
324 unsigned int i;
325 ULONG last_disk_num = 0xffffffff;
326 HTREEITEM diskitem;
327 NTSTATUS Status;
328 OBJECT_ATTRIBUTES attr;
329 UNICODE_STRING us;
330 IO_STATUS_BLOCK iosb;
331 btrfs_filesystem* bfs = nullptr;
332
333 static WCHAR btrfs[] = L"\\Btrfs";
334
335 device_list.clear();
336
337 {
338 mountmgr mm;
339
340 {
341 nt_handle btrfsh;
342
343 us.Length = us.MaximumLength = (uint16_t)(wcslen(btrfs) * sizeof(WCHAR));
344 us.Buffer = btrfs;
345
346 InitializeObjectAttributes(&attr, &us, 0, nullptr, nullptr);
347
348 Status = NtOpenFile(&btrfsh, SYNCHRONIZE | FILE_READ_ATTRIBUTES, &attr, &iosb,
349 FILE_SHARE_READ | FILE_SHARE_WRITE, FILE_SYNCHRONOUS_IO_ALERT);
350 if (NT_SUCCESS(Status)) {
351 ULONG bfssize = 0;
352
353 do {
354 bfssize += 1024;
355
356 if (bfs) free(bfs);
357 bfs = (btrfs_filesystem*)malloc(bfssize);
358
359 Status = NtDeviceIoControlFile(btrfsh, nullptr, nullptr, nullptr, &iosb, IOCTL_BTRFS_QUERY_FILESYSTEMS, nullptr, 0, bfs, bfssize);
360 if (!NT_SUCCESS(Status) && Status != STATUS_BUFFER_OVERFLOW) {
361 free(bfs);
362 bfs = nullptr;
363 break;
364 }
365 } while (Status == STATUS_BUFFER_OVERFLOW);
366
367 if (bfs && bfs->num_devices == 0) { // no mounted filesystems found
368 free(bfs);
369 bfs = nullptr;
370 }
371 }
372 }
373
374 find_devices(hwnd, &GUID_DEVINTERFACE_DISK, mm, device_list);
375 find_devices(hwnd, &GUID_DEVINTERFACE_VOLUME, mm, device_list);
376 find_devices(hwnd, &GUID_DEVINTERFACE_HIDDEN_VOLUME, mm, device_list);
377 }
378
379 #ifndef __REACTOS__ // Disabled because building with our <algorithm> seems complex right now...
380 sort(device_list.begin(), device_list.end(), sort_devices);
381 #endif
382
383 for (i = 0; i < device_list.size(); i++) {
384 if (!device_list[i].ignore) {
385 TVINSERTSTRUCTW tis;
386 HTREEITEM item;
387 wstring name, size;
388
389 if (device_list[i].disk_num != 0xffffffff && device_list[i].disk_num == last_disk_num)
390 tis.hParent = diskitem;
391 else
392 tis.hParent = TVI_ROOT;
393
394 tis.hInsertAfter = TVI_LAST;
395 tis.itemex.mask = TVIF_TEXT | TVIF_STATE | TVIF_PARAM;
396 tis.itemex.state = TVIS_EXPANDED;
397 tis.itemex.stateMask = TVIS_EXPANDED;
398
399 if (device_list[i].disk_num != 0xffffffff) {
400 wstring t;
401
402 if (!load_string(module, device_list[i].part_num != 0 ? IDS_PARTITION : IDS_DISK_NUM, t))
403 throw last_error(GetLastError());
404
405 wstring_sprintf(name, t, device_list[i].part_num != 0 ? device_list[i].part_num : device_list[i].disk_num);
406 } else
407 name = device_list[i].pnp_name;
408
409 // match child Btrfs devices to their parent
410 if (bfs && device_list[i].drive == L"" && device_list[i].fstype == L"Btrfs") {
411 btrfs_filesystem* bfs2 = bfs;
412
413 while (true) {
414 if (RtlCompareMemory(&bfs2->uuid, &device_list[i].fs_uuid, sizeof(BTRFS_UUID)) == sizeof(BTRFS_UUID)) {
415 ULONG j, k;
416 btrfs_filesystem_device* dev;
417
418 for (j = 0; j < bfs2->num_devices; j++) {
419 if (j == 0)
420 dev = &bfs2->device;
421 else
422 dev = (btrfs_filesystem_device*)((uint8_t*)dev + offsetof(btrfs_filesystem_device, name[0]) + dev->name_length);
423
424 if (RtlCompareMemory(&device_list[i].dev_uuid, &device_list[i].dev_uuid, sizeof(BTRFS_UUID)) == sizeof(BTRFS_UUID)) {
425 for (k = 0; k < device_list.size(); k++) {
426 if (k != i && device_list[k].fstype == L"Btrfs" && device_list[k].drive != L"" &&
427 RtlCompareMemory(&device_list[k].fs_uuid, &device_list[i].fs_uuid, sizeof(BTRFS_UUID)) == sizeof(BTRFS_UUID)) {
428 device_list[i].drive = device_list[k].drive;
429 break;
430 }
431 }
432
433 device_list[i].multi_device = bfs2->num_devices > 1;
434
435 break;
436 }
437 }
438
439 break;
440 }
441
442 if (bfs2->next_entry != 0)
443 bfs2 = (btrfs_filesystem*)((uint8_t*)bfs2 + bfs2->next_entry);
444 else
445 break;
446 }
447 }
448
449 name += L" (";
450
451 if (device_list[i].friendly_name != L"") {
452 name += device_list[i].friendly_name;
453 name += L", ";
454 }
455
456 if (device_list[i].drive != L"") {
457 name += device_list[i].drive;
458 name += L", ";
459 }
460
461 if (device_list[i].fstype != L"") {
462 name += device_list[i].fstype;
463 name += L", ";
464 }
465
466 format_size(device_list[i].size, size, false);
467 name += size;
468
469 name += L")";
470
471 tis.itemex.pszText = (WCHAR*)name.c_str();
472 tis.itemex.cchTextMax = (int)name.length();
473 tis.itemex.lParam = (LPARAM)&device_list[i];
474
475 item = (HTREEITEM)SendMessageW(tree, TVM_INSERTITEMW, 0, (LPARAM)&tis);
476 if (!item)
477 throw string_error(IDS_TVM_INSERTITEM_FAILED);
478
479 if (device_list[i].part_num == 0) {
480 diskitem = item;
481 last_disk_num = device_list[i].disk_num;
482 }
483 }
484 }
485 }
486
487 void BtrfsDeviceAdd::AddDevice(HWND hwndDlg) {
488 wstring mess, title;
489 NTSTATUS Status;
490 UNICODE_STRING vn;
491 OBJECT_ATTRIBUTES attr;
492 IO_STATUS_BLOCK iosb;
493
494 if (!sel) {
495 EndDialog(hwndDlg, 0);
496 return;
497 }
498
499 if (sel->fstype != L"") {
500 wstring s;
501
502 if (!load_string(module, IDS_ADD_DEVICE_CONFIRMATION_FS, s))
503 throw last_error(GetLastError());
504
505 wstring_sprintf(mess, s, sel->fstype.c_str());
506 } else {
507 if (!load_string(module, IDS_ADD_DEVICE_CONFIRMATION, mess))
508 throw last_error(GetLastError());
509 }
510
511 if (!load_string(module, IDS_CONFIRMATION_TITLE, title))
512 throw last_error(GetLastError());
513
514 if (MessageBoxW(hwndDlg, mess.c_str(), title.c_str(), MB_YESNO) != IDYES)
515 return;
516
517 win_handle h = CreateFileW(cmdline, FILE_TRAVERSE | FILE_READ_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr,
518 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, nullptr);
519
520 if (h == INVALID_HANDLE_VALUE)
521 throw last_error(GetLastError());
522
523 {
524 nt_handle h2;
525
526 vn.Length = vn.MaximumLength = (uint16_t)(sel->pnp_name.length() * sizeof(WCHAR));
527 vn.Buffer = (WCHAR*)sel->pnp_name.c_str();
528
529 InitializeObjectAttributes(&attr, &vn, OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, nullptr, nullptr);
530
531 Status = NtOpenFile(&h2, FILE_GENERIC_READ | FILE_GENERIC_WRITE, &attr, &iosb, FILE_SHARE_READ, FILE_SYNCHRONOUS_IO_ALERT);
532 if (!NT_SUCCESS(Status))
533 throw ntstatus_error(Status);
534
535 if (!sel->is_disk) {
536 Status = NtFsControlFile(h2, nullptr, nullptr, nullptr, &iosb, FSCTL_LOCK_VOLUME, nullptr, 0, nullptr, 0);
537 if (!NT_SUCCESS(Status))
538 throw string_error(IDS_LOCK_FAILED, Status);
539 }
540
541 Status = NtFsControlFile(h, nullptr, nullptr, nullptr, &iosb, FSCTL_BTRFS_ADD_DEVICE, &h2, sizeof(HANDLE), nullptr, 0);
542 if (!NT_SUCCESS(Status))
543 throw ntstatus_error(Status);
544
545 if (!sel->is_disk) {
546 Status = NtFsControlFile(h2, nullptr, nullptr, nullptr, &iosb, FSCTL_DISMOUNT_VOLUME, nullptr, 0, nullptr, 0);
547 if (!NT_SUCCESS(Status))
548 throw ntstatus_error(Status);
549
550 Status = NtFsControlFile(h2, nullptr, nullptr, nullptr, &iosb, FSCTL_UNLOCK_VOLUME, nullptr, 0, nullptr, 0);
551 if (!NT_SUCCESS(Status))
552 throw ntstatus_error(Status);
553 }
554 }
555
556 EndDialog(hwndDlg, 0);
557 }
558
559 INT_PTR CALLBACK BtrfsDeviceAdd::DeviceAddDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) {
560 try {
561 switch (uMsg) {
562 case WM_INITDIALOG:
563 {
564 EnableThemeDialogTexture(hwndDlg, ETDT_ENABLETAB);
565 populate_device_tree(GetDlgItem(hwndDlg, IDC_DEVICE_TREE));
566 EnableWindow(GetDlgItem(hwndDlg, IDOK), false);
567 break;
568 }
569
570 case WM_COMMAND:
571 switch (HIWORD(wParam)) {
572 case BN_CLICKED:
573 switch (LOWORD(wParam)) {
574 case IDOK:
575 AddDevice(hwndDlg);
576 return true;
577
578 case IDCANCEL:
579 EndDialog(hwndDlg, 0);
580 return true;
581 }
582 break;
583 }
584 break;
585
586 case WM_NOTIFY:
587 switch (((LPNMHDR)lParam)->code) {
588 case TVN_SELCHANGEDW:
589 {
590 NMTREEVIEWW* nmtv = (NMTREEVIEWW*)lParam;
591 TVITEMW tvi;
592 bool enable = false;
593
594 RtlZeroMemory(&tvi, sizeof(TVITEMW));
595 tvi.hItem = nmtv->itemNew.hItem;
596 tvi.mask = TVIF_PARAM | TVIF_HANDLE;
597
598 if (SendMessageW(GetDlgItem(hwndDlg, IDC_DEVICE_TREE), TVM_GETITEMW, 0, (LPARAM)&tvi))
599 sel = tvi.lParam == 0 ? nullptr : (device*)tvi.lParam;
600 else
601 sel = nullptr;
602
603 if (sel)
604 enable = (!sel->is_disk || !sel->has_parts) && !sel->multi_device;
605
606 EnableWindow(GetDlgItem(hwndDlg, IDOK), enable);
607 break;
608 }
609 }
610 break;
611 }
612 } catch (const exception& e) {
613 error_message(hwndDlg, e.what());
614 }
615
616 return false;
617 }
618
619 static INT_PTR CALLBACK stub_DeviceAddDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) {
620 BtrfsDeviceAdd* bda;
621
622 if (uMsg == WM_INITDIALOG) {
623 SetWindowLongPtr(hwndDlg, GWLP_USERDATA, (LONG_PTR)lParam);
624 bda = (BtrfsDeviceAdd*)lParam;
625 } else {
626 bda = (BtrfsDeviceAdd*)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
627 }
628
629 if (bda)
630 return bda->DeviceAddDlgProc(hwndDlg, uMsg, wParam, lParam);
631 else
632 return false;
633 }
634
635 void BtrfsDeviceAdd::ShowDialog() {
636 DialogBoxParamW(module, MAKEINTRESOURCEW(IDD_DEVICE_ADD), hwnd, stub_DeviceAddDlgProc, (LPARAM)this);
637 }
638
639 BtrfsDeviceAdd::BtrfsDeviceAdd(HINSTANCE hinst, HWND hwnd, WCHAR* cmdline) {
640 this->hinst = hinst;
641 this->hwnd = hwnd;
642 this->cmdline = cmdline;
643
644 sel = nullptr;
645 }
646
647 void BtrfsDeviceResize::do_resize(HWND hwndDlg) {
648 NTSTATUS Status;
649 IO_STATUS_BLOCK iosb;
650 btrfs_resize br;
651
652 {
653 win_handle h = CreateFileW(fn.c_str(), FILE_TRAVERSE | FILE_READ_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr,
654 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, nullptr);
655
656 if (h == INVALID_HANDLE_VALUE)
657 throw last_error(GetLastError());
658
659 br.device = dev_id;
660 br.size = new_size;
661
662 Status = NtFsControlFile(h, nullptr, nullptr, nullptr, &iosb, FSCTL_BTRFS_RESIZE, &br, sizeof(btrfs_resize), nullptr, 0);
663
664 if (Status != STATUS_MORE_PROCESSING_REQUIRED && !NT_SUCCESS(Status))
665 throw ntstatus_error(Status);
666 }
667
668 if (Status != STATUS_MORE_PROCESSING_REQUIRED) {
669 wstring s, t, u;
670
671 load_string(module, IDS_RESIZE_SUCCESSFUL, s);
672 format_size(new_size, u, true);
673 wstring_sprintf(t, s, dev_id, u.c_str());
674 MessageBoxW(hwndDlg, t.c_str(), L"", MB_OK);
675
676 EndDialog(hwndDlg, 0);
677 } else {
678 HWND par;
679
680 par = GetParent(hwndDlg);
681 EndDialog(hwndDlg, 0);
682
683 BtrfsBalance bb(fn, false, true);
684 bb.ShowBalance(par);
685 }
686 }
687
688 INT_PTR CALLBACK BtrfsDeviceResize::DeviceResizeDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) {
689 try {
690 switch (uMsg) {
691 case WM_INITDIALOG:
692 {
693 win_handle h;
694 WCHAR s[255];
695 wstring t, u;
696
697 EnableThemeDialogTexture(hwndDlg, ETDT_ENABLETAB);
698
699 GetDlgItemTextW(hwndDlg, IDC_RESIZE_DEVICE_ID, s, sizeof(s) / sizeof(WCHAR));
700 wstring_sprintf(t, s, dev_id);
701 SetDlgItemTextW(hwndDlg, IDC_RESIZE_DEVICE_ID, t.c_str());
702
703 h = CreateFileW(fn.c_str(), FILE_TRAVERSE | FILE_READ_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr,
704 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, nullptr);
705
706 if (h != INVALID_HANDLE_VALUE) {
707 NTSTATUS Status;
708 IO_STATUS_BLOCK iosb;
709 btrfs_device *devices, *bd;
710 ULONG devsize;
711 bool found = false;
712 HWND slider;
713
714 devsize = 1024;
715 devices = (btrfs_device*)malloc(devsize);
716
717 while (true) {
718 Status = NtFsControlFile(h, nullptr, nullptr, nullptr, &iosb, FSCTL_BTRFS_GET_DEVICES, nullptr, 0, devices, devsize);
719 if (Status == STATUS_BUFFER_OVERFLOW) {
720 devsize += 1024;
721
722 free(devices);
723 devices = (btrfs_device*)malloc(devsize);
724 } else
725 break;
726 }
727
728 if (!NT_SUCCESS(Status)) {
729 free(devices);
730 return false;
731 }
732
733 bd = devices;
734
735 while (true) {
736 if (bd->dev_id == dev_id) {
737 memcpy(&dev_info, bd, sizeof(btrfs_device));
738 found = true;
739 break;
740 }
741
742 if (bd->next_entry > 0)
743 bd = (btrfs_device*)((uint8_t*)bd + bd->next_entry);
744 else
745 break;
746 }
747
748 if (!found) {
749 free(devices);
750 return false;
751 }
752
753 free(devices);
754
755 GetDlgItemTextW(hwndDlg, IDC_RESIZE_CURSIZE, s, sizeof(s) / sizeof(WCHAR));
756 format_size(dev_info.size, u, true);
757 wstring_sprintf(t, s, u.c_str());
758 SetDlgItemTextW(hwndDlg, IDC_RESIZE_CURSIZE, t.c_str());
759
760 new_size = dev_info.size;
761
762 GetDlgItemTextW(hwndDlg, IDC_RESIZE_NEWSIZE, new_size_text, sizeof(new_size_text) / sizeof(WCHAR));
763 wstring_sprintf(t, new_size_text, u.c_str());
764 SetDlgItemTextW(hwndDlg, IDC_RESIZE_NEWSIZE, t.c_str());
765
766 slider = GetDlgItem(hwndDlg, IDC_RESIZE_SLIDER);
767 SendMessageW(slider, TBM_SETRANGEMIN, false, 0);
768 SendMessageW(slider, TBM_SETRANGEMAX, false, (LPARAM)(dev_info.max_size / 1048576));
769 SendMessageW(slider, TBM_SETPOS, true, (LPARAM)(new_size / 1048576));
770 } else
771 return false;
772
773 break;
774 }
775
776 case WM_COMMAND:
777 switch (HIWORD(wParam)) {
778 case BN_CLICKED:
779 switch (LOWORD(wParam)) {
780 case IDOK:
781 do_resize(hwndDlg);
782 return true;
783
784 case IDCANCEL:
785 EndDialog(hwndDlg, 0);
786 return true;
787 }
788 break;
789 }
790 break;
791
792 case WM_HSCROLL:
793 {
794 wstring t, u;
795
796 new_size = UInt32x32To64(SendMessageW(GetDlgItem(hwndDlg, IDC_RESIZE_SLIDER), TBM_GETPOS, 0, 0), 1048576);
797
798 format_size(new_size, u, true);
799 wstring_sprintf(t, new_size_text, u.c_str());
800 SetDlgItemTextW(hwndDlg, IDC_RESIZE_NEWSIZE, t.c_str());
801
802 EnableWindow(GetDlgItem(hwndDlg, IDOK), new_size > 0 ? true : false);
803
804 break;
805 }
806 }
807 } catch (const exception& e) {
808 error_message(hwndDlg, e.what());
809 }
810
811 return false;
812 }
813
814 static INT_PTR CALLBACK stub_DeviceResizeDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) {
815 BtrfsDeviceResize* bdr;
816
817 if (uMsg == WM_INITDIALOG) {
818 SetWindowLongPtr(hwndDlg, GWLP_USERDATA, (LONG_PTR)lParam);
819 bdr = (BtrfsDeviceResize*)lParam;
820 } else
821 bdr = (BtrfsDeviceResize*)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
822
823 if (bdr)
824 return bdr->DeviceResizeDlgProc(hwndDlg, uMsg, wParam, lParam);
825 else
826 return false;
827 }
828
829 void BtrfsDeviceResize::ShowDialog(HWND hwnd, const wstring& fn, uint64_t dev_id) {
830 this->dev_id = dev_id;
831 this->fn = fn;
832
833 DialogBoxParamW(module, MAKEINTRESOURCEW(IDD_RESIZE), hwnd, stub_DeviceResizeDlgProc, (LPARAM)this);
834 }
835
836 #ifdef __cplusplus
837 extern "C" {
838 #endif
839
840 void CALLBACK AddDeviceW(HWND hwnd, HINSTANCE hinst, LPWSTR lpszCmdLine, int nCmdShow) {
841 try {
842 win_handle token;
843 TOKEN_PRIVILEGES tp;
844 LUID luid;
845
846 set_dpi_aware();
847
848 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &token))
849 throw last_error(GetLastError());
850
851 if (!LookupPrivilegeValueW(nullptr, L"SeManageVolumePrivilege", &luid))
852 throw last_error(GetLastError());
853
854 tp.PrivilegeCount = 1;
855 tp.Privileges[0].Luid = luid;
856 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
857
858 if (!AdjustTokenPrivileges(token, false, &tp, sizeof(TOKEN_PRIVILEGES), nullptr, nullptr))
859 throw last_error(GetLastError());
860
861 BtrfsDeviceAdd bda(hinst, hwnd, lpszCmdLine);
862 bda.ShowDialog();
863 } catch (const exception& e) {
864 error_message(hwnd, e.what());
865 }
866 }
867
868 void CALLBACK RemoveDeviceW(HWND hwnd, HINSTANCE hinst, LPWSTR lpszCmdLine, int nCmdShow) {
869 try {
870 WCHAR *s, *vol, *dev;
871 uint64_t devid;
872 win_handle h, token;
873 TOKEN_PRIVILEGES tp;
874 LUID luid;
875 NTSTATUS Status;
876 IO_STATUS_BLOCK iosb;
877
878 set_dpi_aware();
879
880 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &token))
881 throw last_error(GetLastError());
882
883 if (!LookupPrivilegeValueW(nullptr, L"SeManageVolumePrivilege", &luid))
884 throw last_error(GetLastError());
885
886 tp.PrivilegeCount = 1;
887 tp.Privileges[0].Luid = luid;
888 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
889
890 if (!AdjustTokenPrivileges(token, false, &tp, sizeof(TOKEN_PRIVILEGES), nullptr, nullptr))
891 throw last_error(GetLastError());
892
893 s = wcsstr(lpszCmdLine, L"|");
894 if (!s)
895 return;
896
897 s[0] = 0;
898
899 vol = lpszCmdLine;
900 dev = &s[1];
901
902 devid = _wtoi(dev);
903 if (devid == 0)
904 return;
905
906 h = CreateFileW(vol, FILE_TRAVERSE | FILE_READ_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr,
907 OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, nullptr);
908
909 if (h == INVALID_HANDLE_VALUE)
910 throw last_error(GetLastError());
911
912 Status = NtFsControlFile(h, nullptr, nullptr, nullptr, &iosb, FSCTL_BTRFS_REMOVE_DEVICE, &devid, sizeof(uint64_t), nullptr, 0);
913 if (!NT_SUCCESS(Status)) {
914 if (Status == STATUS_CANNOT_DELETE)
915 throw string_error(IDS_CANNOT_REMOVE_RAID);
916 else
917 throw ntstatus_error(Status);
918
919 return;
920 }
921
922 BtrfsBalance bb(vol, true);
923 bb.ShowBalance(hwnd);
924 } catch (const exception& e) {
925 error_message(hwnd, e.what());
926 }
927 }
928
929 void CALLBACK ResizeDeviceW(HWND hwnd, HINSTANCE hinst, LPWSTR lpszCmdLine, int nCmdShow) {
930 try {
931 WCHAR *s, *vol, *dev;
932 uint64_t devid;
933 win_handle token;
934 TOKEN_PRIVILEGES tp;
935 LUID luid;
936
937 set_dpi_aware();
938
939 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &token))
940 throw last_error(GetLastError());
941
942 if (!LookupPrivilegeValueW(nullptr, L"SeManageVolumePrivilege", &luid))
943 throw last_error(GetLastError());
944
945 tp.PrivilegeCount = 1;
946 tp.Privileges[0].Luid = luid;
947 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
948
949 if (!AdjustTokenPrivileges(token, false, &tp, sizeof(TOKEN_PRIVILEGES), nullptr, nullptr))
950 throw last_error(GetLastError());
951
952 s = wcsstr(lpszCmdLine, L"|");
953 if (!s)
954 return;
955
956 s[0] = 0;
957
958 vol = lpszCmdLine;
959 dev = &s[1];
960
961 devid = _wtoi(dev);
962 if (devid == 0)
963 return;
964
965 BtrfsDeviceResize bdr;
966 bdr.ShowDialog(hwnd, vol, devid);
967 } catch (const exception& e) {
968 error_message(hwnd, e.what());
969 }
970 }
971
972 #ifdef __cplusplus
973 }
974 #endif