Emit lock file target file paths in lock file discovery from folder watcher
authorClaudio Cambra <claudio.cambra@nextcloud.com>
Fri, 18 Aug 2023 10:35:04 +0000 (18:35 +0800)
committerMatthieu Gallien <matthieu.gallien@nextcloud.com>
Fri, 1 Sep 2023 15:15:45 +0000 (17:15 +0200)
Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
src/gui/folderwatcher.cpp
src/gui/folderwatcher.h

index f971b7139a78014d530726deea0cc6f80d5ddcb6..c56d004417d85ed11387cb5e2f395cd921552563 100644 (file)
@@ -197,7 +197,6 @@ void FolderWatcher::changeDetected(const QStringList &paths)
     QSet<QString> changedPaths;
     QSet<QString> unlockedFiles;
     QSet<QString> lockedFiles;
-    QSet<QString> lockFiles;
 
     for (const auto &path : paths) {
         if (!_testNotificationPath.isEmpty()
@@ -206,12 +205,13 @@ void FolderWatcher::changeDetected(const QStringList &paths)
         }
 
         const auto lockFileNamePattern = filePathLockFilePatternMatch(path);
-        const auto checkResult = checkIfFileIsLockOrUnlock(path,lockFileNamePattern);
+        const auto checkResult = lockFileTargetFilePath(path,lockFileNamePattern);
         if (_shouldWatchForFileUnlocking) {
-            if (checkResult.type == FileLockingInfo::Type::Unlocked && !checkResult.path.isEmpty()) {
+            // Lock file has been deleted, file now unlocked
+            if (checkResult.type == FileLockingInfo::Type::Unlocked && !checkResult.path.isEmpty() && !QFile::exists(path)) {
                 unlockedFiles.insert(checkResult.path);
-            } else if (!lockFileNamePattern.isEmpty()) {
-                lockFiles.insert(path);
+            } else if (!checkResult.path.isEmpty() && QFile::exists(path)) { // Lock file found
+                lockedFiles.insert(checkResult.path);
             }
         }
 
@@ -230,7 +230,7 @@ void FolderWatcher::changeDetected(const QStringList &paths)
     }
 
     qCDebug(lcFolderWatcher) << "Unlocked files:" << unlockedFiles.values();
-    qCDebug(lcFolderWatcher) << "Lock files:" << lockFiles;
+    qCDebug(lcFolderWatcher) << "Locked files:" << lockedFiles;
 
     if (!unlockedFiles.isEmpty()) {
         emit filesLockReleased(unlockedFiles);
@@ -240,8 +240,8 @@ void FolderWatcher::changeDetected(const QStringList &paths)
         emit filesLockImposed(lockedFiles);
     }
 
-    if (!lockFiles.isEmpty()) {
-        emit lockFilesFound(lockFiles);
+    if (!lockedFiles.isEmpty()) {
+        emit lockedFilesFound(lockedFiles);
     }
 
     if (changedPaths.isEmpty()) {
@@ -259,7 +259,7 @@ void FolderWatcher::folderAccountCapabilitiesChanged()
     _shouldWatchForFileUnlocking = _folder->accountState()->account()->capabilities().filesLockAvailable();
 }
 
-FolderWatcher::FileLockingInfo FolderWatcher::checkIfFileIsLockOrUnlock(const QString &path, const QString &lockFileNamePattern) const
+FolderWatcher::FileLockingInfo FolderWatcher::lockFileTargetFilePath(const QString &path, const QString &lockFileNamePattern) const
 {
     FileLockingInfo result;
 
index 0cfd0bd804a2bade72abc033fe12175b48b49c16..4e2c569b9830ff1e70384d77d016b523190aaf2d 100644 (file)
@@ -104,6 +104,8 @@ signals:
 
     void lockFilesFound(const QSet<QString> &files);
 
+    void lockedFilesFound(const QSet<QString> &files);
+
     /**
      * Emitted if some notifications were lost.
      *
@@ -143,9 +145,11 @@ private:
 
     void appendSubPaths(QDir dir, QStringList& subPaths);
 
-    [[nodiscard]] FileLockingInfo checkIfFileIsLockOrUnlock(const QString &path, const QString &lockFileNamePattern) const;
+    [[nodiscard]] FileLockingInfo lockFileTargetFilePath(const QString &path, const QString &lockFileNamePattern) const;
     [[nodiscard]] QString findMatchingUnlockedFileInDir(const QString &dirPath, const QString &lockFileName) const;
 
+    QString findMatchingUnlockedFileInDir(const QString &dirPath, const QString &lockFileName);
+
     /* Check if the path should be igored by the FolderWatcher. */
     [[nodiscard]] bool pathIsIgnored(const QString &path) const;