cmFileTimeComparison.cxx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmFileTimeComparison.h"
  14. // Use a hash table to avoid duplicate file time checks from disk.
  15. #if defined(CMAKE_BUILD_WITH_CMAKE)
  16. # include <cmsys/hash_map.hxx>
  17. #endif
  18. // Use a platform-specific API to get file times efficiently.
  19. #if !defined(_WIN32) || defined(__CYGWIN__)
  20. # define cmFileTimeComparison_Type struct stat
  21. # include <ctype.h>
  22. # include <sys/stat.h>
  23. #else
  24. # define cmFileTimeComparison_Type FILETIME
  25. # include <windows.h>
  26. #endif
  27. //----------------------------------------------------------------------------
  28. class cmFileTimeComparisonInternal
  29. {
  30. public:
  31. // Internal comparison method.
  32. inline bool FileTimeCompare(const char* f1, const char* f2, int* result);
  33. private:
  34. #if defined(CMAKE_BUILD_WITH_CMAKE)
  35. // Use a hash table to efficiently map from file name to modification time.
  36. class HashString
  37. {
  38. public:
  39. size_t operator()(const cmStdString& s) const
  40. {
  41. return h(s.c_str());
  42. }
  43. cmsys::hash<const char*> h;
  44. };
  45. typedef cmsys::hash_map<cmStdString,
  46. cmFileTimeComparison_Type, HashString> FileStatsMap;
  47. FileStatsMap Files;
  48. #endif
  49. // Internal methods to lookup and compare modification times.
  50. inline bool Stat(const char* fname, cmFileTimeComparison_Type* st);
  51. inline int Compare(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 attributes.
  77. WIN32_FILE_ATTRIBUTE_DATA fdata;
  78. if(!GetFileAttributesEx(fname, GetFileExInfoStandard, &fdata))
  79. {
  80. return false;
  81. }
  82. // Copy the file time to the output location.
  83. *st = fdata.ftLastWriteTime;
  84. #endif
  85. #if defined(CMAKE_BUILD_WITH_CMAKE)
  86. // Store the time for future use.
  87. this->Files[fname] = *st;
  88. #endif
  89. return true;
  90. }
  91. //----------------------------------------------------------------------------
  92. cmFileTimeComparison::cmFileTimeComparison()
  93. {
  94. this->Internals = new cmFileTimeComparisonInternal;
  95. }
  96. //----------------------------------------------------------------------------
  97. cmFileTimeComparison::~cmFileTimeComparison()
  98. {
  99. delete this->Internals;
  100. }
  101. //----------------------------------------------------------------------------
  102. bool cmFileTimeComparison::FileTimeCompare(const char* f1, const char* f2, int* result)
  103. {
  104. return this->Internals->FileTimeCompare(f1, f2, result);
  105. }
  106. //----------------------------------------------------------------------------
  107. int cmFileTimeComparisonInternal::Compare(cmFileTimeComparison_Type* s1, cmFileTimeComparison_Type* s2)
  108. {
  109. #if !defined(_WIN32) || defined(__CYGWIN__)
  110. # if cmsys_STAT_HAS_ST_MTIM
  111. // Compare using nanosecond resolution.
  112. if(s1->st_mtim.tv_sec < s2->st_mtim.tv_sec)
  113. {
  114. return -1;
  115. }
  116. else if(s1->st_mtim.tv_sec > s2->st_mtim.tv_sec)
  117. {
  118. return 1;
  119. }
  120. else if(s1->st_mtim.tv_nsec < s2->st_mtim.tv_nsec)
  121. {
  122. return -1;
  123. }
  124. else if(s1->st_mtim.tv_nsec > s2->st_mtim.tv_nsec)
  125. {
  126. return 1;
  127. }
  128. # else
  129. // Compare using 1 second resolution.
  130. if(s1->st_mtime < s2->st_mtime)
  131. {
  132. return -1;
  133. }
  134. else if(s1->st_mtime > s2->st_mtime)
  135. {
  136. return 1;
  137. }
  138. # endif
  139. // Files have the same time.
  140. return 0;
  141. #else
  142. // Compare using system-provided function.
  143. return (int)CompareFileTime(s1, s2);
  144. #endif
  145. }
  146. //----------------------------------------------------------------------------
  147. bool cmFileTimeComparisonInternal::FileTimeCompare(const char* f1,
  148. const char* f2,
  149. int* result)
  150. {
  151. // Get the modification time for each file.
  152. cmFileTimeComparison_Type s1;
  153. cmFileTimeComparison_Type s2;
  154. if(this->Stat(f1, &s1) &&
  155. this->Stat(f2, &s2))
  156. {
  157. // Compare the two modification times.
  158. *result = this->Compare(&s1, &s2);
  159. return true;
  160. }
  161. else
  162. {
  163. // No comparison available. Default to the same time.
  164. *result = 0;
  165. return false;
  166. }
  167. }