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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "propagatorjobs.h"
|
|
|
|
#include "owncloudpropagator_p.h"
|
|
|
|
|
|
|
|
#include "utility.h"
|
|
|
|
#include "syncjournaldb.h"
|
|
|
|
#include "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>
|
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
|
|
|
|
#include <qabstractfileengine.h>
|
|
|
|
#else
|
|
|
|
#include <qsavefile.h>
|
|
|
|
#endif
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QDateTime>
|
|
|
|
#include <qstack.h>
|
|
|
|
#include <QCoreApplication>
|
|
|
|
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
|
2014-11-10 00:34:07 +03:00
|
|
|
namespace OCC {
|
2014-02-18 14:52:38 +04:00
|
|
|
|
|
|
|
// Code copied from Qt5's QDir::removeRecursively
|
2014-10-29 14:23:48 +03:00
|
|
|
// (and modified to report the error)
|
|
|
|
static bool removeRecursively(const QString &path, QString &error)
|
2014-02-18 14:52:38 +04:00
|
|
|
{
|
|
|
|
bool success = true;
|
|
|
|
QDirIterator di(path, QDir::AllEntries | QDir::Hidden | QDir::System | QDir::NoDotAndDotDot);
|
|
|
|
while (di.hasNext()) {
|
|
|
|
di.next();
|
|
|
|
const QFileInfo& fi = di.fileInfo();
|
|
|
|
bool ok;
|
2015-02-12 13:02:56 +03:00
|
|
|
// The use of isSymLink here is okay:
|
|
|
|
// we never want to go into this branch for .lnk files
|
2014-10-29 14:23:48 +03:00
|
|
|
if (fi.isDir() && !fi.isSymLink()) {
|
|
|
|
ok = removeRecursively(di.filePath(), error); // recursive
|
|
|
|
} else {
|
|
|
|
QFile f(di.filePath());
|
|
|
|
ok = f.remove();
|
|
|
|
if (!ok) {
|
2015-02-06 00:00:13 +03:00
|
|
|
error += PropagateLocalRemove::tr("Error removing '%1': %2;").
|
|
|
|
arg(QDir::toNativeSeparators(f.fileName()), f.errorString()) + " ";
|
2014-10-29 14:23:48 +03:00
|
|
|
qDebug() << "Error removing " << f.fileName() << ':' << f.errorString();
|
|
|
|
}
|
|
|
|
}
|
2014-02-18 14:52:38 +04:00
|
|
|
if (!ok)
|
|
|
|
success = false;
|
|
|
|
}
|
2014-10-29 14:23:48 +03:00
|
|
|
if (success) {
|
2014-02-18 14:52:38 +04:00
|
|
|
success = QDir().rmdir(path);
|
2014-10-29 14:23:48 +03:00
|
|
|
if (!success) {
|
2015-02-06 00:00:13 +03:00
|
|
|
error += PropagateLocalRemove::tr("Could not remove directory '%1';")
|
|
|
|
.arg(QDir::toNativeSeparators(path)) + " ";
|
2014-10-29 14:23:48 +03:00
|
|
|
qDebug() << "Error removing directory" << path;
|
|
|
|
}
|
|
|
|
}
|
2014-02-18 14:52:38 +04:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PropagateLocalRemove::start()
|
|
|
|
{
|
|
|
|
if (_propagator->_abortRequested.fetchAndAddRelaxed(0))
|
|
|
|
return;
|
|
|
|
|
2015-04-15 16:19:11 +03:00
|
|
|
QString filename = _propagator->_localDir + _item->_file;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-04-15 16:19:11 +03:00
|
|
|
if (_item->_isDirectory) {
|
2014-10-29 14:23:48 +03:00
|
|
|
QString error;
|
|
|
|
if (QDir(filename).exists() && !removeRecursively(filename, error)) {
|
|
|
|
done(SyncFileItem::NormalError, error);
|
2014-02-18 14:52:38 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
QFile file(filename);
|
2015-02-27 17:27:49 +03:00
|
|
|
if (FileSystem::fileExists(filename) && !file.remove()) {
|
2014-02-18 14:52:38 +04:00
|
|
|
done(SyncFileItem::NormalError, file.errorString());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2015-04-15 16:19:11 +03:00
|
|
|
emit progress(*_item, 0);
|
|
|
|
_propagator->_journal->deleteFileRecord(_item->_originalFile, _item->_isDirectory);
|
2014-02-18 14:52:38 +04:00
|
|
|
_propagator->_journal->commit("Local remove");
|
|
|
|
done(SyncFileItem::Success);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PropagateLocalMkdir::start()
|
|
|
|
{
|
|
|
|
if (_propagator->_abortRequested.fetchAndAddRelaxed(0))
|
|
|
|
return;
|
|
|
|
|
2015-04-15 16:19:11 +03:00
|
|
|
QDir newDir(_propagator->_localDir + _item->_file);
|
2014-05-22 12:16:33 +04:00
|
|
|
QString newDirStr = QDir::toNativeSeparators(newDir.path());
|
2015-04-15 16:19:11 +03:00
|
|
|
if( Utility::fsCasePreserving() && _propagator->localFileNameClash(_item->_file ) ) {
|
2014-05-16 17:20:32 +04:00
|
|
|
qDebug() << "WARN: new directory to create locally already exists!";
|
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;
|
|
|
|
}
|
2015-06-04 16:02:24 +03:00
|
|
|
_propagator->addTouchedFile(newDirStr);
|
2014-05-22 12:16:33 +04:00
|
|
|
QDir localDir(_propagator->_localDir);
|
2015-04-15 16:19:11 +03:00
|
|
|
if (!localDir.mkpath(_item->_file)) {
|
2014-05-22 12:16:33 +04:00
|
|
|
done( SyncFileItem::NormalError, tr("could not create directory %1").arg(newDirStr) );
|
2014-02-18 14:52:38 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
done(SyncFileItem::Success);
|
|
|
|
}
|
|
|
|
|
2014-02-18 16:52:40 +04:00
|
|
|
void PropagateLocalRename::start()
|
|
|
|
{
|
|
|
|
if (_propagator->_abortRequested.fetchAndAddRelaxed(0))
|
|
|
|
return;
|
|
|
|
|
2015-04-15 16:19:11 +03:00
|
|
|
QString existingFile = _propagator->getFilePath(_item->_file);
|
|
|
|
QString targetFile = _propagator->getFilePath(_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) {
|
|
|
|
emit progress(*_item, 0);
|
2014-11-07 13:41:21 +03:00
|
|
|
qDebug() << "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
|
|
|
|
&& _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
|
|
|
|
done(SyncFileItem::NormalError, tr( "File %1 can not be renamed to %2 because of a local file name clash")
|
2015-04-15 16:19:11 +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
|
|
|
|
|
|
|
_propagator->addTouchedFile(existingFile);
|
|
|
|
_propagator->addTouchedFile(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
|
|
|
}
|
|
|
|
|
2015-04-15 16:19:11 +03:00
|
|
|
_propagator->_journal->deleteFileRecord(_item->_originalFile);
|
2014-02-18 16:52:40 +04:00
|
|
|
|
|
|
|
// store the rename file name in the item.
|
2015-04-15 16:19:11 +03:00
|
|
|
_item->_file = _item->_renameTarget;
|
2014-02-18 16:52:40 +04:00
|
|
|
|
2015-04-15 16:19:11 +03:00
|
|
|
SyncJournalFileRecord record(*_item, targetFile);
|
|
|
|
record._path = _item->_renameTarget;
|
2014-02-18 16:52:40 +04:00
|
|
|
|
2015-04-15 16:19:11 +03:00
|
|
|
if (!_item->_isDirectory) { // Directory are saved at the end
|
2014-02-18 16:52:40 +04:00
|
|
|
_propagator->_journal->setFileRecord(record);
|
|
|
|
}
|
|
|
|
_propagator->_journal->commit("localRename");
|
|
|
|
|
|
|
|
|
|
|
|
done(SyncFileItem::Success);
|
|
|
|
}
|
|
|
|
|
2014-02-18 14:52:38 +04:00
|
|
|
}
|