ohmu
·
2026-10-15
1#include "diskio.h"
2#include "common/io.h"
3#include "common/windows/unicode.h"
4
5#include <windows.h>
6#include <winioctl.h>
7
8static bool detectPhysicalDisk(const wchar_t* szDevice, FFlist* result, FFDiskIOOptions* options) {
9 FF_AUTO_CLOSE_FD HANDLE hDevice = CreateFileW(szDevice, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
10 if (hDevice == INVALID_HANDLE_VALUE) {
11 return false;
12 }
13
14 DWORD retSize;
15 char sddBuffer[4096];
16 if (!DeviceIoControl(
17 hDevice,
18 IOCTL_STORAGE_QUERY_PROPERTY,
19 &(STORAGE_PROPERTY_QUERY) {
20 .PropertyId = StorageDeviceProperty,
21 .QueryType = PropertyStandardQuery,
22 },
23 sizeof(STORAGE_PROPERTY_QUERY),
24 &sddBuffer,
25 ARRAY_SIZE(sddBuffer),
26 &retSize,
27 nullptr) ||
28 retSize == 0) {
29 return true;
30 }
31
32 FFDiskIOResult* device = FF_LIST_ADD(FFDiskIOResult, *result);
33 STORAGE_DEVICE_DESCRIPTOR* sdd = (STORAGE_DEVICE_DESCRIPTOR*) sddBuffer;
34
35 ffStrbufInit(&device->name);
36 if (sdd->VendorIdOffset != 0) {
37 ffStrbufSetS(&device->name, (const char*) sddBuffer + sdd->VendorIdOffset);
38 ffStrbufTrim(&device->name, ' ');
39 }
40 if (sdd->ProductIdOffset != 0) {
41 if (device->name.length) {
42 ffStrbufAppendC(&device->name, ' ');
43 }
44
45 ffStrbufAppendS(&device->name, (const char*) sddBuffer + sdd->ProductIdOffset);
46 ffStrbufTrimRight(&device->name, ' ');
47 }
48
49 if (!device->name.length) {
50 ffStrbufSetWS(&device->name, szDevice);
51 }
52
53 if (options->namePrefix.length && !ffStrbufStartsWith(&device->name, &options->namePrefix)) {
54 ffStrbufDestroy(&device->name);
55 result->length--;
56 return true;
57 }
58
59 DISK_PERFORMANCE dp = {};
60 if (DeviceIoControl(hDevice, IOCTL_DISK_PERFORMANCE, nullptr, 0, &dp, sizeof(dp), &retSize, nullptr)) {
61 device->bytesRead = (uint64_t) dp.BytesRead.QuadPart;
62 device->readCount = (uint64_t) dp.ReadCount;
63 device->bytesWritten = (uint64_t) dp.BytesWritten.QuadPart;
64 device->writeCount = (uint64_t) dp.WriteCount;
65 } else {
66 ffStrbufDestroy(&device->name);
67 result->length--;
68 }
69
70 ffStrbufInitWS(&device->devPath, szDevice);
71
72 return true;
73}
74
75const char* ffDiskIOGetIoCounters(FFlist* result, FFDiskIOOptions* options) {
76 {
77 wchar_t szPhysicalDrive[32] = L"\\\\.\\PhysicalDrive";
78 wchar_t* pNum = szPhysicalDrive + strlen("\\\\.\\PhysicalDrive");
79 for (uint32_t idev = 0;; ++idev) {
80 _ultow(idev, pNum, 10);
81
82 if (!detectPhysicalDisk(szPhysicalDrive, result, options)) {
83 break;
84 }
85 }
86 }
87
88 {
89 wchar_t szCdrom[32] = L"\\\\.\\CDROM";
90 wchar_t* pNum = szCdrom + strlen("\\\\.\\CDROM");
91 for (uint32_t idev = 0;; ++idev) {
92 _ultow(idev, pNum, 10);
93
94 if (!detectPhysicalDisk(szCdrom, result, options)) {
95 break;
96 }
97 }
98 }
99
100 return nullptr;
101}