Skip to content

Instantly share code, notes, and snippets.

@altexy
Last active May 27, 2016 10:47
Show Gist options
  • Save altexy/b2e2330b103477c8f781e312c0b8a733 to your computer and use it in GitHub Desktop.
Save altexy/b2e2330b103477c8f781e312c0b8a733 to your computer and use it in GitHub Desktop.
UInt32 Resource::GetDiskInfoEx(PVoid buffer, UInt32 bufferSize, LPDWORD bytesReturned)
{
if (nullptr == buffer)
{
LogEvent(LOG_ERROR, L"Resource::GetDiskInfoEx: null buffer error: 0x%x", ERROR_INVALID_PARAMETER);
return ERROR_INVALID_PARAMETER;
}
struct StorageValueList
{
CLUSPROP_VALUE sig;
GUID guid;
CLUSPROP_SCSI_ADDRESS scsiAddress;
CLUSPROP_DISK_NUMBER diskNumber;
CLUSPROP_PARTITION_INFO_EX partitionInfo;
CLUSPROP_SYNTAX endmark;
};
*bytesReturned = sizeof(StorageValueList);
if (bufferSize < sizeof(StorageValueList))
{
return ERROR_MORE_DATA;
}
::ZeroMemory(buffer, bufferSize);
auto storage = static_cast<StorageValueList*>(buffer);
// CLUSPROP_DISK_SIGNATURE
storage->sig.Syntax.dw = CLUSPROP_SYNTAX_DISK_GUID;
storage->sig.cbLength = sizeof(GUID);
storage->guid = m_diskInfo.GetVolumeGuid();
// CLUSPROP_SCSI_ADDRESS
const auto& scsiAddress = m_diskInfo.GetScsiAddress();
storage->scsiAddress.Syntax.dw = CLUSPROP_SYNTAX_SCSI_ADDRESS;
storage->scsiAddress.Lun = scsiAddress.Lun;
storage->scsiAddress.PathId = scsiAddress.PathId;
storage->scsiAddress.PortNumber = scsiAddress.PortNumber;
storage->scsiAddress.TargetId = scsiAddress.TargetId;
storage->scsiAddress.cbLength = sizeof(storage->scsiAddress.dw);
// CLUSPROP_DISK_NUMBER
storage->diskNumber.Syntax.dw = CLUSPROP_SYNTAX_DISK_NUMBER;
// TODO: Is this correct value for
storage->diskNumber.dw = m_diskDeviceNumber.load();
storage->diskNumber.cbLength = sizeof(storage->diskNumber.dw);
// CLUSPROP_PARTITION_INFO_EX
const auto& props = m_properties.Get();
static const DWORD bufSize = MAX_PATH;
TChar volumeName[bufSize] = { 0 };
DWORD volumeSerialNumber = 0;
DWORD maximumComponentLength = 0;
DWORD fileSystemFlags;
static const DWORD fileSystemNameBufferSize = sizeof(storage->partitionInfo.szFileSystem);
TChar fileSystemName[fileSystemNameBufferSize] = { 0 };
if (!GetVolumeInformation(props.MountPoint, volumeName, bufSize, &volumeSerialNumber, &maximumComponentLength, &fileSystemFlags, fileSystemName, fileSystemNameBufferSize))
{
const auto error = ::GetLastError();
LogEvent(LOG_ERROR, L"Resource::GetDiskInfoEx: GetVolumeInformation error: 0x%x", error);
return error;
}
LogEvent(LOG_INFORMATION, L"Resource::GetDiskInfoEx: volume information: name %s, serial number 0x%x, maximum component length %d, file system flags 0x%x, file system %s",
volumeName, volumeSerialNumber, maximumComponentLength, fileSystemFlags, fileSystemName);
ULARGE_INTEGER totalNumberOfBytes = { 0 };
ULARGE_INTEGER totalNumberOfFreeBytes = { 0 };
//GetDiskFreeSpaceEx
storage->partitionInfo.Syntax.dw = CLUSPROP_SYNTAX_PARTITION_INFO_EX;
storage->partitionInfo.dwFlags = CLUSPROP_PIFLAG_STICKY | CLUSPROP_PIFLAG_USABLE | CLUSPROP_PIFLAG_DEFAULT_QUORUM;
// TODO: remove backslash
wcscpy_s(storage->partitionInfo.szDeviceName, MAX_PATH, props.MountPoint);
wcscpy_s(storage->partitionInfo.szVolumeLabel, MAX_PATH, volumeName);
storage->partitionInfo.dwSerialNumber = volumeSerialNumber;
storage->partitionInfo.rgdwMaximumComponentLength = maximumComponentLength;
storage->partitionInfo.dwFileSystemFlags = fileSystemFlags;
wcscpy_s(storage->partitionInfo.szFileSystem, fileSystemNameBufferSize, fileSystemName);
storage->partitionInfo.TotalSizeInBytes = totalNumberOfBytes;
storage->partitionInfo.FreeSizeInBytes = totalNumberOfFreeBytes;
// TODO: Is this correct value for
storage->partitionInfo.DeviceNumber = m_diskDeviceNumber.load();
storage->partitionInfo.PartitionNumber = m_diskInfo.GetPartitionNumber();
storage->partitionInfo.VolumeGuid = m_diskInfo.GetVolumeGuid();
storage->partitionInfo.cbLength = sizeof(CLUS_PARTITION_INFO_EX);
// CLUSPROP_SYNTAX
storage->endmark.dw = CLUSPROP_SYNTAX_ENDMARK;
return ERROR_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment