Browse Source

Merge pull request #9683 from Chocobo1/win_fs

Add isNetworkFileSystem() detection on Windows
adaptive-webui-19844
Mike Tzou 6 years ago committed by GitHub
parent
commit
18a64f109d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      src/base/filesystemwatcher.cpp
  2. 4
      src/base/filesystemwatcher.h
  3. 24
      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)
m_partialTorrentTimer.setSingleShot(true); m_partialTorrentTimer.setSingleShot(true);
connect(&m_partialTorrentTimer, &QTimer::timeout, this, &FileSystemWatcher::processPartialTorrents); connect(&m_partialTorrentTimer, &QTimer::timeout, this, &FileSystemWatcher::processPartialTorrents);
#ifndef Q_OS_WIN
connect(&m_watchTimer, &QTimer::timeout, this, &FileSystemWatcher::scanNetworkFolders); connect(&m_watchTimer, &QTimer::timeout, this, &FileSystemWatcher::scanNetworkFolders);
#endif
} }
QStringList FileSystemWatcher::directories() const QStringList FileSystemWatcher::directories() const
{ {
QStringList dirs = QFileSystemWatcher::directories(); QStringList dirs = QFileSystemWatcher::directories();
#ifndef Q_OS_WIN
for (const QDir &dir : qAsConst(m_watchedFolders)) for (const QDir &dir : qAsConst(m_watchedFolders))
dirs << dir.canonicalPath(); dirs << dir.canonicalPath();
#endif
return dirs; return dirs;
} }
@ -77,7 +73,7 @@ void FileSystemWatcher::addPath(const QString &path)
{ {
if (path.isEmpty()) return; if (path.isEmpty()) return;
#if !defined Q_OS_WIN && !defined Q_OS_HAIKU #if !defined Q_OS_HAIKU
const QDir dir(path); const QDir dir(path);
if (!dir.exists()) return; if (!dir.exists()) return;
@ -100,13 +96,12 @@ void FileSystemWatcher::addPath(const QString &path)
void FileSystemWatcher::removePath(const QString &path) void FileSystemWatcher::removePath(const QString &path)
{ {
#ifndef Q_OS_WIN
if (m_watchedFolders.removeOne(path)) { if (m_watchedFolders.removeOne(path)) {
if (m_watchedFolders.isEmpty()) if (m_watchedFolders.isEmpty())
m_watchTimer.stop(); m_watchTimer.stop();
return; return;
} }
#endif
// Normal mode // Normal mode
QFileSystemWatcher::removePath(path); QFileSystemWatcher::removePath(path);
} }
@ -116,13 +111,11 @@ void FileSystemWatcher::scanLocalFolder(const QString &path)
QTimer::singleShot(2000, this, [this, path]() { processTorrentsInDir(path); }); QTimer::singleShot(2000, this, [this, path]() { processTorrentsInDir(path); });
} }
#ifndef Q_OS_WIN
void FileSystemWatcher::scanNetworkFolders() void FileSystemWatcher::scanNetworkFolders()
{ {
for (const QDir &dir : qAsConst(m_watchedFolders)) for (const QDir &dir : qAsConst(m_watchedFolders))
processTorrentsInDir(dir); processTorrentsInDir(dir);
} }
#endif
void FileSystemWatcher::processPartialTorrents() void FileSystemWatcher::processPartialTorrents()
{ {

4
src/base/filesystemwatcher.h

@ -56,9 +56,7 @@ signals:
protected slots: protected slots:
void scanLocalFolder(const QString &path); void scanLocalFolder(const QString &path);
void processPartialTorrents(); void processPartialTorrents();
#ifndef Q_OS_WIN
void scanNetworkFolders(); void scanNetworkFolders();
#endif
private: private:
void processTorrentsInDir(const QDir &dir); void processTorrentsInDir(const QDir &dir);
@ -67,10 +65,8 @@ private:
QHash<QString, int> m_partialTorrents; QHash<QString, int> m_partialTorrents;
QTimer m_partialTorrentTimer; QTimer m_partialTorrentTimer;
#ifndef Q_OS_WIN
QList<QDir> m_watchedFolders; QList<QDir> m_watchedFolders;
QTimer m_watchTimer; QTimer m_watchTimer;
#endif
}; };
#endif // FILESYSTEMWATCHER_H #endif // FILESYSTEMWATCHER_H

24
src/base/utils/fs.cpp

@ -30,6 +30,10 @@
#include <cstring> #include <cstring>
#if defined(Q_OS_WIN)
#include <memory>
#endif
#include <QCoreApplication> #include <QCoreApplication>
#include <QDebug> #include <QDebug>
#include <QDir> #include <QDir>
@ -301,9 +305,17 @@ bool Utils::Fs::isRegularFile(const QString &path)
return (st.st_mode & S_IFMT) == S_IFREG; 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) 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);
#elif defined(Q_OS_MAC) || defined(Q_OS_OPENBSD)
QString file = path; QString file = path;
if (!file.endsWith('/')) if (!file.endsWith('/'))
file += '/'; file += '/';
@ -313,12 +325,20 @@ bool Utils::Fs::isNetworkFileSystem(const QString &path)
if (statfs(file.toLocal8Bit().constData(), &buf) != 0) if (statfs(file.toLocal8Bit().constData(), &buf) != 0)
return false; return false;
#if defined(Q_OS_MAC) || defined(Q_OS_OPENBSD)
// XXX: should we make sure HAVE_STRUCT_FSSTAT_F_FSTYPENAME is defined? // XXX: should we make sure HAVE_STRUCT_FSSTAT_F_FSTYPENAME is defined?
return ((strncmp(buf.f_fstypename, "cifs", sizeof(buf.f_fstypename)) == 0) return ((strncmp(buf.f_fstypename, "cifs", sizeof(buf.f_fstypename)) == 0)
|| (strncmp(buf.f_fstypename, "nfs", sizeof(buf.f_fstypename)) == 0) || (strncmp(buf.f_fstypename, "nfs", sizeof(buf.f_fstypename)) == 0)
|| (strncmp(buf.f_fstypename, "smbfs", sizeof(buf.f_fstypename)) == 0)); || (strncmp(buf.f_fstypename, "smbfs", sizeof(buf.f_fstypename)) == 0));
#else #else
QString file = path;
if (!file.endsWith('/'))
file += '/';
file += '.';
struct statfs buf {};
if (statfs(file.toLocal8Bit().constData(), &buf) != 0)
return false;
// Magic number references: // Magic number references:
// 1. /usr/include/linux/magic.h // 1. /usr/include/linux/magic.h
// 2. https://github.com/coreutils/coreutils/blob/master/src/stat.c // 2. https://github.com/coreutils/coreutils/blob/master/src/stat.c

2
src/base/utils/fs.h

@ -62,7 +62,7 @@ namespace Utils
QString tempPath(); QString tempPath();
#if !defined Q_OS_WIN && !defined Q_OS_HAIKU #if !defined Q_OS_HAIKU
bool isNetworkFileSystem(const QString &path); bool isNetworkFileSystem(const QString &path);
#endif #endif
} }

Loading…
Cancel
Save