propagateremotemove.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (C) by Olivier Goffart <ogoffart@owncloud.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. */
  14. #include "propagateremotemove.h"
  15. #include "owncloudpropagator_p.h"
  16. #include "account.h"
  17. #include "syncjournalfilerecord.h"
  18. #include "filesystem.h"
  19. #include <QFile>
  20. #include <QStringList>
  21. namespace OCC {
  22. MoveJob::MoveJob(AccountPtr account, const QString& path,
  23. const QString &destination, QObject* parent)
  24. : AbstractNetworkJob(account, path, parent), _destination(destination)
  25. { }
  26. void MoveJob::start()
  27. {
  28. QNetworkRequest req;
  29. req.setRawHeader("Destination", QUrl::toPercentEncoding(_destination, "/"));
  30. setReply(davRequest("MOVE", path(), req));
  31. setupConnections(reply());
  32. if( reply()->error() != QNetworkReply::NoError ) {
  33. qWarning() << Q_FUNC_INFO << " Network error: " << reply()->errorString();
  34. }
  35. AbstractNetworkJob::start();
  36. }
  37. QString MoveJob::errorString()
  38. {
  39. if (_timedout) {
  40. return tr("Connection timed out");
  41. } else if (reply()->hasRawHeader("OC-ErrorString")) {
  42. return reply()->rawHeader("OC-ErrorString");
  43. } else {
  44. return reply()->errorString();
  45. }
  46. }
  47. bool MoveJob::finished()
  48. {
  49. emit finishedSignal();
  50. return true;
  51. }
  52. void PropagateRemoteMove::start()
  53. {
  54. if (_propagator->_abortRequested.fetchAndAddRelaxed(0))
  55. return;
  56. qDebug() << Q_FUNC_INFO << _item->_file << _item->_renameTarget;
  57. QString targetFile(_propagator->getFilePath(_item->_renameTarget));
  58. if (_item->_file == _item->_renameTarget) {
  59. // The parent has been renamed already so there is nothing more to do.
  60. finalize();
  61. return;
  62. }
  63. if (_item->_file == QLatin1String("Shared") ) {
  64. // Before owncloud 7, there was no permissions system. At the time all the shared files were
  65. // in a directory called "Shared" and were not supposed to be moved, otherwise bad things happened
  66. QString versionString = _propagator->account()->serverVersion();
  67. if (versionString.contains('.') && versionString.split('.')[0].toInt() < 7) {
  68. QString originalFile(_propagator->getFilePath(QLatin1String("Shared")));
  69. _propagator->addTouchedFile(originalFile);
  70. _propagator->addTouchedFile(targetFile);
  71. QString renameError;
  72. if( FileSystem::rename(targetFile, originalFile, &renameError) ) {
  73. done(SyncFileItem::NormalError, tr("This folder must not be renamed. It is renamed back to its original name."));
  74. } else {
  75. done(SyncFileItem::NormalError, tr("This folder must not be renamed. Please name it back to Shared."));
  76. }
  77. return;
  78. }
  79. }
  80. _job = new MoveJob(_propagator->account(),
  81. _propagator->_remoteFolder + _item->_file,
  82. _propagator->_remoteDir + _item->_renameTarget,
  83. this);
  84. connect(_job, SIGNAL(finishedSignal()), this, SLOT(slotMoveJobFinished()));
  85. _propagator->_activeJobs++;
  86. _job->start();
  87. }
  88. void PropagateRemoteMove::abort()
  89. {
  90. if (_job && _job->reply())
  91. _job->reply()->abort();
  92. }
  93. void PropagateRemoteMove::slotMoveJobFinished()
  94. {
  95. _propagator->_activeJobs--;
  96. Q_ASSERT(_job);
  97. qDebug() << Q_FUNC_INFO << _job->reply()->request().url() << "FINISHED WITH STATUS"
  98. << _job->reply()->error()
  99. << (_job->reply()->error() == QNetworkReply::NoError ? QLatin1String("") : _job->reply()->errorString());
  100. QNetworkReply::NetworkError err = _job->reply()->error();
  101. _item->_httpErrorCode = _job->reply()->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
  102. if (err != QNetworkReply::NoError) {
  103. if( checkForProblemsWithShared(_item->_httpErrorCode,
  104. tr("The file was renamed but is part of a read only share. The original file was restored."))) {
  105. return;
  106. }
  107. SyncFileItem::Status status = classifyError(err, _item->_httpErrorCode,
  108. &_propagator->_anotherSyncNeeded);
  109. done(status, _job->errorString());
  110. return;
  111. }
  112. _item->_requestDuration = _job->duration();
  113. _item->_responseTimeStamp = _job->responseTimestamp();
  114. if (_item->_httpErrorCode != 201 ) {
  115. // Normally we expect "201 Created"
  116. // If it is not the case, it might be because of a proxy or gateway intercepting the request, so we must
  117. // throw an error.
  118. done(SyncFileItem::NormalError, tr("Wrong HTTP code returned by server. Expected 201, but received \"%1 %2\".")
  119. .arg(_item->_httpErrorCode).arg(_job->reply()->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString()));
  120. return;
  121. }
  122. finalize();
  123. }
  124. void PropagateRemoteMove::finalize()
  125. {
  126. SyncJournalFileRecord oldRecord =
  127. _propagator->_journal->getFileRecord(_item->_originalFile);
  128. _propagator->_journal->deleteFileRecord(_item->_originalFile);
  129. SyncJournalFileRecord record(*_item, _propagator->getFilePath(_item->_renameTarget));
  130. record._path = _item->_renameTarget;
  131. record._contentChecksum = oldRecord._contentChecksum;
  132. record._contentChecksumType = oldRecord._contentChecksumType;
  133. _propagator->_journal->setFileRecord(record);
  134. _propagator->_journal->commit("Remote Rename");
  135. done(SyncFileItem::Success);
  136. }
  137. }