]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
enhance: Add util::set_timestamps function for setting mtime/atime
authorJoel Rosdahl <joel@rosdahl.net>
Wed, 8 Jun 2022 12:04:55 +0000 (14:04 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Wed, 8 Jun 2022 14:49:44 +0000 (16:49 +0200)
cmake/GenerateConfigurationFile.cmake
cmake/config.h.in
src/ResultRetriever.cpp
src/Util.cpp
src/Util.hpp
src/storage/primary/PrimaryStorage.cpp
src/storage/secondary/FileStorage.cpp
src/util/file.cpp
src/util/file.hpp

index df909337fb89171d87b21a17fa76beda5981f75c..f80258b0f7f9257a0889f693dc395e053d44c9f3 100644 (file)
@@ -36,7 +36,8 @@ set(functions
     strndup
     syslog
     unsetenv
-    utimes)
+    utimensat
+)
 foreach(func IN ITEMS ${functions})
   string(TOUPPER ${func} func_var)
   set(func_var HAVE_${func_var})
index 729a1d4af618242aeba9c790eff75c061630245f..bbc1397f536b52bbbb3cb1d96b6c86e1760e7bd1 100644 (file)
 // Define if you have the "unsetenv" function.
 #cmakedefine HAVE_UNSETENV
 
-// Define if you have the "utimes" function.
-#cmakedefine HAVE_UTIMES
+// Define if you have the "utimensat" function.
+#cmakedefine HAVE_UTIMENSAT
 
 // Define if you have the "PTHREAD_MUTEX_ROBUST" constant.
 #cmakedefine HAVE_PTHREAD_MUTEX_ROBUST
index c1c584ebc815071cde11099407245b528127426c..6f079e1a916f800f002974715fba76c6feaa0532 100644 (file)
@@ -24,6 +24,7 @@
 
 #include <core/exceptions.hpp>
 #include <core/wincompat.hpp>
+#include <util/file.hpp>
 
 #include <fcntl.h>
 #include <sys/stat.h>
@@ -121,7 +122,7 @@ ResultRetriever::on_entry_start(uint8_t entry_number,
 
     // Update modification timestamp to save the file from LRU cleanup (and, if
     // hard-linked, to make the object file newer than the source file).
-    Util::update_mtime(*raw_file);
+    util::set_timestamps(*raw_file);
   } else {
     LOG("Writing to {}", dest_path);
     m_dest_fd = Fd(
index 501e2c3d22a4b7f18be22806a50606af602a78a0..7770fabd48f433f7e64f4fd9a5138adc84c515ec 100644 (file)
@@ -64,12 +64,6 @@ extern "C" {
 #  include <sys/time.h>
 #endif
 
-#ifdef HAVE_UTIME_H
-#  include <utime.h>
-#elif defined(HAVE_SYS_UTIME_H)
-#  include <sys/utime.h>
-#endif
-
 #ifdef HAVE_LINUX_FS_H
 #  include <linux/magic.h>
 #  include <sys/statfs.h>
@@ -1500,16 +1494,6 @@ unsetenv(const std::string& name)
 #endif
 }
 
-void
-update_mtime(const std::string& path)
-{
-#ifdef HAVE_UTIMES
-  utimes(path.c_str(), nullptr);
-#else
-  utime(path.c_str(), nullptr);
-#endif
-}
-
 void
 wipe_path(const std::string& path)
 {
index 6e299f5838042876d9121e7b41fb44dcb7839b44..0cf84567745103a26075a02ba2e9689469d68af9 100644 (file)
@@ -401,9 +401,6 @@ bool unlink_tmp(const std::string& path,
 // Unset environment variable `name`.
 void unsetenv(const std::string& name);
 
-// Set mtime of `path` to the current timestamp.
-void update_mtime(const std::string& path);
-
 // Remove `path` (and its contents if it's a directory). A nonexistent path is
 // not considered an error.
 //
index c89ebb723d5e66f6e50be13d34f6572504f51a14..b3e709bb114be5ba6f457b771b497adc99959867 100644 (file)
@@ -196,7 +196,7 @@ PrimaryStorage::get(const Digest& key, const core::CacheEntryType type) const
     "Retrieved {} from primary storage ({})", key.to_string(), cache_file.path);
 
   // Update modification timestamp to save file from LRU cleanup.
-  Util::update_mtime(cache_file.path);
+  util::set_timestamps(cache_file.path);
   return cache_file.path;
 }
 
@@ -310,7 +310,7 @@ PrimaryStorage::clean_internal_tempdir()
     return;
   }
 
-  Util::update_mtime(m_config.cache_dir());
+  util::set_timestamps(m_config.cache_dir());
 
   const std::string& temp_dir = m_config.temporary_dir();
   if (!Stat::lstat(temp_dir)) {
index 6b3f55ee6f0e3eecee509ec38644aa93b867de1a..f1d550c34d44d41d0ce808586de474c7f69fbb51 100644 (file)
@@ -108,7 +108,7 @@ FileStorageBackend::get(const Digest& key)
   if (m_update_mtime) {
     // Update modification timestamp for potential LRU cleanup by some external
     // mechanism.
-    Util::update_mtime(path);
+    util::set_timestamps(path);
   }
 
   try {
index 85c62c16a6d37a40b2638a82aaf60c32fa3fa476..5616afb7a1a0c2697b51b0991ddece3b3170eb90 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2021 Joel Rosdahl and other contributors
+// Copyright (C) 2021-2022 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
 #include <core/exceptions.hpp>
 #include <fmtmacros.hpp>
 
+#ifdef HAVE_UTIMENSAT
+#  include <fcntl.h>
+#  include <sys/stat.h>
+#else
+#  include <sys/types.h>
+#  ifdef HAVE_UTIME_H
+#    include <utime.h>
+#  elif defined(HAVE_SYS_UTIME_H)
+#    include <sys/utime.h>
+#  endif
+#endif
+
 namespace util {
 
 void
@@ -46,4 +58,27 @@ create_cachedir_tag(const std::string& dir)
   }
 }
 
+void
+set_timestamps(const std::string& path,
+               std::optional<timespec> mtime,
+               std::optional<timespec> atime)
+{
+#ifdef HAVE_UTIMENSAT
+  timespec atime_mtime[2];
+  if (mtime) {
+    atime_mtime[0] = atime ? *atime : *mtime;
+    atime_mtime[1] = *mtime;
+  }
+  const timespec* const timespecs = mtime ? atime_mtime : nullptr;
+  utimensat(AT_FDCWD, path.c_str(), timespecs, 0);
+#else
+  utimbuf atime_mtime;
+  if (mtime) {
+    atime_mtime.actime = atime ? atime->tv_sec : mtime->tv_sec;
+    atime_mtime.modtime = mtime->tv_sec;
+  }
+  utime(path.c_str(), &atime_mtime);
+#endif
+}
+
 } // namespace util
index e0af9dd586eadc4791b4e18f2dd2f60c051a6fc0..d9adfbbdabcde3af46c7bd07b61996f73f4db98f 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2021 Joel Rosdahl and other contributors
+// Copyright (C) 2021-2022 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
@@ -18,6 +18,7 @@
 
 #pragma once
 
+#include <optional>
 #include <string>
 
 namespace util {
@@ -26,4 +27,10 @@ namespace util {
 
 void create_cachedir_tag(const std::string& dir);
 
+// Set atime/mtime of `path`. If `mtime` is std::nullopt, set to the current
+// time. If `atime` is std::nullopt, set to what `mtime` specifies.
+void set_timestamps(const std::string& path,
+                    std::optional<timespec> mtime = std::nullopt,
+                    std::optional<timespec> atime = std::nullopt);
+
 } // namespace util