UI: DriveListManager: Filter mounted drives

This commit is contained in:
Nodir Temirkhodjaev 2024-01-09 14:33:36 +03:00
parent 0fac4b1b84
commit 181d211d5f
8 changed files with 58 additions and 5 deletions

View File

@ -114,7 +114,7 @@ TEST_F(LogReaderTest, logRead)
LogBuffer buf(DriverCommon::bufferSize());
for (;;) {
int nr;
qsizetype nr;
QByteArray &array = buf.array();
ASSERT_TRUE(device.ioctl(
DriverCommon::ioctlGetLog(), nullptr, 0, array.data(), array.size(), &nr));

View File

@ -110,7 +110,7 @@ void DriverWorker::readLog()
return;
QByteArray &array = m_logBuffer->array();
int nr;
qsizetype nr;
const bool success = m_device->ioctl(
DriverCommon::ioctlGetLog(), nullptr, 0, array.data(), array.size(), &nr);

View File

@ -18,7 +18,10 @@ void DriveListManager::initialize()
void DriveListManager::onDriveListChanged()
{
const quint32 driveMask = FileUtil::driveMask();
quint32 driveMask = FileUtil::driveMask();
if (m_checkMounted) {
driveMask = FileUtil::mountedDriveMask(driveMask);
}
if (m_driveMask == driveMask)
return;
@ -35,6 +38,11 @@ void DriveListManager::onDriveListChanged()
void DriveListManager::startPolling()
{
if (m_checkMounted)
return;
m_checkMounted = true;
setupPollingTimer();
m_pollingTimer->start();

View File

@ -28,6 +28,8 @@ private:
void setupPollingTimer();
private:
bool m_checkMounted = false;
quint32 m_driveMask = 0;
QTimer *m_pollingTimer = nullptr;

View File

@ -49,7 +49,7 @@ bool Device::cancelIo()
return CancelIoEx(m_handle, nullptr);
}
bool Device::ioctl(quint32 code, char *in, int inSize, char *out, int outSize, int *retSize)
bool Device::ioctl(quint32 code, char *in, int inSize, char *out, int outSize, qsizetype *retSize)
{
LPOVERLAPPED overlapped = isOverlapped() ? (LPOVERLAPPED) m_buffer.data() : nullptr;

View File

@ -33,7 +33,7 @@ public slots:
bool cancelIo();
bool ioctl(quint32 code, char *in = nullptr, int inSize = 0, char *out = nullptr,
int outSize = 0, int *retSize = nullptr);
int outSize = 0, qsizetype *retSize = nullptr);
void initOverlapped(void *eventHandle = nullptr);

View File

@ -8,6 +8,7 @@
#define WIN32_LEAN_AND_MEAN
#include <qt_windows.h>
#include <winioctl.h>
namespace FileUtil {
@ -53,6 +54,46 @@ quint32 driveMask()
return GetLogicalDrives();
}
static bool isDriveMounted(WCHAR drive)
{
const WCHAR volume[] = { L'\\', L'\\', L'.', L'\\', drive, L':', L'\0' };
const DWORD shareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
const HANDLE volumeHandle =
CreateFileW(volume, GENERIC_READ, shareMode, nullptr, OPEN_EXISTING, 0, nullptr);
if (volumeHandle == INVALID_HANDLE_VALUE)
return false;
DWORD nr;
const bool ok = DeviceIoControl(
volumeHandle, FSCTL_IS_VOLUME_MOUNTED, nullptr, 0, nullptr, 0, &nr, nullptr);
CloseHandle(volumeHandle);
return ok;
}
quint32 mountedDriveMask(quint32 driveMask)
{
quint32 mask = driveMask;
while (mask != 0) {
unsigned long index;
if (!_BitScanForward(&index, mask))
break;
const quint32 bit = (1u << index);
if (!isDriveMounted(L'A' + index)) {
driveMask ^= bit;
}
mask ^= bit;
}
return driveMask;
}
quint32 driveMaskByPath(const QString &path)
{
if (!isDriveFilePath(path))

View File

@ -17,6 +17,8 @@ bool isDriveFilePath(const QString &path);
quint32 driveMask();
quint32 mountedDriveMask(quint32 driveMask);
quint32 driveMaskByPath(const QString &path);
// Convert DOS device name to drive letter (A: .. Z:)