move the file system helpers in their own file

This commit is contained in:
Olivier Goffart 2014-02-18 12:54:40 +01:00
parent 432f377750
commit f7419094f3
7 changed files with 119 additions and 55 deletions

View file

@ -79,6 +79,7 @@ set(libsync_SRCS
mirall/owncloudtheme.cpp mirall/owncloudtheme.cpp
mirall/logger.cpp mirall/logger.cpp
mirall/utility.cpp mirall/utility.cpp
mirall/filesystem.cpp
mirall/connectionvalidator.cpp mirall/connectionvalidator.cpp
mirall/progressdispatcher.cpp mirall/progressdispatcher.cpp
mirall/mirallaccessmanager.cpp mirall/mirallaccessmanager.cpp

72
src/mirall/filesystem.cpp Normal file
View file

@ -0,0 +1,72 @@
/*
* Copyright (C) by Daniel Molkentin <danimo@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; 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 <QFile>
#include <QDebug>
// 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);
}
}

35
src/mirall/filesystem.h Normal file
View file

@ -0,0 +1,35 @@
/*
* 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; 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 <QString>
#include <ctime>
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);
}}

View file

@ -15,16 +15,8 @@
#pragma once #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 { 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) { inline QByteArray parseEtag(const char *header) {
if (!header) if (!header)
return QByteArray(); return QByteArray();

View file

@ -19,6 +19,7 @@
#include "utility.h" #include "utility.h"
#include "syncjournaldb.h" #include "syncjournaldb.h"
#include "syncjournalfilerecord.h" #include "syncjournalfilerecord.h"
#include "filesystem.h"
#include <httpbf.h> #include <httpbf.h>
#include <qfile.h> #include <qfile.h>
#include <qdir.h> #include <qdir.h>
@ -479,7 +480,7 @@ void PropagateDownloadFileLegacy::start()
return; return;
} }
csync_win32_set_file_hidden(tmpFile.fileName().toUtf8().constData(), true); FileSystem::setFileHidden(tmpFile.fileName(), true);
{ {
SyncJournalDb::DownloadInfo pi; SyncJournalDb::DownloadInfo pi;
@ -565,7 +566,7 @@ void PropagateDownloadFileLegacy::start()
bool isConflict = _item._instruction == CSYNC_INSTRUCTION_CONFLICT 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 //In case of conflict, make a backup of the old file
if (isConflict) { if (isConflict) {
QFile f(fn); QFile f(fn);
@ -590,7 +591,7 @@ void PropagateDownloadFileLegacy::start()
tmpFile.setPermissions(existingFile.permissions()); tmpFile.setPermissions(existingFile.permissions());
} }
csync_win32_set_file_hidden(tmpFile.fileName().toUtf8().constData(), false); FileSystem::setFileHidden(tmpFile.fileName(), false);
#ifndef Q_OS_WIN #ifndef Q_OS_WIN
bool success; bool success;
@ -626,10 +627,8 @@ void PropagateDownloadFileLegacy::start()
return; return;
} }
#endif #endif
struct timeval times[2];
times[0].tv_sec = times[1].tv_sec = _item._modtime; FileSystem::setModTime(fn, _item._modtime);
times[0].tv_usec = times[1].tv_usec = 0;
c_utimes(fn.toUtf8().data(), times);
_propagator->_journal->setFileRecord(SyncJournalFileRecord(_item, fn)); _propagator->_journal->setFileRecord(SyncJournalFileRecord(_item, fn));
_propagator->_journal->setDownloadInfo(_item._file, SyncJournalDb::DownloadInfo()); _propagator->_journal->setDownloadInfo(_item._file, SyncJournalDb::DownloadInfo());

View file

@ -18,6 +18,7 @@
#include "syncjournaldb.h" #include "syncjournaldb.h"
#include "syncjournalfilerecord.h" #include "syncjournalfilerecord.h"
#include "utility.h" #include "utility.h"
#include "filesystem.h"
#include <QNetworkAccessManager> #include <QNetworkAccessManager>
#include <QFileInfo> #include <QFileInfo>
#include <cmath> #include <cmath>
@ -354,7 +355,7 @@ void PropagateDownloadFileQNAM::start()
return; return;
} }
csync_win32_set_file_hidden(_tmpFile.fileName().toUtf8().constData(), true); FileSystem::setFileHidden(_tmpFile.fileName(), true);
{ {
SyncJournalDb::DownloadInfo pi; SyncJournalDb::DownloadInfo pi;
@ -426,7 +427,7 @@ void PropagateDownloadFileQNAM::downloadFinished()
bool isConflict = _item._instruction == CSYNC_INSTRUCTION_CONFLICT 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 //In case of conflict, make a backup of the old file
if (isConflict) { if (isConflict) {
QFile f(fn); QFile f(fn);
@ -451,7 +452,7 @@ void PropagateDownloadFileQNAM::downloadFinished()
_tmpFile.setPermissions(existingFile.permissions()); _tmpFile.setPermissions(existingFile.permissions());
} }
csync_win32_set_file_hidden(_tmpFile.fileName().toUtf8().constData(), false); FileSystem::setFileHidden(_tmpFile.fileName(), false);
//FIXME: duplicated code. //FIXME: duplicated code.
#ifndef Q_OS_WIN #ifndef Q_OS_WIN
@ -488,10 +489,7 @@ void PropagateDownloadFileQNAM::downloadFinished()
return; return;
} }
#endif #endif
struct timeval times[2]; FileSystem::setModTime(fn, _item._modtime);
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);
_propagator->_journal->setFileRecord(SyncJournalFileRecord(_item, fn)); _propagator->_journal->setFileRecord(SyncJournalFileRecord(_item, fn));
_propagator->_journal->setDownloadInfo(_item._file, SyncJournalDb::DownloadInfo()); _propagator->_journal->setDownloadInfo(_item._file, SyncJournalDb::DownloadInfo());

View file

@ -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 // Code copied from Qt5's QDir::removeRecursively
static bool removeRecursively(const QString &path) static bool removeRecursively(const QString &path)
{ {