From 703b8c822592d51d449930e1280c2a3dc92698e9 Mon Sep 17 00:00:00 2001 From: Brad King Date: Mon, 14 Apr 2008 11:43:45 -0400 Subject: ENH: Added methods to cmSystemTools to save and restore file modification times. --- Source/cmSystemTools.cxx | 73 ++++++++++++++++++++++++++++++++++++++++++++++++ Source/cmSystemTools.h | 8 +++++- 2 files changed, 80 insertions(+), 1 deletion(-) diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx index 6091ab5..68ebb4f 100644 --- a/Source/cmSystemTools.cxx +++ b/Source/cmSystemTools.cxx @@ -56,6 +56,18 @@ # include "cmELF.h" #endif +class cmSystemToolsFileTime +{ +public: +#if defined(_WIN32) && !defined(__CYGWIN__) + FILETIME timeCreation; + FILETIME timeLastAccess; + FILETIME timeLastWrite; +#else + struct utimbuf timeBuf; +#endif +}; + #if defined(__sgi) && !defined(__GNUC__) # pragma set woff 1375 /* base class destructor not virtual */ #endif @@ -2111,6 +2123,67 @@ bool cmSystemTools::CopyFileTime(const char* fromFile, const char* toFile) } //---------------------------------------------------------------------------- +cmSystemToolsFileTime* cmSystemTools::FileTimeNew() +{ + return new cmSystemToolsFileTime; +} + +//---------------------------------------------------------------------------- +void cmSystemTools::FileTimeDelete(cmSystemToolsFileTime* t) +{ + delete t; +} + +//---------------------------------------------------------------------------- +bool cmSystemTools::FileTimeGet(const char* fname, cmSystemToolsFileTime* t) +{ +#if defined(_WIN32) && !defined(__CYGWIN__) + cmSystemToolsWindowsHandle h = + CreateFile(fname, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0); + if(!h) + { + return false; + } + if(!GetFileTime(h, &t->timeCreation, &t->timeLastAccess, &t->timeLastWrite)) + { + return false; + } +#else + struct stat st; + if(stat(fname, &st) < 0) + { + return false; + } + t->timeBuf.actime = st.st_atime; + t->timeBuf.modtime = st.st_mtime; +#endif + return true; +} + +//---------------------------------------------------------------------------- +bool cmSystemTools::FileTimeSet(const char* fname, cmSystemToolsFileTime* t) +{ +#if defined(_WIN32) && !defined(__CYGWIN__) + cmSystemToolsWindowsHandle h = + CreateFile(toFile, GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0); + if(!h) + { + return false; + } + if(!SetFileTime(h, &t->timeCreation, &t->timeLastAccess, &t->timeLastWrite)) + { + return false; + } +#else + if(utime(fname, &t->timeBuf) < 0) + { + return false; + } +#endif + return true; +} + +//---------------------------------------------------------------------------- static std::string cmSystemToolsExecutableDirectory; void cmSystemTools::FindExecutableDirectory(const char* argv0) { diff --git a/Source/cmSystemTools.h b/Source/cmSystemTools.h index a7abc5f..18c4939 100644 --- a/Source/cmSystemTools.h +++ b/Source/cmSystemTools.h @@ -22,7 +22,7 @@ #include #include - +class cmSystemToolsFileTime; /** \class cmSystemTools * \brief A collection of useful functions for CMake. @@ -363,6 +363,12 @@ public: the first argument to that named by the second. */ static bool CopyFileTime(const char* fromFile, const char* toFile); + /** Save and restore file times. */ + static cmSystemToolsFileTime* FileTimeNew(); + static void FileTimeDelete(cmSystemToolsFileTime*); + static bool FileTimeGet(const char* fname, cmSystemToolsFileTime* t); + static bool FileTimeSet(const char* fname, cmSystemToolsFileTime* t); + /** Find the directory containing the running executable. Save it in a global location to be queried by GetExecutableDirectory later. */ -- cgit v0.12