Browse Source

Add isNetworkFileSystem() detection on Windows

This allows network mounts to be monitored correctly by polling timer.
adaptive-webui-19844
Chocobo1 6 years ago
parent
commit
cff5af2e76
No known key found for this signature in database
GPG Key ID: 210D9C873253A68C
  1. 11
      src/base/filesystemwatcher.cpp
  2. 4
      src/base/filesystemwatcher.h
  3. 16
      src/base/utils/fs.cpp
  4. 2
      src/base/utils/fs.h

11
src/base/filesystemwatcher.cpp

@ -58,18 +58,14 @@ FileSystemWatcher::FileSystemWatcher(QObject *parent) @@ -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) @@ -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) @@ -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) @@ -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()
{

4
src/base/filesystemwatcher.h

@ -56,9 +56,7 @@ signals: @@ -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: @@ -67,10 +65,8 @@ private:
QHash<QString, int> m_partialTorrents;
QTimer m_partialTorrentTimer;
#ifndef Q_OS_WIN
QList<QDir> m_watchedFolders;
QTimer m_watchTimer;
#endif
};
#endif // FILESYSTEMWATCHER_H

16
src/base/utils/fs.cpp

@ -30,6 +30,10 @@ @@ -30,6 +30,10 @@
#include <cstring>
#if defined(Q_OS_WIN)
#include <memory>
#endif
#include <QCoreApplication>
#include <QDebug>
#include <QDir>
@ -301,9 +305,17 @@ bool Utils::Fs::isRegularFile(const QString &path) @@ -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<wchar_t[]> 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) @@ -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) @@ -332,5 +343,6 @@ bool Utils::Fs::isNetworkFileSystem(const QString &path)
return false;
}
#endif
#endif
}
#endif

2
src/base/utils/fs.h

@ -62,7 +62,7 @@ namespace Utils @@ -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
}

Loading…
Cancel
Save