MeasureDiskSpace: Improved total size detection for removable device.

This commit is contained in:
spx 2010-11-28 21:39:46 +00:00
parent 46829cbb6f
commit 62519fbdd5
2 changed files with 79 additions and 82 deletions

View File

@ -459,6 +459,10 @@ double CMeasure::GetValue()
** **
*/ */
double CMeasure::GetRelativeValue() double CMeasure::GetRelativeValue()
{
double range = GetValueRange();
if (range != 0.0)
{ {
double value = GetValue(); double value = GetValue();
@ -467,7 +471,10 @@ double CMeasure::GetRelativeValue()
value -= m_MinValue; value -= m_MinValue;
return value / GetValueRange(); return value / range;
}
return 1.0;
} }
/* /*

View File

@ -58,88 +58,66 @@ bool CMeasureDiskSpace::Update()
if (!m_Drive.empty()) if (!m_Drive.empty())
{ {
BOOL sizeResult = FALSE, labelResult = FALSE;
ULARGE_INTEGER i64TotalBytes, i64FreeBytes;
WCHAR volumeName[MAX_PATH] = {0};
UINT type = GetDriveType(m_Drive.c_str()); UINT type = GetDriveType(m_Drive.c_str());
if (type != DRIVE_NO_ROOT_DIR) if (type != DRIVE_NO_ROOT_DIR)
{ {
if (type != DRIVE_CDROM && (!m_IgnoreRemovable || type != DRIVE_REMOVABLE)) // Ignore CD-ROMS and removable drives if (type != DRIVE_CDROM && (!m_IgnoreRemovable || type != DRIVE_REMOVABLE)) // Ignore CD-ROMS and removable drives
{ {
ULARGE_INTEGER i64FreeBytesToCaller, i64TotalBytes, i64FreeBytes; ULARGE_INTEGER i64FreeBytesToCaller;
UINT oldMode = SetErrorMode(0); UINT oldMode = SetErrorMode(0);
SetErrorMode(oldMode | SEM_FAILCRITICALERRORS); // Prevent the system from displaying message box SetErrorMode(oldMode | SEM_FAILCRITICALERRORS); // Prevent the system from displaying message box
SetLastError(ERROR_SUCCESS); SetLastError(ERROR_SUCCESS);
BOOL result = GetDiskFreeSpaceEx(m_Drive.c_str(), &i64FreeBytesToCaller, &i64TotalBytes, &i64FreeBytes); sizeResult = GetDiskFreeSpaceEx(m_Drive.c_str(), &i64FreeBytesToCaller, &i64TotalBytes, &i64FreeBytes);
DWORD err = GetLastError();
SetErrorMode(oldMode); // Reset SetErrorMode(oldMode); // Reset
if (result)
{
LARGE_INTEGER tmpInt;
if (m_Total)
{
tmpInt.QuadPart = i64TotalBytes.QuadPart;
}
else
{
tmpInt.QuadPart = i64FreeBytes.QuadPart;
}
m_Value = (double)tmpInt.QuadPart;
if (i64TotalBytes.QuadPart != m_OldTotalBytes)
{
// Total size was changed, so set new max value.
tmpInt.QuadPart = i64TotalBytes.QuadPart;
m_MaxValue = (double)tmpInt.QuadPart;
m_OldTotalBytes = i64TotalBytes.QuadPart;
}
}
else if (err == ERROR_NOT_READY)
{
// Media isn't ready, so reset to zero.
m_Value = 0;
m_OldTotalBytes = 0;
}
}
else
{
m_Value = 0;
m_OldTotalBytes = 0;
} }
if (m_Label) if (m_Label)
{ {
if (!m_IgnoreRemovable || type != DRIVE_REMOVABLE) // Ignore removable drives if (!m_IgnoreRemovable || type != DRIVE_REMOVABLE) // Ignore removable drives
{ {
WCHAR volumeName[MAX_PATH] = {0};
UINT oldMode = SetErrorMode(0); UINT oldMode = SetErrorMode(0);
SetErrorMode(oldMode | SEM_FAILCRITICALERRORS); // Prevent the system from displaying message box SetErrorMode(oldMode | SEM_FAILCRITICALERRORS); // Prevent the system from displaying message box
BOOL result = GetVolumeInformation(m_Drive.c_str(), volumeName, MAX_PATH, NULL, NULL, NULL, NULL, 0); labelResult = GetVolumeInformation(m_Drive.c_str(), volumeName, MAX_PATH, NULL, NULL, NULL, NULL, 0);
SetErrorMode(oldMode); // Reset SetErrorMode(oldMode); // Reset
}
}
}
if (result) if (sizeResult)
{
m_Value = (double)((m_Total) ? i64TotalBytes.QuadPart : i64FreeBytes.QuadPart);
if (i64TotalBytes.QuadPart != m_OldTotalBytes)
{
// Total size was changed, so set new max value.
m_MaxValue = (double)i64TotalBytes.QuadPart;
m_OldTotalBytes = i64TotalBytes.QuadPart;
}
}
else
{
m_Value = 0.0;
m_MaxValue = 0.0;
m_OldTotalBytes = 0;
}
if (labelResult)
{ {
m_LabelName = volumeName; m_LabelName = volumeName;
} }
else else
{ {
m_LabelName = L""; if (m_Label || !m_LabelName.empty())
}
}
else
{ {
m_LabelName = L""; m_LabelName = L"";
} }
} }
} }
else // Drive path is invalid.
{
m_Value = 0;
m_OldTotalBytes = 0;
if (m_Label) m_LabelName = L"";
}
}
return PostUpdate(); return PostUpdate();
} }
@ -168,13 +146,17 @@ const WCHAR* CMeasureDiskSpace::GetStringValue(bool autoScale, double scale, int
*/ */
void CMeasureDiskSpace::ReadConfig(CConfigParser& parser, const WCHAR* section) void CMeasureDiskSpace::ReadConfig(CConfigParser& parser, const WCHAR* section)
{ {
double oldMaxValue = m_MaxValue;
CMeasure::ReadConfig(parser, section); CMeasure::ReadConfig(parser, section);
m_Drive = parser.ReadString(section, L"Drive", L"C:\\"); m_Drive = parser.ReadString(section, L"Drive", L"C:\\");
if (m_Drive.empty()) if (m_Drive.empty())
{ {
LSLog(LOG_DEBUG, APPNAME, L"Drive path is not given."); LSLog(LOG_DEBUG, APPNAME, L"Drive path is not given.");
m_Value = 0; m_Value = 0.0;
m_MaxValue = 0.0;
m_OldTotalBytes = 0;
m_LabelName = L""; m_LabelName = L"";
} }
else if (m_Drive[m_Drive.length() - 1] != L'\\') // E.g. "C:" else if (m_Drive[m_Drive.length() - 1] != L'\\') // E.g. "C:"
@ -187,31 +169,39 @@ void CMeasureDiskSpace::ReadConfig(CConfigParser& parser, const WCHAR* section)
m_IgnoreRemovable = (1 == parser.ReadInt(section, L"IgnoreRemovable", 1)); m_IgnoreRemovable = (1 == parser.ReadInt(section, L"IgnoreRemovable", 1));
// Set the m_MaxValue // Set the m_MaxValue
if (!m_Initialized)
{
BOOL result = FALSE;
ULARGE_INTEGER i64TotalBytes;
if (!m_Drive.empty()) if (!m_Drive.empty())
{ {
UINT type = GetDriveType(m_Drive.c_str()); UINT type = GetDriveType(m_Drive.c_str());
if (type != DRIVE_NO_ROOT_DIR && if (type != DRIVE_NO_ROOT_DIR &&
type != DRIVE_CDROM && (!m_IgnoreRemovable || type != DRIVE_REMOVABLE)) // Ignore CD-ROMS and removable drives type != DRIVE_CDROM && (!m_IgnoreRemovable || type != DRIVE_REMOVABLE)) // Ignore CD-ROMS and removable drives
{ {
ULARGE_INTEGER i64FreeBytesToCaller, i64TotalBytes, i64FreeBytes; ULARGE_INTEGER i64FreeBytesToCaller, i64FreeBytes;
UINT oldMode = SetErrorMode(0); UINT oldMode = SetErrorMode(0);
SetErrorMode(oldMode | SEM_FAILCRITICALERRORS); // Prevent the system from displaying message box SetErrorMode(oldMode | SEM_FAILCRITICALERRORS); // Prevent the system from displaying message box
BOOL result = GetDiskFreeSpaceEx(m_Drive.c_str(), &i64FreeBytesToCaller, &i64TotalBytes, &i64FreeBytes); result = GetDiskFreeSpaceEx(m_Drive.c_str(), &i64FreeBytesToCaller, &i64TotalBytes, &i64FreeBytes);
SetErrorMode(oldMode); // Reset SetErrorMode(oldMode); // Reset
}
}
if (result) if (result)
{ {
LARGE_INTEGER tmpInt; m_MaxValue = (double)i64TotalBytes.QuadPart;
tmpInt.QuadPart = i64TotalBytes.QuadPart;
m_MaxValue = (double)tmpInt.QuadPart;
m_OldTotalBytes = i64TotalBytes.QuadPart; m_OldTotalBytes = i64TotalBytes.QuadPart;
} }
else else
{ {
m_MaxValue = 0.0;
m_OldTotalBytes = 0; m_OldTotalBytes = 0;
} }
} }
else
{
m_MaxValue = oldMaxValue;
} }
} }