1
0

cmFileTimeComparison.cxx 7.6 KB

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