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) @@ -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

24
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);
#elif defined(Q_OS_MAC) || defined(Q_OS_OPENBSD)
QString file = path;
if (!file.endsWith('/'))
file += '/';
@ -313,12 +325,20 @@ bool Utils::Fs::isNetworkFileSystem(const QString &path) @@ -313,12 +325,20 @@ bool Utils::Fs::isNetworkFileSystem(const QString &path)
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)
|| (strncmp(buf.f_fstypename, "nfs", sizeof(buf.f_fstypename)) == 0)
|| (strncmp(buf.f_fstypename, "smbfs", sizeof(buf.f_fstypename)) == 0));
#else
QString file = path;
if (!file.endsWith('/'))
file += '/';
file += '.';
struct statfs buf {};
if (statfs(file.toLocal8Bit().constData(), &buf) != 0)
return false;
// Magic number references:
// 1. /usr/include/linux/magic.h
// 2. https://github.com/coreutils/coreutils/blob/master/src/stat.c

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