cmFileTimeComparison.cxx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. } else if (s1->st_mtim.tv_sec > s2->st_mtim.tv_sec) {
  125. return 1;
  126. } else if (s1->st_mtim.tv_nsec < s2->st_mtim.tv_nsec) {
  127. return -1;
  128. } else if (s1->st_mtim.tv_nsec > s2->st_mtim.tv_nsec) {
  129. return 1;
  130. }
  131. #elif CMake_STAT_HAS_ST_MTIMESPEC
  132. // Compare using nanosecond resolution.
  133. if (s1->st_mtimespec.tv_sec < s2->st_mtimespec.tv_sec) {
  134. return -1;
  135. } else if (s1->st_mtimespec.tv_sec > s2->st_mtimespec.tv_sec) {
  136. return 1;
  137. } else if (s1->st_mtimespec.tv_nsec < s2->st_mtimespec.tv_nsec) {
  138. return -1;
  139. } else if (s1->st_mtimespec.tv_nsec > s2->st_mtimespec.tv_nsec) {
  140. return 1;
  141. }
  142. #else
  143. // Compare using 1 second resolution.
  144. if (s1->st_mtime < s2->st_mtime) {
  145. return -1;
  146. } else if (s1->st_mtime > s2->st_mtime) {
  147. return 1;
  148. }
  149. #endif
  150. // Files have the same time.
  151. return 0;
  152. #else
  153. // Compare using system-provided function.
  154. return (int)CompareFileTime(s1, s2);
  155. #endif
  156. }
  157. bool cmFileTimeComparisonInternal::TimesDiffer(cmFileTimeComparison_Type* s1,
  158. cmFileTimeComparison_Type* s2)
  159. {
  160. #if !defined(_WIN32) || defined(__CYGWIN__)
  161. #if CMake_STAT_HAS_ST_MTIM
  162. // Times are integers in units of 1ns.
  163. long long bil = 1000000000;
  164. long long t1 = s1->st_mtim.tv_sec * bil + s1->st_mtim.tv_nsec;
  165. long long t2 = s2->st_mtim.tv_sec * bil + s2->st_mtim.tv_nsec;
  166. if (t1 < t2) {
  167. return (t2 - t1) >= bil;
  168. } else if (t2 < t1) {
  169. return (t1 - t2) >= bil;
  170. } else {
  171. return false;
  172. }
  173. #elif CMake_STAT_HAS_ST_MTIMESPEC
  174. // Times are integers in units of 1ns.
  175. long long bil = 1000000000;
  176. long long t1 = s1->st_mtimespec.tv_sec * bil + s1->st_mtimespec.tv_nsec;
  177. long long t2 = s2->st_mtimespec.tv_sec * bil + s2->st_mtimespec.tv_nsec;
  178. if (t1 < t2) {
  179. return (t2 - t1) >= bil;
  180. } else if (t2 < t1) {
  181. return (t1 - t2) >= bil;
  182. } else {
  183. return false;
  184. }
  185. #else
  186. // Times are integers in units of 1s.
  187. if (s1->st_mtime < s2->st_mtime) {
  188. return (s2->st_mtime - s1->st_mtime) >= 1;
  189. } else if (s1->st_mtime > s2->st_mtime) {
  190. return (s1->st_mtime - s2->st_mtime) >= 1;
  191. } else {
  192. return false;
  193. }
  194. #endif
  195. #else
  196. // Times are integers in units of 100ns.
  197. LARGE_INTEGER t1;
  198. LARGE_INTEGER t2;
  199. t1.LowPart = s1->dwLowDateTime;
  200. t1.HighPart = s1->dwHighDateTime;
  201. t2.LowPart = s2->dwLowDateTime;
  202. t2.HighPart = s2->dwHighDateTime;
  203. if (t1.QuadPart < t2.QuadPart) {
  204. return (t2.QuadPart - t1.QuadPart) >= static_cast<LONGLONG>(10000000);
  205. } else if (t2.QuadPart < t1.QuadPart) {
  206. return (t1.QuadPart - t2.QuadPart) >= static_cast<LONGLONG>(10000000);
  207. } else {
  208. return false;
  209. }
  210. #endif
  211. }
  212. bool cmFileTimeComparisonInternal::FileTimeCompare(const char* f1,
  213. const char* f2, int* result)
  214. {
  215. // Get the modification time for each file.
  216. cmFileTimeComparison_Type s1;
  217. cmFileTimeComparison_Type s2;
  218. if (this->Stat(f1, &s1) && this->Stat(f2, &s2)) {
  219. // Compare the two modification times.
  220. *result = this->Compare(&s1, &s2);
  221. return true;
  222. } else {
  223. // No comparison available. Default to the same time.
  224. *result = 0;
  225. return false;
  226. }
  227. }
  228. bool cmFileTimeComparisonInternal::FileTimesDiffer(const char* f1,
  229. const char* f2)
  230. {
  231. // Get the modification time for each file.
  232. cmFileTimeComparison_Type s1;
  233. cmFileTimeComparison_Type s2;
  234. if (this->Stat(f1, &s1) && this->Stat(f2, &s2)) {
  235. // Compare the two modification times.
  236. return this->TimesDiffer(&s1, &s2);
  237. } else {
  238. // No comparison available. Default to different times.
  239. return true;
  240. }
  241. }