Checking is a drive is readable in Qt pops up a "no disk" error in Windows 7
-
Here is a snippet of code:
@
QStringList getAvailableDrives()
{
QStringList drives;
QFileInfoList fiList = QDir::drives();
bool firstRemovableDone = false;QString path; foreach( const QFileInfo& fi, fiList ) { path = fi.absoluteFilePath(); if ( !fi.exists() ) { continue; } if ( !QFile::exists( path ) ) { continue; } if ( !fi.isReadable() ) { continue; } // ...
@
The issue is I am in a corporate setting with network drives, mapped drives, whatever. The QDir::drives() is returning all my normal drives, but is also returning "R:/", "S:/", "T:/" and "X:/". I can't access these drives and have no idea where they are coming from. But, fi.exists(), QFile::exists() both say they agree. Then it falls down to fi.isReadable(). In the past (4.7, 4.8) this returned false. But, 5.1.1 is doing something different and Windows 7 is popping up a dialog that requires User input. I don't want this if I am checking if the drive is readable.
The dialog is like this:
There is no disk in the drive. Please insert a disk into drive \Device\Harddisk3\DR8.
[Cancel] [Try Again] [Continue]This comes from line 791 of qfilesystemengine_win.cpp in the ::_waccess call:
@ // calculate user permissions
if (what & QFileSystemMetaData::UserReadPermission) {
if (::_waccess((wchar_t*)entry.nativeFilePath().utf16(), R_OK) == 0)
data.entryFlags |= QFileSystemMetaData::UserReadPermission;
data.knownFlagsMask |= QFileSystemMetaData::UserReadPermission;
}
@So far, I have been able to work-around this by calling the following code before the fi.isReadable():
@
bool isValidDrive( const QString& path )
{
bool valid = true; // default#ifdef Q_OS_WIN
LPCWSTR szHD = (LPCWSTR)path.utf16();
UCHAR szFileSys[255], szVolNameBuff[255];
DWORD dwSerial, dwMFL, dwSysFlags;
BOOL bSuccess;bSuccess = ::GetVolumeInformationW( szHD, (LPWSTR)szVolNameBuff, 255, &dwSerial, &dwMFL, &dwSysFlags, (LPWSTR)szFileSys, 255 ); valid = bSuccess; if ( !bSuccess ) { qDebug() << "Error number " << ::GetLastError(); return false; }
#endif // Q_OS_WIN
return valid;
}
@But I am looking for a more OS independent way of checking. Suggestions?