Răsfoiți Sursa

Merge topic 'cmFileTimes'

5b53cfda24 cmSystemTools: Remove cmSystemToolsFileTime interface
9c576a88d9 Use cmFileTimes instead of cmSystemToolsFileTime interface
4b45a5d5c7 cmFileTimes: New RAII based cmFileTimes class

Acked-by: Kitware Robot <[email protected]>
Merge-request: !3358
Brad King 6 ani în urmă
părinte
comite
d1a570f18c

+ 2 - 0
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

+ 2 - 1
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);

+ 5 - 14
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;
 }
 

+ 10 - 14
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
         << "\"";

+ 127 - 0
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));
+}

+ 40 - 0
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

+ 0 - 122
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.  */

+ 0 - 14
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();
 

+ 1 - 0
bootstrap

@@ -306,6 +306,7 @@ CMAKE_CXX_SOURCES="\
   cmFileInstaller \
   cmFileTime \
   cmFileTimeCache \
+  cmFileTimes \
   cmFindBase \
   cmFindCommon \
   cmFindFileCommand \