我使用DeviceIoControl
来返回物理磁盘扇区的大小。一直以来它都返回512字节,直到最近开始返回4096字节。检查结果中的STORAGE_ACCESS_ALIGNMENT_DESCRIPTOR
,我发现逻辑字节大小和物理字节大小已经交换了位置 - 磁盘扇区的逻辑字节大小不应该始终大于或等于物理扇区大小吗?
#include <Windows.h>
#include <iostream>
#include <Windows.h>
#pragma comment(lib, "Kernel32.lib")
int main()
{
HANDLE hDevice;
char cDisk = 'c'; // Get metadata about the C:\ disk
// Build the logical drive path and get the drive device handle
std::wstring logicalDrive = L"\\\\.\\";
wchar_t drive[3];
drive[0] = cDisk;
drive[1] = L':';
drive[2] = L'\0';
logicalDrive.append(drive);
hDevice = CreateFile(
logicalDrive.c_str(),
0,
0,
NULL,
OPEN_EXISTING,
0,
NULL);
if (hDevice == INVALID_HANDLE_VALUE)
{
std::cerr << "Error\n";
return -1;
}
// Now that we have the device handle for the disk, let us get disk's metadata
DWORD outsize;
STORAGE_PROPERTY_QUERY storageQuery;
memset(&storageQuery, 0, sizeof(STORAGE_PROPERTY_QUERY));
storageQuery.PropertyId = StorageAccessAlignmentProperty;
storageQuery.QueryType = PropertyStandardQuery;
STORAGE_ACCESS_ALIGNMENT_DESCRIPTOR diskAlignment = {0};
memset(&diskAlignment, 0, sizeof(STORAGE_ACCESS_ALIGNMENT_DESCRIPTOR));
if (!DeviceIoControl(hDevice,
IOCTL_STORAGE_QUERY_PROPERTY,
&storageQuery,
sizeof(STORAGE_PROPERTY_QUERY),
&diskAlignment,
sizeof(STORAGE_ACCESS_ALIGNMENT_DESCRIPTOR),
&outsize,
NULL)
)
{
std::cerr << "Error\n";
return -1;
}
std::cout << "Physical sector size: " diskAlignment.BytesPerPhysicalSector << std::endl;
std::cout << "Logical sector size: " diskAlignment.BytesPerLogicalSector << std::endl;
return 0;
}
运行上述代码的结果如下:
Physical sector size: 4096
Logical sector size: 512
运行fsutil
将产生相同的意外结果。
C:\WINDOWS\system32>fsutil fsinfo ntfsinfo c:
NTFS Version : 3.1
LFS Version : 2.0
Number Sectors : 0x000000001741afff
Total Clusters : 0x0000000002e835ff
Free Clusters : 0x0000000000999d28
Total Reserved : 0x0000000000003260
Bytes Per Sector : 512
Bytes Per Physical Sector : 4096
Bytes Per Cluster : 4096
Bytes Per FileRecord Segment : 1024
Clusters Per FileRecord Segment : 0
我做错了什么?