CMake/Source/cmFileTimeComparison.cxx

259 lines
7.2 KiB
C++
Raw Permalink Normal View History

Simplify CMake per-source license notices Per-source copyright/license notice headers that spell out copyright holder names and years are hard to maintain and often out-of-date or plain wrong. Precise contributor information is already maintained automatically by the version control tool. Ultimately it is the receiver of a file who is responsible for determining its licensing status, and per-source notices are merely a convenience. Therefore it is simpler and more accurate for each source to have a generic notice of the license name and references to more detailed information on copyright holders and full license terms. Our `Copyright.txt` file now contains a list of Contributors whose names appeared source-level copyright notices. It also references version control history for more precise information. Therefore we no longer need to spell out the list of Contributors in each source file notice. Replace CMake per-source copyright/license notice headers with a short description of the license and links to `Copyright.txt` and online information available from "https://cmake.org/licensing". The online URL also handles cases of modules being copied out of our source into other projects, so we can drop our notices about replacing links with full license text. Run the `Utilities/Scripts/filter-notices.bash` script to perform the majority of the replacements mechanically. Manually fix up shebang lines and trailing newlines in a few files. Manually update the notices in a few files that the script does not handle.
2016-09-27 22:01:08 +03:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
2005-10-12 21:36:07 +04:00
#include "cmFileTimeComparison.h"
#include <cmConfigure.h>
#include <string>
#include <time.h>
#include <utility>
// Use a hash table to avoid duplicate file time checks from disk.
2005-10-12 21:36:07 +04:00
#if defined(CMAKE_BUILD_WITH_CMAKE)
#ifdef CMake_HAVE_CXX_UNORDERED_MAP
#include <unordered_map>
#else
#include <cmsys/hash_map.hxx>
2005-10-12 21:36:07 +04:00
#endif
#endif
2005-10-12 21:36:07 +04:00
// Use a platform-specific API to get file times efficiently.
2005-10-12 21:36:07 +04:00
#if !defined(_WIN32) || defined(__CYGWIN__)
#include <sys/stat.h>
#define cmFileTimeComparison_Type struct stat
2005-10-12 21:36:07 +04:00
#else
#include <cmsys/Encoding.hxx>
#include <windows.h>
#define cmFileTimeComparison_Type FILETIME
2005-10-12 21:36:07 +04:00
#endif
class cmFileTimeComparisonInternal
{
public:
// Internal comparison method.
2005-10-12 21:36:07 +04:00
inline bool FileTimeCompare(const char* f1, const char* f2, int* result);
bool FileTimesDiffer(const char* f1, const char* f2);
2005-10-12 21:36:07 +04:00
private:
#if defined(CMAKE_BUILD_WITH_CMAKE)
// Use a hash table to efficiently map from file name to modification time.
2005-10-12 21:36:07 +04:00
class HashString
{
2005-10-12 21:36:07 +04:00
public:
size_t operator()(const std::string& s) const { return h(s.c_str()); }
#ifdef CMake_HAVE_CXX_UNORDERED_MAP
std::hash<const char*> h;
#else
2005-10-12 21:36:07 +04:00
cmsys::hash<const char*> h;
#endif
};
#ifdef CMake_HAVE_CXX_UNORDERED_MAP
typedef std::unordered_map<std::string,
#else
typedef cmsys::hash_map<std::string,
#endif
cmFileTimeComparison_Type, HashString>
FileStatsMap;
2005-10-12 21:36:07 +04:00
FileStatsMap Files;
#endif
// Internal methods to lookup and compare modification times.
2005-10-12 21:36:07 +04:00
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);
2005-10-12 21:36:07 +04:00
};
bool cmFileTimeComparisonInternal::Stat(const char* fname,
cmFileTimeComparison_Type* st)
2005-10-12 21:36:07 +04:00
{
#if defined(CMAKE_BUILD_WITH_CMAKE)
// Use the stored time if available.
cmFileTimeComparisonInternal::FileStatsMap::iterator fit =
this->Files.find(fname);
if (fit != this->Files.end()) {
2005-10-12 21:36:07 +04:00
*st = fit->second;
return true;
}
2005-10-12 21:36:07 +04:00
#endif
2005-10-12 21:36:07 +04:00
#if !defined(_WIN32) || defined(__CYGWIN__)
// POSIX version. Use the stat function.
2005-10-12 21:36:07 +04:00
int res = ::stat(fname, st);
if (res != 0) {
2005-10-12 21:36:07 +04:00
return false;
}
2005-10-12 21:36:07 +04:00
#else
2006-05-10 23:46:45 +04:00
// 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)) {
2005-10-12 21:36:07 +04:00
return false;
}
// Copy the file time to the output location.
*st = fdata.ftLastWriteTime;
2005-10-12 21:36:07 +04:00
#endif
2005-10-12 21:36:07 +04:00
#if defined(CMAKE_BUILD_WITH_CMAKE)
// Store the time for future use.
2005-10-12 21:36:07 +04:00
this->Files[fname] = *st;
#endif
2005-10-12 21:36:07 +04:00
return true;
}
cmFileTimeComparison::cmFileTimeComparison()
{
2006-03-15 19:02:08 +03:00
this->Internals = new cmFileTimeComparisonInternal;
2005-10-12 21:36:07 +04:00
}
cmFileTimeComparison::~cmFileTimeComparison()
{
2006-03-15 19:02:08 +03:00
delete this->Internals;
2005-10-12 21:36:07 +04:00
}
bool cmFileTimeComparison::FileTimeCompare(const char* f1, const char* f2,
int* result)
2005-10-12 21:36:07 +04:00
{
2006-03-15 19:02:08 +03:00
return this->Internals->FileTimeCompare(f1, f2, result);
2005-10-12 21:36:07 +04:00
}
bool cmFileTimeComparison::FileTimesDiffer(const char* f1, const char* f2)
{
return this->Internals->FileTimesDiffer(f1, f2);
}
int cmFileTimeComparisonInternal::Compare(cmFileTimeComparison_Type* s1,
2006-05-10 23:46:45 +04:00
cmFileTimeComparison_Type* s2)
2005-10-12 21:36:07 +04:00
{
#if !defined(_WIN32) || defined(__CYGWIN__)
#if CMake_STAT_HAS_ST_MTIM
2005-10-12 21:36:07 +04:00
// Compare using nanosecond resolution.
if (s1->st_mtim.tv_sec < s2->st_mtim.tv_sec) {
2005-10-12 21:36:07 +04:00
return -1;
2016-08-18 21:36:29 +03:00
}
if (s1->st_mtim.tv_sec > s2->st_mtim.tv_sec) {
2005-10-12 21:36:07 +04:00
return 1;
2016-08-18 21:36:29 +03:00
}
if (s1->st_mtim.tv_nsec < s2->st_mtim.tv_nsec) {
2005-10-12 21:36:07 +04:00
return -1;
2016-08-18 21:36:29 +03:00
}
if (s1->st_mtim.tv_nsec > s2->st_mtim.tv_nsec) {
2005-10-12 21:36:07 +04:00
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
2005-10-12 21:36:07 +04:00
// Compare using 1 second resolution.
if (s1->st_mtime < s2->st_mtime) {
2005-10-12 21:36:07 +04:00
return -1;
} else if (s1->st_mtime > s2->st_mtime) {
2005-10-12 21:36:07 +04:00
return 1;
}
#endif
// Files have the same time.
return 0;
2005-10-12 21:36:07 +04:00
#else
// Compare using system-provided function.
2005-10-12 21:36:07 +04:00
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;
2016-08-18 21:36:29 +03:00
}
if (t2 < t1) {
return (t1 - t2) >= bil;
}
2016-08-18 21:36:29 +03:00
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)
2005-10-12 21:36:07 +04:00
{
// Get the modification time for each file.
2005-10-12 21:36:07 +04:00
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;
}
2016-08-18 21:36:29 +03:00
// No comparison available. Default to the same time.
*result = 0;
return false;
2005-10-12 21:36:07 +04:00
}
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);
}
2016-08-18 21:36:29 +03:00
// No comparison available. Default to different times.
return true;
}