diff options
author | Maksim Denisov <denisov.maksim@huawei.com> | 2024-09-11 10:21:13 +0200 |
---|---|---|
committer | Maksim Denisov <denisov.maksim@huawei.com> | 2024-09-13 14:41:00 +0200 |
commit | ddf48a60029bed958fb2cf0238dbb0bca0b770fb (patch) | |
tree | 6c42568d14624a36a4f9b07d578d2a001764065d | |
parent | f60d39620b29aeaf1addeb244bdd6e15ddf4894c (diff) | |
download | justbuild-ddf48a60029bed958fb2cf0238dbb0bca0b770fb.tar.gz |
Add to ProtocolTraits static functions that provide protocol-specific behaviour
-rw-r--r-- | src/buildtool/common/TARGETS | 9 | ||||
-rw-r--r-- | src/buildtool/common/artifact_digest_factory.cpp | 6 | ||||
-rw-r--r-- | src/buildtool/common/bazel_digest_factory.cpp | 3 | ||||
-rw-r--r-- | src/buildtool/common/protocol_traits.hpp | 12 | ||||
-rw-r--r-- | src/buildtool/crypto/TARGETS | 1 | ||||
-rw-r--r-- | src/buildtool/crypto/hash_info.cpp | 5 | ||||
-rw-r--r-- | src/buildtool/execution_api/remote/bazel/bazel_network_reader.cpp | 5 |
7 files changed, 32 insertions, 9 deletions
diff --git a/src/buildtool/common/TARGETS b/src/buildtool/common/TARGETS index 1f280d78..4372db88 100644 --- a/src/buildtool/common/TARGETS +++ b/src/buildtool/common/TARGETS @@ -52,7 +52,7 @@ , ["src/utils/cpp", "gsl"] , ["src/utils/cpp", "expected"] ] - , "private-deps": [["src/buildtool/crypto", "hasher"]] + , "private-deps": [["src/buildtool/crypto", "hasher"], "protocol_traits"] , "stage": ["src", "buildtool", "common"] } , "artifact_digest_factory": @@ -68,7 +68,11 @@ , ["src/utils/cpp", "expected"] ] , "private-deps": - ["bazel_digest_factory", "bazel_types", ["@", "gsl", "", "gsl"]] + [ "bazel_digest_factory" + , "bazel_types" + , ["@", "gsl", "", "gsl"] + , "protocol_traits" + ] , "stage": ["src", "buildtool", "common"] } , "common": @@ -196,6 +200,7 @@ { "type": ["@", "rules", "CC", "library"] , "name": ["protocol_traits"] , "hdrs": ["protocol_traits.hpp"] + , "deps": [["src/buildtool/crypto", "hash_function"]] , "stage": ["src", "buildtool", "common"] } } diff --git a/src/buildtool/common/artifact_digest_factory.cpp b/src/buildtool/common/artifact_digest_factory.cpp index dd9da28d..f3ea30c1 100644 --- a/src/buildtool/common/artifact_digest_factory.cpp +++ b/src/buildtool/common/artifact_digest_factory.cpp @@ -17,15 +17,17 @@ #include "gsl/gsl" #include "src/buildtool/common/bazel_digest_factory.hpp" #include "src/buildtool/common/bazel_types.hpp" +#include "src/buildtool/common/protocol_traits.hpp" auto ArtifactDigestFactory::Create(HashFunction::Type hash_type, std::string hash, std::size_t size, bool is_tree) noexcept -> expected<ArtifactDigest, std::string> { - const bool kTreesAllowed = hash_type == HashFunction::Type::GitSHA1; auto hash_info = - HashInfo::Create(hash_type, std::move(hash), kTreesAllowed and is_tree); + HashInfo::Create(hash_type, + std::move(hash), + ProtocolTraits::IsTreeAllowed(hash_type) and is_tree); if (not hash_info) { return unexpected{std::move(hash_info).error()}; } diff --git a/src/buildtool/common/bazel_digest_factory.cpp b/src/buildtool/common/bazel_digest_factory.cpp index 4ff885aa..3a1f8519 100644 --- a/src/buildtool/common/bazel_digest_factory.cpp +++ b/src/buildtool/common/bazel_digest_factory.cpp @@ -16,12 +16,13 @@ #include <utility> +#include "src/buildtool/common/protocol_traits.hpp" #include "src/buildtool/crypto/hasher.hpp" auto BazelDigestFactory::Create(HashInfo const& hash_info, std::int64_t size) noexcept -> bazel_re::Digest { - auto hash = hash_info.HashType() == HashFunction::Type::GitSHA1 + auto hash = ProtocolTraits::IsNative(hash_info.HashType()) ? Prefix(hash_info.Hash(), hash_info.IsTree()) : hash_info.Hash(); diff --git a/src/buildtool/common/protocol_traits.hpp b/src/buildtool/common/protocol_traits.hpp index 49850233..f1a51cbe 100644 --- a/src/buildtool/common/protocol_traits.hpp +++ b/src/buildtool/common/protocol_traits.hpp @@ -15,6 +15,8 @@ #ifndef INCLUDED_SRC_BUILDTOOL_COMMON_PROTOCOL_TRAITS_HPP #define INCLUDED_SRC_BUILDTOOL_COMMON_PROTOCOL_TRAITS_HPP +#include "src/buildtool/crypto/hash_function.hpp" + class ProtocolTraits final { public: [[nodiscard]] static auto Instance() noexcept -> ProtocolTraits& { @@ -26,6 +28,16 @@ class ProtocolTraits final { } void SetCompatible(bool value = true) noexcept { compatible_ = value; } + inline static constexpr auto IsNative(HashFunction::Type hash_type) noexcept + -> bool { + return hash_type == HashFunction::Type::GitSHA1; + } + + inline static constexpr auto IsTreeAllowed( + HashFunction::Type hash_type) noexcept -> bool { + return IsNative(hash_type); + } + private: bool compatible_ = false; }; diff --git a/src/buildtool/crypto/TARGETS b/src/buildtool/crypto/TARGETS index 4cacb13f..e4a84676 100644 --- a/src/buildtool/crypto/TARGETS +++ b/src/buildtool/crypto/TARGETS @@ -35,6 +35,7 @@ , ["@", "gsl", "", "gsl"] , ["@", "fmt", "", "fmt"] , ["src/utils/cpp", "hex_string"] + , ["src/buildtool/common", "protocol_traits"] ] , "stage": ["src", "buildtool", "crypto"] } diff --git a/src/buildtool/crypto/hash_info.cpp b/src/buildtool/crypto/hash_info.cpp index 3b14d445..4fb87161 100644 --- a/src/buildtool/crypto/hash_info.cpp +++ b/src/buildtool/crypto/hash_info.cpp @@ -16,6 +16,7 @@ #include "fmt/core.h" #include "gsl/gsl" // Ensures +#include "src/buildtool/common/protocol_traits.hpp" #include "src/buildtool/crypto/hasher.hpp" #include "src/utils/cpp/hex_string.hpp" @@ -58,7 +59,7 @@ auto HashInfo::HashData(HashFunction hash_function, return HashInfo{ hash_digest.HexString(), hash_function.GetType(), - is_tree and hash_function.GetType() == HashFunction::Type::GitSHA1}; + is_tree and ProtocolTraits::IsTreeAllowed(hash_function.GetType())}; } auto HashInfo::HashFile(HashFunction hash_function, @@ -85,7 +86,7 @@ auto HashInfo::ValidateInput(HashFunction::Type type, std::string const& hash, bool is_tree) noexcept -> std::optional<std::string> { - if (type != HashFunction::Type::GitSHA1 and is_tree) { + if (is_tree and not ProtocolTraits::IsTreeAllowed(type)) { return fmt::format( "HashInfo: hash {} is expected to be {}.\nTrees are " "not allowed in this mode.", diff --git a/src/buildtool/execution_api/remote/bazel/bazel_network_reader.cpp b/src/buildtool/execution_api/remote/bazel/bazel_network_reader.cpp index 4b411d91..d78a63de 100644 --- a/src/buildtool/execution_api/remote/bazel/bazel_network_reader.cpp +++ b/src/buildtool/execution_api/remote/bazel/bazel_network_reader.cpp @@ -18,6 +18,7 @@ #include "src/buildtool/common/artifact_digest_factory.hpp" #include "src/buildtool/common/bazel_digest_factory.hpp" +#include "src/buildtool/common/protocol_traits.hpp" #include "src/buildtool/execution_api/bazel_msg/bazel_msg_factory.hpp" #include "src/buildtool/execution_api/common/message_limits.hpp" #include "src/buildtool/file_system/file_system_manager.hpp" @@ -71,7 +72,7 @@ auto BazelNetworkReader::ReadDirectory(ArtifactDigest const& digest) auto BazelNetworkReader::ReadGitTree(ArtifactDigest const& digest) const noexcept -> std::optional<GitRepo::tree_entries_t> { - ExpectsAudit(hash_function_.GetType() == HashFunction::Type::GitSHA1); + ExpectsAudit(ProtocolTraits::IsNative(hash_function_.GetType())); auto read_blob = ReadSingleBlob(digest); if (not read_blob) { @@ -145,7 +146,7 @@ auto BazelNetworkReader::DumpBlob(Artifact::ObjectInfo const& info, auto BazelNetworkReader::MakeAuxiliaryMap( std::vector<bazel_re::Directory>&& full_tree) const noexcept -> std::optional<DirectoryMap> { - ExpectsAudit(hash_function_.GetType() == HashFunction::Type::PlainSHA256); + ExpectsAudit(not ProtocolTraits::IsNative(hash_function_.GetType())); DirectoryMap result; result.reserve(full_tree.size()); |