diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 554b8f153..dc7c05173 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -79,6 +79,7 @@ set(libsync_SRCS mirall/owncloudtheme.cpp mirall/logger.cpp mirall/utility.cpp + mirall/filesystem.cpp mirall/connectionvalidator.cpp mirall/progressdispatcher.cpp mirall/mirallaccessmanager.cpp diff --git a/src/mirall/filesystem.cpp b/src/mirall/filesystem.cpp new file mode 100644 index 000000000..c6422b33c --- /dev/null +++ b/src/mirall/filesystem.cpp @@ -0,0 +1,72 @@ +/* + * Copyright (C) by Daniel Molkentin + * + * 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; version 2 of the License. + * + * 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 "filesystem.h" +#include +#include + +// We use some internals of csync: +extern "C" int c_utimes(const char *, const struct timeval *); +extern "C" void csync_win32_set_file_hidden( const char *file, bool h ); + +namespace Mirall { + +bool FileSystem::fileEquals(const QString& fn1, const QString& fn2) +{ + // compare two files with given filename and return true if they have the same content + QFile f1(fn1); + QFile f2(fn2); + if (!f1.open(QIODevice::ReadOnly) || !f2.open(QIODevice::ReadOnly)) { + qDebug() << "fileEquals: Failed to open " << fn1 << "or" << fn2; + return false; + } + + if (f1.size() != f2.size()) { + return false; + } + + const int BufferSize = 16 * 1024; + char buffer1[BufferSize]; + char buffer2[BufferSize]; + do { + int r = f1.read(buffer1, BufferSize); + if (f2.read(buffer2, BufferSize) != r) { + // this should normaly not happen: the file are supposed to have the same size. + return false; + } + if (r <= 0) { + return true; + } + if (memcmp(buffer1, buffer2, r) != 0) { + return false; + } + } while (true); + return false; +} + +void FileSystem::setFileHidden(const QString& filename, bool hidden) +{ + return csync_win32_set_file_hidden(filename.toUtf8().constData(), hidden); +} + +void FileSystem::setModTime(const QString& filename, time_t modTime) +{ + struct timeval times[2]; + times[0].tv_sec = times[1].tv_sec = modTime; + times[0].tv_usec = times[1].tv_usec = 0; + c_utimes(filename.toUtf8().data(), times); +} + + + +} \ No newline at end of file diff --git a/src/mirall/filesystem.h b/src/mirall/filesystem.h new file mode 100644 index 000000000..27f5e7a2d --- /dev/null +++ b/src/mirall/filesystem.h @@ -0,0 +1,35 @@ +/* + * Copyright (C) by Olivier Goffart + * + * 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; version 2 of the License. + * + * 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. + */ + +#pragma once + +#include +#include + +namespace Mirall { + +/** + * This file contains file system helper. + */ + +namespace FileSystem { + +/** compare two files with given filename and return true if they have the same content */ +bool fileEquals(const QString &fn1, const QString &fn2); + +/** Mark the file as hidden (only has effects on windows) */ +void setFileHidden(const QString& filename, bool hidden); + +void setModTime(const QString &filename, time_t modTime); + +}} diff --git a/src/mirall/owncloudpropagator_p.h b/src/mirall/owncloudpropagator_p.h index dd0fda8e7..3f98f6604 100644 --- a/src/mirall/owncloudpropagator_p.h +++ b/src/mirall/owncloudpropagator_p.h @@ -15,16 +15,8 @@ #pragma once -// We use some internals of csync: -extern "C" int c_utimes(const char *, const struct timeval *); -extern "C" void csync_win32_set_file_hidden( const char *file, bool h ); - - namespace Mirall { -/** compare two files with given filename and return true if they have the same content */ -bool fileEquals(const QString &fn1, const QString &fn2); - inline QByteArray parseEtag(const char *header) { if (!header) return QByteArray(); diff --git a/src/mirall/propagator_legacy.cpp b/src/mirall/propagator_legacy.cpp index da3ff151d..8c9c23609 100644 --- a/src/mirall/propagator_legacy.cpp +++ b/src/mirall/propagator_legacy.cpp @@ -19,6 +19,7 @@ #include "utility.h" #include "syncjournaldb.h" #include "syncjournalfilerecord.h" +#include "filesystem.h" #include #include #include @@ -479,7 +480,7 @@ void PropagateDownloadFileLegacy::start() return; } - csync_win32_set_file_hidden(tmpFile.fileName().toUtf8().constData(), true); + FileSystem::setFileHidden(tmpFile.fileName(), true); { SyncJournalDb::DownloadInfo pi; @@ -565,7 +566,7 @@ void PropagateDownloadFileLegacy::start() bool isConflict = _item._instruction == CSYNC_INSTRUCTION_CONFLICT - && !fileEquals(fn, tmpFile.fileName()); // compare the files to see if there was an actual conflict. + && !FileSystem::fileEquals(fn, tmpFile.fileName()); // compare the files to see if there was an actual conflict. //In case of conflict, make a backup of the old file if (isConflict) { QFile f(fn); @@ -590,7 +591,7 @@ void PropagateDownloadFileLegacy::start() tmpFile.setPermissions(existingFile.permissions()); } - csync_win32_set_file_hidden(tmpFile.fileName().toUtf8().constData(), false); + FileSystem::setFileHidden(tmpFile.fileName(), false); #ifndef Q_OS_WIN bool success; @@ -626,10 +627,8 @@ void PropagateDownloadFileLegacy::start() return; } #endif - struct timeval times[2]; - times[0].tv_sec = times[1].tv_sec = _item._modtime; - times[0].tv_usec = times[1].tv_usec = 0; - c_utimes(fn.toUtf8().data(), times); + + FileSystem::setModTime(fn, _item._modtime); _propagator->_journal->setFileRecord(SyncJournalFileRecord(_item, fn)); _propagator->_journal->setDownloadInfo(_item._file, SyncJournalDb::DownloadInfo()); diff --git a/src/mirall/propagator_qnam.cpp b/src/mirall/propagator_qnam.cpp index ee7a7762c..256b48336 100644 --- a/src/mirall/propagator_qnam.cpp +++ b/src/mirall/propagator_qnam.cpp @@ -18,6 +18,7 @@ #include "syncjournaldb.h" #include "syncjournalfilerecord.h" #include "utility.h" +#include "filesystem.h" #include #include #include @@ -354,7 +355,7 @@ void PropagateDownloadFileQNAM::start() return; } - csync_win32_set_file_hidden(_tmpFile.fileName().toUtf8().constData(), true); + FileSystem::setFileHidden(_tmpFile.fileName(), true); { SyncJournalDb::DownloadInfo pi; @@ -426,7 +427,7 @@ void PropagateDownloadFileQNAM::downloadFinished() bool isConflict = _item._instruction == CSYNC_INSTRUCTION_CONFLICT - && !fileEquals(fn, _tmpFile.fileName()); // compare the files to see if there was an actual conflict. + && !FileSystem::fileEquals(fn, _tmpFile.fileName()); // compare the files to see if there was an actual conflict. //In case of conflict, make a backup of the old file if (isConflict) { QFile f(fn); @@ -451,7 +452,7 @@ void PropagateDownloadFileQNAM::downloadFinished() _tmpFile.setPermissions(existingFile.permissions()); } - csync_win32_set_file_hidden(_tmpFile.fileName().toUtf8().constData(), false); + FileSystem::setFileHidden(_tmpFile.fileName(), false); //FIXME: duplicated code. #ifndef Q_OS_WIN @@ -488,10 +489,7 @@ void PropagateDownloadFileQNAM::downloadFinished() return; } #endif - struct timeval times[2]; - times[0].tv_sec = times[1].tv_sec = _item._modtime; - times[0].tv_usec = times[1].tv_usec = 0; - c_utimes(fn.toUtf8().data(), times); + FileSystem::setModTime(fn, _item._modtime); _propagator->_journal->setFileRecord(SyncJournalFileRecord(_item, fn)); _propagator->_journal->setDownloadInfo(_item._file, SyncJournalDb::DownloadInfo()); diff --git a/src/mirall/propagatorjobs.cpp b/src/mirall/propagatorjobs.cpp index 2c5d4b3a3..26b6c4992 100644 --- a/src/mirall/propagatorjobs.cpp +++ b/src/mirall/propagatorjobs.cpp @@ -101,39 +101,6 @@ void PropagateNeonJob::slotRestoreJobCompleted(const SyncFileItem& item ) } } - -// compare two files with given filename and return true if they have the same content -bool fileEquals(const QString &fn1, const QString &fn2) { - QFile f1(fn1); - QFile f2(fn2); - if (!f1.open(QIODevice::ReadOnly) || !f2.open(QIODevice::ReadOnly)) { - qDebug() << "fileEquals: Failed to open " << fn1 << "or" << fn2; - return false; - } - - if (f1.size() != f2.size()) { - return false; - } - - const int BufferSize = 16 * 1024; - char buffer1[BufferSize]; - char buffer2[BufferSize]; - do { - int r = f1.read(buffer1, BufferSize); - if (f2.read(buffer2, BufferSize) != r) { - // this should normaly not happen: the file are supposed to have the same size. - return false; - } - if (r <= 0) { - return true; - } - if (memcmp(buffer1, buffer2, r) != 0) { - return false; - } - } while (true); - return false; -} - // Code copied from Qt5's QDir::removeRecursively static bool removeRecursively(const QString &path) {