From a7637751e65b49ddaeef5913c37e2987e21e7d33 Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Sat, 18 Jun 2022 20:08:10 +0200 Subject: [PATCH] fix: Use a cleanup stamp instead of directory timestamp for cleanup 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 | 1 - src/storage/primary/PrimaryStorage.cpp | 46 ++++++++++++-------------- src/storage/primary/PrimaryStorage.hpp | 1 - 3 files changed, 21 insertions(+), 27 deletions(-) diff --git a/src/storage/Storage.cpp b/src/storage/Storage.cpp index 098807779..0b28e8a2c 100644 --- a/src/storage/Storage.cpp +++ b/src/storage/Storage.cpp @@ -217,7 +217,6 @@ Storage::~Storage() void Storage::initialize() { - primary.initialize(); add_secondary_storages(); } diff --git a/src/storage/primary/PrimaryStorage.cpp b/src/storage/primary/PrimaryStorage.cpp index 3584776d7..f812d8494 100644 --- a/src/storage/primary/PrimaryStorage.cpp +++ b/src/storage/primary/PrimaryStorage.cpp @@ -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 diff --git a/src/storage/primary/PrimaryStorage.hpp b/src/storage/primary/PrimaryStorage.hpp index bb8b50545..9f2255457 100644 --- a/src/storage/primary/PrimaryStorage.hpp +++ b/src/storage/primary/PrimaryStorage.hpp @@ -46,7 +46,6 @@ class PrimaryStorage public: PrimaryStorage(const Config& config); - void initialize(); void finalize(); // --- Cache entry handling --- -- 2.47.2