2014-02-18 14:52:38 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) by Olivier Goffart <ogoffart@owncloud.com>
|
|
|
|
* Copyright (C) by Klaas Freitag <freitag@owncloud.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
|
2020-06-26 10:43:06 +03:00
|
|
|
#include "account.h"
|
|
|
|
#include "propagatedownloadencrypted.h"
|
2014-02-18 14:52:38 +04:00
|
|
|
#include "propagatorjobs.h"
|
2017-12-25 13:46:38 +03:00
|
|
|
#include "owncloudpropagator.h"
|
2014-02-18 14:52:38 +04:00
|
|
|
#include "owncloudpropagator_p.h"
|
2016-10-10 17:55:31 +03:00
|
|
|
#include "propagateremotemove.h"
|
2017-08-16 09:36:52 +03:00
|
|
|
#include "common/utility.h"
|
2017-09-01 19:11:43 +03:00
|
|
|
#include "common/syncjournaldb.h"
|
|
|
|
#include "common/syncjournalfilerecord.h"
|
2015-02-25 12:51:05 +03:00
|
|
|
#include "filesystem.h"
|
2014-02-18 14:52:38 +04:00
|
|
|
#include <qfile.h>
|
|
|
|
#include <qdir.h>
|
|
|
|
#include <qdiriterator.h>
|
|
|
|
#include <qtemporaryfile.h>
|
|
|
|
#include <qsavefile.h>
|
|
|
|
#include <QDateTime>
|
|
|
|
#include <qstack.h>
|
|
|
|
#include <QCoreApplication>
|
|
|
|
|
2020-08-13 14:00:56 +03:00
|
|
|
#include <ctime>
|
2014-02-18 14:52:38 +04:00
|
|
|
|
|
|
|
|
2014-11-10 00:34:07 +03:00
|
|
|
namespace OCC {
|
2014-02-18 14:52:38 +04:00
|
|
|
|
2017-12-28 22:33:10 +03:00
|
|
|
Q_LOGGING_CATEGORY(lcPropagateLocalRemove, "nextcloud.sync.propagator.localremove", QtInfoMsg)
|
|
|
|
Q_LOGGING_CATEGORY(lcPropagateLocalMkdir, "nextcloud.sync.propagator.localmkdir", QtInfoMsg)
|
|
|
|
Q_LOGGING_CATEGORY(lcPropagateLocalRename, "nextcloud.sync.propagator.localrename", QtInfoMsg)
|
2017-05-09 15:24:11 +03:00
|
|
|
|
2017-05-10 10:37:10 +03:00
|
|
|
QByteArray localFileIdFromFullId(const QByteArray &id)
|
|
|
|
{
|
|
|
|
return id.left(8);
|
|
|
|
}
|
|
|
|
|
2015-09-02 16:19:34 +03:00
|
|
|
/**
|
|
|
|
* The code will update the database in case of error.
|
2015-10-05 06:20:09 +03:00
|
|
|
* If everything goes well (no error, returns true), the caller is responsible for removing the entries
|
2015-09-02 16:19:34 +03:00
|
|
|
* in the database. But in case of error, we need to remove the entries from the database of the files
|
|
|
|
* that were deleted.
|
|
|
|
*
|
2017-01-18 12:19:05 +03:00
|
|
|
* \a path is relative to propagator()->_localDir + _item->_file and should start with a slash
|
2015-09-02 16:19:34 +03:00
|
|
|
*/
|
|
|
|
bool PropagateLocalRemove::removeRecursively(const QString &path)
|
2014-02-18 14:52:38 +04:00
|
|
|
{
|
2020-09-22 12:47:40 +03:00
|
|
|
QString absolute = propagator()->fullLocalPath(_item->_file + path);
|
2018-05-22 12:49:02 +03:00
|
|
|
QStringList errors;
|
|
|
|
QList<QPair<QString, bool>> deleted;
|
|
|
|
bool success = FileSystem::removeRecursively(
|
|
|
|
absolute,
|
2018-12-07 12:06:56 +03:00
|
|
|
[&deleted](const QString &path, bool isDir) {
|
2018-05-22 12:49:02 +03:00
|
|
|
// by prepending, a folder deletion may be followed by content deletions
|
|
|
|
deleted.prepend(qMakePair(path, isDir));
|
|
|
|
},
|
|
|
|
&errors);
|
2015-09-02 16:19:34 +03:00
|
|
|
|
2018-05-22 12:49:02 +03:00
|
|
|
if (!success) {
|
|
|
|
// We need to delete the entries from the database now from the deleted vector.
|
|
|
|
// Do it while avoiding redundant delete calls to the journal.
|
|
|
|
QString deletedDir;
|
|
|
|
foreach (const auto &it, deleted) {
|
2020-09-22 12:47:40 +03:00
|
|
|
if (!it.first.startsWith(propagator()->localPath()))
|
2018-05-22 12:49:02 +03:00
|
|
|
continue;
|
|
|
|
if (!deletedDir.isEmpty() && it.first.startsWith(deletedDir))
|
|
|
|
continue;
|
|
|
|
if (it.second) {
|
|
|
|
deletedDir = it.first;
|
2015-09-02 16:19:34 +03:00
|
|
|
}
|
2020-09-22 12:47:40 +03:00
|
|
|
propagator()->_journal->deleteFileRecord(it.first.mid(propagator()->localPath().size()), it.second);
|
2014-10-29 14:23:48 +03:00
|
|
|
}
|
2018-05-22 12:49:02 +03:00
|
|
|
|
|
|
|
_error = errors.join(", ");
|
2014-10-29 14:23:48 +03:00
|
|
|
}
|
2014-02-18 14:52:38 +04:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PropagateLocalRemove::start()
|
|
|
|
{
|
2021-03-19 12:40:26 +03:00
|
|
|
qCInfo(lcPropagateLocalRemove) << "Start propagate local remove job";
|
|
|
|
|
2017-12-25 13:46:38 +03:00
|
|
|
_moveToTrash = propagator()->syncOptions()._moveFilesToTrash;
|
|
|
|
|
2020-02-05 14:57:09 +03:00
|
|
|
if (propagator()->_abortRequested)
|
2014-02-18 14:52:38 +04:00
|
|
|
return;
|
|
|
|
|
2020-09-22 12:47:40 +03:00
|
|
|
const QString filename = propagator()->fullLocalPath(_item->_file);
|
2021-03-19 12:40:26 +03:00
|
|
|
qCInfo(lcPropagateLocalRemove) << "Going to delete:" << filename;
|
2015-11-23 21:33:49 +03:00
|
|
|
|
2017-01-17 16:29:12 +03:00
|
|
|
if (propagator()->localFileNameClash(_item->_file)) {
|
2014-05-26 20:17:18 +04:00
|
|
|
done(SyncFileItem::NormalError, tr("Could not remove %1 because of a local file name clash").arg(QDir::toNativeSeparators(filename)));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-25 13:46:38 +03:00
|
|
|
QString removeError;
|
|
|
|
if (_moveToTrash) {
|
|
|
|
if ((QDir(filename).exists() || FileSystem::fileExists(filename))
|
|
|
|
&& !FileSystem::moveToTrash(filename, &removeError)) {
|
2016-01-05 13:58:18 +03:00
|
|
|
done(SyncFileItem::NormalError, removeError);
|
2014-02-18 14:52:38 +04:00
|
|
|
return;
|
|
|
|
}
|
2018-01-25 16:35:21 +03:00
|
|
|
} else {
|
2017-12-25 13:46:38 +03:00
|
|
|
if (_item->isDirectory()) {
|
|
|
|
if (QDir(filename).exists() && !removeRecursively(QString())) {
|
|
|
|
done(SyncFileItem::NormalError, _error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (FileSystem::fileExists(filename)
|
|
|
|
&& !FileSystem::remove(filename, &removeError)) {
|
|
|
|
done(SyncFileItem::NormalError, removeError);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2014-02-18 14:52:38 +04:00
|
|
|
}
|
2017-02-14 14:46:44 +03:00
|
|
|
propagator()->reportProgress(*_item, 0);
|
2017-08-24 18:31:46 +03:00
|
|
|
propagator()->_journal->deleteFileRecord(_item->_originalFile, _item->isDirectory());
|
2017-01-17 16:29:12 +03:00
|
|
|
propagator()->_journal->commit("Local remove");
|
2014-02-18 14:52:38 +04:00
|
|
|
done(SyncFileItem::Success);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PropagateLocalMkdir::start()
|
|
|
|
{
|
2020-02-05 14:57:09 +03:00
|
|
|
if (propagator()->_abortRequested)
|
2014-02-18 14:52:38 +04:00
|
|
|
return;
|
|
|
|
|
2020-12-07 18:33:36 +03:00
|
|
|
startLocalMkdir();
|
2020-06-26 10:43:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void PropagateLocalMkdir::setDeleteExistingFile(bool enabled)
|
|
|
|
{
|
|
|
|
_deleteExistingFile = enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PropagateLocalMkdir::startLocalMkdir()
|
|
|
|
{
|
2020-09-22 12:47:40 +03:00
|
|
|
QDir newDir(propagator()->fullLocalPath(_item->_file));
|
2014-05-22 12:16:33 +04:00
|
|
|
QString newDirStr = QDir::toNativeSeparators(newDir.path());
|
2015-12-22 13:26:07 +03:00
|
|
|
|
|
|
|
// When turning something that used to be a file into a directory
|
|
|
|
// we need to delete the file first.
|
|
|
|
QFileInfo fi(newDirStr);
|
2018-01-17 12:59:47 +03:00
|
|
|
if (fi.exists() && fi.isFile()) {
|
|
|
|
if (_deleteExistingFile) {
|
|
|
|
QString removeError;
|
|
|
|
if (!FileSystem::remove(newDirStr, &removeError)) {
|
|
|
|
done(SyncFileItem::NormalError,
|
|
|
|
tr("could not delete file %1, error: %2")
|
|
|
|
.arg(newDirStr, removeError));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (_item->_instruction == CSYNC_INSTRUCTION_CONFLICT) {
|
|
|
|
QString error;
|
|
|
|
if (!propagator()->createConflict(_item, _associatedComposite, &error)) {
|
|
|
|
done(SyncFileItem::SoftError, error);
|
|
|
|
return;
|
|
|
|
}
|
2015-12-22 13:26:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-17 16:29:12 +03:00
|
|
|
if (Utility::fsCasePreserving() && propagator()->localFileNameClash(_item->_file)) {
|
2017-05-11 16:36:47 +03:00
|
|
|
qCWarning(lcPropagateLocalMkdir) << "New folder to create locally already exists with different case:" << _item->_file;
|
2014-05-22 12:16:33 +04:00
|
|
|
done(SyncFileItem::NormalError, tr("Attention, possible case sensitivity clash with %1").arg(newDirStr));
|
2014-05-16 17:20:32 +04:00
|
|
|
return;
|
|
|
|
}
|
2017-01-17 16:29:12 +03:00
|
|
|
emit propagator()->touchedFile(newDirStr);
|
2020-09-22 12:47:40 +03:00
|
|
|
QDir localDir(propagator()->localPath());
|
2015-04-15 16:19:11 +03:00
|
|
|
if (!localDir.mkpath(_item->_file)) {
|
2021-04-26 20:27:26 +03:00
|
|
|
done(SyncFileItem::NormalError, tr("Could not create folder %1").arg(newDirStr));
|
2014-02-18 14:52:38 +04:00
|
|
|
return;
|
|
|
|
}
|
2015-06-17 14:56:37 +03:00
|
|
|
|
|
|
|
// Insert the directory into the database. The correct etag will be set later,
|
2015-07-09 16:05:37 +03:00
|
|
|
// once all contents have been propagated, because should_update_metadata is true.
|
2015-06-17 14:56:37 +03:00
|
|
|
// Adding an entry with a dummy etag to the database still makes sense here
|
|
|
|
// so the database is aware that this folder exists even if the sync is aborted
|
|
|
|
// before the correct etag is stored.
|
2019-01-21 13:19:45 +03:00
|
|
|
SyncFileItem newItem(*_item);
|
|
|
|
newItem._etag = "_invalid_";
|
2021-06-28 13:32:57 +03:00
|
|
|
const auto result = propagator()->updateMetadata(newItem);
|
|
|
|
if (!result) {
|
|
|
|
done(SyncFileItem::FatalError, tr("Error updating metadata: %1").arg(result.error()));
|
|
|
|
return;
|
|
|
|
} else if (*result == Vfs::ConvertToPlaceholderResult::Locked) {
|
|
|
|
done(SyncFileItem::SoftError, tr("The file %1 is currently in use").arg(newItem._file));
|
2016-04-07 12:47:04 +03:00
|
|
|
return;
|
|
|
|
}
|
2017-01-17 16:29:12 +03:00
|
|
|
propagator()->_journal->commit("localMkdir");
|
2015-06-17 14:56:37 +03:00
|
|
|
|
2018-01-17 12:59:47 +03:00
|
|
|
auto resultStatus = _item->_instruction == CSYNC_INSTRUCTION_CONFLICT
|
|
|
|
? SyncFileItem::Conflict
|
|
|
|
: SyncFileItem::Success;
|
|
|
|
done(resultStatus);
|
2014-02-18 14:52:38 +04:00
|
|
|
}
|
|
|
|
|
2014-02-18 16:52:40 +04:00
|
|
|
void PropagateLocalRename::start()
|
|
|
|
{
|
2020-02-05 14:57:09 +03:00
|
|
|
if (propagator()->_abortRequested)
|
2014-02-18 16:52:40 +04:00
|
|
|
return;
|
|
|
|
|
2020-09-22 12:47:40 +03:00
|
|
|
QString existingFile = propagator()->fullLocalPath(propagator()->adjustRenamedPath(_item->_file));
|
|
|
|
QString targetFile = propagator()->fullLocalPath(_item->_renameTarget);
|
2014-11-07 13:41:21 +03:00
|
|
|
|
2015-04-15 16:19:11 +03:00
|
|
|
// if the file is a file underneath a moved dir, the _item->file is equal
|
|
|
|
// to _item->renameTarget and the file is not moved as a result.
|
|
|
|
if (_item->_file != _item->_renameTarget) {
|
2017-02-14 14:46:44 +03:00
|
|
|
propagator()->reportProgress(*_item, 0);
|
2017-05-09 15:24:11 +03:00
|
|
|
qCDebug(lcPropagateLocalRename) << "MOVE " << existingFile << " => " << targetFile;
|
2014-05-26 19:00:40 +04:00
|
|
|
|
2015-04-15 16:19:11 +03:00
|
|
|
if (QString::compare(_item->_file, _item->_renameTarget, Qt::CaseInsensitive) != 0
|
2017-01-17 16:29:12 +03:00
|
|
|
&& propagator()->localFileNameClash(_item->_renameTarget)) {
|
2014-10-17 18:11:25 +04:00
|
|
|
// Only use localFileNameClash for the destination if we know that the source was not
|
|
|
|
// the one conflicting (renaming A.txt -> a.txt is OK)
|
|
|
|
|
2014-10-17 17:58:48 +04:00
|
|
|
// Fixme: the file that is the reason for the clash could be named here,
|
|
|
|
// it would have to come out the localFileNameClash function
|
2017-05-17 11:54:57 +03:00
|
|
|
done(SyncFileItem::NormalError,
|
2021-04-12 13:38:15 +03:00
|
|
|
tr("File %1 cannot be renamed to %2 because of a local file name clash")
|
2017-05-17 11:54:57 +03:00
|
|
|
.arg(QDir::toNativeSeparators(_item->_file))
|
|
|
|
.arg(QDir::toNativeSeparators(_item->_renameTarget)));
|
2014-10-17 17:58:48 +04:00
|
|
|
return;
|
|
|
|
}
|
2014-11-07 13:41:21 +03:00
|
|
|
|
2017-01-17 16:29:12 +03:00
|
|
|
emit propagator()->touchedFile(existingFile);
|
|
|
|
emit propagator()->touchedFile(targetFile);
|
2015-03-11 12:51:36 +03:00
|
|
|
QString renameError;
|
|
|
|
if (!FileSystem::rename(existingFile, targetFile, &renameError)) {
|
|
|
|
done(SyncFileItem::NormalError, renameError);
|
2014-05-26 17:01:26 +04:00
|
|
|
return;
|
2014-05-26 16:51:53 +04:00
|
|
|
}
|
2014-02-18 16:52:40 +04:00
|
|
|
}
|
|
|
|
|
2017-09-13 20:02:38 +03:00
|
|
|
SyncJournalFileRecord oldRecord;
|
|
|
|
propagator()->_journal->getFileRecord(_item->_originalFile, &oldRecord);
|
2017-01-17 16:29:12 +03:00
|
|
|
propagator()->_journal->deleteFileRecord(_item->_originalFile);
|
2014-02-18 16:52:40 +04:00
|
|
|
|
2019-06-21 10:39:33 +03:00
|
|
|
auto &vfs = propagator()->syncOptions()._vfs;
|
|
|
|
auto pinState = vfs->pinState(_item->_originalFile);
|
2021-07-23 18:30:48 +03:00
|
|
|
if (!vfs->setPinState(_item->_originalFile, PinState::Inherited)) {
|
|
|
|
qCWarning(lcPropagateLocalRename) << "Could not set pin state of" << _item->_originalFile << "to inherited";
|
|
|
|
}
|
2019-06-21 10:39:33 +03:00
|
|
|
|
2016-10-10 17:55:31 +03:00
|
|
|
const auto oldFile = _item->_file;
|
2014-02-18 16:52:40 +04:00
|
|
|
|
2017-08-24 18:31:46 +03:00
|
|
|
if (!_item->isDirectory()) { // Directories are saved at the end
|
2019-01-21 13:19:45 +03:00
|
|
|
SyncFileItem newItem(*_item);
|
|
|
|
if (oldRecord.isValid()) {
|
|
|
|
newItem._checksumHeader = oldRecord._checksumHeader;
|
|
|
|
}
|
2021-06-28 13:32:57 +03:00
|
|
|
const auto result = propagator()->updateMetadata(newItem);
|
|
|
|
if (!result) {
|
|
|
|
done(SyncFileItem::FatalError, tr("Error updating metadata: %1").arg(result.error()));
|
|
|
|
return;
|
|
|
|
} else if (*result == Vfs::ConvertToPlaceholderResult::Locked) {
|
|
|
|
done(SyncFileItem::SoftError, tr("The file %1 is currently in use").arg(newItem._file));
|
2016-04-07 12:47:04 +03:00
|
|
|
return;
|
|
|
|
}
|
2016-10-10 17:55:31 +03:00
|
|
|
} else {
|
2018-12-23 12:19:20 +03:00
|
|
|
propagator()->_renamedDirectories.insert(oldFile, _item->_renameTarget);
|
2017-01-17 16:29:12 +03:00
|
|
|
if (!PropagateRemoteMove::adjustSelectiveSync(propagator()->_journal, oldFile, _item->_renameTarget)) {
|
2021-06-28 13:32:57 +03:00
|
|
|
done(SyncFileItem::FatalError, tr("Failed to rename file"));
|
2016-10-10 17:55:31 +03:00
|
|
|
return;
|
|
|
|
}
|
2014-02-18 16:52:40 +04:00
|
|
|
}
|
2019-06-21 10:39:33 +03:00
|
|
|
if (pinState && *pinState != PinState::Inherited
|
|
|
|
&& !vfs->setPinState(_item->_renameTarget, *pinState)) {
|
|
|
|
done(SyncFileItem::NormalError, tr("Error setting pin state"));
|
|
|
|
return;
|
|
|
|
}
|
2014-02-18 16:52:40 +04:00
|
|
|
|
2017-01-17 16:29:12 +03:00
|
|
|
propagator()->_journal->commit("localRename");
|
2014-02-18 16:52:40 +04:00
|
|
|
|
|
|
|
done(SyncFileItem::Success);
|
|
|
|
}
|
2014-02-18 14:52:38 +04:00
|
|
|
}
|