-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriveutils.cpp
More file actions
62 lines (53 loc) · 1.75 KB
/
Copy pathdriveutils.cpp
File metadata and controls
62 lines (53 loc) · 1.75 KB
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
#include "driveutils.h"
#ifdef Q_OS_WIN
#include <windows.h>
#include <winioctl.h>
#include <QDir>
#endif
QString driveLetterToPhysicalDrive(const QString &driveLetter) {
#ifdef Q_OS_WIN
// Expect something like "G:/" or "G:"
QString driveRoot = driveLetter.left(2); // "G:"
QString volumePath = QString("\\\\.\\%1").arg(driveRoot);
HANDLE hDevice = CreateFileW((LPCWSTR)volumePath.utf16(),
0, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL);
if (hDevice == INVALID_HANDLE_VALUE) {
return QString(); // Failed
}
STORAGE_DEVICE_NUMBER sdn;
DWORD bytesReturned = 0;
bool success = DeviceIoControl(hDevice,
IOCTL_STORAGE_GET_DEVICE_NUMBER,
NULL, 0,
&sdn, sizeof(sdn),
&bytesReturned, NULL);
CloseHandle(hDevice);
if (success) {
return QString("\\\\.\\PhysicalDrive%1").arg(sdn.DeviceNumber);
} else {
return QString();
}
#else
return driveLetter; // Non-Windows fallback
#endif
}
QString getSystemDriveLetter() {
#ifdef Q_OS_WIN
wchar_t sysPath[MAX_PATH];
GetSystemWindowsDirectoryW(sysPath, MAX_PATH);
QString sysDir = QString::fromWCharArray(sysPath);
return sysDir.left(3).toUpper(); // Example: "C:/"
#else
return "/"; // Unix-based fallback
#endif
}
bool isSystemDrive(const QString &path) {
#ifdef Q_OS_WIN
QString normalized = QDir::toNativeSeparators(path).toUpper();
QString sysDrive = getSystemDriveLetter();
return normalized.startsWith(sysDrive);
#else
return false; // On Linux/macOS, scanning root "/" isn't blocked here
#endif
}