diff options
-rw-r--r-- | Source/CMakeLists.txt | 2 | ||||
-rw-r--r-- | Source/CPack/cmCPackGenerator.cxx | 3 | ||||
-rw-r--r-- | Source/cmFileCommand.cxx | 19 | ||||
-rw-r--r-- | Source/cmFileCopier.cxx | 24 | ||||
-rw-r--r-- | Source/cmFileTimes.cxx | 127 | ||||
-rw-r--r-- | Source/cmFileTimes.h | 40 | ||||
-rw-r--r-- | Source/cmSystemTools.cxx | 122 | ||||
-rw-r--r-- | Source/cmSystemTools.h | 14 | ||||
-rwxr-xr-x | bootstrap | 1 |
9 files changed, 187 insertions, 165 deletions
diff --git a/Source/CMakeLists.txt b/Source/CMakeLists.txt index d7a0f3ee58..695e075987 100644 --- a/Source/CMakeLists.txt +++ b/Source/CMakeLists.txt @@ -242,6 +242,8 @@ set(SRCS cmFileTime.h cmFileTimeCache.cxx cmFileTimeCache.h + cmFileTimes.cxx + cmFileTimes.h cmFortranParserImpl.cxx cmFSPermissions.cxx cmFSPermissions.h diff --git a/Source/CPack/cmCPackGenerator.cxx b/Source/CPack/cmCPackGenerator.cxx index 127bcf9dd9..7e07ff4888 100644 --- a/Source/CPack/cmCPackGenerator.cxx +++ b/Source/CPack/cmCPackGenerator.cxx @@ -15,6 +15,7 @@ #include "cmCryptoHash.h" #include "cmDuration.h" #include "cmFSPermissions.h" +#include "cmFileTimes.h" #include "cmGeneratedFileStream.h" #include "cmGlobalGenerator.h" #include "cmMakefile.h" @@ -388,7 +389,7 @@ int cmCPackGenerator::InstallProjectViaInstalledDirectories( } /* If it is not a symlink then do a plain copy */ else if (!(cmSystemTools::CopyFileIfDifferent(inFile, filePath) && - cmSystemTools::CopyFileTime(inFile, filePath))) { + cmFileTimes::Copy(inFile, filePath))) { cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem copying file: " << inFile << " -> " << filePath << std::endl); diff --git a/Source/cmFileCommand.cxx b/Source/cmFileCommand.cxx index 17a6a74d70..c0e263ad2e 100644 --- a/Source/cmFileCommand.cxx +++ b/Source/cmFileCommand.cxx @@ -25,6 +25,7 @@ #include "cmFileCopier.h" #include "cmFileInstaller.h" #include "cmFileLockPool.h" +#include "cmFileTimes.h" #include "cmGeneratorExpression.h" #include "cmGlobalGenerator.h" #include "cmHexFileConverter.h" @@ -52,8 +53,6 @@ # include <windows.h> #endif -class cmSystemToolsFileTime; - #if defined(_WIN32) // libcurl doesn't support file:// urls for unicode filenames on Windows. // Convert string from UTF-8 to ACP if this is a file:// URL. @@ -1114,8 +1113,7 @@ bool cmFileCommand::HandleRPathChangeCommand( return false; } bool success = true; - cmSystemToolsFileTime* ft = cmSystemTools::FileTimeNew(); - bool have_ft = cmSystemTools::FileTimeGet(file, ft); + cmFileTimes const ft(file); std::string emsg; bool changed; if (!cmSystemTools::ChangeRPath(file, oldRPath, newRPath, &emsg, &changed)) { @@ -1139,11 +1137,8 @@ bool cmFileCommand::HandleRPathChangeCommand( message += "\""; this->Makefile->DisplayStatus(message, -1); } - if (have_ft) { - cmSystemTools::FileTimeSet(file, ft); - } + ft.Store(file); } - cmSystemTools::FileTimeDelete(ft); return success; } @@ -1182,8 +1177,7 @@ bool cmFileCommand::HandleRPathRemoveCommand( return false; } bool success = true; - cmSystemToolsFileTime* ft = cmSystemTools::FileTimeNew(); - bool have_ft = cmSystemTools::FileTimeGet(file, ft); + cmFileTimes const ft(file); std::string emsg; bool removed; if (!cmSystemTools::RemoveRPath(file, &emsg, &removed)) { @@ -1203,11 +1197,8 @@ bool cmFileCommand::HandleRPathRemoveCommand( message += "\""; this->Makefile->DisplayStatus(message, -1); } - if (have_ft) { - cmSystemTools::FileTimeSet(file, ft); - } + ft.Store(file); } - cmSystemTools::FileTimeDelete(ft); return success; } diff --git a/Source/cmFileCopier.cxx b/Source/cmFileCopier.cxx index 972cd6e19b..49e8cd5b3c 100644 --- a/Source/cmFileCopier.cxx +++ b/Source/cmFileCopier.cxx @@ -5,6 +5,7 @@ #include "cmFSPermissions.h" #include "cmFileCommand.h" +#include "cmFileTimes.h" #include "cmMakefile.h" #include "cmSystemTools.h" #include "cmsys/Directory.hxx" @@ -75,20 +76,15 @@ bool cmFileCopier::SetPermissions(const std::string& toFile, // Writing to an NTFS alternate stream changes the modification // time, so we need to save and restore its original value. - cmSystemToolsFileTime* file_time_orig = cmSystemTools::FileTimeNew(); - cmSystemTools::FileTimeGet(toFile, file_time_orig); - - cmsys::ofstream permissionStream(mode_t_adt_filename.c_str()); - - if (permissionStream) { - permissionStream << std::oct << permissions << std::endl; + cmFileTimes file_time_orig(toFile); + { + cmsys::ofstream permissionStream(mode_t_adt_filename.c_str()); + if (permissionStream) { + permissionStream << std::oct << permissions << std::endl; + } + permissionStream.close(); } - - permissionStream.close(); - - cmSystemTools::FileTimeSet(toFile, file_time_orig); - - cmSystemTools::FileTimeDelete(file_time_orig); + file_time_orig.Store(toFile); } #endif @@ -614,7 +610,7 @@ bool cmFileCopier::InstallFile(const std::string& fromFile, if (cmSystemTools::GetPermissions(toFile, perm)) { cmSystemTools::SetPermissions(toFile, perm | mode_owner_write); } - if (!cmSystemTools::CopyFileTime(fromFile, toFile)) { + if (!cmFileTimes::Copy(fromFile, toFile)) { std::ostringstream e; e << this->Name << " cannot set modification time on \"" << toFile << "\""; diff --git a/Source/cmFileTimes.cxx b/Source/cmFileTimes.cxx new file mode 100644 index 0000000000..fd4f6797fe --- /dev/null +++ b/Source/cmFileTimes.cxx @@ -0,0 +1,127 @@ +/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying + file Copyright.txt or https://cmake.org/licensing for details. */ +#include "cmFileTimes.h" + +#include "cmAlgorithms.h" +#include "cm_sys_stat.h" + +#include <utility> + +#if defined(_WIN32) +# include "cmSystemTools.h" +# include <windows.h> +#else +# include <utime.h> +#endif + +#if defined(_WIN32) && \ + (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__MINGW32__)) +# include <io.h> +#endif + +#ifdef _WIN32 +class cmFileTimes::WindowsHandle +{ +public: + WindowsHandle(HANDLE h) + : handle_(h) + { + } + ~WindowsHandle() + { + if (this->handle_ != INVALID_HANDLE_VALUE) { + CloseHandle(this->handle_); + } + } + explicit operator bool() const + { + return this->handle_ != INVALID_HANDLE_VALUE; + } + bool operator!() const { return this->handle_ == INVALID_HANDLE_VALUE; } + operator HANDLE() const { return this->handle_; } + +private: + HANDLE handle_; +}; +#endif + +class cmFileTimes::Times +{ +public: +#if defined(_WIN32) && !defined(__CYGWIN__) + FILETIME timeCreation; + FILETIME timeLastAccess; + FILETIME timeLastWrite; +#else + struct utimbuf timeBuf; +#endif +}; + +cmFileTimes::cmFileTimes() = default; +cmFileTimes::cmFileTimes(std::string const& fileName) +{ + Load(fileName); +} +cmFileTimes::~cmFileTimes() = default; + +bool cmFileTimes::Load(std::string const& fileName) +{ + std::unique_ptr<Times> ptr; + if (IsValid()) { + // Invalidate this and re-use times + ptr.swap(this->times); + } else { + ptr = cm::make_unique<Times>(); + } + +#if defined(_WIN32) && !defined(__CYGWIN__) + cmFileTimes::WindowsHandle handle = + CreateFileW(cmSystemTools::ConvertToWindowsExtendedPath(fileName).c_str(), + GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, + FILE_FLAG_BACKUP_SEMANTICS, 0); + if (!handle) { + return false; + } + if (!GetFileTime(handle, &ptr->timeCreation, &ptr->timeLastAccess, + &ptr->timeLastWrite)) { + return false; + } +#else + struct stat st; + if (stat(fileName.c_str(), &st) < 0) { + return false; + } + ptr->timeBuf.actime = st.st_atime; + ptr->timeBuf.modtime = st.st_mtime; +#endif + // Accept times + this->times = std::move(ptr); + return true; +} + +bool cmFileTimes::Store(std::string const& fileName) const +{ + if (!IsValid()) { + return false; + } + +#if defined(_WIN32) && !defined(__CYGWIN__) + cmFileTimes::WindowsHandle handle = CreateFileW( + cmSystemTools::ConvertToWindowsExtendedPath(fileName).c_str(), + FILE_WRITE_ATTRIBUTES, 0, 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0); + if (!handle) { + return false; + } + return SetFileTime(handle, &this->times->timeCreation, + &this->times->timeLastAccess, + &this->times->timeLastWrite) != 0; +#else + return utime(fileName.c_str(), &this->times->timeBuf) >= 0; +#endif +} + +bool cmFileTimes::Copy(std::string const& fromFile, std::string const& toFile) +{ + cmFileTimes fileTimes; + return (fileTimes.Load(fromFile) && fileTimes.Store(toFile)); +} diff --git a/Source/cmFileTimes.h b/Source/cmFileTimes.h new file mode 100644 index 0000000000..cbf0fe20a9 --- /dev/null +++ b/Source/cmFileTimes.h @@ -0,0 +1,40 @@ +/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying + file Copyright.txt or https://cmake.org/licensing for details. */ +#ifndef cmFileTimes_h +#define cmFileTimes_h + +#include "cmConfigure.h" // IWYU pragma: keep + +#include <memory> // IWYU pragma: keep +#include <string> + +/** \class cmFileTimes + * \brief Loads and stores file times. + */ +class cmFileTimes +{ +public: + cmFileTimes(); + //! Calls Load() + cmFileTimes(std::string const& fileName); + ~cmFileTimes(); + + //! @return true, if file times were loaded successfully + bool IsValid() const { return (times != nullptr); } + //! Try to load the file times from @a fileName and @return IsValid() + bool Load(std::string const& fileName); + //! Stores the file times at @a fileName (if IsValid()) + bool Store(std::string const& fileName) const; + + //! Copies the file times of @a fromFile to @a toFile + static bool Copy(std::string const& fromFile, std::string const& toFile); + +private: +#ifdef _WIN32 + class WindowsHandle; +#endif + class Times; + std::unique_ptr<Times> times; +}; + +#endif diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx index 545e6c5d88..9fdcfae93c 100644 --- a/Source/cmSystemTools.cxx +++ b/Source/cmSystemTools.cxx @@ -6,7 +6,6 @@ #include "cmDuration.h" #include "cmProcessOutput.h" #include "cmRange.h" -#include "cm_sys_stat.h" #include "cm_uv.h" #if defined(CMAKE_BUILD_WITH_CMAKE) @@ -60,7 +59,6 @@ #else # include <sys/time.h> # include <unistd.h> -# include <utime.h> #endif #if defined(_WIN32) && \ @@ -90,18 +88,6 @@ static bool cm_isspace(char c) return ((c & 0x80) == 0) && isspace(c); } -class cmSystemToolsFileTime -{ -public: -#if defined(_WIN32) && !defined(__CYGWIN__) - FILETIME timeCreation; - FILETIME timeLastAccess; - FILETIME timeLastWrite; -#else - struct utimbuf timeBuf; -#endif -}; - #if !defined(HAVE_ENVIRON_NOT_REQUIRE_PROTOTYPE) // For GetEnvironmentVariables # if defined(_WIN32) @@ -134,29 +120,6 @@ static int cm_archive_read_open_file(struct archive* a, const char* file, #endif #ifdef _WIN32 -class cmSystemToolsWindowsHandle -{ -public: - cmSystemToolsWindowsHandle(HANDLE h) - : handle_(h) - { - } - ~cmSystemToolsWindowsHandle() - { - if (this->handle_ != INVALID_HANDLE_VALUE) { - CloseHandle(this->handle_); - } - } - explicit operator bool() const - { - return this->handle_ != INVALID_HANDLE_VALUE; - } - bool operator!() const { return this->handle_ == INVALID_HANDLE_VALUE; } - operator HANDLE() const { return this->handle_; } - -private: - HANDLE handle_; -}; #elif defined(__APPLE__) # include <crt_externs.h> @@ -2098,91 +2061,6 @@ void cmSystemTools::DoNotInheritStdPipes() #endif } -bool cmSystemTools::CopyFileTime(const std::string& fromFile, - const std::string& toFile) -{ -#if defined(_WIN32) && !defined(__CYGWIN__) - cmSystemToolsWindowsHandle hFrom = CreateFileW( - SystemTools::ConvertToWindowsExtendedPath(fromFile).c_str(), GENERIC_READ, - FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0); - cmSystemToolsWindowsHandle hTo = CreateFileW( - SystemTools::ConvertToWindowsExtendedPath(toFile).c_str(), - FILE_WRITE_ATTRIBUTES, 0, 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0); - if (!hFrom || !hTo) { - return false; - } - FILETIME timeCreation; - FILETIME timeLastAccess; - FILETIME timeLastWrite; - if (!GetFileTime(hFrom, &timeCreation, &timeLastAccess, &timeLastWrite)) { - return false; - } - return SetFileTime(hTo, &timeCreation, &timeLastAccess, &timeLastWrite) != 0; -#else - struct stat fromStat; - if (stat(fromFile.c_str(), &fromStat) < 0) { - return false; - } - - struct utimbuf buf; - buf.actime = fromStat.st_atime; - buf.modtime = fromStat.st_mtime; - return utime(toFile.c_str(), &buf) >= 0; -#endif -} - -cmSystemToolsFileTime* cmSystemTools::FileTimeNew() -{ - return new cmSystemToolsFileTime; -} - -void cmSystemTools::FileTimeDelete(cmSystemToolsFileTime* t) -{ - delete t; -} - -bool cmSystemTools::FileTimeGet(const std::string& fname, - cmSystemToolsFileTime* t) -{ -#if defined(_WIN32) && !defined(__CYGWIN__) - cmSystemToolsWindowsHandle h = CreateFileW( - SystemTools::ConvertToWindowsExtendedPath(fname).c_str(), GENERIC_READ, - FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0); - if (!h) { - return false; - } - if (!GetFileTime(h, &t->timeCreation, &t->timeLastAccess, - &t->timeLastWrite)) { - return false; - } -#else - struct stat st; - if (stat(fname.c_str(), &st) < 0) { - return false; - } - t->timeBuf.actime = st.st_atime; - t->timeBuf.modtime = st.st_mtime; -#endif - return true; -} - -bool cmSystemTools::FileTimeSet(const std::string& fname, - const cmSystemToolsFileTime* t) -{ -#if defined(_WIN32) && !defined(__CYGWIN__) - cmSystemToolsWindowsHandle h = CreateFileW( - SystemTools::ConvertToWindowsExtendedPath(fname).c_str(), - FILE_WRITE_ATTRIBUTES, 0, 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0); - if (!h) { - return false; - } - return SetFileTime(h, &t->timeCreation, &t->timeLastAccess, - &t->timeLastWrite) != 0; -#else - return utime(fname.c_str(), &t->timeBuf) >= 0; -#endif -} - #ifdef _WIN32 # ifndef CRYPT_SILENT # define CRYPT_SILENT 0x40 /* Not defined by VS 6 version of header. */ diff --git a/Source/cmSystemTools.h b/Source/cmSystemTools.h index d145d478b9..fefa413293 100644 --- a/Source/cmSystemTools.h +++ b/Source/cmSystemTools.h @@ -15,8 +15,6 @@ #include <string> #include <vector> -class cmSystemToolsFileTime; - /** \class cmSystemTools * \brief A collection of useful functions for CMake. * @@ -470,18 +468,6 @@ public: static void EnsureStdPipes(); - /** Copy the file create/access/modify times from the file named by - the first argument to that named by the second. */ - static bool CopyFileTime(const std::string& fromFile, - const std::string& toFile); - - /** Save and restore file times. */ - static cmSystemToolsFileTime* FileTimeNew(); - static void FileTimeDelete(cmSystemToolsFileTime*); - static bool FileTimeGet(const std::string& fname, cmSystemToolsFileTime* t); - static bool FileTimeSet(const std::string& fname, - const cmSystemToolsFileTime* t); - /** Random seed generation. */ static unsigned int RandomSeed(); @@ -306,6 +306,7 @@ CMAKE_CXX_SOURCES="\ cmFileInstaller \ cmFileTime \ cmFileTimeCache \ + cmFileTimes \ cmFindBase \ cmFindCommon \ cmFindFileCommand \ |