]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
fix: Use a cleanup stamp instead of directory timestamp for cleanup
authorJoel Rosdahl <joel@rosdahl.net>
Sat, 18 Jun 2022 18:08:10 +0000 (20:08 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sat, 20 Aug 2022 11:54:17 +0000 (13:54 +0200)
In order to know when to clean up the temporary directory, ccache checks
mtime of $CCACHE_DIR and updates it to "now" on cleanup. This doesn't
work well when the configuration file is modified often since that also
updates the same mtime.

Fix this by using a separate cleanup stamp file instead.

(cherry picked from commit ec27506da3a03e0f5bf5f80494174fcdd9a43b0e)

src/storage/Storage.cpp
src/storage/primary/PrimaryStorage.cpp
src/storage/primary/PrimaryStorage.hpp

index 098807779968f745474efd8644e0503f0611cbb9..0b28e8a2c0d534a41a0e0363f1ec78763e27a0d4 100644 (file)
@@ -217,7 +217,6 @@ Storage::~Storage()
 void
 Storage::initialize()
 {
-  primary.initialize();
   add_secondary_storages();
 }
 
index 3584776d7838842df25cdbe6b69a1712e4a35987..f812d849452910df34b031e8665467989dbccd36 100644 (file)
@@ -95,18 +95,12 @@ PrimaryStorage::PrimaryStorage(const Config& config) : m_config(config)
 }
 
 void
-PrimaryStorage::initialize()
+PrimaryStorage::finalize()
 {
-  MTR_SCOPE("primary_storage", "clean_internal_tempdir");
-
   if (m_config.temporary_dir() == m_config.default_temporary_dir()) {
     clean_internal_tempdir();
   }
-}
 
-void
-PrimaryStorage::finalize()
-{
   if (!m_config.stats()) {
     return;
   }
@@ -304,29 +298,31 @@ PrimaryStorage::look_up_cache_file(const Digest& key,
 void
 PrimaryStorage::clean_internal_tempdir()
 {
+  MTR_SCOPE("primary_storage", "clean_internal_tempdir");
+
   const time_t now = time(nullptr);
-  const auto dir_st = Stat::stat(m_config.cache_dir(), Stat::OnError::log);
-  if (!dir_st || dir_st.mtime() + k_tempdir_cleanup_interval >= now) {
+  const auto cleaned_stamp = FMT("{}/.cleaned", m_config.temporary_dir());
+  const auto cleaned_stat = Stat::stat(cleaned_stamp);
+  if (cleaned_stat
+      && cleaned_stat.mtime() + k_tempdir_cleanup_interval >= now) {
     // No cleanup needed.
     return;
   }
 
-  util::set_timestamps(m_config.cache_dir());
-
-  const std::string& temp_dir = m_config.temporary_dir();
-  if (!Stat::lstat(temp_dir)) {
-    return;
-  }
-
-  Util::traverse(temp_dir, [now](const std::string& path, bool is_dir) {
-    if (is_dir) {
-      return;
-    }
-    const auto st = Stat::lstat(path, Stat::OnError::log);
-    if (st && st.mtime() + k_tempdir_cleanup_interval < now) {
-      Util::unlink_tmp(path);
-    }
-  });
+  LOG("Cleaning up {}", m_config.temporary_dir());
+  Util::ensure_dir_exists(m_config.temporary_dir());
+  Util::traverse(m_config.temporary_dir(),
+                 [now](const std::string& path, bool is_dir) {
+                   if (is_dir) {
+                     return;
+                   }
+                   const auto st = Stat::lstat(path, Stat::OnError::log);
+                   if (st && st.mtime() + k_tempdir_cleanup_interval < now) {
+                     Util::unlink_tmp(path);
+                   }
+                 });
+
+  Util::write_file(cleaned_stamp, "");
 }
 
 nonstd::optional<core::StatisticsCounters>
index bb8b505459e829e5cfa6963a55cc24d131ed917f..9f22554574994efe1d5f98b96231a9970687a65e 100644 (file)
@@ -46,7 +46,6 @@ class PrimaryStorage
 public:
   PrimaryStorage(const Config& config);
 
-  void initialize();
   void finalize();
 
   // --- Cache entry handling ---