mirror of
https://github.com/nextcloud/desktop.git
synced 2024-11-27 17:37:36 +03:00
Refactor the renameReplace in its own function
Share a bit more code between legacy and qnam download job
This commit is contained in:
parent
58bda69f8b
commit
496d900fee
4 changed files with 62 additions and 74 deletions
|
@ -15,6 +15,16 @@
|
|||
#include <QFile>
|
||||
#include <QDebug>
|
||||
|
||||
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
|
||||
#include <qabstractfileengine.h>
|
||||
#endif
|
||||
|
||||
#ifdef Q_OS_WIN
|
||||
#include <windef.h>
|
||||
#include <winbase.h>
|
||||
#endif
|
||||
|
||||
|
||||
// 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 );
|
||||
|
@ -67,6 +77,45 @@ void FileSystem::setModTime(const QString& filename, time_t modTime)
|
|||
c_utimes(filename.toUtf8().data(), times);
|
||||
}
|
||||
|
||||
bool FileSystem::renameReplace(const QString& originFileName, const QString& destinationFileName, QString* errorString)
|
||||
{
|
||||
#ifndef Q_OS_WIN
|
||||
bool success;
|
||||
QFile orig(originFileName);
|
||||
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
|
||||
success = orig.fileEngine()->rename(destinationFileName);
|
||||
// qDebug() << "Renaming " << tmpFile.fileName() << " to " << fn;
|
||||
#else
|
||||
// We want a rename that also overwite. QFile::rename does not overwite.
|
||||
// Qt 5.1 has QSaveFile::renameOverwrite we cold use.
|
||||
// ### FIXME
|
||||
QFile::remove(destinationFileName);
|
||||
success = orig.rename(destinationFileName);
|
||||
#endif
|
||||
if (!success) {
|
||||
*errorString = orig.errorString();
|
||||
qDebug() << "FAIL: renaming temp file to final failed: " << *errorString ;
|
||||
return false;
|
||||
}
|
||||
#else //Q_OS_WIN
|
||||
BOOL ok;
|
||||
ok = MoveFileEx((wchar_t*)originFileName.utf16(),
|
||||
(wchar_t*)destinationFileName.utf16(),
|
||||
MOVEFILE_REPLACE_EXISTING+MOVEFILE_COPY_ALLOWED+MOVEFILE_WRITE_THROUGH);
|
||||
if (!ok) {
|
||||
wchar_t *string = 0;
|
||||
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
|
||||
NULL, ::GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
||||
(LPWSTR)&string, 0, NULL);
|
||||
|
||||
*errorString = QString::fromWCharArray(string);
|
||||
LocalFree((HLOCAL)string);
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -32,4 +32,11 @@ void setFileHidden(const QString& filename, bool hidden);
|
|||
|
||||
void setModTime(const QString &filename, time_t modTime);
|
||||
|
||||
/**
|
||||
* Rename the file \a originFileName to \a destinationFileName, and overwrite the destination if it
|
||||
* already exists
|
||||
*/
|
||||
bool renameReplace(const QString &originFileName, const QString &destinationFileName,
|
||||
QString *errorString);
|
||||
|
||||
}}
|
||||
|
|
|
@ -25,11 +25,6 @@
|
|||
#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>
|
||||
|
@ -44,11 +39,6 @@
|
|||
#include <neon/ne_compress.h>
|
||||
#include <neon/ne_redirect.h>
|
||||
|
||||
#ifdef Q_OS_WIN
|
||||
#include <windef.h>
|
||||
#include <winbase.h>
|
||||
#endif
|
||||
|
||||
#include <time.h>
|
||||
|
||||
|
||||
|
@ -593,40 +583,11 @@ void PropagateDownloadFileLegacy::start()
|
|||
|
||||
FileSystem::setFileHidden(tmpFile.fileName(), false);
|
||||
|
||||
#ifndef Q_OS_WIN
|
||||
bool success;
|
||||
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
|
||||
success = tmpFile.fileEngine()->rename(fn);
|
||||
// qDebug() << "Renaming " << tmpFile.fileName() << " to " << fn;
|
||||
#else
|
||||
// We want a rename that also overwite. QFile::rename does not overwite.
|
||||
// Qt 5.1 has QSaveFile::renameOverwrite we cold use.
|
||||
// ### FIXME
|
||||
QFile::remove(fn);
|
||||
success = tmpFile.rename(fn);
|
||||
#endif
|
||||
// unixoids
|
||||
if (!success) {
|
||||
qDebug() << "FAIL: renaming temp file to final failed: " << tmpFile.errorString();
|
||||
done(SyncFileItem::NormalError, tmpFile.errorString());
|
||||
QString error;
|
||||
if (!FileSystem::renameReplace(tmpFile.fileName(), fn, &error)) {
|
||||
done(SyncFileItem::NormalError, error);
|
||||
return;
|
||||
}
|
||||
#else //Q_OS_WIN
|
||||
BOOL ok;
|
||||
ok = MoveFileEx((wchar_t*)tmpFile.fileName().utf16(),
|
||||
(wchar_t*)QString(_propagator->_localDir + _item._file).utf16(),
|
||||
MOVEFILE_REPLACE_EXISTING+MOVEFILE_COPY_ALLOWED+MOVEFILE_WRITE_THROUGH);
|
||||
if (!ok) {
|
||||
wchar_t *string = 0;
|
||||
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
|
||||
NULL, ::GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
||||
(LPWSTR)&string, 0, NULL);
|
||||
|
||||
done(SyncFileItem::NormalError, QString::fromWCharArray(string));
|
||||
LocalFree((HLOCAL)string);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
FileSystem::setModTime(fn, _item._modtime);
|
||||
|
||||
|
|
|
@ -454,41 +454,12 @@ void PropagateDownloadFileQNAM::downloadFinished()
|
|||
|
||||
FileSystem::setFileHidden(_tmpFile.fileName(), false);
|
||||
|
||||
//FIXME: duplicated code.
|
||||
#ifndef Q_OS_WIN
|
||||
bool success;
|
||||
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
|
||||
success = _tmpFile.fileEngine()->rename(fn);
|
||||
// qDebug() << "Renaming " << tmpFile.fileName() << " to " << fn;
|
||||
#else
|
||||
// We want a rename that also overwite. QFile::rename does not overwite.
|
||||
// Qt 5.1 has QSaveFile::renameOverwrite we cold use.
|
||||
// ### FIXME
|
||||
QFile::remove(fn);
|
||||
success = _tmpFile.rename(fn);
|
||||
#endif
|
||||
// unixoids
|
||||
if (!success) {
|
||||
qDebug() << "FAIL: renaming temp file to final failed: " << _tmpFile.errorString();
|
||||
done(SyncFileItem::NormalError, _tmpFile.errorString());
|
||||
QString error;
|
||||
if (!FileSystem::renameReplace(_tmpFile.fileName(), fn, &error)) {
|
||||
done(SyncFileItem::NormalError, error);
|
||||
return;
|
||||
}
|
||||
#else //Q_OS_WIN
|
||||
BOOL ok;
|
||||
ok = MoveFileEx((wchar_t*)_tmpFile.fileName().utf16(),
|
||||
(wchar_t*)QString(_propagator->_localDir + _item._file).utf16(),
|
||||
MOVEFILE_REPLACE_EXISTING+MOVEFILE_COPY_ALLOWED+MOVEFILE_WRITE_THROUGH);
|
||||
if (!ok) {
|
||||
wchar_t *string = 0;
|
||||
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
|
||||
NULL, ::GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
||||
(LPWSTR)&string, 0, NULL);
|
||||
|
||||
done(SyncFileItem::NormalError, QString::fromWCharArray(string));
|
||||
LocalFree((HLOCAL)string);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
FileSystem::setModTime(fn, _item._modtime);
|
||||
|
||||
_propagator->_journal->setFileRecord(SyncJournalFileRecord(_item, fn));
|
||||
|
|
Loading…
Reference in a new issue