summaryrefslogtreecommitdiffstats
path: root/qmake/library/ioutils.cpp
diff options
context:
space:
mode:
authorSimon Hausmann <simon.hausmann@qt.io>2017-02-24 16:22:53 +0100
committerSimon Hausmann <simon.hausmann@qt.io>2017-04-12 15:50:51 +0000
commit2ad7f6ddf5042d7442c97a89b083ca2853cf5721 (patch)
tree55eb85622019b54f09d01d96d608744c4c512efb /qmake/library/ioutils.cpp
parent7f29367c09d069958e0e9fb539ee88df04877b7e (diff)
Preserve last modification timestamps of installed files
On non-windows platforms, we use the "-p" parameter of install(1) to preserve the last modification timestamps of files. On Windows the use of copy does not preserve them. As a cross-platform solution, this patch introduces a simple built-in install command in qmake to copy files. Task-number: QTBUG-59004 Change-Id: I3064d29a2b8c7b009a1efbf8f00b84c079ea5417 Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@qt.io>
Diffstat (limited to 'qmake/library/ioutils.cpp')
-rw-r--r--qmake/library/ioutils.cpp70
1 files changed, 70 insertions, 0 deletions
diff --git a/qmake/library/ioutils.cpp b/qmake/library/ioutils.cpp
index 67d2f541a1..39264952c6 100644
--- a/qmake/library/ioutils.cpp
+++ b/qmake/library/ioutils.cpp
@@ -37,8 +37,13 @@
# include <sys/types.h>
# include <sys/stat.h>
# include <unistd.h>
+# include <utime.h>
+# include <fcntl.h>
+# include <errno.h>
#endif
+#define fL1S(s) QString::fromLatin1(s)
+
QT_BEGIN_NAMESPACE
using namespace QMakeInternal;
@@ -183,4 +188,69 @@ QString IoUtils::shellQuoteWin(const QString &arg)
return ret;
}
+#if defined(PROEVALUATOR_FULL)
+
+# if defined(Q_OS_WIN)
+static QString windowsErrorCode()
+{
+ wchar_t *string = 0;
+ FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
+ NULL,
+ GetLastError(),
+ MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
+ (LPWSTR)&string,
+ 0,
+ NULL);
+ QString ret = QString::fromWCharArray(string);
+ LocalFree((HLOCAL)string);
+ return ret.trimmed();
+}
+# endif
+
+bool IoUtils::touchFile(const QString &targetFileName, const QString &referenceFileName, QString *errorString)
+{
+# ifdef Q_OS_UNIX
+ struct stat st;
+ if (stat(referenceFileName.toLocal8Bit().constData(), &st)) {
+ *errorString = fL1S("Cannot stat() reference file %1: %2.").arg(referenceFileName, fL1S(strerror(errno)));
+ return false;
+ }
+# if defined(_POSIX_VERSION) && _POSIX_VERSION >= 200809L
+ const struct timespec times[2] = { { 0, UTIME_NOW }, st.st_mtim };
+ const bool utimeError = utimensat(AT_FDCWD, targetFileName.toLocal8Bit().constData(), times, 0) < 0;
+# else
+ struct utimbuf utb;
+ utb.actime = time(0);
+ utb.modtime = st.st_mtime;
+ const bool utimeError= utime(targetFileName.toLocal8Bit().constData(), &utb) < 0;
+# endif
+ if (utimeError) {
+ *errorString = fL1S("Cannot touch %1: %2.").arg(targetFileName, fL1S(strerror(errno)));
+ return false;
+ }
+# else
+ HANDLE rHand = CreateFile((wchar_t*)referenceFileName.utf16(),
+ GENERIC_READ, FILE_SHARE_READ,
+ NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
+ if (rHand == INVALID_HANDLE_VALUE) {
+ *errorString = fL1S("Cannot open reference file %1: %2").arg(referenceFileName, windowsErrorCode());
+ return false;
+ }
+ FILETIME ft;
+ GetFileTime(rHand, 0, 0, &ft);
+ CloseHandle(rHand);
+ HANDLE wHand = CreateFile((wchar_t*)targetFileName.utf16(),
+ GENERIC_WRITE, FILE_SHARE_READ,
+ NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
+ if (wHand == INVALID_HANDLE_VALUE) {
+ *errorString = fL1S("Cannot open %1: %2").arg(targetFileName, windowsErrorCode());
+ return false;
+ }
+ SetFileTime(wHand, 0, 0, &ft);
+ CloseHandle(wHand);
+# endif
+ return true;
+}
+#endif
+
QT_END_NAMESPACE