[Pkg-owncloud-commits] [owncloud-client] 50/333: Refactor the renameReplace in its own function

Sandro Knauß hefee-guest at moszumanska.debian.org
Thu Apr 17 23:16:32 UTC 2014


This is an automated email from the git hooks/post-receive script.

hefee-guest pushed a commit to branch master
in repository owncloud-client.

commit 496d900fee25697b905217e298956bafba4a778a
Author: Olivier Goffart <ogoffart at woboq.com>
Date:   Tue Feb 18 14:05:29 2014 +0100

    Refactor the renameReplace in its own function
    
    Share a bit more code between legacy and qnam download job
---
 src/mirall/filesystem.cpp        | 49 ++++++++++++++++++++++++++++++++++++++++
 src/mirall/filesystem.h          |  7 ++++++
 src/mirall/propagator_legacy.cpp | 45 +++---------------------------------
 src/mirall/propagator_qnam.cpp   | 37 ++++--------------------------
 4 files changed, 63 insertions(+), 75 deletions(-)

diff --git a/src/mirall/filesystem.cpp b/src/mirall/filesystem.cpp
index c6422b3..b4ce413 100644
--- a/src/mirall/filesystem.cpp
+++ b/src/mirall/filesystem.cpp
@@ -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;
+}
+
 
 
 }
\ No newline at end of file
diff --git a/src/mirall/filesystem.h b/src/mirall/filesystem.h
index 27f5e7a..f4a1671 100644
--- a/src/mirall/filesystem.h
+++ b/src/mirall/filesystem.h
@@ -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);
+
 }}
diff --git a/src/mirall/propagator_legacy.cpp b/src/mirall/propagator_legacy.cpp
index 4106db0..e3b7d01 100644
--- a/src/mirall/propagator_legacy.cpp
+++ b/src/mirall/propagator_legacy.cpp
@@ -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());
-        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);
+    QString error;
+    if (!FileSystem::renameReplace(tmpFile.fileName(), fn, &error)) {
+        done(SyncFileItem::NormalError, error);
         return;
     }
-    #endif
 
     FileSystem::setModTime(fn, _item._modtime);
 
diff --git a/src/mirall/propagator_qnam.cpp b/src/mirall/propagator_qnam.cpp
index 256b483..7d0ce0e 100644
--- a/src/mirall/propagator_qnam.cpp
+++ b/src/mirall/propagator_qnam.cpp
@@ -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());
-        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);
+    QString error;
+    if (!FileSystem::renameReplace(_tmpFile.fileName(), fn, &error)) {
+        done(SyncFileItem::NormalError, error);
         return;
     }
-#endif
+
     FileSystem::setModTime(fn, _item._modtime);
 
     _propagator->_journal->setFileRecord(SyncJournalFileRecord(_item, fn));

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/owncloud-client.git



More information about the Pkg-owncloud-commits mailing list