From: Joel Rosdahl Date: Sun, 4 Sep 2022 11:07:49 +0000 (+0200) Subject: refactor: Pass Config instead of Context to Result::Writer X-Git-Tag: v4.7~81 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e2bfc0067cde4a39e51e33a870343b8e657a4858;p=thirdparty%2Fccache.git refactor: Pass Config instead of Context to Result::Writer --- diff --git a/src/Result.cpp b/src/Result.cpp index 7eb7777d5..9f7204f27 100644 --- a/src/Result.cpp +++ b/src/Result.cpp @@ -287,8 +287,8 @@ Reader::read_entry(uint8_t entry_number, Reader::Consumer& consumer) consumer.on_entry_end(); } -Writer::Writer(Context& ctx, const std::string& result_path) - : m_ctx(ctx), +Writer::Writer(const Config& config, const std::string& result_path) + : m_config(config), m_result_path(result_path) { } @@ -334,11 +334,11 @@ Writer::do_finalize() AtomicFile atomic_result_file(m_result_path, AtomicFile::Mode::binary); core::CacheEntryHeader header(core::CacheEntryType::result, - compression::type_from_config(m_ctx.config), - compression::level_from_config(m_ctx.config), + compression::type_from_config(m_config), + compression::level_from_config(m_config), time(nullptr), CCACHE_VERSION, - m_ctx.config.namespace_()); + m_config.namespace_()); header.set_entry_size_from_payload_size(payload_size); core::FileWriter file_writer(atomic_result_file.stream()); @@ -349,9 +349,8 @@ Writer::do_finalize() uint8_t entry_number = 0; for (const auto& entry : m_entries_to_write) { - const bool store_raw = - entry.value_type == ValueType::path - && should_store_raw_file(m_ctx.config, entry.file_type); + const bool store_raw = entry.value_type == ValueType::path + && should_store_raw_file(m_config, entry.file_type); const uint64_t entry_size = entry.value_type == ValueType::data ? entry.value.size() @@ -425,7 +424,7 @@ Result::Writer::write_raw_file_entry(const std::string& path, const auto raw_file = get_raw_file_path(m_result_path, entry_number); const auto old_stat = Stat::stat(raw_file); try { - Util::clone_hard_link_or_copy_file(m_ctx, path, raw_file, true); + Util::clone_hard_link_or_copy_file(m_config, path, raw_file, true); } catch (core::Error& e) { throw core::Error( FMT("Failed to store {} as raw file {}: {}", path, raw_file, e.what())); diff --git a/src/Result.hpp b/src/Result.hpp index 6e537a8c1..a67d71066 100644 --- a/src/Result.hpp +++ b/src/Result.hpp @@ -134,7 +134,7 @@ private: class Writer { public: - Writer(Context& ctx, const std::string& result_path); + Writer(const Config& config, const std::string& result_path); // Register content to include in the result. Does not throw. void write_data(FileType file_type, const std::string& data); @@ -155,7 +155,7 @@ private: std::string value; }; - Context& m_ctx; + const Config& m_config; const std::string m_result_path; std::vector m_entries_to_write; diff --git a/src/ResultRetriever.cpp b/src/ResultRetriever.cpp index bc456bfb4..5e2ec0f62 100644 --- a/src/ResultRetriever.cpp +++ b/src/ResultRetriever.cpp @@ -121,7 +121,8 @@ ResultRetriever::on_entry_start(uint8_t entry_number, } else if (dest_path == "/dev/null") { LOG_RAW("Not writing to /dev/null"); } else if (raw_file) { - Util::clone_hard_link_or_copy_file(m_ctx, *raw_file, dest_path, false); + Util::clone_hard_link_or_copy_file( + m_ctx.config, *raw_file, dest_path, false); // Update modification timestamp to save the file from LRU cleanup (and, if // hard-linked, to make the object file newer than the source file). diff --git a/src/Util.cpp b/src/Util.cpp index cbe9ed5c0..d389a40b8 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -25,6 +25,7 @@ #include "TemporaryFile.hpp" #include "Win32Util.hpp" +#include #include #include #include @@ -268,12 +269,12 @@ clone_file(const std::string& src, const std::string& dest, bool via_tmp_file) #endif // FILE_CLONING_SUPPORTED void -clone_hard_link_or_copy_file(const Context& ctx, +clone_hard_link_or_copy_file(const Config& config, const std::string& source, const std::string& dest, bool via_tmp_file) { - if (ctx.config.file_clone()) { + if (config.file_clone()) { #ifdef FILE_CLONING_SUPPORTED LOG("Cloning {} to {}", source, dest); try { @@ -286,7 +287,7 @@ clone_hard_link_or_copy_file(const Context& ctx, LOG("Not cloning {} to {} since it's unsupported", source, dest); #endif } - if (ctx.config.hard_link()) { + if (config.hard_link()) { LOG("Hard linking {} to {}", source, dest); try { Util::hard_link(source, dest); diff --git a/src/Util.hpp b/src/Util.hpp index fbbd4079c..2b2f0c1ff 100644 --- a/src/Util.hpp +++ b/src/Util.hpp @@ -31,6 +31,7 @@ #include #include +class Config; class Context; namespace Util { @@ -87,7 +88,7 @@ void clone_file(const std::string& src, // Clone, hard link or copy a file from `source` to `dest` depending on settings // in `ctx`. If cloning or hard linking cannot and should not be done the file // will be copied instead. Throws `core::Error` on error. -void clone_hard_link_or_copy_file(const Context& ctx, +void clone_hard_link_or_copy_file(const Config& config, const std::string& source, const std::string& dest, bool via_tmp_file = false); diff --git a/src/ccache.cpp b/src/ccache.cpp index 0d052eea9..afbce17b2 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -880,7 +880,7 @@ write_result(Context& ctx, const std::string& stdout_data, const std::string& stderr_data) { - Result::Writer result_writer(ctx, result_path); + Result::Writer result_writer(ctx.config, result_path); if (!stderr_data.empty()) { result_writer.write_data(Result::FileType::stderr_output, stderr_data);