Skip to content

Commit

Permalink
Use local folder path when setting readonly state.
Browse files Browse the repository at this point in the history
Signed-off-by: alex-z <blackslayer4@gmail.com>
  • Loading branch information
allexzander committed Sep 4, 2023
1 parent c892f32 commit d0a80c7
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/gui/editlocallyjob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,7 @@ void EditLocallyJob::lockFile()

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

Expand Down Expand Up @@ -709,7 +713,11 @@ void Folder::slotLockedFilesFound(const QSet<QString> &files)
disconnect(_fileLockFailure);
qCWarning(lcFolder) << "Failed to lock a file:" << remoteFilePath << message;
});
_accountState->account()->setLockFileState(remoteFilePath, remotePathTrailingSlash(), journalDb(), SyncFileItem::LockStatus::LockedItem);
_accountState->account()->setLockFileState(remoteFilePath,
remotePathTrailingSlash(),
path(),
journalDb(),
SyncFileItem::LockStatus::LockedItem);
}
}

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

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

shareFolder->journalDb()->schedulePathForRemoteDiscovery(fileData.serverRelativePath);
shareFolder->scheduleThisFolderSoon();
Expand Down
3 changes: 2 additions & 1 deletion src/libsync/account.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,7 @@ std::shared_ptr<UserStatusConnector> Account::userStatusConnector() const

void Account::setLockFileState(const QString &serverRelativePath,
const QString &remoteSyncPathWithTrailingSlash,
const QString &localSyncPath,
SyncJournalDb * const journal,
const SyncFileItem::LockStatus lockStatus)
{
Expand All @@ -934,7 +935,7 @@ void Account::setLockFileState(const QString &serverRelativePath,
return;
}
lockStatusJobInProgress.push_back(lockStatus);
auto job = std::make_unique<LockFileJob>(sharedFromThis(), journal, serverRelativePath, remoteSyncPathWithTrailingSlash, lockStatus);
auto job = std::make_unique<LockFileJob>(sharedFromThis(), journal, serverRelativePath, remoteSyncPathWithTrailingSlash, localSyncPath, lockStatus);
connect(job.get(), &LockFileJob::finishedWithoutError, this, [this, serverRelativePath, lockStatus]() {
const auto foundLockStatusJobInProgress = _lockStatusChangeInprogress.find(serverRelativePath);
if (foundLockStatusJobInProgress != _lockStatusChangeInprogress.end()) {
Expand Down
1 change: 1 addition & 0 deletions src/libsync/account.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ class OWNCLOUDSYNC_EXPORT Account : public QObject

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

Expand Down
11 changes: 8 additions & 3 deletions src/libsync/lockfilejobs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,18 @@ LockFileJob::LockFileJob(const AccountPtr account,
SyncJournalDb* const journal,
const QString &path,
const QString &remoteSyncPathWithTrailingSlash,
const QString &localSyncPath,
const SyncFileItem::LockStatus requestedLockState,
QObject *parent)
: AbstractNetworkJob(account, path, parent)
, _journal(journal)
, _requestedLockState(requestedLockState)
, _remoteSyncPathWithTrailingSlash(remoteSyncPathWithTrailingSlash)
, _localSyncPath(localSyncPath)
{
if (!_localSyncPath.endsWith(QLatin1Char('/'))) {
_localSyncPath.append(QLatin1Char('/'));
}
}

void LockFileJob::start()
Expand Down Expand Up @@ -169,9 +174,9 @@ SyncJournalFileRecord LockFileJob::handleReply()
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(relativePathInDb, true);
if ((_lockStatus == SyncFileItem::LockStatus::LockedItem)
&& (_lockOwnerType != SyncFileItem::LockOwnerType::UserLock || _userId != account()->davUser())) {
FileSystem::setFileReadOnly(_localSyncPath + 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 @@ -23,6 +23,7 @@ class OWNCLOUDSYNC_EXPORT LockFileJob : public AbstractNetworkJob
SyncJournalDb* const journal,
const QString &path,
const QString &remoteSyncPathWithTrailingSlash,
const QString &localSyncPath,
const SyncFileItem::LockStatus requestedLockState,
QObject *parent = nullptr);
void start() override;
Expand Down Expand Up @@ -56,6 +57,7 @@ class OWNCLOUDSYNC_EXPORT LockFileJob : public AbstractNetworkJob
qint64 _lockTime = 0;
qint64 _lockTimeout = 0;
QString _remoteSyncPathWithTrailingSlash;
QString _localSyncPath;
};

}
Expand Down
35 changes: 31 additions & 4 deletions test/testlockfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ private slots:

fakeFolder.account()->setLockFileState(QStringLiteral("/") + testFileName,
QStringLiteral("/"),
fakeFolder.localPath(),
&fakeFolder.syncJournal(),
OCC::SyncFileItem::LockStatus::LockedItem);

Expand Down Expand Up @@ -82,6 +83,7 @@ private slots:

fakeFolder.account()->setLockFileState(QStringLiteral("/") + testFileName,
QStringLiteral("/"),
fakeFolder.localPath(),
&fakeFolder.syncJournal(),
OCC::SyncFileItem::LockStatus::LockedItem);

Expand All @@ -105,6 +107,7 @@ private slots:

fakeFolder.account()->setLockFileState(QStringLiteral("/") + testFileName,
QStringLiteral("/"),
fakeFolder.localPath(),
&fakeFolder.syncJournal(),
OCC::SyncFileItem::LockStatus::LockedItem);

Expand All @@ -131,6 +134,7 @@ private slots:

fakeFolder.account()->setLockFileState(QStringLiteral("/") + testFileName,
QStringLiteral("/"),
fakeFolder.localPath(),
&fakeFolder.syncJournal(),
OCC::SyncFileItem::LockStatus::LockedItem);

Expand All @@ -151,7 +155,12 @@ private slots:

QVERIFY(fakeFolder.syncOnce());

auto job = new OCC::LockFileJob(fakeFolder.account(), &fakeFolder.syncJournal(), QStringLiteral("/") + testFileName, QStringLiteral("/"), OCC::SyncFileItem::LockStatus::LockedItem);
auto job = new OCC::LockFileJob(fakeFolder.account(),
&fakeFolder.syncJournal(),
QStringLiteral("/") + testFileName,
QStringLiteral("/"),
fakeFolder.localPath(),
OCC::SyncFileItem::LockStatus::LockedItem);

QSignalSpy jobSuccess(job, &OCC::LockFileJob::finishedWithoutError);
QSignalSpy jobFailure(job, &OCC::LockFileJob::finishedWithError);
Expand Down Expand Up @@ -184,7 +193,12 @@ private slots:

QVERIFY(fakeFolder.syncOnce());

auto lockFileJob = new OCC::LockFileJob(fakeFolder.account(), &fakeFolder.syncJournal(), QStringLiteral("/") + testFileName, QStringLiteral("/"), OCC::SyncFileItem::LockStatus::LockedItem);
auto lockFileJob = new OCC::LockFileJob(fakeFolder.account(),
&fakeFolder.syncJournal(),
QStringLiteral("/") + testFileName,
QStringLiteral("/"),
fakeFolder.localPath(),
OCC::SyncFileItem::LockStatus::LockedItem);

QSignalSpy lockFileJobSuccess(lockFileJob, &OCC::LockFileJob::finishedWithoutError);
QSignalSpy lockFileJobFailure(lockFileJob, &OCC::LockFileJob::finishedWithError);
Expand All @@ -196,7 +210,12 @@ private slots:

QVERIFY(fakeFolder.syncOnce());

auto unlockFileJob = new OCC::LockFileJob(fakeFolder.account(), &fakeFolder.syncJournal(), QStringLiteral("/") + testFileName, QStringLiteral("/"), OCC::SyncFileItem::LockStatus::UnlockedItem);
auto unlockFileJob = new OCC::LockFileJob(fakeFolder.account(),
&fakeFolder.syncJournal(),
QStringLiteral("/") + testFileName,
QStringLiteral("/"),
fakeFolder.localPath(),
OCC::SyncFileItem::LockStatus::UnlockedItem);

QSignalSpy unlockFileJobSuccess(unlockFileJob, &OCC::LockFileJob::finishedWithoutError);
QSignalSpy unlockFileJobFailure(unlockFileJob, &OCC::LockFileJob::finishedWithError);
Expand Down Expand Up @@ -254,6 +273,7 @@ private slots:
&fakeFolder.syncJournal(),
QStringLiteral("/") + testFileName,
QStringLiteral("/"),
fakeFolder.localPath(),
OCC::SyncFileItem::LockStatus::LockedItem);

QSignalSpy jobSuccess(job, &OCC::LockFileJob::finishedWithoutError);
Expand Down Expand Up @@ -306,6 +326,7 @@ private slots:
&fakeFolder.syncJournal(),
QStringLiteral("/") + testFileName,
QStringLiteral("/"),
fakeFolder.localPath(),
OCC::SyncFileItem::LockStatus::LockedItem);

QSignalSpy jobSuccess(job, &OCC::LockFileJob::finishedWithoutError);
Expand Down Expand Up @@ -358,6 +379,7 @@ private slots:
&fakeFolder.syncJournal(),
QStringLiteral("/") + testFileName,
QStringLiteral("/"),
fakeFolder.localPath(),
OCC::SyncFileItem::LockStatus::UnlockedItem);

QSignalSpy jobSuccess(job, &OCC::LockFileJob::finishedWithoutError);
Expand Down Expand Up @@ -405,8 +427,10 @@ private slots:
QVERIFY(fakeFolder.syncOnce());

auto job = new OCC::LockFileJob(fakeFolder.account(),
&fakeFolder.syncJournal(), QStringLiteral("/") + testFileName,
&fakeFolder.syncJournal(),
QStringLiteral("/") + testFileName,
QStringLiteral("/"),
fakeFolder.localPath(),
OCC::SyncFileItem::LockStatus::UnlockedItem);

QSignalSpy jobSuccess(job, &OCC::LockFileJob::finishedWithoutError);
Expand Down Expand Up @@ -444,6 +468,7 @@ private slots:
&fakeFolder.syncJournal(),
QStringLiteral("/") + testFileName,
QStringLiteral("/"),
fakeFolder.localPath(),
OCC::SyncFileItem::LockStatus::LockedItem);

QSignalSpy lockFileJobSuccess(lockFileJob, &OCC::LockFileJob::finishedWithoutError);
Expand All @@ -460,6 +485,7 @@ private slots:
&fakeFolder.syncJournal(),
QStringLiteral("/") + testFileName,
QStringLiteral("/"),
fakeFolder.localPath(),
OCC::SyncFileItem::LockStatus::UnlockedItem);

QSignalSpy unlockFileJobSuccess(unlockFileJob, &OCC::LockFileJob::finishedWithoutError);
Expand Down Expand Up @@ -512,6 +538,7 @@ private slots:
&fakeFolder.syncJournal(),
QStringLiteral("/") + testFileName,
QStringLiteral("/"),
fakeFolder.localPath(),
OCC::SyncFileItem::LockStatus::UnlockedItem);

QSignalSpy jobSuccess(job, &OCC::LockFileJob::finishedWithoutError);
Expand Down

0 comments on commit d0a80c7

Please sign in to comment.