summaryrefslogtreecommitdiffstats
path: root/Source/cmFileTimeComparison.cxx
blob: 61e419c2cf363cbfa17a5b93a1b8bec0a1d93892 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
#include "cmFileTimeComparison.h"

#include <string>
#include <time.h>
#include <unordered_map>
#include <utility>

// Use a platform-specific API to get file times efficiently.
#if !defined(_WIN32) || defined(__CYGWIN__)
#include "cm_sys_stat.h"
#define cmFileTimeComparison_Type struct stat
#else
#include "cmsys/Encoding.hxx"
#include <windows.h>
#define cmFileTimeComparison_Type FILETIME
#endif

class cmFileTimeComparisonInternal
{
public:
  // Internal comparison method.
  inline bool FileTimeCompare(const char* f1, const char* f2, int* result);

  bool FileTimesDiffer(const char* f1, const char* f2);

private:
  typedef std::unordered_map<std::string, cmFileTimeComparison_Type>
    FileStatsMap;
  FileStatsMap Files;

  // Internal methods to lookup and compare modification times.
  inline bool Stat(const char* fname, cmFileTimeComparison_Type* st);
  inline int Compare(cmFileTimeComparison_Type* st1,
                     cmFileTimeComparison_Type* st2);
  inline bool TimesDiffer(cmFileTimeComparison_Type* st1,
                          cmFileTimeComparison_Type* st2);
};

bool cmFileTimeComparisonInternal::Stat(const char* fname,
                                        cmFileTimeComparison_Type* st)
{
  // Use the stored time if available.
  cmFileTimeComparisonInternal::FileStatsMap::iterator fit =
    this->Files.find(fname);
  if (fit != this->Files.end()) {
    *st = fit->second;
    return true;
  }

#if !defined(_WIN32) || defined(__CYGWIN__)
  // POSIX version.  Use the stat function.
  int res = ::stat(fname, st);
  if (res != 0) {
    return false;
  }
#else
  // Windows version.  Get the modification time from extended file
  // attributes.
  WIN32_FILE_ATTRIBUTE_DATA fdata;
  if (!GetFileAttributesExW(cmsys::Encoding::ToWide(fname).c_str(),
                            GetFileExInfoStandard, &fdata)) {
    return false;
  }

  // Copy the file time to the output location.
  *st = fdata.ftLastWriteTime;
#endif

  // Store the time for future use.
  this->Files[fname] = *st;
  return true;
}

cmFileTimeComparison::cmFileTimeComparison()
{
  this->Internals = new cmFileTimeComparisonInternal;
}

cmFileTimeComparison::~cmFileTimeComparison()
{
  delete this->Internals;
}

bool cmFileTimeComparison::FileTimeCompare(const char* f1, const char* f2,
                                           int* result)
{
  return this->Internals->FileTimeCompare(f1, f2, result);
}

bool cmFileTimeComparison::FileTimesDiffer(const char* f1, const char* f2)
{
  return this->Internals->FileTimesDiffer(f1, f2);
}

int cmFileTimeComparisonInternal::Compare(cmFileTimeComparison_Type* s1,
                                          cmFileTimeComparison_Type* s2)
{
#if !defined(_WIN32) || defined(__CYGWIN__)
#if CMake_STAT_HAS_ST_MTIM
  // Compare using nanosecond resolution.
  if (s1->st_mtim.tv_sec < s2->st_mtim.tv_sec) {
    return -1;
  }
  if (s1->st_mtim.tv_sec > s2->st_mtim.tv_sec) {
    return 1;
  }
  if (s1->st_mtim.tv_nsec < s2->st_mtim.tv_nsec) {
    return -1;
  }
  if (s1->st_mtim.tv_nsec > s2->st_mtim.tv_nsec) {
    return 1;
  }
#elif CMake_STAT_HAS_ST_MTIMESPEC
  // Compare using nanosecond resolution.
  if (s1->st_mtimespec.tv_sec < s2->st_mtimespec.tv_sec) {
    return -1;
  } else if (s1->st_mtimespec.tv_sec > s2->st_mtimespec.tv_sec) {
    return 1;
  } else if (s1->st_mtimespec.tv_nsec < s2->st_mtimespec.tv_nsec) {
    return -1;
  } else if (s1->st_mtimespec.tv_nsec > s2->st_mtimespec.tv_nsec) {
    return 1;
  }
#else
  // Compare using 1 second resolution.
  if (s1->st_mtime < s2->st_mtime) {
    return -1;
  } else if (s1->st_mtime > s2->st_mtime) {
    return 1;
  }
#endif
  // Files have the same time.
  return 0;
#else
  // Compare using system-provided function.
  return (int)CompareFileTime(s1, s2);
#endif
}

bool cmFileTimeComparisonInternal::TimesDiffer(cmFileTimeComparison_Type* s1,
                                               cmFileTimeComparison_Type* s2)
{
#if !defined(_WIN32) || defined(__CYGWIN__)
#if CMake_STAT_HAS_ST_MTIM
  // Times are integers in units of 1ns.
  long long bil = 1000000000;
  long long t1 = s1->st_mtim.tv_sec * bil + s1->st_mtim.tv_nsec;
  long long t2 = s2->st_mtim.tv_sec * bil + s2->st_mtim.tv_nsec;
  if (t1 < t2) {
    return (t2 - t1) >= bil;
  }
  if (t2 < t1) {
    return (t1 - t2) >= bil;
  }
  return false;
#elif CMake_STAT_HAS_ST_MTIMESPEC
  // Times are integers in units of 1ns.
  long long bil = 1000000000;
  long long t1 = s1->st_mtimespec.tv_sec * bil + s1->st_mtimespec.tv_nsec;
  long long t2 = s2->st_mtimespec.tv_sec * bil + s2->st_mtimespec.tv_nsec;
  if (t1 < t2) {
    return (t2 - t1) >= bil;
  } else if (t2 < t1) {
    return (t1 - t2) >= bil;
  } else {
    return false;
  }
#else
  // Times are integers in units of 1s.
  if (s1->st_mtime < s2->st_mtime) {
    return (s2->st_mtime - s1->st_mtime) >= 1;
  } else if (s1->st_mtime > s2->st_mtime) {
    return (s1->st_mtime - s2->st_mtime) >= 1;
  } else {
    return false;
  }
#endif
#else
  // Times are integers in units of 100ns.
  LARGE_INTEGER t1;
  LARGE_INTEGER t2;
  t1.LowPart = s1->dwLowDateTime;
  t1.HighPart = s1->dwHighDateTime;
  t2.LowPart = s2->dwLowDateTime;
  t2.HighPart = s2->dwHighDateTime;
  if (t1.QuadPart < t2.QuadPart) {
    return (t2.QuadPart - t1.QuadPart) >= static_cast<LONGLONG>(10000000);
  } else if (t2.QuadPart < t1.QuadPart) {
    return (t1.QuadPart - t2.QuadPart) >= static_cast<LONGLONG>(10000000);
  } else {
    return false;
  }
#endif
}

bool cmFileTimeComparisonInternal::FileTimeCompare(const char* f1,
                                                   const char* f2, int* result)
{
  // Get the modification time for each file.
  cmFileTimeComparison_Type s1;
  cmFileTimeComparison_Type s2;
  if (this->Stat(f1, &s1) && this->Stat(f2, &s2)) {
    // Compare the two modification times.
    *result = this->Compare(&s1, &s2);
    return true;
  }
  // No comparison available.  Default to the same time.
  *result = 0;
  return false;
}

bool cmFileTimeComparisonInternal::FileTimesDiffer(const char* f1,
                                                   const char* f2)
{
  // Get the modification time for each file.
  cmFileTimeComparison_Type s1;
  cmFileTimeComparison_Type s2;
  if (this->Stat(f1, &s1) && this->Stat(f2, &s2)) {
    // Compare the two modification times.
    return this->TimesDiffer(&s1, &s2);
  }
  // No comparison available.  Default to different times.
  return true;
}