]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
fix: Fix restoring of timestamps in recompressor
authorJoel Rosdahl <joel@rosdahl.net>
Wed, 11 Mar 2026 19:05:11 +0000 (20:05 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Wed, 11 Mar 2026 19:28:24 +0000 (20:28 +0100)
Fixes incorrect fix in 86af29da704f0e879c55230aad9a22d5df330bc3.

src/ccache/core/filerecompressor.cpp

index cd5cd6df9a5d5d18fd696d3b2ea776ceab2dc006..83767d12fbf34177ce456a51bb41d84300b4da58 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2022-2024 Joel Rosdahl and other contributors
+// Copyright (C) 2022-2026 Joel Rosdahl and other contributors
 //
 // See doc/authors.adoc for a complete list of contributors.
 //
@@ -35,21 +35,28 @@ FileRecompressor::recompress(const DirEntry& dir_entry,
                              std::optional<int8_t> level,
                              KeepAtime keep_atime)
 {
-  core::CacheEntry::Header header(dir_entry.path());
-
-  const int8_t wanted_level =
-    level ? (*level == 0 ? core::CacheEntry::default_compression_level : *level)
-          : 0;
-
   std::optional<DirEntry> new_dir_entry;
 
-  // Restore mtime/atime to keep cache LRU cleanup working as expected:
-  DEFER([&] {
+  // Restore mtime/atime to keep cache LRU cleanup working as expected.
+  //
+  // - For the --trim-dir/--trim-recompress case, always reset timestamps since
+  //   atime may be used for LRU cleanup (keep_atime == KeepAtime::yes).
+  // - For the --recompress case, only reset timestamps if mtime has changed
+  //   since local cache LRU cleanup always uses mtime (new_cache_file.commit()
+  //   has succeeded so new_dir_entry exists).
+  auto restore_timestamps = [&] {
     if (keep_atime == KeepAtime::yes || new_dir_entry) {
       util::set_timestamps(
         dir_entry.path(), dir_entry.mtime(), dir_entry.atime());
     }
-  });
+  };
+  DEFER(restore_timestamps());
+
+  core::CacheEntry::Header header(dir_entry.path());
+
+  const int8_t wanted_level =
+    level ? (*level == 0 ? core::CacheEntry::default_compression_level : *level)
+          : 0;
 
   if (header.compression_level != wanted_level) {
     const auto cache_file_data = util::value_or_throw<core::Error>(