From: Hamatoma Date: Wed, 24 Feb 2016 18:44:14 +0000 (+0100) Subject: rebackgui: WIN32 copy() and setTimes() X-Git-Url: https://gitweb.hamatoma.de/?a=commitdiff_plain;h=7ed2716145724778f63e0d6f8cef47630fcf9c1e;p=reqt rebackgui: WIN32 copy() and setTimes() --- diff --git a/base/ReFileUtils.cpp b/base/ReFileUtils.cpp index 1c9f135..4b6fbef 100644 --- a/base/ReFileUtils.cpp +++ b/base/ReFileUtils.cpp @@ -218,7 +218,7 @@ QString ReFileUtils::copy(const QString& source, const QString& target, rc = QObject::tr("file can be read only partitionally: %1 [%2/%3]") .arg(source).arg(totalBytes).arg(filesize); if (rc.isEmpty()){ - if (! setTimes(target2, sourceInfo->lastModified(), + if (! setTimes(target2, sourceInfo->lastModified(), sourceInfo->lastRead())) rc = QObject::tr("cannot set date/time (%1): %2") .arg(errno).arg(target); @@ -926,6 +926,46 @@ bool ReFileUtils::setTimes(const char* filename, const QDateTime& modified, #endif return rc; } +/** + * Sets the filetimes. + * + * @param filename name of the file to change + * @param modified the new modification time + * @param accessed the new access time. + * If m_undefinedTime the current time is taken + * @param logger the logger + * @return true: success + */ + bool ReFileUtils::setTimes(const QString& filename, const QDateTime& modified, + const QDateTime& accessed, ReLogger* logger) { + bool rc = true; +#if defined _WIN32 + // utime does not work with VS10-32-bit + FILETIME accessed2 = unixTimeToFileTime(accessed.toMSecsSinceEpoch()); + FILETIME modified2 = unixTimeToFileTime(modified.toMSecsSinceEpoch()); + const ushort* name = filename.utf16(); + + HANDLE handle = CreateFileW(reinterpret_cast(name), 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 + rc = setTimes(I18N::s2b(filename).constData(), modified, accessed, logger); +#endif + return rc; +} + + bool rc = true; /** Sets the read position of a file. * @param file file to process diff --git a/base/ReFileUtils.hpp b/base/ReFileUtils.hpp index 2702895..12f0dc1 100644 --- a/base/ReFileUtils.hpp +++ b/base/ReFileUtils.hpp @@ -103,6 +103,8 @@ public: ReLogger* logger = NULL); static bool setTimes(const char* filename, const QDateTime& modified, const QDateTime& accessed = m_undefinedTime, ReLogger* logger = NULL); + static bool setTimes(const QString& filename, const QDateTime& modified, + const QDateTime& accessed = m_undefinedTime, ReLogger* logger = NULL); static void splitUrl(const QString& url, QString* protocol, QString* host, QString* path, QString* node, QString* params = NULL); static int64_t tell(FILE* file);