cmFileTimeComparison.cxx 6.5 KB

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