From: Hamatoma Date: Wed, 24 Feb 2016 17:53:04 +0000 (+0100) Subject: rebackgui: WIN32 corrections X-Git-Url: https://gitweb.hamatoma.de/?a=commitdiff_plain;h=e7f3f2d78eb2133cd48dad24967df83f7fc7c466;p=reqt rebackgui: WIN32 corrections * using Win32 CopyFile() --- diff --git a/base/ReFileUtils.cpp b/base/ReFileUtils.cpp index 90a1d1e..1c9f135 100644 --- a/base/ReFileUtils.cpp +++ b/base/ReFileUtils.cpp @@ -19,6 +19,7 @@ enum { LOC_SET_TIMES_1, // 12504 LOC_MAKE_DIR_1, // 12505 LOC_MAKE_DIR_2, // 12506 + LOC_SET_TIMES_2, // 12507 }; QDateTime ReFileUtils::m_undefinedTime; @@ -165,7 +166,15 @@ QString ReFileUtils::cleanPath(const QString& path) { */ QString ReFileUtils::copy(const QString& source, const QString& target, const QFileInfo* sourceInfo, QByteArray& buffer){ - QString rc; + QString rc; +#if defined _WIN32 + const ushort* src = source.utf16(); + const ushort* trg = target.utf16(); + if (! CopyFile(reinterpret_cast(src), + reinterpret_cast(trg), false)) + rc = QObject::tr("copy file failed (%1): %2") + .arg(GetLastError()).arg(target); +#else QByteArray source2 = I18N::s2b(source); QByteArray target2 = I18N::s2b(target); QFileInfo sourceInfo2; @@ -221,6 +230,7 @@ QString ReFileUtils::copy(const QString& source, const QString& target, fclose(fpSource); } } +#endif return rc; } @@ -876,12 +886,34 @@ bool ReFileUtils::setTimes(const char* filename, const QDateTime& modified, vals[1].tv_usec = millisec % 1000 * 1000; if (utimes(filename, vals) != 0) { if (logger != NULL) - logger->logv(LOG_ERROR, LOC_SET_TIMES_1, - "cannot change times (%d): $s", errno, filename); + logger->logv(LOG_ERROR, LOC_SET_TIMES_1, + "cannot change times (%d): %s", errno, filename); rc = false; } +#elif defined _WIN32 + // utime does not work with VS10-32-bit + FILETIME accessed2 = unixTimeToFileTime(accessed.toMSecsSinceEpoch()); + FILETIME modified2 = unixTimeToFileTime(modified.toMSecsSinceEpoch()); + + HANDLE handle = CreateFileA(filename, GENERIC_WRITE, FILE_SHARE_WRITE, + NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); + if (handle == INVALID_HANDLE_VALUE){ + if (logger != NULL) + logger->logv(LOG_ERROR, LOC_SET_TIMES_2, + "cannot open file for time change (%d): %s", GetLastError(), filename); + rc = false; + } else if (! SetFileTime(handle, NULL, &accessed2, &modified2)){ + rc = false; + if (logger != NULL) + logger->logv(LOG_ERROR, LOC_SET_TIMES_1, + "cannot change times (%d): %s", errno, filename); + CloseHandle(handle); + } #else - // ANSII-C: + // QT solution: + QString filename2(filename); + QDir info(filename2); + if (info.set struct utimbuf times; times.actime = time_t(accessed.currentMSecsSinceEpoch() / 1000); times.modtime = time_t(modified.currentMSecsSinceEpoch() / 1000); @@ -1016,6 +1048,24 @@ QByteArray ReFileUtils::tempFile(const char* node, const char* parent, return rc; } +#if defined _WIN32 +/** + * Converts the unix time (milliseconds from 1.1.1970) to the WIN32 filetime. + * + * @param milliSecFromEpoche time as milliseconds from 1.1.1970 + * @return time as WIN32 filetime + */ +FILETIME ReFileUtils::unixTimeToFileTime(qint64 milliSecFromEpoche) +{ + FILETIME fileTime; + // ll = Int32x32To64(t, 10000000) + 116444736000000000; + LONGLONG value = milliSecFromEpoche * 10000LL + 116444736000000000LL; + fileTime.dwLowDateTime = (DWORD) value; + fileTime.dwHighDateTime = DWORD (value >> 32); + return fileTime; +} +#endif + /** * Writes a string into a given file. * diff --git a/base/ReFileUtils.hpp b/base/ReFileUtils.hpp index 2571afb..2702895 100644 --- a/base/ReFileUtils.hpp +++ b/base/ReFileUtils.hpp @@ -112,6 +112,9 @@ public: bool withSeparator = true); static QByteArray tempFile(const char* node, const char* parent = NULL, bool deleteIfExists = true); +#if defined _WIN32 + static FILETIME unixTimeToFileTime(qint64 milliSecFromEpoche); +#endif static void writeToFile(const char* filename, const char* content, size_t contentLength = (size_t) - 1, const char* mode = "w"); public: diff --git a/base/rebase.hpp b/base/rebase.hpp index 56a121b..fd6898b 100644 --- a/base/rebase.hpp +++ b/base/rebase.hpp @@ -28,6 +28,8 @@ #include #include #include +#include +#include #endif #include diff --git a/gui/ReSettings.hpp b/gui/ReSettings.hpp index 135d639..eed6b29 100644 --- a/gui/ReSettings.hpp +++ b/gui/ReSettings.hpp @@ -71,8 +71,8 @@ typedef QMap*> ReChapterMap; class ReSettings { public: - static QString TRUE; - static QString FALSE; + static QString m_TRUE; + static QString m_FALSE; public: ReSettings(const QString& path, const QString& prefix, ReLogger* logger); ~ReSettings();