2014-11-11 18:09:01 +03:00
|
|
|
/*
|
|
|
|
* Copyright (C) by Olivier Goffart <ogoffart@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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "propagateremotemove.h"
|
|
|
|
#include "owncloudpropagator_p.h"
|
|
|
|
#include "account.h"
|
|
|
|
#include "syncjournalfilerecord.h"
|
2015-03-11 12:51:36 +03:00
|
|
|
#include "filesystem.h"
|
2014-11-11 18:09:01 +03:00
|
|
|
#include <QFile>
|
2015-02-12 22:10:31 +03:00
|
|
|
#include <QStringList>
|
2014-11-11 18:09:01 +03:00
|
|
|
|
2014-12-02 16:20:13 +03:00
|
|
|
namespace OCC {
|
2014-11-11 18:09:01 +03:00
|
|
|
|
2014-12-18 14:09:48 +03:00
|
|
|
MoveJob::MoveJob(AccountPtr account, const QString& path,
|
2014-11-11 18:09:01 +03:00
|
|
|
const QString &destination, QObject* parent)
|
|
|
|
: AbstractNetworkJob(account, path, parent), _destination(destination)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
|
|
void MoveJob::start()
|
|
|
|
{
|
|
|
|
QNetworkRequest req;
|
2014-11-11 18:27:06 +03:00
|
|
|
req.setRawHeader("Destination", QUrl::toPercentEncoding(_destination, "/"));
|
2014-11-11 18:09:01 +03:00
|
|
|
setReply(davRequest("MOVE", path(), req));
|
|
|
|
setupConnections(reply());
|
|
|
|
|
|
|
|
if( reply()->error() != QNetworkReply::NoError ) {
|
|
|
|
qWarning() << Q_FUNC_INFO << " Network error: " << reply()->errorString();
|
|
|
|
}
|
|
|
|
AbstractNetworkJob::start();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QString MoveJob::errorString()
|
|
|
|
{
|
2015-02-26 17:52:07 +03:00
|
|
|
if (_timedout) {
|
|
|
|
return tr("Connection timed out");
|
|
|
|
} else if (reply()->hasRawHeader("OC-ErrorString")) {
|
|
|
|
return reply()->rawHeader("OC-ErrorString");
|
|
|
|
} else {
|
|
|
|
return reply()->errorString();
|
|
|
|
}
|
2014-11-11 18:09:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MoveJob::finished()
|
|
|
|
{
|
|
|
|
emit finishedSignal();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PropagateRemoteMove::start()
|
|
|
|
{
|
|
|
|
if (_propagator->_abortRequested.fetchAndAddRelaxed(0))
|
|
|
|
return;
|
|
|
|
|
2015-04-15 16:19:11 +03:00
|
|
|
qDebug() << Q_FUNC_INFO << _item->_file << _item->_renameTarget;
|
2014-11-11 18:09:01 +03:00
|
|
|
|
2015-04-15 16:19:11 +03:00
|
|
|
QString targetFile(_propagator->getFilePath(_item->_renameTarget));
|
2014-11-07 13:41:21 +03:00
|
|
|
|
2015-04-15 16:19:11 +03:00
|
|
|
if (_item->_file == _item->_renameTarget) {
|
2015-10-05 06:20:09 +03:00
|
|
|
// The parent has been renamed already so there is nothing more to do.
|
2014-11-11 18:09:01 +03:00
|
|
|
finalize();
|
|
|
|
return;
|
2015-02-12 14:59:00 +03:00
|
|
|
}
|
2015-04-15 16:19:11 +03:00
|
|
|
if (_item->_file == QLatin1String("Shared") ) {
|
2015-02-12 14:59:00 +03:00
|
|
|
// Before owncloud 7, there was no permissions system. At the time all the shared files were
|
2015-10-05 06:20:09 +03:00
|
|
|
// in a directory called "Shared" and were not supposed to be moved, otherwise bad things happened
|
2015-02-12 14:59:00 +03:00
|
|
|
|
|
|
|
QString versionString = _propagator->account()->serverVersion();
|
|
|
|
if (versionString.contains('.') && versionString.split('.')[0].toInt() < 7) {
|
|
|
|
QString originalFile(_propagator->getFilePath(QLatin1String("Shared")));
|
|
|
|
_propagator->addTouchedFile(originalFile);
|
|
|
|
_propagator->addTouchedFile(targetFile);
|
2015-03-11 12:51:36 +03:00
|
|
|
QString renameError;
|
|
|
|
if( FileSystem::rename(targetFile, originalFile, &renameError) ) {
|
2015-02-12 14:59:00 +03:00
|
|
|
done(SyncFileItem::NormalError, tr("This folder must not be renamed. It is renamed back to its original name."));
|
|
|
|
} else {
|
|
|
|
done(SyncFileItem::NormalError, tr("This folder must not be renamed. Please name it back to Shared."));
|
|
|
|
}
|
|
|
|
return;
|
2014-11-11 18:09:01 +03:00
|
|
|
}
|
|
|
|
}
|
2015-02-12 14:59:00 +03:00
|
|
|
|
|
|
|
_job = new MoveJob(_propagator->account(),
|
2015-04-15 16:19:11 +03:00
|
|
|
_propagator->_remoteFolder + _item->_file,
|
|
|
|
_propagator->_remoteDir + _item->_renameTarget,
|
2015-02-12 14:59:00 +03:00
|
|
|
this);
|
|
|
|
connect(_job, SIGNAL(finishedSignal()), this, SLOT(slotMoveJobFinished()));
|
2016-02-25 19:40:24 +03:00
|
|
|
_propagator->_activeJobList.append(this);
|
2015-02-12 14:59:00 +03:00
|
|
|
_job->start();
|
|
|
|
|
2014-11-11 18:09:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void PropagateRemoteMove::abort()
|
|
|
|
{
|
|
|
|
if (_job && _job->reply())
|
|
|
|
_job->reply()->abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PropagateRemoteMove::slotMoveJobFinished()
|
|
|
|
{
|
2016-02-25 19:40:24 +03:00
|
|
|
_propagator->_activeJobList.removeOne(this);
|
2014-11-11 18:09:01 +03:00
|
|
|
|
|
|
|
Q_ASSERT(_job);
|
|
|
|
|
|
|
|
qDebug() << Q_FUNC_INFO << _job->reply()->request().url() << "FINISHED WITH STATUS"
|
|
|
|
<< _job->reply()->error()
|
|
|
|
<< (_job->reply()->error() == QNetworkReply::NoError ? QLatin1String("") : _job->reply()->errorString());
|
|
|
|
|
|
|
|
QNetworkReply::NetworkError err = _job->reply()->error();
|
2015-04-15 16:19:11 +03:00
|
|
|
_item->_httpErrorCode = _job->reply()->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
2014-11-11 18:09:01 +03:00
|
|
|
|
|
|
|
if (err != QNetworkReply::NoError) {
|
|
|
|
|
2015-04-15 16:19:11 +03:00
|
|
|
if( checkForProblemsWithShared(_item->_httpErrorCode,
|
2014-11-11 18:09:01 +03:00
|
|
|
tr("The file was renamed but is part of a read only share. The original file was restored."))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-30 16:34:50 +03:00
|
|
|
SyncFileItem::Status status = classifyError(err, _item->_httpErrorCode,
|
|
|
|
&_propagator->_anotherSyncNeeded);
|
2014-11-11 18:09:01 +03:00
|
|
|
done(status, _job->errorString());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-15 16:19:11 +03:00
|
|
|
_item->_requestDuration = _job->duration();
|
|
|
|
_item->_responseTimeStamp = _job->responseTimestamp();
|
2014-11-11 18:09:01 +03:00
|
|
|
|
2015-04-15 16:19:11 +03:00
|
|
|
if (_item->_httpErrorCode != 201 ) {
|
2015-10-05 06:20:09 +03:00
|
|
|
// Normally we expect "201 Created"
|
2014-11-11 18:09:01 +03:00
|
|
|
// If it is not the case, it might be because of a proxy or gateway intercepting the request, so we must
|
|
|
|
// throw an error.
|
2015-02-06 00:00:13 +03:00
|
|
|
done(SyncFileItem::NormalError, tr("Wrong HTTP code returned by server. Expected 201, but received \"%1 %2\".")
|
2015-04-15 16:19:11 +03:00
|
|
|
.arg(_item->_httpErrorCode).arg(_job->reply()->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString()));
|
2014-11-11 18:09:01 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
finalize();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void PropagateRemoteMove::finalize()
|
|
|
|
{
|
2015-11-10 17:05:00 +03:00
|
|
|
SyncJournalFileRecord oldRecord =
|
|
|
|
_propagator->_journal->getFileRecord(_item->_originalFile);
|
2015-04-15 16:19:11 +03:00
|
|
|
_propagator->_journal->deleteFileRecord(_item->_originalFile);
|
2015-11-10 17:05:00 +03:00
|
|
|
|
2015-04-15 16:19:11 +03:00
|
|
|
SyncJournalFileRecord record(*_item, _propagator->getFilePath(_item->_renameTarget));
|
|
|
|
record._path = _item->_renameTarget;
|
2015-11-23 13:53:06 +03:00
|
|
|
record._contentChecksum = oldRecord._contentChecksum;
|
|
|
|
record._contentChecksumType = oldRecord._contentChecksumType;
|
2014-11-11 18:09:01 +03:00
|
|
|
|
|
|
|
_propagator->_journal->setFileRecord(record);
|
|
|
|
_propagator->_journal->commit("Remote Rename");
|
|
|
|
done(SyncFileItem::Success);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|