]> gitweb.hamatoma.de Git - reqt/commitdiff
rebackgui: WIN32 copy() and setTimes()
authorHamatoma <git.tortouse@hm.f-r-e-i.de>
Wed, 24 Feb 2016 18:44:14 +0000 (19:44 +0100)
committerHamatoma <git.tortouse@hm.f-r-e-i.de>
Wed, 24 Feb 2016 18:44:14 +0000 (19:44 +0100)
base/ReFileUtils.cpp
base/ReFileUtils.hpp

index 1c9f1352133fbe5bfefa7f67dc822184501c9593..4b6fbef77fbfcea2dfba394724b283bbb6c15789 100644 (file)
@@ -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 <code>m_undefinedTime</code> the current time is taken
+ * @param logger       the logger
+ * @return                     <code>true</code>: 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<const WCHAR*>(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
index 2702895c2a500bfb9a6587df4808505abb85ddb1..12f0dc13f90bfd3d424ebfb831b6025292867b0a 100644 (file)
@@ -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);