cmFileTimeComparison.cxx 7.5 KB

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