cmFileTimeComparison.cxx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 std::string& f1, const std::string& f2,
  22. int* result);
  23. bool FileTimesDiffer(const std::string& f1, const std::string& f2);
  24. private:
  25. typedef std::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 std::string& 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 std::string& 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.c_str(), 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 std::string& f1,
  75. const std::string& f2, int* result)
  76. {
  77. return this->Internals->FileTimeCompare(f1, f2, result);
  78. }
  79. bool cmFileTimeComparison::FileTimesDiffer(const std::string& f1,
  80. const std::string& f2)
  81. {
  82. return this->Internals->FileTimesDiffer(f1, f2);
  83. }
  84. int cmFileTimeComparisonInternal::Compare(cmFileTimeComparison_Type* s1,
  85. cmFileTimeComparison_Type* s2)
  86. {
  87. #if !defined(_WIN32) || defined(__CYGWIN__)
  88. # if CMake_STAT_HAS_ST_MTIM
  89. // Compare using nanosecond resolution.
  90. if (s1->st_mtim.tv_sec < s2->st_mtim.tv_sec) {
  91. return -1;
  92. }
  93. if (s1->st_mtim.tv_sec > s2->st_mtim.tv_sec) {
  94. return 1;
  95. }
  96. if (s1->st_mtim.tv_nsec < s2->st_mtim.tv_nsec) {
  97. return -1;
  98. }
  99. if (s1->st_mtim.tv_nsec > s2->st_mtim.tv_nsec) {
  100. return 1;
  101. }
  102. # elif CMake_STAT_HAS_ST_MTIMESPEC
  103. // Compare using nanosecond resolution.
  104. if (s1->st_mtimespec.tv_sec < s2->st_mtimespec.tv_sec) {
  105. return -1;
  106. }
  107. if (s1->st_mtimespec.tv_sec > s2->st_mtimespec.tv_sec) {
  108. return 1;
  109. }
  110. if (s1->st_mtimespec.tv_nsec < s2->st_mtimespec.tv_nsec) {
  111. return -1;
  112. }
  113. if (s1->st_mtimespec.tv_nsec > s2->st_mtimespec.tv_nsec) {
  114. return 1;
  115. }
  116. # else
  117. // Compare using 1 second resolution.
  118. if (s1->st_mtime < s2->st_mtime) {
  119. return -1;
  120. }
  121. if (s1->st_mtime > s2->st_mtime) {
  122. return 1;
  123. }
  124. # endif
  125. // Files have the same time.
  126. return 0;
  127. #else
  128. // Compare using system-provided function.
  129. return (int)CompareFileTime(s1, s2);
  130. #endif
  131. }
  132. bool cmFileTimeComparisonInternal::TimesDiffer(cmFileTimeComparison_Type* s1,
  133. cmFileTimeComparison_Type* s2)
  134. {
  135. #if !defined(_WIN32) || defined(__CYGWIN__)
  136. # if CMake_STAT_HAS_ST_MTIM
  137. // Times are integers in units of 1ns.
  138. long long bil = 1000000000;
  139. long long t1 = s1->st_mtim.tv_sec * bil + s1->st_mtim.tv_nsec;
  140. long long t2 = s2->st_mtim.tv_sec * bil + s2->st_mtim.tv_nsec;
  141. if (t1 < t2) {
  142. return (t2 - t1) >= bil;
  143. }
  144. if (t2 < t1) {
  145. return (t1 - t2) >= bil;
  146. }
  147. return false;
  148. # elif CMake_STAT_HAS_ST_MTIMESPEC
  149. // Times are integers in units of 1ns.
  150. long long bil = 1000000000;
  151. long long t1 = s1->st_mtimespec.tv_sec * bil + s1->st_mtimespec.tv_nsec;
  152. long long t2 = s2->st_mtimespec.tv_sec * bil + s2->st_mtimespec.tv_nsec;
  153. if (t1 < t2) {
  154. return (t2 - t1) >= bil;
  155. }
  156. if (t2 < t1) {
  157. return (t1 - t2) >= bil;
  158. }
  159. return false;
  160. # else
  161. // Times are integers in units of 1s.
  162. if (s1->st_mtime < s2->st_mtime) {
  163. return (s2->st_mtime - s1->st_mtime) >= 1;
  164. }
  165. if (s1->st_mtime > s2->st_mtime) {
  166. return (s1->st_mtime - s2->st_mtime) >= 1;
  167. }
  168. return false;
  169. # endif
  170. #else
  171. // Times are integers in units of 100ns.
  172. LARGE_INTEGER t1;
  173. LARGE_INTEGER t2;
  174. t1.LowPart = s1->dwLowDateTime;
  175. t1.HighPart = s1->dwHighDateTime;
  176. t2.LowPart = s2->dwLowDateTime;
  177. t2.HighPart = s2->dwHighDateTime;
  178. if (t1.QuadPart < t2.QuadPart) {
  179. return (t2.QuadPart - t1.QuadPart) >= static_cast<LONGLONG>(10000000);
  180. } else if (t2.QuadPart < t1.QuadPart) {
  181. return (t1.QuadPart - t2.QuadPart) >= static_cast<LONGLONG>(10000000);
  182. } else {
  183. return false;
  184. }
  185. #endif
  186. }
  187. bool cmFileTimeComparisonInternal::FileTimeCompare(const std::string& f1,
  188. const std::string& f2,
  189. int* result)
  190. {
  191. // Get the modification time for each file.
  192. cmFileTimeComparison_Type s1;
  193. cmFileTimeComparison_Type s2;
  194. if (this->Stat(f1, &s1) && this->Stat(f2, &s2)) {
  195. // Compare the two modification times.
  196. *result = this->Compare(&s1, &s2);
  197. return true;
  198. }
  199. // No comparison available. Default to the same time.
  200. *result = 0;
  201. return false;
  202. }
  203. bool cmFileTimeComparisonInternal::FileTimesDiffer(const std::string& f1,
  204. const std::string& f2)
  205. {
  206. // Get the modification time for each file.
  207. cmFileTimeComparison_Type s1;
  208. cmFileTimeComparison_Type s2;
  209. if (this->Stat(f1, &s1) && this->Stat(f2, &s2)) {
  210. // Compare the two modification times.
  211. return this->TimesDiffer(&s1, &s2);
  212. }
  213. // No comparison available. Default to different times.
  214. return true;
  215. }