diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2017-06-13 11:45:59 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2017-07-09 11:07:38 +0000 |
commit | 1bc5f619ea1cae8cfec2ac53a8206ceb28845c8f (patch) | |
tree | b1bd5ff5b061abd56fedd8ee3e17286dd0070b05 | |
parent | 6e861d8412b28472571d675e6a600f4a6dcbb4b7 (diff) |
QStorageInfo/Windows: Improve error handling
Bail out of QStorageInfoPrivate::doStats() should an error occur
and set the ready/valid flags accordingly.
Task-number: QTBUG-6039
Change-Id: Id5354b31329d951599ae991aa7edde0515c90514
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
-rw-r--r-- | src/corelib/io/qstorageinfo_win.cpp | 52 |
1 files changed, 34 insertions, 18 deletions
diff --git a/src/corelib/io/qstorageinfo_win.cpp b/src/corelib/io/qstorageinfo_win.cpp index f02e46f095..3830c5480c 100644 --- a/src/corelib/io/qstorageinfo_win.cpp +++ b/src/corelib/io/qstorageinfo_win.cpp @@ -43,36 +43,52 @@ #include <QtCore/qfileinfo.h> #include <QtCore/qvarlengtharray.h> +#include "qfilesystementry_p.h" + #include <qt_windows.h> QT_BEGIN_NAMESPACE static const int defaultBufferSize = MAX_PATH + 1; -void QStorageInfoPrivate::initRootPath() +static QString canonicalPath(const QString &rootPath) { - rootPath = QFileInfo(rootPath).canonicalFilePath(); - - if (rootPath.isEmpty()) - return; - - QString path = QDir::toNativeSeparators(rootPath); - rootPath.clear(); + QString path = QDir::toNativeSeparators(QFileInfo(rootPath).canonicalFilePath()); + if (path.isEmpty()) + return path; if (path.startsWith(QLatin1String("\\\\?\\"))) path.remove(0, 4); if (path.length() < 2 || path.at(1) != QLatin1Char(':')) - return; + return QString(); + path[0] = path[0].toUpper(); if (!(path.at(0).unicode() >= 'A' && path.at(0).unicode() <= 'Z')) - return; + return QString(); if (!path.endsWith(QLatin1Char('\\'))) path.append(QLatin1Char('\\')); + return path; +} + +void QStorageInfoPrivate::initRootPath() +{ + // Do not unnecessarily call QFileInfo::canonicalFilePath() if the path is + // already a drive root since it may hang on network drives. + const QString path = QFileSystemEntry::isDriveRootPath(rootPath) + ? QDir::toNativeSeparators(rootPath) + : canonicalPath(rootPath); + + if (path.isEmpty()) { + valid = ready = false; + return; + } // ### test if disk mounted to folder on other disk wchar_t buffer[defaultBufferSize]; if (::GetVolumePathName(reinterpret_cast<const wchar_t *>(path.utf16()), buffer, defaultBufferSize)) rootPath = QDir::fromNativeSeparators(QString::fromWCharArray(buffer)); + else + valid = ready = false; } static inline QByteArray getDevice(const QString &rootPath) @@ -108,11 +124,14 @@ static inline QByteArray getDevice(const QString &rootPath) void QStorageInfoPrivate::doStat() { + valid = ready = true; initRootPath(); - if (rootPath.isEmpty()) + if (!valid || !ready) return; retrieveVolumeInfo(); + if (!valid || !ready) + return; device = getDevice(rootPath); retrieveDiskFreeSpace(); } @@ -137,9 +156,6 @@ void QStorageInfoPrivate::retrieveVolumeInfo() ready = false; valid = ::GetLastError() == ERROR_NOT_READY; } else { - ready = true; - valid = true; - fileSystemType = QString::fromWCharArray(fileSystemTypeBuffer).toLatin1(); name = QString::fromWCharArray(nameBuffer); @@ -154,10 +170,10 @@ void QStorageInfoPrivate::retrieveDiskFreeSpace() const UINT oldmode = ::SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX); const QString path = QDir::toNativeSeparators(rootPath); - ::GetDiskFreeSpaceEx(reinterpret_cast<const wchar_t *>(path.utf16()), - PULARGE_INTEGER(&bytesAvailable), - PULARGE_INTEGER(&bytesTotal), - PULARGE_INTEGER(&bytesFree)); + ready = ::GetDiskFreeSpaceEx(reinterpret_cast<const wchar_t *>(path.utf16()), + PULARGE_INTEGER(&bytesAvailable), + PULARGE_INTEGER(&bytesTotal), + PULARGE_INTEGER(&bytesFree)); ::SetErrorMode(oldmode); } |