resolve cyclic dependency with zstd
[platform/upstream/cmake.git] / Source / cmFileTimeCache.cxx
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
5 #include <string>
6 #include <unordered_map>
7 #include <utility>
8
9 cmFileTimeCache::cmFileTimeCache() = default;
10
11 cmFileTimeCache::~cmFileTimeCache() = default;
12
13 bool cmFileTimeCache::Load(std::string const& fileName, cmFileTime& fileTime)
14 {
15   // Use the stored time if available.
16   {
17     auto fit = this->Cache.find(fileName);
18     if (fit != this->Cache.end()) {
19       fileTime = fit->second;
20       return true;
21     }
22   }
23   // Read file time from OS
24   if (!fileTime.Load(fileName)) {
25     return false;
26   }
27   // Store file time in cache
28   this->Cache[fileName] = fileTime;
29   return true;
30 }
31
32 bool cmFileTimeCache::Remove(std::string const& fileName)
33 {
34   return (this->Cache.erase(fileName) != 0);
35 }
36
37 bool cmFileTimeCache::Compare(std::string const& f1, std::string const& f2,
38                               int* result)
39 {
40   // Get the modification time for each file.
41   cmFileTime ft1;
42   cmFileTime ft2;
43   if (this->Load(f1, ft1) && this->Load(f2, ft2)) {
44     // Compare the two modification times.
45     *result = ft1.Compare(ft2);
46     return true;
47   }
48   // No comparison available.  Default to the same time.
49   *result = 0;
50   return false;
51 }
52
53 bool cmFileTimeCache::DifferS(std::string const& f1, std::string const& f2)
54 {
55   // Get the modification time for each file.
56   cmFileTime ft1;
57   cmFileTime ft2;
58   if (this->Load(f1, ft1) && this->Load(f2, ft2)) {
59     // Compare the two modification times.
60     return ft1.DifferS(ft2);
61   }
62   // No comparison available.  Default to different times.
63   return true;
64 }