From: Joel Rosdahl Date: Mon, 7 Sep 2020 07:09:20 +0000 (+0200) Subject: Move manifest functions into a Manifest namespace X-Git-Tag: v4.0~121 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=39eabc26928490444e26b42be73e484fcc0b692e;p=thirdparty%2Fccache.git Move manifest functions into a Manifest namespace --- diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 250d2e19d..92fcbc67b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -14,6 +14,7 @@ set( Hash.cpp Lockfile.cpp Logging.cpp + Manifest.cpp MiniTrace.cpp NullCompressor.cpp NullDecompressor.cpp @@ -37,7 +38,6 @@ set( execute.cpp hashutil.cpp language.cpp - manifest.cpp stats.cpp version.cpp) diff --git a/src/manifest.cpp b/src/Manifest.cpp similarity index 96% rename from src/manifest.cpp rename to src/Manifest.cpp index 57d621188..8363e7f42 100644 --- a/src/manifest.cpp +++ b/src/Manifest.cpp @@ -16,7 +16,7 @@ // this program; if not, write to the Free Software Foundation, Inc., 51 // Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -#include "manifest.hpp" +#include "Manifest.hpp" #include "AtomicFile.hpp" #include "CacheEntryReader.hpp" @@ -111,8 +111,6 @@ using Logging::log; using nonstd::nullopt; using nonstd::optional; -const uint8_t k_manifest_magic[4] = {'c', 'C', 'm', 'F'}; -const uint8_t k_manifest_version = 2; const uint32_t k_max_manifest_entries = 100; const uint32_t k_max_manifest_file_info_entries = 10000; @@ -158,6 +156,8 @@ template<> struct hash } // namespace std +namespace { + struct ResultEntry { // Indexes to file_infos. @@ -278,7 +278,7 @@ struct FileStats int64_t ctime; }; -static std::unique_ptr +std::unique_ptr read_manifest(const std::string& path, FILE* dump_stream = nullptr) { File file(path, "rb"); @@ -286,7 +286,7 @@ read_manifest(const std::string& path, FILE* dump_stream = nullptr) return {}; } - CacheEntryReader reader(file.get(), k_manifest_magic, k_manifest_version); + CacheEntryReader reader(file.get(), Manifest::k_magic, Manifest::k_version); if (dump_stream) { reader.dump_header(dump_stream); @@ -337,7 +337,7 @@ read_manifest(const std::string& path, FILE* dump_stream = nullptr) return mf; } -static bool +bool write_manifest(const Config& config, const std::string& path, const ManifestData& mf) @@ -358,8 +358,8 @@ write_manifest(const Config& config, AtomicFile atomic_manifest_file(path, AtomicFile::Mode::binary); CacheEntryWriter writer(atomic_manifest_file.stream(), - k_manifest_magic, - k_manifest_version, + Manifest::k_magic, + Manifest::k_version, Compression::type_from_config(config), Compression::level_from_config(config), payload_size); @@ -392,7 +392,7 @@ write_manifest(const Config& config, return true; } -static bool +bool verify_result(const Context& ctx, const ManifestData& mf, const ResultEntry& result, @@ -473,9 +473,16 @@ verify_result(const Context& ctx, return true; } +} // namespace + +namespace Manifest { + +const uint8_t k_magic[4] = {'c', 'C', 'm', 'F'}; +const uint8_t k_version = 2; + // Try to get the result name from a manifest file. Returns nullopt on failure. optional -manifest_get(const Context& ctx, const std::string& path) +get(const Context& ctx, const std::string& path) { std::unique_ptr mf; try { @@ -509,13 +516,13 @@ manifest_get(const Context& ctx, const std::string& path) // Put the result name into a manifest file given a set of included files. // Returns true on success, otherwise false. bool -manifest_put(const Config& config, - const std::string& path, - const Digest& result_name, - const std::unordered_map& included_files, +put(const Config& config, + const std::string& path, + const Digest& result_name, + const std::unordered_map& included_files, - time_t time_of_compilation, - bool save_timestamp) + time_t time_of_compilation, + bool save_timestamp) { // We don't bother to acquire a lock when writing the manifest to disk. A // race between two processes will only result in one lost entry, which is @@ -570,7 +577,7 @@ manifest_put(const Config& config, } bool -manifest_dump(const std::string& path, FILE* stream) +dump(const std::string& path, FILE* stream) { std::unique_ptr mf; try { @@ -611,3 +618,5 @@ manifest_dump(const std::string& path, FILE* stream) return true; } + +} // namespace Manifest diff --git a/src/manifest.hpp b/src/Manifest.hpp similarity index 64% rename from src/manifest.hpp rename to src/Manifest.hpp index e95c704c9..f358a2b4b 100644 --- a/src/manifest.hpp +++ b/src/Manifest.hpp @@ -29,15 +29,18 @@ class Config; class Context; class Digest; -extern const uint8_t k_manifest_magic[4]; -extern const uint8_t k_manifest_version; - -nonstd::optional manifest_get(const Context& ctx, - const std::string& path); -bool manifest_put(const Config& config, - const std::string& path, - const Digest& result_name, - const std::unordered_map& included_files, - time_t time_of_compilation, - bool save_timestamp); -bool manifest_dump(const std::string& path, FILE* stream); +namespace Manifest { + +extern const uint8_t k_magic[4]; +extern const uint8_t k_version; + +nonstd::optional get(const Context& ctx, const std::string& path); +bool put(const Config& config, + const std::string& path, + const Digest& result_name, + const std::unordered_map& included_files, + time_t time_of_compilation, + bool save_timestamp); +bool dump(const std::string& path, FILE* stream); + +} // namespace Manifest diff --git a/src/ccache.cpp b/src/ccache.cpp index 0a4ae70e5..6b25b4e19 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -29,6 +29,7 @@ #include "FormatNonstdStringView.hpp" #include "Hash.hpp" #include "Logging.hpp" +#include "Manifest.hpp" #include "MiniTrace.hpp" #include "ProgressBar.hpp" #include "Result.hpp" @@ -48,7 +49,6 @@ #include "execute.hpp" #include "hashutil.hpp" #include "language.hpp" -#include "manifest.hpp" #include "stats.hpp" #include "third_party/fmt/core.h" @@ -742,12 +742,12 @@ update_manifest_file(Context& ctx) MTR_BEGIN("manifest", "manifest_put"); log("Adding result name to {}", ctx.manifest_path()); - if (!manifest_put(ctx.config, - ctx.manifest_path(), - ctx.result_name(), - ctx.included_files, - ctx.time_of_compilation, - save_timestamp)) { + if (!Manifest::put(ctx.config, + ctx.manifest_path(), + ctx.result_name(), + ctx.included_files, + ctx.time_of_compilation, + save_timestamp)) { log("Failed to add result name to {}", ctx.manifest_path()); } else { auto st = Stat::stat(ctx.manifest_path(), Stat::OnError::log); @@ -1362,7 +1362,7 @@ calculate_result_name(Context& ctx, if (direct_mode) { hash.hash_delimiter("manifest version"); - hash.hash(k_manifest_version); + hash.hash(Manifest::k_version); } // clang will emit warnings for unused linker flags, so we shouldn't skip @@ -1605,7 +1605,7 @@ calculate_result_name(Context& ctx, log("Looking for result name in {}", ctx.manifest_path()); MTR_BEGIN("manifest", "manifest_get"); - result_name = manifest_get(ctx, ctx.manifest_path()); + result_name = Manifest::get(ctx, ctx.manifest_path()); MTR_END("manifest", "manifest_get"); if (result_name) { log("Got result name from manifest"); @@ -2268,7 +2268,7 @@ handle_main_options(int argc, const char* const* argv) } case DUMP_MANIFEST: - return manifest_dump(arg, stdout) ? 0 : 1; + return Manifest::dump(arg, stdout) ? 0 : 1; case DUMP_RESULT: { ResultDumper result_dumper(stdout); diff --git a/src/compress.cpp b/src/compress.cpp index d05127e72..3f3fd28f6 100644 --- a/src/compress.cpp +++ b/src/compress.cpp @@ -24,11 +24,11 @@ #include "Context.hpp" #include "File.hpp" #include "Logging.hpp" +#include "Manifest.hpp" #include "Result.hpp" #include "StdMakeUnique.hpp" #include "ThreadPool.hpp" #include "ZstdCompressor.hpp" -#include "manifest.hpp" #include "stats.hpp" #include "third_party/fmt/core.h" @@ -126,7 +126,7 @@ create_reader(const CacheFile& cache_file, FILE* stream) case CacheFile::Type::manifest: return std::make_unique( - stream, k_manifest_magic, k_manifest_version); + stream, Manifest::k_magic, Manifest::k_version); case CacheFile::Type::unknown: assert(false); // Handled at function entry.