summaryrefslogtreecommitdiffstats
path: root/Source/cmFileTime.cxx
diff options
context:
space:
mode:
authorSebastian Holtermann <sebholt@xwmw.org>2019-03-13 15:54:01 (GMT)
committerSebastian Holtermann <sebholt@xwmw.org>2019-03-18 14:09:10 (GMT)
commit6f24c4e93f6ae897b793f894b67543d86c7de477 (patch)
tree0394dff24abf759349bcc6c2914b4fb126e394a4 /Source/cmFileTime.cxx
parentb06f8c93e5bc65394fe55b3f7ae923fe296048b3 (diff)
downloadCMake-6f24c4e93f6ae897b793f894b67543d86c7de477.zip
CMake-6f24c4e93f6ae897b793f894b67543d86c7de477.tar.gz
CMake-6f24c4e93f6ae897b793f894b67543d86c7de477.tar.bz2
New cmFileTime class
The new cmFileTime class stores the file modification time as an OS independent nanosecond count. Its main use is to load and compare file modification times in nanosecond and second resolution.
Diffstat (limited to 'Source/cmFileTime.cxx')
-rw-r--r--Source/cmFileTime.cxx49
1 files changed, 49 insertions, 0 deletions
diff --git a/Source/cmFileTime.cxx b/Source/cmFileTime.cxx
new file mode 100644
index 0000000..253457f
--- /dev/null
+++ b/Source/cmFileTime.cxx
@@ -0,0 +1,49 @@
+/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
+ file Copyright.txt or https://cmake.org/licensing for details. */
+#include "cmFileTime.h"
+
+#include <string>
+#include <time.h>
+
+// Use a platform-specific API to get file times efficiently.
+#if !defined(_WIN32) || defined(__CYGWIN__)
+# include "cm_sys_stat.h"
+#else
+# include "cmsys/Encoding.hxx"
+# include <windows.h>
+#endif
+
+bool cmFileTime::Load(std::string const& fileName)
+{
+#if !defined(_WIN32) || defined(__CYGWIN__)
+ // POSIX version. Use the stat function.
+ struct stat fst;
+ if (::stat(fileName.c_str(), &fst) != 0) {
+ return false;
+ }
+# if CMake_STAT_HAS_ST_MTIM
+ // Nanosecond resolution
+ this->NS = fst.st_mtim.tv_sec * NsPerS + fst.st_mtim.tv_nsec;
+# elif CMake_STAT_HAS_ST_MTIMESPEC
+ // Nanosecond resolution
+ this->NS = fst.st_mtimespec.tv_sec * NsPerS + fst.st_mtimespec.tv_nsec;
+# else
+ // Second resolution
+ this->NS = fst.st_mtime * NsPerS;
+# endif
+#else
+ // Windows version. Get the modification time from extended file attributes.
+ WIN32_FILE_ATTRIBUTE_DATA fdata;
+ if (!GetFileAttributesExW(cmsys::Encoding::ToWide(fileName).c_str(),
+ GetFileExInfoStandard, &fdata)) {
+ return false;
+ }
+
+ // Copy the file time to the output location.
+ this->NS = (static_cast<NSC>(fdata.ftLastWriteTime.dwHighDateTime) << 32) |
+ static_cast<NSC>(fdata.ftLastWriteTime.dwLowDateTime);
+ // The file time resolution is 100 ns.
+ this->NS *= 100;
+#endif
+ return true;
+}