diff options
author | Joel Rosdahl <joel@rosdahl.net> | 2022-09-13 22:00:08 +0200 |
---|---|---|
committer | Joel Rosdahl <joel@rosdahl.net> | 2022-09-21 17:06:31 +0200 |
commit | 3fcfd751f87a445f71525481aa5206424a8e1df4 (patch) | |
tree | 2362e30dfaeebe0491ee7952b16c784525f4d90d /src/core | |
parent | 0db6d06df4d70432fbf279759a7d726782115ac8 (diff) | |
download | ccache-3fcfd751f87a445f71525481aa5206424a8e1df4.tar.gz |
refactor: Extract timestamp logic from Manifest
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/Manifest.cpp | 46 | ||||
-rw-r--r-- | src/core/Manifest.hpp | 23 |
2 files changed, 20 insertions, 49 deletions
diff --git a/src/core/Manifest.cpp b/src/core/Manifest.cpp index f2a3e9cc..060fd930 100644 --- a/src/core/Manifest.cpp +++ b/src/core/Manifest.cpp @@ -143,8 +143,7 @@ bool Manifest::add_result( const Digest& result_key, const std::unordered_map<std::string, Digest>& included_files, - const time_t time_of_compilation, - const bool save_timestamp) + const FileStater& stat_file_function) { if (m_results.size() > k_max_manifest_entries) { // Normally, there shouldn't be many result entries in the manifest since @@ -183,12 +182,8 @@ Manifest::add_result( file_info_indexes.reserve(included_files.size()); for (const auto& [path, digest] : included_files) { - file_info_indexes.push_back(get_file_info_index(path, - digest, - mf_files, - mf_file_infos, - time_of_compilation, - save_timestamp)); + file_info_indexes.push_back(get_file_info_index( + path, digest, mf_files, mf_file_infos, stat_file_function)); } ResultEntry entry{std::move(file_info_indexes), result_key}; @@ -287,8 +282,7 @@ Manifest::get_file_info_index( const Digest& digest, const std::unordered_map<std::string, uint32_t>& mf_files, const std::unordered_map<FileInfo, uint32_t>& mf_file_infos, - const time_t time_of_compilation, - const bool save_timestamp) + const FileStater& file_stater) { FileInfo fi; @@ -302,34 +296,10 @@ Manifest::get_file_info_index( fi.digest = digest; - // file_stat.{m,c}time() have a resolution of 1 second, so we can cache the - // file's mtime and ctime only if they're at least one second older than - // time_of_compilation. - // - // file_stat.ctime() may be 0, so we have to check time_of_compilation against - // MAX(mtime, ctime). - // - // ccache only reads mtime/ctime if file_stat_match sloppiness is enabled, so - // mtimes/ctimes are stored as a dummy value (-1) if not enabled. This reduces - // the number of file_info entries for the common case. - - const auto file_stat = Stat::stat(path, Stat::OnError::log); - if (file_stat) { - if (save_timestamp - && time_of_compilation - > std::max(file_stat.mtime(), file_stat.ctime())) { - fi.mtime = file_stat.mtime(); - fi.ctime = file_stat.ctime(); - } else { - fi.mtime = -1; - fi.ctime = -1; - } - fi.fsize = file_stat.size(); - } else { - fi.mtime = -1; - fi.ctime = -1; - fi.fsize = 0; - } + const auto file_stat = file_stater(path); + fi.mtime = file_stat.mtime; + fi.ctime = file_stat.ctime; + fi.fsize = file_stat.size; const auto fi_it = mf_file_infos.find(fi); if (fi_it != mf_file_infos.end()) { diff --git a/src/core/Manifest.hpp b/src/core/Manifest.hpp index f277093f..399194bc 100644 --- a/src/core/Manifest.hpp +++ b/src/core/Manifest.hpp @@ -24,6 +24,7 @@ #include <third_party/nonstd/span.hpp> #include <cstdint> +#include <functional> #include <optional> #include <string> #include <unordered_map> @@ -38,6 +39,15 @@ class Manifest : public Serializer public: static const uint8_t k_format_version; + struct FileStats + { + uint64_t size; + int64_t mtime; + int64_t ctime; + }; + + using FileStater = std::function<FileStats(std::string)>; + Manifest() = default; void read(nonstd::span<const uint8_t> data); @@ -46,8 +56,7 @@ public: bool add_result(const Digest& result_key, const std::unordered_map<std::string, Digest>& included_files, - time_t time_of_compilation, - bool save_timestamp); + const FileStater& stat_file); // core::Serializer uint32_t serialized_size() const override; @@ -56,13 +65,6 @@ public: void inspect(FILE* stream) const; private: - struct FileStats - { - uint64_t size; - int64_t mtime; - int64_t ctime; - }; - struct FileInfo { uint32_t index; // Index to m_files. @@ -95,8 +97,7 @@ private: const Digest& digest, const std::unordered_map<std::string, uint32_t>& mf_files, const std::unordered_map<FileInfo, uint32_t>& mf_file_infos, - time_t time_of_compilation, - bool save_timestamp); + const FileStater& file_state); bool result_matches(const Context& ctx, |