Skip to content

Commit

Permalink
Check a wehter file is locked before we move
Browse files Browse the repository at this point in the history
Fixes: #8765, #8766
  • Loading branch information
TheOneRing committed Jun 22, 2021
1 parent fbd3809 commit b60b543
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 25 deletions.
4 changes: 4 additions & 0 deletions changelog/unreleased/8761
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ We fixed an issue where files locked by office etc, where not correctly synced,
when Windows Virtual files are enabled.

https://github.com/owncloud/client/issues/8761
https://github.com/owncloud/client/issues/8765
https://github.com/owncloud/client/issues/8766
https://github.com/owncloud/client/pull/8763
https://github.com/owncloud/client/pull/8768
10 changes: 10 additions & 0 deletions src/common/filesystembase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ bool FileSystem::rename(const QString &originFileName,
bool success = false;
QString error;
#ifdef Q_OS_WIN
if (FileSystem::isFileLocked(originFileName, FileSystem::LockMode::Exclusive)) {
*errorString = QCoreApplication::tr("FileSystem", "Can't rename locked file %1").arg(originFileName);
qCWarning(lcFileSystem) << "Renaming failed: " << *errorString;
return false;
}
QString orig = longWinPath(originFileName);
QString dest = longWinPath(destinationFileName);

Expand Down Expand Up @@ -191,6 +196,11 @@ bool FileSystem::uncheckedRenameReplace(const QString &originFileName,
}

#else //Q_OS_WIN
if (FileSystem::isFileLocked(originFileName, FileSystem::LockMode::Exclusive)) {
*errorString = QCoreApplication::tr("FileSystem", "Can't rename locked file %1").arg(originFileName);
qCWarning(lcFileSystem) << "Renaming failed: " << *errorString;
return false;
}
// You can not overwrite a read-only file on windows.
if (!QFileInfo(destinationFileName).isWritable()) {
setFileReadOnly(destinationFileName, false);
Expand Down
15 changes: 8 additions & 7 deletions src/libsync/owncloudpropagator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -691,15 +691,16 @@ bool OwncloudPropagator::createConflict(const SyncFileItemPtr &item,
emit touchedFile(fn);
emit touchedFile(conflictFilePath);

// If the file is locked, we want to retry this sync when it
// becomes available again.
if (FileSystem::isFileLocked(fn, FileSystem::LockMode::Exclusive)) {
emit seenLockedFile(fn, FileSystem::LockMode::Exclusive);
if (error)
*error = tr("File %1 is locked").arg(fn);
return false;
}
if (!FileSystem::rename(fn, conflictFilePath, &renameError)) {
// If the rename fails, don't replace it.

// If the file is locked, we want to retry this sync when it
// becomes available again.
if (FileSystem::isFileLocked(fn, FileSystem::LockMode::Exclusive)) {
emit seenLockedFile(fn, FileSystem::LockMode::Exclusive);
}

if (error)
*error = renameError;
return false;
Expand Down
27 changes: 17 additions & 10 deletions src/libsync/propagatedownload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,14 @@ void PropagateDownloadFile::startDownload()
done(SyncFileItem::NormalError, tr("File %1 can not be downloaded because of a local file name clash!").arg(QDir::toNativeSeparators(_item->_file)));
return;
}

// If the file is locked, we want to retry this sync when it
// becomes available again
const auto targetFile = propagator()->fullLocalPath(_item->_file);
if (FileSystem::isFileLocked(targetFile, FileSystem::LockMode::Exclusive)) {
emit propagator()->seenLockedFile(targetFile, FileSystem::LockMode::Exclusive);
done(SyncFileItem::SoftError, tr("File %1 is locked").arg(QDir::toNativeSeparators(_item->_file)));
return;
}
propagator()->reportProgress(*_item, 0);

QString tmpFileName;
Expand Down Expand Up @@ -940,20 +947,20 @@ void PropagateDownloadFile::downloadFinished()
return;
}
}
// If the file is locked, we want to retry this sync when it
// becomes available again
if (FileSystem::isFileLocked(fn, FileSystem::LockMode::Exclusive)) {
emit propagator()->seenLockedFile(fn, FileSystem::LockMode::Exclusive);
done(SyncFileItem::SoftError, tr("File %1 is locked").arg(fn));
return;
}

QString error;
emit propagator()->touchedFile(fn);
// The fileChanged() check is done above to generate better error messages.
if (!FileSystem::uncheckedRenameReplace(_tmpFile.fileName(), fn, &error)) {
qCWarning(lcPropagateDownload) << QStringLiteral("Rename failed: %1 => %2").arg(_tmpFile.fileName()).arg(fn);
// If the file is locked, we want to retry this sync when it
// becomes available again, otherwise try again directly
if (FileSystem::isFileLocked(fn, FileSystem::LockMode::Exclusive)) {
emit propagator()->seenLockedFile(fn, FileSystem::LockMode::Exclusive);
} else {
propagator()->_anotherSyncNeeded = true;
}

qCWarning(lcPropagateDownload) << "Rename failed:" << _tmpFile.fileName() << "=>" << fn;
propagator()->_anotherSyncNeeded = true;
done(SyncFileItem::SoftError, error);
return;
}
Expand Down
5 changes: 0 additions & 5 deletions src/libsync/propagateremotemove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,6 @@ void PropagateRemoteMove::start()

QString origin = propagator()->adjustRenamedPath(_item->_file);
qCDebug(lcPropagateRemoteMove) << origin << _item->_renameTarget;

QString targetFile(propagator()->fullLocalPath(_item->_renameTarget));

if (origin == _item->_renameTarget) {
// The parent has been renamed already so there is nothing more to do.
finalize();
Expand Down Expand Up @@ -147,8 +144,6 @@ void PropagateRemoteMove::start()
<< folderTargetAlt << "to" << folderTarget;
}
}
qCDebug(lcPropagateRemoteMove) << remoteSource << remoteDestination;

_job = new MoveJob(propagator()->account(), remoteSource, remoteDestination, this);
connect(_job.data(), &MoveJob::finishedSignal, this, &PropagateRemoteMove::slotMoveJobFinished);
propagator()->_activeJobList.append(this);
Expand Down
10 changes: 7 additions & 3 deletions src/libsync/propagatorjobs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,15 @@ void PropagateLocalRename::start()
// it would have to come out the localFileNameClash function
done(SyncFileItem::NormalError,
tr("File %1 can not be renamed to %2 because of a local file name clash")
.arg(QDir::toNativeSeparators(_item->_file))
.arg(QDir::toNativeSeparators(_item->_renameTarget)));
.arg(QDir::toNativeSeparators(_item->_file),
QDir::toNativeSeparators(_item->_renameTarget)));
return;
}
if (FileSystem::isFileLocked(existingFile, FileSystem::LockMode::Exclusive)) {
emit propagator()->seenLockedFile(existingFile, FileSystem::LockMode::Exclusive);
done(SyncFileItem::SoftError, tr("Could not rename %1 to %2, file is locked").arg(existingFile, targetFile));
return;
}

emit propagator()->touchedFile(existingFile);
emit propagator()->touchedFile(targetFile);
QString renameError;
Expand Down

0 comments on commit b60b543

Please sign in to comment.