cmFileTime.cxx 1.5 KB

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