cmFileTimeComparison.cxx 7.2 KB

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