From db9df55d1a95c99d18e95ba3e9756cb9588b2af4 Mon Sep 17 00:00:00 2001 From: Yue-Lan Date: Tue, 2 Apr 2024 11:18:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DFilesystemWatcher=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E7=BA=BF=E7=A8=8B=E5=AE=89=E5=85=A8=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit note: 实际使用中发现发送信号的同时添加文件会导致fdCacheMap异常,所以发送信号的流程也需要加锁,另外还需要发送完信号释放锁,避免调用removeWatchFile()后死锁 --- src/filesystem/filewatcher/libkyfilewatcher.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/filesystem/filewatcher/libkyfilewatcher.cpp b/src/filesystem/filewatcher/libkyfilewatcher.cpp index 741afea..1eab122 100644 --- a/src/filesystem/filewatcher/libkyfilewatcher.cpp +++ b/src/filesystem/filewatcher/libkyfilewatcher.cpp @@ -307,7 +307,9 @@ QStringList FileWatcher::getWatchList(int attr) void FileWatcher::sendSignal(int wfd, QString name, int mask) { + std::unique_lock lock(this->listLocker); QString url = this->fdCacheMap[wfd]; + QString path = this->fdCacheMap[wfd]; // 丢弃那些已经被移除的或者设定为不触发的监视器的事件 if (! this->watchList.contains(this->fdCacheMap[wfd]) || this->watchList[this->fdCacheMap[wfd]].type == NEVER) @@ -372,18 +374,25 @@ void FileWatcher::sendSignal(int wfd, QString name, int mask) emit this->fileIgnoredAutomatic(isSubFile ? name : this->fdCacheMap[wfd], isSubFile ? this->fdCacheMap[wfd] : QString()); qDebug() << "File: "<removeWatchFile(this->fdCacheMap[wfd]); + lock.release(); + this->listLocker.unlock(); + this->removeWatchFile(path); } if (mask & IN_UNMOUNT) { emit this->fileUnmount(isSubFile ? name : this->fdCacheMap[wfd], isSubFile ? this->fdCacheMap[wfd] : QString()); qDebug() << "File: "<removeWatchFile(this->fdCacheMap[wfd]); + lock.release(); + this->listLocker.unlock(); + this->removeWatchFile(path); } // 处理ONESHOT情况;做一个contains判断,避免之前被autoremove的文件索引出错 - if (this->fdCacheMap.contains(wfd) && this->watchList[this->fdCacheMap[wfd]].type == ONESHOT) - this->removeWatchFile(this->fdCacheMap[wfd]); + if (this->fdCacheMap.contains(wfd) && this->watchList[this->fdCacheMap[wfd]].type == ONESHOT) { + lock.release(); + this->listLocker.unlock(); + this->removeWatchFile(path); + } } int FileWatcher::addWatchFile(FileWatcher::FileDescription node)