Skip to content
Snippets Groups Projects
Commit 6f24c4e9 authored by Sebastian Holtermann's avatar Sebastian Holtermann
Browse files

New cmFileTime class

The new cmFileTime class stores the file modification time as an OS independent
nanosecond count.  Its main use is to load and compare file modification times
in nanosecond and second resolution.
parent b06f8c93
No related branches found
No related tags found
No related merge requests found
...@@ -236,6 +236,8 @@ set(SRCS ...@@ -236,6 +236,8 @@ set(SRCS
cmFileLockResult.h cmFileLockResult.h
cmFilePathChecksum.cxx cmFilePathChecksum.cxx
cmFilePathChecksum.h cmFilePathChecksum.h
cmFileTime.cxx
cmFileTime.h
cmFileTimeComparison.cxx cmFileTimeComparison.cxx
cmFileTimeComparison.h cmFileTimeComparison.h
cmFortranParserImpl.cxx cmFortranParserImpl.cxx
...@@ -796,6 +798,8 @@ foreach(check ...@@ -796,6 +798,8 @@ foreach(check
else() else()
set(CMake_${check} 0) set(CMake_${check} 0)
endif() endif()
set_property(SOURCE cmFileTime.cxx APPEND PROPERTY
COMPILE_DEFINITIONS CMake_${check}=${CMake_${check}})
set_property(SOURCE cmFileTimeComparison.cxx APPEND PROPERTY set_property(SOURCE cmFileTimeComparison.cxx APPEND PROPERTY
COMPILE_DEFINITIONS CMake_${check}=${CMake_${check}}) COMPILE_DEFINITIONS CMake_${check}=${CMake_${check}})
endforeach() endforeach()
......
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmFileTime.h"
#include <string>
#include <time.h>
// Use a platform-specific API to get file times efficiently.
#if !defined(_WIN32) || defined(__CYGWIN__)
# include "cm_sys_stat.h"
#else
# include "cmsys/Encoding.hxx"
# include <windows.h>
#endif
bool cmFileTime::Load(std::string const& fileName)
{
#if !defined(_WIN32) || defined(__CYGWIN__)
// POSIX version. Use the stat function.
struct stat fst;
if (::stat(fileName.c_str(), &fst) != 0) {
return false;
}
# if CMake_STAT_HAS_ST_MTIM
// Nanosecond resolution
this->NS = fst.st_mtim.tv_sec * NsPerS + fst.st_mtim.tv_nsec;
# elif CMake_STAT_HAS_ST_MTIMESPEC
// Nanosecond resolution
this->NS = fst.st_mtimespec.tv_sec * NsPerS + fst.st_mtimespec.tv_nsec;
# else
// Second resolution
this->NS = fst.st_mtime * NsPerS;
# endif
#else
// Windows version. Get the modification time from extended file attributes.
WIN32_FILE_ATTRIBUTE_DATA fdata;
if (!GetFileAttributesExW(cmsys::Encoding::ToWide(fileName).c_str(),
GetFileExInfoStandard, &fdata)) {
return false;
}
// Copy the file time to the output location.
this->NS = (static_cast<NSC>(fdata.ftLastWriteTime.dwHighDateTime) << 32) |
static_cast<NSC>(fdata.ftLastWriteTime.dwLowDateTime);
// The file time resolution is 100 ns.
this->NS *= 100;
#endif
return true;
}
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#ifndef cmFileTime_h
#define cmFileTime_h
#include "cmConfigure.h" // IWYU pragma: keep
#include <string>
/** \class cmFileTime
* \brief Abstract file modification time with support for comparison with
* other file modification times.
*/
class cmFileTime
{
public:
typedef long long NSC;
static constexpr NSC NsPerS = 1000000000;
cmFileTime() = default;
~cmFileTime() = default;
/**
* @brief Loads the file time of fileName from the file system
* @return true on success
*/
bool Load(std::string const& fileName);
/**
* @brief Return true if this is older than ftm
*/
bool Older(cmFileTime const& ftm) const { return (this->NS - ftm.NS) < 0; }
/**
* @brief Return true if this is newer than ftm
*/
bool Newer(cmFileTime const& ftm) const { return (ftm.NS - this->NS) < 0; }
/**
* @brief Return true if this is the same as ftm
*/
bool Equal(cmFileTime const& ftm) const { return this->NS == ftm.NS; }
/**
* @brief Return true if this is not the same as ftm
*/
bool Differ(cmFileTime const& ftm) const { return this->NS != ftm.NS; }
/**
* @brief Compare file modification times.
* @return -1, 0, +1 for this older, same, or newer than ftm.
*/
int Compare(cmFileTime const& ftm)
{
NSC const diff = this->NS - ftm.NS;
if (diff == 0) {
return 0;
}
return (diff < 0) ? -1 : 1;
}
// -- Comparison in second resolution
/**
* @brief Return true if this is at least a second older than ftm
*/
bool OlderS(cmFileTime const& ftm) const
{
return (ftm.NS - this->NS) >= cmFileTime::NsPerS;
}
/**
* @brief Return true if this is at least a second newer than ftm
*/
bool NewerS(cmFileTime const& ftm) const
{
return (this->NS - ftm.NS) >= cmFileTime::NsPerS;
}
/**
* @brief Return true if this is within the same second as ftm
*/
bool EqualS(cmFileTime const& ftm) const
{
NSC diff = this->NS - ftm.NS;
if (diff < 0) {
diff = -diff;
}
return (diff < cmFileTime::NsPerS);
}
/**
* @brief Return true if this is older or newer than ftm by at least a second
*/
bool DifferS(cmFileTime const& ftm) const
{
NSC diff = this->NS - ftm.NS;
if (diff < 0) {
diff = -diff;
}
return (diff >= cmFileTime::NsPerS);
}
/**
* @brief Compare file modification times.
* @return -1: this at least a second older, 0: this within the same second
* as ftm, +1: this at least a second newer than ftm.
*/
int CompareS(cmFileTime const& ftm) const
{
NSC const diff = this->NS - ftm.NS;
if (diff <= -cmFileTime::NsPerS) {
return -1;
}
if (diff >= cmFileTime::NsPerS) {
return 1;
}
return 0;
}
/**
* @brief The file modification time in nanoseconds
*/
NSC GetNS() const { return this->NS; }
private:
NSC NS = 0;
};
#endif
...@@ -304,6 +304,7 @@ CMAKE_CXX_SOURCES="\ ...@@ -304,6 +304,7 @@ CMAKE_CXX_SOURCES="\
cmFileCommand \ cmFileCommand \
cmFileCopier \ cmFileCopier \
cmFileInstaller \ cmFileInstaller \
cmFileTime \
cmFileTimeComparison \ cmFileTimeComparison \
cmFindBase \ cmFindBase \
cmFindCommon \ cmFindCommon \
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment