1
0

cmFileTimeComparison.cxx 7.9 KB

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