From cff5af2e7653672c7855cb6eb2cc808806422bb6 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Fri, 12 Oct 2018 00:00:48 +0800 Subject: [PATCH] Add isNetworkFileSystem() detection on Windows This allows network mounts to be monitored correctly by polling timer. --- src/base/filesystemwatcher.cpp | 11 ++--------- src/base/filesystemwatcher.h | 4 ---- src/base/utils/fs.cpp | 16 ++++++++++++++-- src/base/utils/fs.h | 2 +- 4 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/base/filesystemwatcher.cpp b/src/base/filesystemwatcher.cpp index 550a7f6a0..4c6622d5a 100644 --- a/src/base/filesystemwatcher.cpp +++ b/src/base/filesystemwatcher.cpp @@ -58,18 +58,14 @@ FileSystemWatcher::FileSystemWatcher(QObject *parent) m_partialTorrentTimer.setSingleShot(true); connect(&m_partialTorrentTimer, &QTimer::timeout, this, &FileSystemWatcher::processPartialTorrents); -#ifndef Q_OS_WIN connect(&m_watchTimer, &QTimer::timeout, this, &FileSystemWatcher::scanNetworkFolders); -#endif } QStringList FileSystemWatcher::directories() const { QStringList dirs = QFileSystemWatcher::directories(); -#ifndef Q_OS_WIN for (const QDir &dir : qAsConst(m_watchedFolders)) dirs << dir.canonicalPath(); -#endif return dirs; } @@ -77,7 +73,7 @@ void FileSystemWatcher::addPath(const QString &path) { if (path.isEmpty()) return; -#if !defined Q_OS_WIN && !defined Q_OS_HAIKU +#if !defined Q_OS_HAIKU const QDir dir(path); if (!dir.exists()) return; @@ -100,13 +96,12 @@ void FileSystemWatcher::addPath(const QString &path) void FileSystemWatcher::removePath(const QString &path) { -#ifndef Q_OS_WIN if (m_watchedFolders.removeOne(path)) { if (m_watchedFolders.isEmpty()) m_watchTimer.stop(); return; } -#endif + // Normal mode QFileSystemWatcher::removePath(path); } @@ -116,13 +111,11 @@ void FileSystemWatcher::scanLocalFolder(const QString &path) QTimer::singleShot(2000, this, [this, path]() { processTorrentsInDir(path); }); } -#ifndef Q_OS_WIN void FileSystemWatcher::scanNetworkFolders() { for (const QDir &dir : qAsConst(m_watchedFolders)) processTorrentsInDir(dir); } -#endif void FileSystemWatcher::processPartialTorrents() { diff --git a/src/base/filesystemwatcher.h b/src/base/filesystemwatcher.h index 5079659f6..ac17d7124 100644 --- a/src/base/filesystemwatcher.h +++ b/src/base/filesystemwatcher.h @@ -56,9 +56,7 @@ signals: protected slots: void scanLocalFolder(const QString &path); void processPartialTorrents(); -#ifndef Q_OS_WIN void scanNetworkFolders(); -#endif private: void processTorrentsInDir(const QDir &dir); @@ -67,10 +65,8 @@ private: QHash m_partialTorrents; QTimer m_partialTorrentTimer; -#ifndef Q_OS_WIN QList m_watchedFolders; QTimer m_watchTimer; -#endif }; #endif // FILESYSTEMWATCHER_H diff --git a/src/base/utils/fs.cpp b/src/base/utils/fs.cpp index e4d361795..b44323cc3 100644 --- a/src/base/utils/fs.cpp +++ b/src/base/utils/fs.cpp @@ -30,6 +30,10 @@ #include +#if defined(Q_OS_WIN) +#include +#endif + #include #include #include @@ -301,9 +305,17 @@ bool Utils::Fs::isRegularFile(const QString &path) return (st.st_mode & S_IFMT) == S_IFREG; } -#if !defined Q_OS_WIN && !defined Q_OS_HAIKU +#if !defined Q_OS_HAIKU bool Utils::Fs::isNetworkFileSystem(const QString &path) { +#if defined(Q_OS_WIN) + const std::wstring pathW {path.toStdWString()}; + std::unique_ptr volumePath {new wchar_t[path.length() + 1] {}}; + if (!::GetVolumePathNameW(pathW.c_str(), volumePath.get(), (path.length() + 1))) + return false; + + return (::GetDriveTypeW(volumePath.get()) == DRIVE_REMOTE); +#else QString file = path; if (!file.endsWith('/')) file += '/'; @@ -312,7 +324,6 @@ bool Utils::Fs::isNetworkFileSystem(const QString &path) struct statfs buf {}; if (statfs(file.toLocal8Bit().constData(), &buf) != 0) return false; - #if defined(Q_OS_MAC) || defined(Q_OS_OPENBSD) // XXX: should we make sure HAVE_STRUCT_FSSTAT_F_FSTYPENAME is defined? return ((strncmp(buf.f_fstypename, "cifs", sizeof(buf.f_fstypename)) == 0) @@ -332,5 +343,6 @@ bool Utils::Fs::isNetworkFileSystem(const QString &path) return false; } #endif +#endif } #endif diff --git a/src/base/utils/fs.h b/src/base/utils/fs.h index 08cdc29db..132ed6fbd 100644 --- a/src/base/utils/fs.h +++ b/src/base/utils/fs.h @@ -62,7 +62,7 @@ namespace Utils QString tempPath(); -#if !defined Q_OS_WIN && !defined Q_OS_HAIKU +#if !defined Q_OS_HAIKU bool isNetworkFileSystem(const QString &path); #endif }