]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Remove CacheFile, using Stat with path member instead
authorJoel Rosdahl <joel@rosdahl.net>
Thu, 10 Nov 2022 08:38:37 +0000 (09:38 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 27 Nov 2022 20:33:51 +0000 (21:33 +0100)
src/storage/local/CMakeLists.txt
src/storage/local/CacheFile.cpp [deleted file]
src/storage/local/CacheFile.hpp [deleted file]
src/storage/local/LocalStorage.cpp
src/storage/local/LocalStorage.hpp
src/storage/local/LocalStorage_cleanup.cpp
src/storage/local/LocalStorage_compress.cpp
src/storage/local/util.cpp
src/storage/local/util.hpp
unittest/test_storage_local_util.cpp

index 7b0d4f05a1f6a1706cab5f5c5d483a11a7426c53..c189aec462e643fe364f3d63a936e0fd6ae412c1 100644 (file)
@@ -1,6 +1,5 @@
 set(
   sources
-  CacheFile.cpp
   LocalStorage.cpp
   LocalStorage_cleanup.cpp
   LocalStorage_compress.cpp
diff --git a/src/storage/local/CacheFile.cpp b/src/storage/local/CacheFile.cpp
deleted file mode 100644 (file)
index a2716e8..0000000
+++ /dev/null
@@ -1,47 +0,0 @@
-// Copyright (C) 2019-2022 Joel Rosdahl and other contributors
-//
-// See doc/AUTHORS.adoc for a complete list of contributors.
-//
-// This program is free software; you can redistribute it and/or modify it
-// under the terms of the GNU General Public License as published by the Free
-// Software Foundation; either version 3 of the License, or (at your option)
-// any later version.
-//
-// This program is distributed in the hope that it will be useful, but WITHOUT
-// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
-// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
-// more details.
-//
-// You should have received a copy of the GNU General Public License along with
-// this program; if not, write to the Free Software Foundation, Inc., 51
-// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
-
-#include "CacheFile.hpp"
-
-#include <core/Manifest.hpp>
-#include <core/Result.hpp>
-#include <util/string.hpp>
-
-const Stat&
-CacheFile::lstat() const
-{
-  if (!m_stat) {
-    m_stat = Stat::lstat(m_path);
-  }
-
-  return *m_stat;
-}
-
-CacheFile::Type
-CacheFile::type() const
-{
-  if (util::ends_with(m_path, "M")) {
-    return Type::manifest;
-  } else if (util::ends_with(m_path, "R")) {
-    return Type::result;
-  } else if (util::ends_with(m_path, "W")) {
-    return Type::raw;
-  } else {
-    return Type::unknown;
-  }
-}
diff --git a/src/storage/local/CacheFile.hpp b/src/storage/local/CacheFile.hpp
deleted file mode 100644 (file)
index e653363..0000000
+++ /dev/null
@@ -1,50 +0,0 @@
-// Copyright (C) 2019-2022 Joel Rosdahl and other contributors
-//
-// See doc/AUTHORS.adoc for a complete list of contributors.
-//
-// This program is free software; you can redistribute it and/or modify it
-// under the terms of the GNU General Public License as published by the Free
-// Software Foundation; either version 3 of the License, or (at your option)
-// any later version.
-//
-// This program is distributed in the hope that it will be useful, but WITHOUT
-// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
-// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
-// more details.
-//
-// You should have received a copy of the GNU General Public License along with
-// this program; if not, write to the Free Software Foundation, Inc., 51
-// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
-
-#pragma once
-
-#include <Stat.hpp>
-
-#include <optional>
-#include <string>
-
-class CacheFile
-{
-public:
-  enum class Type { result, manifest, raw, unknown };
-
-  explicit CacheFile(const std::string& path);
-
-  const Stat& lstat() const;
-  const std::string& path() const;
-  Type type() const;
-
-private:
-  std::string m_path;
-  mutable std::optional<Stat> m_stat;
-};
-
-inline CacheFile::CacheFile(const std::string& path) : m_path(path)
-{
-}
-
-inline const std::string&
-CacheFile::path() const
-{
-  return m_path;
-}
index 001e2a94ef1fe1b5ea6aee10ffd472dd26f0ebfb..274a4460381acbc41093e078fa7b7fafa8ddddfb 100644 (file)
@@ -30,6 +30,7 @@
 #include <storage/local/StatsFile.hpp>
 #include <util/Duration.hpp>
 #include <util/file.hpp>
+#include <util/string.hpp>
 
 #ifdef HAVE_UNISTD_H
 #  include <unistd.h>
@@ -91,6 +92,20 @@ calculate_wanted_cache_level(const uint64_t files_in_level_1)
   return k_max_cache_levels;
 }
 
+FileType
+file_type_from_path(std::string_view path)
+{
+  if (util::ends_with(path, "M")) {
+    return FileType::manifest;
+  } else if (util::ends_with(path, "R")) {
+    return FileType::result;
+  } else if (util::ends_with(path, "W")) {
+    return FileType::raw;
+  } else {
+    return FileType::unknown;
+  }
+}
+
 LocalStorage::LocalStorage(const Config& config) : m_config(config)
 {
 }
index 54a8e9fe13f75b7d8a93b5fa10cabb7f48a5ae4a..7ba28707481a1b0bb77173a0766a48b31472e50a 100644 (file)
@@ -31,6 +31,7 @@
 
 #include <cstdint>
 #include <optional>
+#include <string_view>
 #include <vector>
 
 class Config;
@@ -46,6 +47,10 @@ struct CompressionStatistics
   uint64_t on_disk_size;
 };
 
+enum class FileType { result, manifest, raw, unknown };
+
+FileType file_type_from_path(std::string_view path);
+
 class LocalStorage
 {
 public:
index 3039e838d2fe3afb0dada4ec64ffe5f69348daac..1293af4ac77efe724c92d5ccc82143e060cd208f 100644 (file)
@@ -28,7 +28,6 @@
 #include <core/CacheEntry.hpp>
 #include <core/exceptions.hpp>
 #include <fmtmacros.hpp>
-#include <storage/local/CacheFile.hpp>
 #include <storage/local/StatsFile.hpp>
 #include <storage/local/util.hpp>
 #include <util/file.hpp>
@@ -104,7 +103,7 @@ LocalStorage::clean_dir(const std::string& subdir,
 {
   LOG("Cleaning up cache directory {}", subdir);
 
-  std::vector<CacheFile> files = get_level_1_files(
+  auto files = get_level_1_files(
     subdir, [&](double progress) { progress_receiver(progress / 3); });
 
   uint64_t cache_size = 0;
@@ -118,31 +117,31 @@ LocalStorage::clean_dir(const std::string& subdir,
        ++i, progress_receiver(1.0 / 3 + 1.0 * i / files.size() / 3)) {
     const auto& file = files[i];
 
-    if (!file.lstat().is_regular()) {
+    if (!file.is_regular()) {
       // Not a file or missing file.
       continue;
     }
 
     // Delete any tmp files older than 1 hour right away.
-    if (file.lstat().mtime() + util::Duration(3600) < current_time
+    if (file.mtime() + util::Duration(3600) < current_time
         && TemporaryFile::is_tmp_file(file.path())) {
       Util::unlink_tmp(file.path());
       continue;
     }
 
-    if (namespace_ && file.type() == CacheFile::Type::raw) {
+    if (namespace_ && file_type_from_path(file.path()) == FileType::raw) {
       const auto result_filename =
         FMT("{}R", file.path().substr(0, file.path().length() - 2));
       raw_files_map[result_filename].push_back(file.path());
     }
 
-    cache_size += file.lstat().size_on_disk();
+    cache_size += file.size_on_disk();
     files_in_cache += 1;
   }
 
   // Sort according to modification time, oldest first.
   std::sort(files.begin(), files.end(), [](const auto& f1, const auto& f2) {
-    return f1.lstat().mtime() < f2.lstat().mtime();
+    return f1.mtime() < f2.mtime();
   });
 
   LOG("Before cleanup: {:.0f} KiB, {:.0f} files",
@@ -154,14 +153,14 @@ LocalStorage::clean_dir(const std::string& subdir,
        ++i, progress_receiver(2.0 / 3 + 1.0 * i / files.size() / 3)) {
     const auto& file = files[i];
 
-    if (!file.lstat() || file.lstat().is_directory()) {
+    if (!file || file.is_directory()) {
       continue;
     }
 
     if ((max_size == 0 || cache_size <= max_size)
         && (max_files == 0 || files_in_cache <= max_files)
         && (!max_age
-            || file.lstat().mtime() > (current_time - util::Duration(*max_age)))
+            || file.mtime() > (current_time - util::Duration(*max_age)))
         && (!namespace_ || max_age)) {
       break;
     }
@@ -179,7 +178,7 @@ LocalStorage::clean_dir(const std::string& subdir,
 
       // For namespace eviction we need to remove raw files based on result
       // filename since they don't have a header.
-      if (file.type() == CacheFile::Type::result) {
+      if (file_type_from_path(file.path()) == FileType::result) {
         const auto entry = raw_files_map.find(file.path());
         if (entry != raw_files_map.end()) {
           for (const auto& raw_file : entry->second) {
@@ -210,8 +209,7 @@ LocalStorage::clean_dir(const std::string& subdir,
       delete_file(o_file, 0, nullptr, nullptr);
     }
 
-    delete_file(
-      file.path(), file.lstat().size_on_disk(), &cache_size, &files_in_cache);
+    delete_file(file.path(), file.size_on_disk(), &cache_size, &files_in_cache);
     cleaned = true;
   }
 
@@ -250,7 +248,7 @@ wipe_dir(const std::string& subdir, const ProgressReceiver& progress_receiver)
 {
   LOG("Clearing out cache directory {}", subdir);
 
-  const std::vector<CacheFile> files = get_level_1_files(
+  const auto files = get_level_1_files(
     subdir, [&](double progress) { progress_receiver(progress / 2); });
 
   for (size_t i = 0; i < files.size(); ++i) {
index 81caa40b62854b7c4e39337fb54367cd52183d7d..6fbe0eba6d7c8d184641c2ce7862602d22e207db 100644 (file)
@@ -58,19 +58,19 @@ LocalStorage::get_compression_statistics(
   for_each_level_1_subdir(
     m_config.cache_dir(),
     [&](const auto& subdir, const auto& sub_progress_receiver) {
-      const std::vector<CacheFile> files = get_level_1_files(
+      const auto files = get_level_1_files(
         subdir, [&](double progress) { sub_progress_receiver(progress / 2); });
 
       for (size_t i = 0; i < files.size(); ++i) {
         const auto& cache_file = files[i];
-        cs.on_disk_size += cache_file.lstat().size_on_disk();
+        cs.on_disk_size += cache_file.size_on_disk();
 
         try {
           core::CacheEntry::Header header(cache_file.path());
-          cs.compr_size += cache_file.lstat().size();
+          cs.compr_size += cache_file.size();
           cs.content_size += header.entry_size;
         } catch (core::Error&) {
-          cs.incompr_size += cache_file.lstat().size();
+          cs.incompr_size += cache_file.size();
         }
 
         sub_progress_receiver(1.0 / 2 + 1.0 * i / files.size() / 2);
@@ -96,17 +96,16 @@ LocalStorage::recompress(const std::optional<int8_t> level,
   for_each_level_1_subdir(
     m_config.cache_dir(),
     [&](const auto& subdir, const auto& sub_progress_receiver) {
-      std::vector<CacheFile> files =
-        get_level_1_files(subdir, [&](double progress) {
-          sub_progress_receiver(0.1 * progress);
-        });
+      auto files = get_level_1_files(subdir, [&](double progress) {
+        sub_progress_receiver(0.1 * progress);
+      });
 
       auto stats_file = subdir + "/stats";
 
       for (size_t i = 0; i < files.size(); ++i) {
         const auto& file = files[i];
 
-        if (file.type() != CacheFile::Type::unknown) {
+        if (file_type_from_path(file.path()) != FileType::unknown) {
           thread_pool.enqueue(
             [&recompressor, &incompressible_size, level, stats_file, file] {
               try {
@@ -118,11 +117,11 @@ LocalStorage::recompress(const std::optional<int8_t> level,
                 });
               } catch (core::Error&) {
                 // Ignore for now.
-                incompressible_size += file.lstat().size_on_disk();
+                incompressible_size += file.size_on_disk();
               }
             });
         } else if (!TemporaryFile::is_tmp_file(file.path())) {
-          incompressible_size += file.lstat().size_on_disk();
+          incompressible_size += file.size_on_disk();
         }
 
         sub_progress_receiver(0.1 + 0.9 * i / files.size());
index 1035925d04c5125b85c114b39392049b1b003b87..6480906db1382014c50532e8c718b2d2deb39723 100644 (file)
@@ -40,11 +40,11 @@ for_each_level_1_subdir(const std::string& cache_dir,
   progress_receiver(1.0);
 }
 
-std::vector<CacheFile>
+std::vector<Stat>
 get_level_1_files(const std::string& dir,
                   const ProgressReceiver& progress_receiver)
 {
-  std::vector<CacheFile> files;
+  std::vector<Stat> files;
 
   if (!Stat::stat(dir)) {
     return files;
@@ -60,7 +60,7 @@ get_level_1_files(const std::string& dir,
     }
 
     if (!is_dir) {
-      files.emplace_back(path);
+      files.emplace_back(Stat::lstat(path));
     } else if (path != dir
                && path.find('/', dir.size() + 1) == std::string::npos) {
       ++level_2_directories;
index 8aa9b8734315f3a2002f9ae8c02da5c597334056..325f04e74733068abc24692503e3564c0b0d3078 100644 (file)
@@ -18,7 +18,7 @@
 
 #pragma once
 
-#include <storage/local/CacheFile.hpp>
+#include <Stat.hpp>
 
 #include <functional>
 #include <string>
@@ -55,8 +55,7 @@ void for_each_level_1_subdir(const std::string& cache_dir,
 // Parameters:
 // - dir: The directory to traverse recursively.
 // - progress_receiver: Function that will be called for progress updates.
-std::vector<CacheFile>
-get_level_1_files(const std::string& dir,
-                  const ProgressReceiver& progress_receiver);
+std::vector<Stat> get_level_1_files(const std::string& dir,
+                                    const ProgressReceiver& progress_receiver);
 
 } // namespace storage::local
index 3ed23c5545403b22846993bc52ed3f3c5d15b175..efc5ff4613ea4927a9323cfccc851cd894c111a6 100644 (file)
@@ -109,13 +109,13 @@ TEST_CASE("storage::local::get_level_1_files")
     });
 
     CHECK(files[0].path() == os_path("0/1/file_b"));
-    CHECK(files[0].lstat().size() == 1);
+    CHECK(files[0].size() == 1);
     CHECK(files[1].path() == os_path("0/1/file_c"));
-    CHECK(files[1].lstat().size() == 2);
+    CHECK(files[1].size() == 2);
     CHECK(files[2].path() == os_path("0/f/c/file_d"));
-    CHECK(files[2].lstat().size() == 3);
+    CHECK(files[2].size() == 3);
     CHECK(files[3].path() == os_path("0/file_a"));
-    CHECK(files[3].lstat().size() == 0);
+    CHECK(files[3].size() == 0);
   }
 }