From: Joel Rosdahl Date: Thu, 10 Nov 2022 09:15:12 +0000 (+0100) Subject: enhance: Only keep atime if needed X-Git-Tag: v4.8~92 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0437820b47abe55e49f5fdb2ed189c4a1f23a611;p=thirdparty%2Fccache.git enhance: Only keep atime if needed - For the --recompress case, only reset timestamps if mtime has changed since local cache LRU cleanup always uses mtime. - For the --trim-dir/--trim-recompress case, always reset timestamps since atime may be used for LRU cleanup. --- diff --git a/src/core/FileRecompressor.cpp b/src/core/FileRecompressor.cpp index 03818b013..131a85f95 100644 --- a/src/core/FileRecompressor.cpp +++ b/src/core/FileRecompressor.cpp @@ -29,7 +29,8 @@ namespace core { int64_t FileRecompressor::recompress(const std::string& cache_file, - const std::optional level) + std::optional level, + KeepAtime keep_atime) { core::CacheEntry::Header header(cache_file); @@ -59,7 +60,9 @@ FileRecompressor::recompress(const std::string& cache_file, } // Restore mtime/atime to keep cache LRU cleanup working as expected: - util::set_timestamps(cache_file, old_stat.mtime(), old_stat.atime()); + if (keep_atime == KeepAtime::yes || new_stat) { + util::set_timestamps(cache_file, old_stat.mtime(), old_stat.atime()); + } m_content_size += header.entry_size; m_old_size += old_stat.size_on_disk(); diff --git a/src/core/FileRecompressor.hpp b/src/core/FileRecompressor.hpp index 289b0d75e..b2fdb131b 100644 --- a/src/core/FileRecompressor.hpp +++ b/src/core/FileRecompressor.hpp @@ -29,11 +29,14 @@ namespace core { class FileRecompressor { public: + enum class KeepAtime { yes, no }; + FileRecompressor() = default; // Returns on-disk size change in KiB. int64_t recompress(const std::string& cache_file, - const std::optional level); + const std::optional level, + KeepAtime keep_atime); uint64_t content_size() const; uint64_t old_size() const; diff --git a/src/core/mainoptions.cpp b/src/core/mainoptions.cpp index a1866b5c8..7b3e639ad 100644 --- a/src/core/mainoptions.cpp +++ b/src/core/mainoptions.cpp @@ -316,7 +316,9 @@ trim_dir(const std::string& dir, for (const auto& file : files) { thread_pool.enqueue([&] { try { - recompressor.recompress(file.path, *recompress_level); + recompressor.recompress(file.path, + *recompress_level, + core::FileRecompressor::KeepAtime::yes); } catch (core::Error&) { // Ignore for now. incompressible_size += file.stat.size_on_disk(); diff --git a/src/storage/local/LocalStorage_compress.cpp b/src/storage/local/LocalStorage_compress.cpp index 4aa587ad9..81caa40b6 100644 --- a/src/storage/local/LocalStorage_compress.cpp +++ b/src/storage/local/LocalStorage_compress.cpp @@ -110,8 +110,8 @@ LocalStorage::recompress(const std::optional level, thread_pool.enqueue( [&recompressor, &incompressible_size, level, stats_file, file] { try { - int64_t size_change_kibibyte = - recompressor.recompress(file.path(), level); + int64_t size_change_kibibyte = recompressor.recompress( + file.path(), level, core::FileRecompressor::KeepAtime::no); StatsFile(stats_file).update([=](auto& cs) { cs.increment(core::Statistic::cache_size_kibibyte, size_change_kibibyte);