cmFileTimeComparison.cxx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 <unordered_map>
  7. #include <utility>
  8. // Use a platform-specific API to get file times efficiently.
  9. #if !defined(_WIN32) || defined(__CYGWIN__)
  10. #include "cm_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 std::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. }
  105. if (s1->st_mtimespec.tv_sec > s2->st_mtimespec.tv_sec) {
  106. return 1;
  107. }
  108. if (s1->st_mtimespec.tv_nsec < s2->st_mtimespec.tv_nsec) {
  109. return -1;
  110. }
  111. if (s1->st_mtimespec.tv_nsec > s2->st_mtimespec.tv_nsec) {
  112. return 1;
  113. }
  114. #else
  115. // Compare using 1 second resolution.
  116. if (s1->st_mtime < s2->st_mtime) {
  117. return -1;
  118. }
  119. if (s1->st_mtime > s2->st_mtime) {
  120. return 1;
  121. }
  122. #endif
  123. // Files have the same time.
  124. return 0;
  125. #else
  126. // Compare using system-provided function.
  127. return (int)CompareFileTime(s1, s2);
  128. #endif
  129. }
  130. bool cmFileTimeComparisonInternal::TimesDiffer(cmFileTimeComparison_Type* s1,
  131. cmFileTimeComparison_Type* s2)
  132. {
  133. #if !defined(_WIN32) || defined(__CYGWIN__)
  134. #if CMake_STAT_HAS_ST_MTIM
  135. // Times are integers in units of 1ns.
  136. long long bil = 1000000000;
  137. long long t1 = s1->st_mtim.tv_sec * bil + s1->st_mtim.tv_nsec;
  138. long long t2 = s2->st_mtim.tv_sec * bil + s2->st_mtim.tv_nsec;
  139. if (t1 < t2) {
  140. return (t2 - t1) >= bil;
  141. }
  142. if (t2 < t1) {
  143. return (t1 - t2) >= bil;
  144. }
  145. return false;
  146. #elif CMake_STAT_HAS_ST_MTIMESPEC
  147. // Times are integers in units of 1ns.
  148. long long bil = 1000000000;
  149. long long t1 = s1->st_mtimespec.tv_sec * bil + s1->st_mtimespec.tv_nsec;
  150. long long t2 = s2->st_mtimespec.tv_sec * bil + s2->st_mtimespec.tv_nsec;
  151. if (t1 < t2) {
  152. return (t2 - t1) >= bil;
  153. }
  154. if (t2 < t1) {
  155. return (t1 - t2) >= bil;
  156. }
  157. return false;
  158. #else
  159. // Times are integers in units of 1s.
  160. if (s1->st_mtime < s2->st_mtime) {
  161. return (s2->st_mtime - s1->st_mtime) >= 1;
  162. }
  163. if (s1->st_mtime > s2->st_mtime) {
  164. return (s1->st_mtime - s2->st_mtime) >= 1;
  165. }
  166. return false;
  167. #endif
  168. #else
  169. // Times are integers in units of 100ns.
  170. LARGE_INTEGER t1;
  171. LARGE_INTEGER t2;
  172. t1.LowPart = s1->dwLowDateTime;
  173. t1.HighPart = s1->dwHighDateTime;
  174. t2.LowPart = s2->dwLowDateTime;
  175. t2.HighPart = s2->dwHighDateTime;
  176. if (t1.QuadPart < t2.QuadPart) {
  177. return (t2.QuadPart - t1.QuadPart) >= static_cast<LONGLONG>(10000000);
  178. } else if (t2.QuadPart < t1.QuadPart) {
  179. return (t1.QuadPart - t2.QuadPart) >= static_cast<LONGLONG>(10000000);
  180. } else {
  181. return false;
  182. }
  183. #endif
  184. }
  185. bool cmFileTimeComparisonInternal::FileTimeCompare(const char* f1,
  186. const char* f2, int* result)
  187. {
  188. // Get the modification time for each file.
  189. cmFileTimeComparison_Type s1;
  190. cmFileTimeComparison_Type s2;
  191. if (this->Stat(f1, &s1) && this->Stat(f2, &s2)) {
  192. // Compare the two modification times.
  193. *result = this->Compare(&s1, &s2);
  194. return true;
  195. }
  196. // No comparison available. Default to the same time.
  197. *result = 0;
  198. return false;
  199. }
  200. bool cmFileTimeComparisonInternal::FileTimesDiffer(const char* f1,
  201. const char* f2)
  202. {
  203. // Get the modification time for each file.
  204. cmFileTimeComparison_Type s1;
  205. cmFileTimeComparison_Type s2;
  206. if (this->Stat(f1, &s1) && this->Stat(f2, &s2)) {
  207. // Compare the two modification times.
  208. return this->TimesDiffer(&s1, &s2);
  209. }
  210. // No comparison available. Default to different times.
  211. return true;
  212. }