From: Christian Kamm Date: Thu, 11 Jul 2019 11:31:54 +0000 (+0200) Subject: FolderWatcher: Wait for ready before testing #7305 X-Git-Tag: archive/raspbian/3.16.7-1_deb13u1+rpi1~1^2~12^2~22^2~47^2~2 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=28e4fc54a1a6afad79410376550d4417ee612cc4;p=nextcloud-desktop.git FolderWatcher: Wait for ready before testing #7305 --- diff --git a/src/gui/folderwatcher.cpp b/src/gui/folderwatcher.cpp index 6151b1116..80685e3cf 100644 --- a/src/gui/folderwatcher.cpp +++ b/src/gui/folderwatcher.cpp @@ -92,6 +92,19 @@ void FolderWatcher::startNotificatonTest(const QString &path) Q_ASSERT(_testNotificationPath.isEmpty()); _testNotificationPath = path; + // Don't do the local file modification immediately: + // wait for FolderWatchPrivate::_ready + startNotificationTestWhenReady(); +} + +void FolderWatcher::startNotificationTestWhenReady() +{ + if (!_d->_ready) { + QTimer::singleShot(1000, this, &FolderWatcher::startNotificationTestWhenReady); + return; + } + + auto path = _testNotificationPath; if (QFile::exists(path)) { auto mtime = FileSystem::getModTime(path); FileSystem::setModTime(path, mtime + 1); @@ -100,13 +113,14 @@ void FolderWatcher::startNotificatonTest(const QString &path) f.open(QIODevice::WriteOnly | QIODevice::Append); } - QTimer::singleShot(5000, this, [&]() { + QTimer::singleShot(5000, this, [this]() { if (!_testNotificationPath.isEmpty()) emit becameUnreliable(tr("The watcher did not receive a test notification.")); _testNotificationPath.clear(); }); } + int FolderWatcher::testLinuxWatchCount() const { #ifdef Q_OS_LINUX diff --git a/src/gui/folderwatcher.h b/src/gui/folderwatcher.h index ef1a7c394..f7d75fcc4 100644 --- a/src/gui/folderwatcher.h +++ b/src/gui/folderwatcher.h @@ -109,6 +109,9 @@ protected slots: void changeDetected(const QString &path); void changeDetected(const QStringList &paths); +private slots: + void startNotificationTestWhenReady(); + protected: QHash _pendingPathes; diff --git a/src/gui/folderwatcher_linux.h b/src/gui/folderwatcher_linux.h index ba651f700..277736b40 100644 --- a/src/gui/folderwatcher_linux.h +++ b/src/gui/folderwatcher_linux.h @@ -41,6 +41,9 @@ public: int testWatchCount() const { return _pathToWatch.size(); } + /// On linux the watcher is ready when the ctor finished. + bool _ready = true; + protected slots: void slotReceivedNotification(int fd); void slotAddFolderRecursive(const QString &path); diff --git a/src/gui/folderwatcher_mac.h b/src/gui/folderwatcher_mac.h index 4ec666945..e68124f82 100644 --- a/src/gui/folderwatcher_mac.h +++ b/src/gui/folderwatcher_mac.h @@ -37,6 +37,9 @@ public: QStringList addCoalescedPaths(const QStringList &) const; void doNotifyParent(const QStringList &); + /// On OSX the watcher is ready when the ctor finished. + bool _ready = true; + private: FolderWatcher *_parent; diff --git a/src/gui/folderwatcher_win.cpp b/src/gui/folderwatcher_win.cpp index 4907b5adf..8799431a1 100644 --- a/src/gui/folderwatcher_win.cpp +++ b/src/gui/folderwatcher_win.cpp @@ -84,6 +84,8 @@ void WatcherThread::watchChanges(size_t fileNotifyBufferSize, break; } + emit ready(); + HANDLE handles[] = { _resultEvent, _stopEvent }; DWORD result = WaitForMultipleObjects( 2, handles, @@ -204,6 +206,8 @@ FolderWatcherPrivate::FolderWatcherPrivate(FolderWatcher *p, const QString &path _parent, SLOT(changeDetected(const QString &))); connect(_thread, SIGNAL(lostChanges()), _parent, SIGNAL(lostChanges())); + connect(_thread, &WatcherThread::ready, + this, [this]() { _ready = 1; }); _thread->start(); } diff --git a/src/gui/folderwatcher_win.h b/src/gui/folderwatcher_win.h index a556e9146..c89bfbbae 100644 --- a/src/gui/folderwatcher_win.h +++ b/src/gui/folderwatcher_win.h @@ -54,6 +54,7 @@ protected: signals: void changed(const QString &path); void lostChanges(); + void ready(); private: QString _path; @@ -74,6 +75,9 @@ public: FolderWatcherPrivate(FolderWatcher *p, const QString &path); ~FolderWatcherPrivate(); + /// Set to non-zero once the WatcherThread is capturing events. + QAtomicInt _ready; + private: FolderWatcher *_parent; WatcherThread *_thread;