]> 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, 18 Jun 2022 18:08:58 +0000 (20:08 +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.

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

index 3a9ab09f19582ed8b12c82af23a05c0391a92d17..b6ea1e19acb0a9192f0f837db409d85f0bc4daeb 100644 (file)
@@ -217,7 +217,6 @@ Storage::~Storage()
 void
 Storage::initialize()
 {
-  primary.initialize();
   add_secondary_storages();
 }
 
index 8b14c4bb395850fbe38372bf7e04bc6b9d7b917d..63813abb0232695bec801ad4daf0902fd625bc1e 100644 (file)
@@ -94,18 +94,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;
   }
@@ -303,29 +297,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, "");
 }
 
 std::optional<core::StatisticsCounters>
index 593f0c2aee1027ab740154c69112fbde9bf63d14..d9f558c5fd960a3e0a402ff53f075e3b77f02c0c 100644 (file)
@@ -45,7 +45,6 @@ class PrimaryStorage
 public:
   PrimaryStorage(const Config& config);
 
-  void initialize();
   void finalize();
 
   // --- Cache entry handling ---