Skip to content

Commit 792dab6

Browse files
committed
Fix files not unlocking after lock time expired
Signed-off-by: Claudio Cambra <claudio.cambra@gmail.com>
1 parent 70a6588 commit 792dab6

4 files changed

Lines changed: 40 additions & 4 deletions

File tree

src/common/syncjournaldb.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,7 +1351,7 @@ bool SyncJournalDb::updateFileRecordChecksum(const QString &filename,
13511351
}
13521352

13531353
bool SyncJournalDb::updateLocalMetadata(const QString &filename,
1354-
qint64 modtime, qint64 size, quint64 inode)
1354+
qint64 modtime, qint64 size, quint64 inode, const SyncJournalFileLockInfo &lockInfo)
13551355

13561356
{
13571357
QMutexLocker locker(&_mutex);
@@ -1365,7 +1365,9 @@ bool SyncJournalDb::updateLocalMetadata(const QString &filename,
13651365
}
13661366

13671367
const auto query = _queryManager.get(PreparedSqlQueryManager::SetFileRecordLocalMetadataQuery, QByteArrayLiteral("UPDATE metadata"
1368-
" SET inode=?2, modtime=?3, filesize=?4"
1368+
" SET inode=?2, modtime=?3, filesize=?4, lock=?5, lockType=?6,"
1369+
" lockOwnerDisplayName=?7, lockOwnerId=?8, lockOwnerEditor = ?9,"
1370+
" lockTime=?10, lockTimeout=?11"
13691371
" WHERE phash == ?1;"),
13701372
_db);
13711373
if (!query) {
@@ -1376,6 +1378,13 @@ bool SyncJournalDb::updateLocalMetadata(const QString &filename,
13761378
query->bindValue(2, inode);
13771379
query->bindValue(3, modtime);
13781380
query->bindValue(4, size);
1381+
query->bindValue(5, lockInfo._locked ? 1 : 0);
1382+
query->bindValue(6, lockInfo._lockOwnerDisplayName);
1383+
query->bindValue(7, lockInfo._lockOwnerId);
1384+
query->bindValue(8, lockInfo._lockOwnerType);
1385+
query->bindValue(9, lockInfo._lockEditorApp);
1386+
query->bindValue(10, lockInfo._lockTime);
1387+
query->bindValue(11, lockInfo._lockTimeout);
13791388
return query->exec();
13801389
}
13811390

src/common/syncjournaldb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class OCSYNC_EXPORT SyncJournalDb : public QObject
7878
const QByteArray &contentChecksum,
7979
const QByteArray &contentChecksumType);
8080
[[nodiscard]] bool updateLocalMetadata(const QString &filename,
81-
qint64 modtime, qint64 size, quint64 inode);
81+
qint64 modtime, qint64 size, quint64 inode, const SyncJournalFileLockInfo &lockInfo);
8282

8383
/// Return value for hasHydratedOrDehydratedFiles()
8484
struct HasHydratedDehydrated

src/libsync/discovery.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,20 @@ void ProcessDirectoryJob::processFile(PathTuple path,
373373
<< " | e2eeMangledName: " << dbEntry.e2eMangledName() << "/" << serverEntry.e2eMangledName
374374
<< " | file lock: " << localFileIsLocked << "//" << serverFileIsLocked;
375375

376+
if(serverEntry.locked == SyncFileItem::LockStatus::LockedItem) {
377+
const auto lockExpirationTime = serverEntry.lockTime + serverEntry.lockTimeout;
378+
const auto timeRemaining = QDateTime::currentDateTime().secsTo(QDateTime::fromSecsSinceEpoch(lockExpirationTime));
379+
const auto timerInterval = qMax(5ll, timeRemaining);
380+
381+
qCInfo(lcDisco) << "Will re-check lock status for:" << path._original << "in:" << timerInterval << "seconds.";
382+
383+
// lockTimeout is in secs, QTimer takes msecs
384+
QTimer::singleShot(timerInterval * 1000, this, [this, path]{
385+
qCInfo(lcDisco) << "Lock for file:" << path._original << "has expired. Rescanning.";
386+
startAsyncServerQuery();
387+
});
388+
}
389+
376390
if (localEntry.isValid()
377391
&& !serverEntry.isValid()
378392
&& !dbEntry.isValid()
@@ -516,6 +530,7 @@ void ProcessDirectoryJob::processFileAnalyzeRemoteInfo(
516530
const bool isVirtualE2EePlaceholder = isDbEntryAnE2EePlaceholder && serverEntry.size >= Constants::e2EeTagSize;
517531
const qint64 sizeOnServer = isVirtualE2EePlaceholder ? serverEntry.size - Constants::e2EeTagSize : serverEntry.size;
518532
const bool metaDataSizeNeedsUpdateForE2EeFilePlaceholder = isVirtualE2EePlaceholder && dbEntry._fileSize == serverEntry.size;
533+
const bool serverEntryLockedAsBool = serverEntry.locked == SyncFileItem::LockStatus::LockedItem;
519534

520535
if (serverEntry.isDirectory != dbEntry.isDirectory()) {
521536
// If the type of the entity changed, it's like NEW, but
@@ -562,6 +577,8 @@ void ProcessDirectoryJob::processFileAnalyzeRemoteInfo(
562577
}
563578
item->_instruction = CSYNC_INSTRUCTION_UPDATE_METADATA;
564579
item->_direction = SyncFileItem::Down;
580+
} else if(serverEntryLockedAsBool != dbEntry._lockstate._locked) {
581+
item->_instruction = CSYNC_INSTRUCTION_UPDATE_METADATA;
565582
} else {
566583
// if (is virtual mode enabled and folder is encrypted - check if the size is the same as on the server and then - trigger server query
567584
// to update a placeholder with corrected size (-16 Bytes)

src/libsync/syncengine.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,17 @@ void OCC::SyncEngine::slotItemDiscovered(const OCC::SyncFileItemPtr &item)
391391
emit itemCompleted(item);
392392
} else {
393393
// Update only outdated data from the disk.
394-
if (!_journal->updateLocalMetadata(item->_file, item->_modtime, item->_size, item->_inode)) {
394+
395+
SyncJournalFileLockInfo lockInfo;
396+
lockInfo._locked = item->_locked == SyncFileItem::LockStatus::LockedItem;
397+
lockInfo._lockTime = item->_lockTime;
398+
lockInfo._lockTimeout = item->_lockTimeout;
399+
lockInfo._lockOwnerId = item->_lockOwnerId;
400+
lockInfo._lockOwnerType = static_cast<qint64>(item->_lockOwnerType);
401+
lockInfo._lockOwnerDisplayName = item->_lockOwnerDisplayName;
402+
lockInfo._lockEditorApp = item->_lockOwnerDisplayName;
403+
404+
if (!_journal->updateLocalMetadata(item->_file, item->_modtime, item->_size, item->_inode, lockInfo)) {
395405
qCWarning(lcEngine) << "Could not update local metadata for file" << item->_file;
396406
}
397407
}

0 commit comments

Comments
 (0)