cmFileTimeComparison.cxx 7.6 KB

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