Skip to content

Commit

Permalink
Lock a file during the completion of its upload
Browse files Browse the repository at this point in the history
  • Loading branch information
TheOneRing committed Mar 9, 2022
1 parent 1fc81bf commit 8b479d5
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
6 changes: 6 additions & 0 deletions changelog/unreleased/enterprise-5052
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Bugfix: Don't publish upload if we can't finish the transaction in the client

When a file ges locked during an upload we aborted after the upload finished on the server.
Resulting in a divergence of the local and remote state which could lead to conflicts.

https://github.com/owncloud/enterprise/issues/5052
4 changes: 4 additions & 0 deletions src/libsync/propagateupload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,10 @@ void PropagateUploadFileCommon::finalize()
if (quotaIt != propagator()->_folderQuota.end())
quotaIt.value() -= _item->_size;


#ifdef Q_OS_WIN
m_fileLock.close();
#endif
// Update the database entry
const auto result = propagator()->updateMetadata(*_item);
if (!result) {
Expand Down
4 changes: 4 additions & 0 deletions src/libsync/propagateupload.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ public slots:

/** Bases headers that need to be sent on the PUT, or in the MOVE for chunking-ng */
QMap<QByteArray, QByteArray> headers();

#ifdef Q_OS_WIN
Utility::Handle m_fileLock;
#endif
};

/**
Expand Down
13 changes: 13 additions & 0 deletions src/libsync/propagateuploadng.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,19 @@ void PropagateUploadFileNG::doFinalMove()

const QUrl source = Utility::concatUrlPath(chunkUrl(), QStringLiteral("/.file"));

#ifdef Q_OS_WIN
// Try to accuire a lock on the file and keep it until we done.
// If the file is locked, abort before we perform the move on the server
const QString fileName = propagator()->fullLocalPath(_item->_file);
const auto lockMode = propagator()->syncOptions().requiredLockMode();
m_fileLock = FileSystem::lockFile(fileName, lockMode);
if (!m_fileLock) {
emit propagator()->seenLockedFile(fileName, lockMode);
abortWithError(SyncFileItem::SoftError, tr("%1 the file is currently in use").arg(QDir::toNativeSeparators(fileName)));
return;
}
#endif

auto job = new MoveJob(propagator()->account(), source, destination, headers, this);
_jobs.append(job);
connect(job, &MoveJob::finishedSignal, this, &PropagateUploadFileNG::slotMoveJobFinished);
Expand Down

0 comments on commit 8b479d5

Please sign in to comment.