]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Remove Stat::OnError::throw_error
authorJoel Rosdahl <joel@rosdahl.net>
Thu, 27 Jul 2023 17:11:09 +0000 (19:11 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Wed, 2 Aug 2023 11:36:34 +0000 (13:36 +0200)
This is in preparation for moving the Stat class to util where we don't
want to know about core::Error.

src/Stat.cpp
src/Stat.hpp
src/ccache.cpp
src/core/FileRecompressor.cpp
src/core/Result.cpp
src/core/ResultExtractor.cpp
src/core/ResultRetriever.cpp
src/core/mainoptions.cpp
src/storage/local/LocalStorage.cpp
unittest/test_Stat.cpp

index 15293e12b1e6f5a9b4a4f06a80af423579545a7e..1d3c4f235acb9b39885f094653341c5eb40f5d12 100644 (file)
@@ -205,7 +205,7 @@ win32_lstat(const char* path, Stat::stat_t* st)
 
 Stat::Stat(StatFunction stat_function,
            const std::string& path,
-           Stat::OnError on_error)
+           Stat::LogOnError log_on_error)
   : m_path(path)
 {
   int result = stat_function(path.c_str(), &m_stat);
@@ -213,10 +213,7 @@ Stat::Stat(StatFunction stat_function,
     m_errno = 0;
   } else {
     m_errno = errno;
-    if (on_error == OnError::throw_error) {
-      throw core::Error(FMT("failed to stat {}: {}", path, strerror(errno)));
-    }
-    if (on_error == OnError::log) {
+    if (log_on_error == LogOnError::yes) {
       LOG("Failed to stat {}: {}", path, strerror(errno));
     }
 
@@ -227,7 +224,7 @@ Stat::Stat(StatFunction stat_function,
 }
 
 Stat
-Stat::stat(const std::string& path, OnError on_error)
+Stat::stat(const std::string& path, LogOnError log_on_error)
 {
   return Stat(
 #ifdef _WIN32
@@ -236,11 +233,11 @@ Stat::stat(const std::string& path, OnError on_error)
     ::stat,
 #endif
     path,
-    on_error);
+    log_on_error);
 }
 
 Stat
-Stat::lstat(const std::string& path, OnError on_error)
+Stat::lstat(const std::string& path, LogOnError log_on_error)
 {
   return Stat(
 #ifdef _WIN32
@@ -249,5 +246,5 @@ Stat::lstat(const std::string& path, OnError on_error)
     ::lstat,
 #endif
     path,
-    on_error);
+    log_on_error);
 }
index 5452872a92bdd456057a1ff85ec496d0c7411b11..c29224b84e101d817c33389dd8a5a1304a58e956 100644 (file)
 class Stat
 {
 public:
-  enum class OnError {
-    // Ignore any error (including missing file) from the underlying stat call.
-    // On error, error_number() will return the error number (AKA errno) and
-    // the query functions will return 0 or false.
-    ignore,
-    // Like above but log an error message as well.
-    log,
-    // Throw Error on errors (including missing file).
-    throw_error,
-  };
+  enum class LogOnError { no, yes };
 
 #if defined(_WIN32)
   struct stat_t
@@ -98,20 +89,13 @@ public:
   // error_number() will return -1 and other accessors will return false or 0.
   Stat();
 
-  // Run stat(2).
-  //
-  // Arguments:
-  // - path: Path to stat.
-  // - on_error: What to do on errors (including missing file).
-  static Stat stat(const std::string& path, OnError on_error = OnError::ignore);
-
-  // Run lstat(2) if available, otherwise stat(2).
-  //
-  // Arguments:
-  // - path: Path to (l)stat.
-  // - on_error: What to do on errors (including missing file).
+  // Run stat(2) on `path`.
+  static Stat stat(const std::string& path,
+                   LogOnError log_on_error = LogOnError::no);
+
+  // Run lstat(2) on `path` if available, otherwise stat(2).
   static Stat lstat(const std::string& path,
-                    OnError on_error = OnError::ignore);
+                    LogOnError log_on_error = LogOnError::no);
 
   // Return true if the file could be (l)stat-ed (i.e., the file exists),
   // otherwise false.
@@ -149,7 +133,9 @@ public:
 protected:
   using StatFunction = int (*)(const char*, stat_t*);
 
-  Stat(StatFunction stat_function, const std::string& path, OnError on_error);
+  Stat(StatFunction stat_function,
+       const std::string& path,
+       LogOnError log_on_error);
 
 private:
   std::string m_path;
index c9e6ed4312d3ae4890f182ed0e2b4aa568c75c0c..db14fd0d7c0f5d766101f2fd5dd2193153612f8d 100644 (file)
@@ -352,7 +352,7 @@ do_remember_include_file(Context& ctx,
   }
 #endif
 
-  auto st = Stat::stat(path, Stat::OnError::log);
+  auto st = Stat::stat(path, Stat::LogOnError::yes);
   if (!st) {
     return false;
   }
@@ -402,7 +402,7 @@ do_remember_include_file(Context& ctx,
       // hash pch.sum instead of pch when it exists
       // to prevent hashing a very large .pch file every time
       std::string pch_sum_path = FMT("{}.sum", path);
-      if (Stat::stat(pch_sum_path, Stat::OnError::log)) {
+      if (Stat::stat(pch_sum_path, Stat::LogOnError::yes)) {
         path = std::move(pch_sum_path);
         using_pch_sum = true;
         LOG("Using pch.sum file {}", path);
@@ -860,7 +860,7 @@ update_manifest(Context& ctx,
 
   const bool added = ctx.manifest.add_result(
     result_key, ctx.included_files, [&](const std::string& path) {
-      auto stat = Stat::stat(path, Stat::OnError::log);
+      auto stat = Stat::stat(path, Stat::LogOnError::yes);
       bool cache_time =
         save_timestamp
         && ctx.time_of_compilation > std::max(stat.mtime(), stat.ctime());
@@ -1368,7 +1368,7 @@ hash_nvcc_host_compiler(const Context& ctx,
       } else {
         std::string path = find_executable(ctx, compiler, ctx.orig_args[0]);
         if (!path.empty()) {
-          auto st = Stat::stat(path, Stat::OnError::log);
+          auto st = Stat::stat(path, Stat::LogOnError::yes);
           TRY(hash_compiler(ctx, hash, st, ccbin, false));
         }
       }
@@ -1405,7 +1405,7 @@ hash_common_info(const Context& ctx,
   const std::string compiler_path = args[0];
 #endif
 
-  auto st = Stat::stat(compiler_path, Stat::OnError::log);
+  auto st = Stat::stat(compiler_path, Stat::LogOnError::yes);
   if (!st) {
     return tl::unexpected(Statistic::could_not_find_compiler);
   }
@@ -1744,7 +1744,7 @@ hash_argument(const Context& ctx,
     } else {
       path = args[i].substr(eq_pos + 1);
     }
-    auto st = Stat::stat(path, Stat::OnError::log);
+    auto st = Stat::stat(path, Stat::LogOnError::yes);
     if (st) {
       // If given an explicit specs file, then hash that file, but don't
       // include the path to it in the hash.
@@ -1755,7 +1755,7 @@ hash_argument(const Context& ctx,
   }
 
   if (util::starts_with(args[i], "-fplugin=")) {
-    auto st = Stat::stat(&args[i][9], Stat::OnError::log);
+    auto st = Stat::stat(&args[i][9], Stat::LogOnError::yes);
     if (st) {
       hash.hash_delimiter("plugin");
       TRY(hash_compiler(ctx, hash, st, &args[i][9], false));
@@ -1765,7 +1765,7 @@ hash_argument(const Context& ctx,
 
   if (args[i] == "-Xclang" && i + 3 < args.size() && args[i + 1] == "-load"
       && args[i + 2] == "-Xclang") {
-    auto st = Stat::stat(args[i + 3], Stat::OnError::log);
+    auto st = Stat::stat(args[i + 3], Stat::LogOnError::yes);
     if (st) {
       hash.hash_delimiter("plugin");
       TRY(hash_compiler(ctx, hash, st, args[i + 3], false));
index 92aa6c7a3ee9b34f0d6f9171c2dc79965f706edc..11e1f479906cf7a58f56f506733209975c6759f8 100644 (file)
@@ -56,7 +56,7 @@ FileRecompressor::recompress(const Stat& stat,
     new_cache_file.write(
       core::CacheEntry::serialize(header, cache_entry.payload()));
     new_cache_file.commit();
-    new_stat = Stat::lstat(stat.path(), Stat::OnError::log);
+    new_stat = Stat::lstat(stat.path(), Stat::LogOnError::yes);
   }
 
   // Restore mtime/atime to keep cache LRU cleanup working as expected:
index b51b912f78ab271d9c331714a1403715cbea6105..01af0986db3d10d0949cd3a8af48487da3996e34 100644 (file)
@@ -284,10 +284,10 @@ Serializer::serialize(util::Bytes& output)
     const bool store_raw =
       is_file_entry && should_store_raw_file(m_config, entry.file_type);
     const uint64_t file_size =
-      is_file_entry ? Stat::stat(std::get<std::string>(entry.data),
-                                 Stat::OnError::throw_error)
-                        .size()
-                    : std::get<nonstd::span<const uint8_t>>(entry.data).size();
+      is_file_entry
+        ? Stat::stat(std::get<std::string>(entry.data), Stat::LogOnError::yes)
+            .size()
+        : std::get<nonstd::span<const uint8_t>>(entry.data).size();
 
     LOG("Storing {} entry #{} {} ({} bytes){}",
         store_raw ? "raw" : "embedded",
index 1969876cc58f5ed6d95d6b9b946cc006eda663ec..fe1366ab4bdd888b057ccdff4e49ae108a05c4e4 100644 (file)
@@ -73,7 +73,11 @@ ResultExtractor::on_raw_file(uint8_t file_number,
     throw Error("Raw entry for non-local result");
   }
   const auto raw_file_path = (*m_get_raw_file_path)(file_number);
-  const auto st = Stat::stat(raw_file_path, Stat::OnError::throw_error);
+  const auto st = Stat::stat(raw_file_path, Stat::LogOnError::yes);
+  if (!st) {
+    throw Error(
+      FMT("Failed to stat {}: {}", raw_file_path, strerror(st.error_number())));
+  }
   if (st.size() != file_size) {
     throw Error(FMT("Bad file size of {} (actual {} bytes, expected {} bytes)",
                     raw_file_path,
index 5e5b3a9b975a9045eed285d91d15134d8ed25d78..45cf533b23c82dfe7925b110b3eebc8120e2858c 100644 (file)
@@ -104,7 +104,11 @@ ResultRetriever::on_raw_file(uint8_t file_number,
   }
   const auto raw_file_path =
     m_ctx.storage.local.get_raw_file_path(*m_result_key, file_number);
-  const auto st = Stat::stat(raw_file_path, Stat::OnError::throw_error);
+  const auto st = Stat::stat(raw_file_path, Stat::LogOnError::yes);
+  if (!st) {
+    throw Error(
+      FMT("Failed to stat {}: {}", raw_file_path, strerror(st.error_number())));
+  }
   if (st.size() != file_size) {
     throw core::Error(
       FMT("Bad file size of {} (actual {} bytes, expected {} bytes)",
index 2cfc8e50fb944337c8a3deddae34c640290c329c..0c28a7acdedb77d5b306595b7e0553fc55bef292 100644 (file)
@@ -719,7 +719,7 @@ process_main_options(int argc, const char* const* argv)
       }
       Statistics statistics(StatsLog(config.stats_log()).read());
       const auto timestamp =
-        Stat::stat(config.stats_log(), Stat::OnError::log).mtime();
+        Stat::stat(config.stats_log(), Stat::LogOnError::yes).mtime();
       PRINT_RAW(
         stdout,
         statistics.format_human_readable(config, timestamp, verbosity, true));
index 96963c35b8b9cd3d776314a86d11205ad9e92c50..ad185ab17254991a301011680604bad34422c0ac 100644 (file)
@@ -533,7 +533,7 @@ LocalStorage::put(const Hash::Digest& key,
 
   increment_statistic(Statistic::local_storage_write);
 
-  const auto new_stat = Stat::stat(cache_file.path, Stat::OnError::log);
+  const auto new_stat = Stat::stat(cache_file.path, Stat::LogOnError::yes);
   if (!new_stat) {
     return;
   }
@@ -1424,7 +1424,7 @@ LocalStorage::clean_internal_tempdir()
       if (is_dir) {
         return;
       }
-      const auto st = Stat::lstat(path, Stat::OnError::log);
+      const auto st = Stat::lstat(path, Stat::LogOnError::yes);
       if (st && st.mtime() + k_tempdir_cleanup_interval < now) {
         util::remove(path);
       }
index fdba8c2bafac806c7be0f16973cfdf478bfd9575..1e45d905fe97431294c6e742a1bdc42c8b2024c7 100644 (file)
@@ -174,10 +174,8 @@ TEST_CASE("Default constructor")
 TEST_CASE("Named constructors")
 {
   CHECK(!Stat::stat("does_not_exist"));
-  CHECK(!Stat::stat("does_not_exist", Stat::OnError::ignore));
-  CHECK(!Stat::stat("does_not_exist", Stat::OnError::log));
-  CHECK_THROWS_WITH(Stat::stat("does_not_exist", Stat::OnError::throw_error),
-                    "failed to stat does_not_exist: No such file or directory");
+  CHECK(!Stat::stat("does_not_exist", Stat::LogOnError::no));
+  CHECK(!Stat::stat("does_not_exist", Stat::LogOnError::yes));
 }
 
 TEST_CASE("Same i-node as")
@@ -339,7 +337,7 @@ TEST_CASE("Symlinks" * doctest::skip(!symlinks_supported()))
 
   SUBCASE("file lstat")
   {
-    auto stat = Stat::lstat("file", Stat::OnError::ignore);
+    auto stat = Stat::lstat("file", Stat::LogOnError::no);
     CHECK(stat);
     CHECK(stat.error_number() == 0);
     CHECK(!stat.is_directory());
@@ -356,7 +354,7 @@ TEST_CASE("Symlinks" * doctest::skip(!symlinks_supported()))
 
   SUBCASE("file stat")
   {
-    auto stat = Stat::stat("file", Stat::OnError::ignore);
+    auto stat = Stat::stat("file", Stat::LogOnError::no);
     CHECK(stat);
     CHECK(stat.error_number() == 0);
     CHECK(!stat.is_directory());
@@ -373,7 +371,7 @@ TEST_CASE("Symlinks" * doctest::skip(!symlinks_supported()))
 
   SUBCASE("symlink lstat")
   {
-    auto stat = Stat::lstat("symlink", Stat::OnError::ignore);
+    auto stat = Stat::lstat("symlink", Stat::LogOnError::no);
     CHECK(stat);
     CHECK(stat.error_number() == 0);
     CHECK(!stat.is_directory());
@@ -391,7 +389,7 @@ TEST_CASE("Symlinks" * doctest::skip(!symlinks_supported()))
 
   SUBCASE("symlink stat")
   {
-    auto stat = Stat::stat("symlink", Stat::OnError::ignore);
+    auto stat = Stat::stat("symlink", Stat::LogOnError::no);
     CHECK(stat);
     CHECK(stat.error_number() == 0);
     CHECK(!stat.is_directory());