cmFileTime.cxx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmFileTime.h"
  4. #include <ctime>
  5. #include <string>
  6. // Use a platform-specific API to get file times efficiently.
  7. #if !defined(_WIN32) || defined(__CYGWIN__)
  8. # include "cm_sys_stat.h"
  9. #else
  10. # include <windows.h>
  11. # include "cmsys/Encoding.hxx"
  12. #endif
  13. bool cmFileTime::Load(std::string const& fileName)
  14. {
  15. #if !defined(_WIN32) || defined(__CYGWIN__)
  16. // POSIX version. Use the stat function.
  17. struct stat fst;
  18. if (::stat(fileName.c_str(), &fst) != 0) {
  19. return false;
  20. }
  21. # if CMake_STAT_HAS_ST_MTIM
  22. // Nanosecond resolution
  23. this->Time = fst.st_mtim.tv_sec * UtPerS + fst.st_mtim.tv_nsec;
  24. # elif CMake_STAT_HAS_ST_MTIMESPEC
  25. // Nanosecond resolution
  26. this->Time = fst.st_mtimespec.tv_sec * UtPerS + fst.st_mtimespec.tv_nsec;
  27. # else
  28. // Second resolution
  29. this->Time = fst.st_mtime * UtPerS;
  30. # endif
  31. #else
  32. // Windows version. Get the modification time from extended file attributes.
  33. WIN32_FILE_ATTRIBUTE_DATA fdata;
  34. if (!GetFileAttributesExW(cmsys::Encoding::ToWide(fileName).c_str(),
  35. GetFileExInfoStandard, &fdata)) {
  36. return false;
  37. }
  38. // Copy the file time to the output location.
  39. using uint64 = unsigned long long;
  40. this->Time = static_cast<TimeType>(
  41. (uint64(fdata.ftLastWriteTime.dwHighDateTime) << 32) +
  42. fdata.ftLastWriteTime.dwLowDateTime);
  43. #endif
  44. return true;
  45. }