cmFileTimeCache.cxx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmFileTimeCache.h"
  4. #include <string>
  5. #include <unordered_map>
  6. #include <utility>
  7. cmFileTimeCache::cmFileTimeCache() = default;
  8. cmFileTimeCache::~cmFileTimeCache() = default;
  9. bool cmFileTimeCache::Load(std::string const& fileName, cmFileTime& fileTime)
  10. {
  11. // Use the stored time if available.
  12. {
  13. auto fit = this->FileTimes.find(fileName);
  14. if (fit != this->FileTimes.end()) {
  15. fileTime = fit->second;
  16. return true;
  17. }
  18. }
  19. // Read file time from OS
  20. if (!fileTime.Load(fileName)) {
  21. return false;
  22. }
  23. // Store file time in cache
  24. this->FileTimes[fileName] = fileTime;
  25. return true;
  26. }
  27. bool cmFileTimeCache::FileTimeCompare(std::string const& f1,
  28. std::string const& f2, int* result)
  29. {
  30. // Get the modification time for each file.
  31. cmFileTime ft1, ft2;
  32. if (this->Load(f1, ft1) && this->Load(f2, ft2)) {
  33. // Compare the two modification times.
  34. *result = ft1.Compare(ft2);
  35. return true;
  36. }
  37. // No comparison available. Default to the same time.
  38. *result = 0;
  39. return false;
  40. }
  41. bool cmFileTimeCache::FileTimesDiffer(std::string const& f1,
  42. std::string const& f2)
  43. {
  44. // Get the modification time for each file.
  45. cmFileTime ft1, ft2;
  46. if (this->Load(f1, ft1) && this->Load(f2, ft2)) {
  47. // Compare the two modification times.
  48. return ft1.DifferS(ft2);
  49. }
  50. // No comparison available. Default to different times.
  51. return true;
  52. }