Merge pull request #4399 from nextcloud/bugfix/doNotRemoveRenamedFileWithSpacesInName

avoid deleting renamed file with spaces in name
This commit is contained in:
Matthieu Gallien 2022-04-06 10:36:00 +02:00 committed by GitHub
commit 468a33cfa8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 0 deletions

View file

@ -1054,6 +1054,10 @@ void ProcessDirectoryJob::processFileAnalyzeLocalInfo(
if (!localEntry.renameName.isEmpty()) { if (!localEntry.renameName.isEmpty()) {
handleInvalidSpaceRename(SyncFileItem::Down); handleInvalidSpaceRename(SyncFileItem::Down);
item->_instruction = CSYNC_INSTRUCTION_NEW;
item->_direction = SyncFileItem::Up;
item->_originalFile = item->_file;
item->_file = item->_renameTarget;
finalize(); finalize();
return; return;
} }

View file

@ -20,6 +20,7 @@
#include "deletejob.h" #include "deletejob.h"
#include "common/asserts.h" #include "common/asserts.h"
#include "encryptfolderjob.h" #include "encryptfolderjob.h"
#include "filesystem.h"
#include <QFile> #include <QFile>
#include <QLoggingCategory> #include <QLoggingCategory>
@ -169,6 +170,18 @@ void PropagateRemoteMkdir::finalizeMkColJob(QNetworkReply::NetworkError err, con
void PropagateRemoteMkdir::slotMkdir() void PropagateRemoteMkdir::slotMkdir()
{ {
if (!_item->_originalFile.isEmpty() && !_item->_renameTarget.isEmpty() && _item->_renameTarget != _item->_originalFile) {
const auto existingFile = propagator()->fullLocalPath(propagator()->adjustRenamedPath(_item->_originalFile));
const auto targetFile = propagator()->fullLocalPath(_item->_renameTarget);
QString renameError;
if (!FileSystem::rename(existingFile, targetFile, &renameError)) {
done(SyncFileItem::NormalError, renameError);
return;
}
emit propagator()->touchedFile(existingFile);
emit propagator()->touchedFile(targetFile);
}
const auto path = _item->_file; const auto path = _item->_file;
const auto slashPosition = path.lastIndexOf('/'); const auto slashPosition = path.lastIndexOf('/');
const auto parentPath = slashPosition >= 0 ? path.left(slashPosition) : QString(); const auto parentPath = slashPosition >= 0 ? path.left(slashPosition) : QString();

View file

@ -194,6 +194,18 @@ void PropagateUploadFileCommon::setDeleteExisting(bool enabled)
void PropagateUploadFileCommon::start() void PropagateUploadFileCommon::start()
{ {
if (!_item->_originalFile.isEmpty() && !_item->_renameTarget.isEmpty() && _item->_renameTarget != _item->_originalFile) {
const auto existingFile = propagator()->fullLocalPath(propagator()->adjustRenamedPath(_item->_originalFile));
const auto targetFile = propagator()->fullLocalPath(_item->_renameTarget);
QString renameError;
if (!FileSystem::rename(existingFile, targetFile, &renameError)) {
done(SyncFileItem::NormalError, renameError);
return;
}
emit propagator()->touchedFile(existingFile);
emit propagator()->touchedFile(targetFile);
}
const auto path = _item->_file; const auto path = _item->_file;
const auto slashPosition = path.lastIndexOf('/'); const auto slashPosition = path.lastIndexOf('/');
const auto parentPath = slashPosition >= 0 ? path.left(slashPosition) : QString(); const auto parentPath = slashPosition >= 0 ? path.left(slashPosition) : QString();

View file

@ -223,6 +223,7 @@ private slots:
fakeFolder.localModifier().insert(fileWithSpaces4); fakeFolder.localModifier().insert(fileWithSpaces4);
fakeFolder.localModifier().insert(fileWithSpaces5); fakeFolder.localModifier().insert(fileWithSpaces5);
fakeFolder.localModifier().insert(fileWithSpaces6); fakeFolder.localModifier().insert(fileWithSpaces6);
fakeFolder.localModifier().mkdir(QStringLiteral(" with spaces "));
QVERIFY(fakeFolder.syncOnce()); QVERIFY(fakeFolder.syncOnce());
@ -244,6 +245,10 @@ private slots:
QVERIFY(fakeFolder.currentLocalState().find("A/bla")); QVERIFY(fakeFolder.currentLocalState().find("A/bla"));
QVERIFY(!fakeFolder.currentLocalState().find(fileWithSpaces6)); QVERIFY(!fakeFolder.currentLocalState().find(fileWithSpaces6));
QVERIFY(fakeFolder.currentLocalState().find(QStringLiteral("with spaces")));
QVERIFY(!fakeFolder.currentLocalState().find(QStringLiteral(" with spaces ")));
fakeFolder.syncEngine().setLocalDiscoveryOptions(LocalDiscoveryStyle::DatabaseAndFilesystem, {QStringLiteral("foo"), QStringLiteral("bar"), QStringLiteral("bla"), QStringLiteral("A/foo"), QStringLiteral("A/bar"), QStringLiteral("A/bla")});
QVERIFY(fakeFolder.syncOnce()); QVERIFY(fakeFolder.syncOnce());
QVERIFY(fakeFolder.currentRemoteState().find(fileWithSpaces1.trimmed())); QVERIFY(fakeFolder.currentRemoteState().find(fileWithSpaces1.trimmed()));
@ -275,6 +280,11 @@ private slots:
QVERIFY(!fakeFolder.currentRemoteState().find(fileWithSpaces6)); QVERIFY(!fakeFolder.currentRemoteState().find(fileWithSpaces6));
QVERIFY(fakeFolder.currentLocalState().find("A/bla")); QVERIFY(fakeFolder.currentLocalState().find("A/bla"));
QVERIFY(!fakeFolder.currentLocalState().find(fileWithSpaces6)); QVERIFY(!fakeFolder.currentLocalState().find(fileWithSpaces6));
QVERIFY(fakeFolder.currentRemoteState().find(QStringLiteral("with spaces")));
QVERIFY(!fakeFolder.currentRemoteState().find(QStringLiteral(" with spaces ")));
QVERIFY(fakeFolder.currentLocalState().find(QStringLiteral("with spaces")));
QVERIFY(!fakeFolder.currentLocalState().find(QStringLiteral(" with spaces ")));
} }
void testCreateFileWithTrailingSpaces_localAndRemoteTrimmedDoNotExist_renameFile() void testCreateFileWithTrailingSpaces_localAndRemoteTrimmedDoNotExist_renameFile()