cmFileTimeComparison.cxx 7.8 KB

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