Skip to content

Commit

Permalink
Fix a crash when saving the locked Word document file due to unnecess…
Browse files Browse the repository at this point in the history
…ary spam to the lock state change from folder watcher logic.

Signed-off-by: alex-z <blackslayer4@gmail.com>
  • Loading branch information
allexzander committed Sep 4, 2023
1 parent 54d9905 commit c892f32
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 47 deletions.
1 change: 1 addition & 0 deletions src/gui/editlocallyjob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,7 @@ void EditLocallyJob::lockFile()
this, &EditLocallyJob::fileLockError));

_folderForFile->accountState()->account()->setLockFileState(_relPath,
_folderForFile->remotePathTrailingSlash(),
_folderForFile->journalDb(),
SyncFileItem::LockStatus::LockedItem);
}
Expand Down
9 changes: 3 additions & 6 deletions src/gui/folder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ void Folder::slotFilesLockReleased(const QSet<QString> &files)
disconnect(_officeFileLockReleaseUnlockFailure);
qCWarning(lcFolder) << "Failed to unlock a file:" << remoteFilePath << message;
});
_accountState->account()->setLockFileState(remoteFilePath, journalDb(), SyncFileItem::LockStatus::UnlockedItem);
_accountState->account()->setLockFileState(remoteFilePath, remotePathTrailingSlash(), journalDb(), SyncFileItem::LockStatus::UnlockedItem);
}
}

Expand All @@ -690,10 +690,7 @@ void Folder::slotLockedFilesFound(const QSet<QString> &files)
const auto fileRecordPath = fileFromLocalPath(file);
SyncJournalFileRecord rec;

const auto canLockFile = journalDb()->getFileRecord(fileRecordPath, &rec) && rec.isValid()
&& (!rec._lockstate._locked
|| (rec._lockstate._lockOwnerType == static_cast<qint64>(SyncFileItem::LockOwnerType::UserLock)
&& rec._lockstate._lockOwnerId == _accountState->account()->davUser()));
const auto canLockFile = journalDb()->getFileRecord(fileRecordPath, &rec) && rec.isValid() && !rec._lockstate._locked;

if (!canLockFile) {
qCDebug(lcFolder) << "Skipping locking file" << file << "with rec.isValid():" << rec.isValid()
Expand All @@ -712,7 +709,7 @@ void Folder::slotLockedFilesFound(const QSet<QString> &files)
disconnect(_fileLockFailure);
qCWarning(lcFolder) << "Failed to lock a file:" << remoteFilePath << message;
});
_accountState->account()->setLockFileState(remoteFilePath, journalDb(), SyncFileItem::LockStatus::LockedItem);
_accountState->account()->setLockFileState(remoteFilePath, remotePathTrailingSlash(), journalDb(), SyncFileItem::LockStatus::LockedItem);
}
}

Expand Down
50 changes: 31 additions & 19 deletions src/gui/folderwatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
#include <QDir>
#include <QMutexLocker>
#include <QStringList>
#include <QTimer>

#include <array>
#include <cstdint>
Expand Down Expand Up @@ -157,6 +156,20 @@ void FolderWatcher::startNotificationTestWhenReady()
});
}

void FolderWatcher::lockChageDebouncingTimerTimedOut()
{
if (!_unlockedFiles.isEmpty()) {
const auto unlockedFilesCopy = _unlockedFiles;
emit filesLockReleased(unlockedFilesCopy);
_unlockedFiles.clear();
}
if (!_lockedFiles.isEmpty()) {
const auto lockedFilesCopy = _lockedFiles;
emit filesLockImposed(lockedFilesCopy);
emit lockedFilesFound(lockedFilesCopy);
_lockedFiles.clear();
}
}

int FolderWatcher::testLinuxWatchCount() const
{
Expand Down Expand Up @@ -195,8 +208,6 @@ void FolderWatcher::changeDetected(const QStringList &paths)
_timer.restart();

QSet<QString> changedPaths;
QSet<QString> unlockedFiles;
QSet<QString> lockedFiles;

for (const auto &path : paths) {
if (!_testNotificationPath.isEmpty()
Expand All @@ -209,17 +220,20 @@ void FolderWatcher::changeDetected(const QStringList &paths)
if (_shouldWatchForFileUnlocking) {
// Lock file has been deleted, file now unlocked
if (checkResult.type == FileLockingInfo::Type::Unlocked && !checkResult.path.isEmpty() && !QFile::exists(path)) {
unlockedFiles.insert(checkResult.path);
_lockedFiles.remove(checkResult.path);
_unlockedFiles.insert(checkResult.path);
} else if (!checkResult.path.isEmpty() && QFile::exists(path)) { // Lock file found
lockedFiles.insert(checkResult.path);
_unlockedFiles.remove(checkResult.path);
_lockedFiles.insert(checkResult.path);
}
}

if (checkResult.type == FileLockingInfo::Type::Locked && !checkResult.path.isEmpty()) {
lockedFiles.insert(checkResult.path);
_unlockedFiles.remove(checkResult.path);
_lockedFiles.insert(checkResult.path);
}

qCDebug(lcFolderWatcher) << "Locked files:" << lockedFiles.values();
qCDebug(lcFolderWatcher) << "Locked files:" << _lockedFiles.values();

// ------- handle ignores:
if (pathIsIgnored(path)) {
Expand All @@ -229,19 +243,17 @@ void FolderWatcher::changeDetected(const QStringList &paths)
changedPaths.insert(path);
}

qCDebug(lcFolderWatcher) << "Unlocked files:" << unlockedFiles.values();
qCDebug(lcFolderWatcher) << "Locked files:" << lockedFiles;
qCDebug(lcFolderWatcher) << "Unlocked files:" << _unlockedFiles.values();
qCDebug(lcFolderWatcher) << "Locked files:" << _lockedFiles;

if (!unlockedFiles.isEmpty()) {
emit filesLockReleased(unlockedFiles);
}

if (!lockedFiles.isEmpty()) {
emit filesLockImposed(lockedFiles);
}

if (!lockedFiles.isEmpty()) {
emit lockedFilesFound(lockedFiles);
if (!_lockedFiles.isEmpty() || !_unlockedFiles.isEmpty()) {
if (_lockChageDebouncingTimer.isActive()) {
_lockChageDebouncingTimer.stop();
}
_lockChageDebouncingTimer.setInterval(500);
_lockChageDebouncingTimer.setSingleShot(true);
_lockChageDebouncingTimer.start();
_lockChageDebouncingTimer.connect(&_lockChageDebouncingTimer, &QTimer::timeout, this, &FolderWatcher::lockChageDebouncingTimerTimedOut, Qt::UniqueConnection);
}

if (changedPaths.isEmpty()) {
Expand Down
9 changes: 7 additions & 2 deletions src/gui/folderwatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
#include <QScopedPointer>
#include <QSet>
#include <QDir>

class QTimer;
#include <QTimer>

namespace OCC {

Expand Down Expand Up @@ -130,6 +129,7 @@ protected slots:

private slots:
void startNotificationTestWhenReady();
void lockChageDebouncingTimerTimedOut();

protected:
QHash<QString, int> _pendingPathes;
Expand All @@ -156,6 +156,11 @@ private slots:
/** Path of the expected test notification */
QString _testNotificationPath;

QSet<QString> _unlockedFiles;
QSet<QString> _lockedFiles;

QTimer _lockChageDebouncingTimer;

friend class FolderWatcherPrivate;
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/gui/socketapi/socketapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,7 @@ void SocketApi::setFileLock(const QString &localFile, const SyncFileItem::LockSt
return;
}

shareFolder->accountState()->account()->setLockFileState(fileData.serverRelativePath, shareFolder->journalDb(), lockState);
shareFolder->accountState()->account()->setLockFileState(fileData.serverRelativePath, shareFolder->remotePathTrailingSlash(), shareFolder->journalDb(), lockState);

shareFolder->journalDb()->schedulePathForRemoteDiscovery(fileData.serverRelativePath);
shareFolder->scheduleThisFolderSoon();
Expand Down
18 changes: 16 additions & 2 deletions src/libsync/account.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -924,11 +924,25 @@ std::shared_ptr<UserStatusConnector> Account::userStatusConnector() const
}

void Account::setLockFileState(const QString &serverRelativePath,
const QString &remoteSyncPathWithTrailingSlash,
SyncJournalDb * const journal,
const SyncFileItem::LockStatus lockStatus)
{
auto job = std::make_unique<LockFileJob>(sharedFromThis(), journal, serverRelativePath, lockStatus);
connect(job.get(), &LockFileJob::finishedWithoutError, this, [this]() {
auto& lockStatusJobInProgress = _lockStatusChangeInprogress[serverRelativePath];
if (lockStatusJobInProgress.contains(lockStatus)) {
qCWarning(lcAccount) << "Already running a job with lockStatus:" << lockStatus << " for: " << serverRelativePath;
return;
}
lockStatusJobInProgress.push_back(lockStatus);
auto job = std::make_unique<LockFileJob>(sharedFromThis(), journal, serverRelativePath, remoteSyncPathWithTrailingSlash, lockStatus);
connect(job.get(), &LockFileJob::finishedWithoutError, this, [this, serverRelativePath, lockStatus]() {
const auto foundLockStatusJobInProgress = _lockStatusChangeInprogress.find(serverRelativePath);
if (foundLockStatusJobInProgress != _lockStatusChangeInprogress.end()) {
foundLockStatusJobInProgress.value().removeAll(lockStatus);
if (foundLockStatusJobInProgress.value().isEmpty()) {
_lockStatusChangeInprogress.erase(foundLockStatusJobInProgress);
}
}
Q_EMIT lockFileSuccess();
});
connect(job.get(), &LockFileJob::finishedWithError, this, [lockStatus, serverRelativePath, this](const int httpErrorCode, const QString &errorString, const QString &lockOwnerName) {
Expand Down
3 changes: 3 additions & 0 deletions src/libsync/account.h
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ class OWNCLOUDSYNC_EXPORT Account : public QObject
[[nodiscard]] std::shared_ptr<UserStatusConnector> userStatusConnector() const;

void setLockFileState(const QString &serverRelativePath,
const QString &remoteSyncPathWithTrailingSlash,
SyncJournalDb * const journal,
const SyncFileItem::LockStatus lockStatus);

Expand Down Expand Up @@ -436,6 +437,8 @@ protected Q_SLOTS:

std::shared_ptr<UserStatusConnector> _userStatusConnector;

QHash<QString, QVector<SyncFileItem::LockStatus>> _lockStatusChangeInprogress;

/* IMPORTANT - remove later - FIXME MS@2019-12-07 -->
* TODO: For "Log out" & "Remove account": Remove client CA certs and KEY!
*
Expand Down
8 changes: 5 additions & 3 deletions src/libsync/lockfilejobs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ Q_LOGGING_CATEGORY(lcLockFileJob, "nextcloud.sync.networkjob.lockfile", QtInfoMs
LockFileJob::LockFileJob(const AccountPtr account,
SyncJournalDb* const journal,
const QString &path,
const QString &remoteSyncPathWithTrailingSlash,
const SyncFileItem::LockStatus requestedLockState,
QObject *parent)
: AbstractNetworkJob(account, path, parent)
, _journal(journal)
, _requestedLockState(requestedLockState)
, _remoteSyncPathWithTrailingSlash(remoteSyncPathWithTrailingSlash)
{
}

Expand Down Expand Up @@ -164,12 +166,12 @@ SyncJournalFileRecord LockFileJob::handleReply()
}
}

const auto relativePath = path().mid(1);
if (_journal->getFileRecord(relativePath, &record) && record.isValid()) {
const auto relativePathInDb = path().mid(_remoteSyncPathWithTrailingSlash.size());
if (_journal->getFileRecord(relativePathInDb, &record) && record.isValid()) {
setFileRecordLocked(record);
if (_lockOwnerType != SyncFileItem::LockOwnerType::UserLock ||
_userId != account()->davUser()) {
FileSystem::setFileReadOnly(relativePath, true);
FileSystem::setFileReadOnly(relativePathInDb, true);
}
const auto result = _journal->setFileRecord(record);
if (!result) {
Expand Down
2 changes: 2 additions & 0 deletions src/libsync/lockfilejobs.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class OWNCLOUDSYNC_EXPORT LockFileJob : public AbstractNetworkJob
explicit LockFileJob(const AccountPtr account,
SyncJournalDb* const journal,
const QString &path,
const QString &remoteSyncPathWithTrailingSlash,
const SyncFileItem::LockStatus requestedLockState,
QObject *parent = nullptr);
void start() override;
Expand Down Expand Up @@ -54,6 +55,7 @@ class OWNCLOUDSYNC_EXPORT LockFileJob : public AbstractNetworkJob
QString _userId;
qint64 _lockTime = 0;
qint64 _lockTimeout = 0;
QString _remoteSyncPathWithTrailingSlash;
};

}
Expand Down
1 change: 1 addition & 0 deletions src/libsync/syncfileitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class OWNCLOUDSYNC_EXPORT SyncFileItem
Q_ENUM(Status)

enum class LockStatus {
Unset = -1,
UnlockedItem = 0,
LockedItem = 1,
};
Expand Down
Loading

0 comments on commit c892f32

Please sign in to comment.