cmFileTimeComparison.cxx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 "cmFileTimeComparison.h"
  4. #include <string>
  5. #include <time.h>
  6. #include <utility>
  7. #include "cm_unordered_map.hxx"
  8. // Use a platform-specific API to get file times efficiently.
  9. #if !defined(_WIN32) || defined(__CYGWIN__)
  10. #include <sys/stat.h>
  11. #define cmFileTimeComparison_Type struct stat
  12. #else
  13. #include <cmsys/Encoding.hxx>
  14. #include <windows.h>
  15. #define cmFileTimeComparison_Type FILETIME
  16. #endif
  17. class cmFileTimeComparisonInternal
  18. {
  19. public:
  20. // Internal comparison method.
  21. inline bool FileTimeCompare(const char* f1, const char* f2, int* result);
  22. bool FileTimesDiffer(const char* f1, const char* f2);
  23. private:
  24. typedef CM_UNORDERED_MAP<std::string, cmFileTimeComparison_Type>
  25. FileStatsMap;
  26. FileStatsMap Files;
  27. // Internal methods to lookup and compare modification times.
  28. inline bool Stat(const char* fname, cmFileTimeComparison_Type* st);
  29. inline int Compare(cmFileTimeComparison_Type* st1,
  30. cmFileTimeComparison_Type* st2);
  31. inline bool TimesDiffer(cmFileTimeComparison_Type* st1,
  32. cmFileTimeComparison_Type* st2);
  33. };
  34. bool cmFileTimeComparisonInternal::Stat(const char* fname,
  35. cmFileTimeComparison_Type* st)
  36. {
  37. // Use the stored time if available.
  38. cmFileTimeComparisonInternal::FileStatsMap::iterator fit =
  39. this->Files.find(fname);
  40. if (fit != this->Files.end()) {
  41. *st = fit->second;
  42. return true;
  43. }
  44. #if !defined(_WIN32) || defined(__CYGWIN__)
  45. // POSIX version. Use the stat function.
  46. int res = ::stat(fname, st);
  47. if (res != 0) {
  48. return false;
  49. }
  50. #else
  51. // Windows version. Get the modification time from extended file
  52. // attributes.
  53. WIN32_FILE_ATTRIBUTE_DATA fdata;
  54. if (!GetFileAttributesExW(cmsys::Encoding::ToWide(fname).c_str(),
  55. GetFileExInfoStandard, &fdata)) {
  56. return false;
  57. }
  58. // Copy the file time to the output location.
  59. *st = fdata.ftLastWriteTime;
  60. #endif
  61. // Store the time for future use.
  62. this->Files[fname] = *st;
  63. return true;
  64. }
  65. cmFileTimeComparison::cmFileTimeComparison()
  66. {
  67. this->Internals = new cmFileTimeComparisonInternal;
  68. }
  69. cmFileTimeComparison::~cmFileTimeComparison()
  70. {
  71. delete this->Internals;
  72. }
  73. bool cmFileTimeComparison::FileTimeCompare(const char* f1, const char* f2,
  74. int* result)
  75. {
  76. return this->Internals->FileTimeCompare(f1, f2, result);
  77. }
  78. bool cmFileTimeComparison::FileTimesDiffer(const char* f1, const char* f2)
  79. {
  80. return this->Internals->FileTimesDiffer(f1, f2);
  81. }
  82. int cmFileTimeComparisonInternal::Compare(cmFileTimeComparison_Type* s1,
  83. cmFileTimeComparison_Type* s2)
  84. {
  85. #if !defined(_WIN32) || defined(__CYGWIN__)
  86. #if CMake_STAT_HAS_ST_MTIM
  87. // Compare using nanosecond resolution.
  88. if (s1->st_mtim.tv_sec < s2->st_mtim.tv_sec) {
  89. return -1;
  90. }
  91. if (s1->st_mtim.tv_sec > s2->st_mtim.tv_sec) {
  92. return 1;
  93. }
  94. if (s1->st_mtim.tv_nsec < s2->st_mtim.tv_nsec) {
  95. return -1;
  96. }
  97. if (s1->st_mtim.tv_nsec > s2->st_mtim.tv_nsec) {
  98. return 1;
  99. }
  100. #elif CMake_STAT_HAS_ST_MTIMESPEC
  101. // Compare using nanosecond resolution.
  102. if (s1->st_mtimespec.tv_sec < s2->st_mtimespec.tv_sec) {
  103. return -1;
  104. } else if (s1->st_mtimespec.tv_sec > s2->st_mtimespec.tv_sec) {
  105. return 1;
  106. } else if (s1->st_mtimespec.tv_nsec < s2->st_mtimespec.tv_nsec) {
  107. return -1;
  108. } else if (s1->st_mtimespec.tv_nsec > s2->st_mtimespec.tv_nsec) {
  109. return 1;
  110. }
  111. #else
  112. // Compare using 1 second resolution.
  113. if (s1->st_mtime < s2->st_mtime) {
  114. return -1;
  115. } else if (s1->st_mtime > s2->st_mtime) {
  116. return 1;
  117. }
  118. #endif
  119. // Files have the same time.
  120. return 0;
  121. #else
  122. // Compare using system-provided function.
  123. return (int)CompareFileTime(s1, s2);
  124. #endif
  125. }
  126. bool cmFileTimeComparisonInternal::TimesDiffer(cmFileTimeComparison_Type* s1,
  127. cmFileTimeComparison_Type* s2)
  128. {
  129. #if !defined(_WIN32) || defined(__CYGWIN__)
  130. #if CMake_STAT_HAS_ST_MTIM
  131. // Times are integers in units of 1ns.
  132. long long bil = 1000000000;
  133. long long t1 = s1->st_mtim.tv_sec * bil + s1->st_mtim.tv_nsec;
  134. long long t2 = s2->st_mtim.tv_sec * bil + s2->st_mtim.tv_nsec;
  135. if (t1 < t2) {
  136. return (t2 - t1) >= bil;
  137. }
  138. if (t2 < t1) {
  139. return (t1 - t2) >= bil;
  140. }
  141. return false;
  142. #elif CMake_STAT_HAS_ST_MTIMESPEC
  143. // Times are integers in units of 1ns.
  144. long long bil = 1000000000;
  145. long long t1 = s1->st_mtimespec.tv_sec * bil + s1->st_mtimespec.tv_nsec;
  146. long long t2 = s2->st_mtimespec.tv_sec * bil + s2->st_mtimespec.tv_nsec;
  147. if (t1 < t2) {
  148. return (t2 - t1) >= bil;
  149. } else if (t2 < t1) {
  150. return (t1 - t2) >= bil;
  151. } else {
  152. return false;
  153. }
  154. #else
  155. // Times are integers in units of 1s.
  156. if (s1->st_mtime < s2->st_mtime) {
  157. return (s2->st_mtime - s1->st_mtime) >= 1;
  158. } else if (s1->st_mtime > s2->st_mtime) {
  159. return (s1->st_mtime - s2->st_mtime) >= 1;
  160. } else {
  161. return false;
  162. }
  163. #endif
  164. #else
  165. // Times are integers in units of 100ns.
  166. LARGE_INTEGER t1;
  167. LARGE_INTEGER t2;
  168. t1.LowPart = s1->dwLowDateTime;
  169. t1.HighPart = s1->dwHighDateTime;
  170. t2.LowPart = s2->dwLowDateTime;
  171. t2.HighPart = s2->dwHighDateTime;
  172. if (t1.QuadPart < t2.QuadPart) {
  173. return (t2.QuadPart - t1.QuadPart) >= static_cast<LONGLONG>(10000000);
  174. } else if (t2.QuadPart < t1.QuadPart) {
  175. return (t1.QuadPart - t2.QuadPart) >= static_cast<LONGLONG>(10000000);
  176. } else {
  177. return false;
  178. }
  179. #endif
  180. }
  181. bool cmFileTimeComparisonInternal::FileTimeCompare(const char* f1,
  182. const char* f2, int* result)
  183. {
  184. // Get the modification time for each file.
  185. cmFileTimeComparison_Type s1;
  186. cmFileTimeComparison_Type s2;
  187. if (this->Stat(f1, &s1) && this->Stat(f2, &s2)) {
  188. // Compare the two modification times.
  189. *result = this->Compare(&s1, &s2);
  190. return true;
  191. }
  192. // No comparison available. Default to the same time.
  193. *result = 0;
  194. return false;
  195. }
  196. bool cmFileTimeComparisonInternal::FileTimesDiffer(const char* f1,
  197. const char* f2)
  198. {
  199. // Get the modification time for each file.
  200. cmFileTimeComparison_Type s1;
  201. cmFileTimeComparison_Type s2;
  202. if (this->Stat(f1, &s1) && this->Stat(f2, &s2)) {
  203. // Compare the two modification times.
  204. return this->TimesDiffer(&s1, &s2);
  205. }
  206. // No comparison available. Default to different times.
  207. return true;
  208. }