diff options
author | Maksim Denisov <denisov.maksim@huawei.com> | 2025-02-25 13:42:22 +0100 |
---|---|---|
committer | Maksim Denisov <denisov.maksim@huawei.com> | 2025-02-27 09:03:30 +0100 |
commit | 461312b57d3b49f92861d2c6c5e8a6b13ffa839b (patch) | |
tree | c642cd0d3379e6886d1b3847d38661e249e75d58 /src/buildtool/execution_api | |
parent | eccc7dcfb22fb9c6c42bbcd5566cd044acd1a2f3 (diff) | |
download | justbuild-461312b57d3b49f92861d2c6c5e8a6b13ffa839b.tar.gz |
ArtifactBlob: Use static function for construction
Diffstat (limited to 'src/buildtool/execution_api')
9 files changed, 70 insertions, 66 deletions
diff --git a/src/buildtool/execution_api/bazel_msg/bazel_msg_factory.cpp b/src/buildtool/execution_api/bazel_msg/bazel_msg_factory.cpp index 6eb35e4c..ccc0f2d7 100644 --- a/src/buildtool/execution_api/bazel_msg/bazel_msg_factory.cpp +++ b/src/buildtool/execution_api/bazel_msg/bazel_msg_factory.cpp @@ -164,13 +164,14 @@ struct DirectoryNodeBundle final { // SHA256 is used since bazel types are processed here. HashFunction const hash_function{HashFunction::Type::PlainSHA256}; - auto digest = ArtifactDigestFactory::HashDataAs<ObjectType::File>( - hash_function, *content); - - return DirectoryNodeBundle{ - .message = CreateDirectoryNode(dir_name, digest), - .blob = ArtifactBlob{ - std::move(digest), std::move(*content), /*is_exec=*/false}}; + auto blob = ArtifactBlob::FromMemory( + hash_function, ObjectType::File, *std::move(content)); + if (not blob.has_value()) { + return std::nullopt; + } + auto const digest = blob->GetDigest(); + return DirectoryNodeBundle{.message = CreateDirectoryNode(dir_name, digest), + .blob = *std::move(blob)}; } /// \brief Create bundle for protobuf message Command from args strings. @@ -201,11 +202,12 @@ struct DirectoryNodeBundle final { if (not content) { return std::nullopt; } - auto digest = ArtifactDigestFactory::HashDataAs<ObjectType::File>( - request.hash_function, *content); - return ArtifactBlob{std::move(digest), - std::move(*content), - /*is_exec=*/false}; + auto blob = ArtifactBlob::FromMemory( + request.hash_function, ObjectType::File, *std::move(content)); + if (not blob.has_value()) { + return std::nullopt; + } + return *std::move(blob); } /// \brief Create bundle for protobuf message Action from Command. @@ -240,11 +242,12 @@ struct DirectoryNodeBundle final { if (not content) { return std::nullopt; } - auto digest = ArtifactDigestFactory::HashDataAs<ObjectType::File>( - request.hash_function, *content); - return ArtifactBlob{std::move(digest), - std::move(*content), - /*is_exec=*/false}; + auto blob = ArtifactBlob::FromMemory( + request.hash_function, ObjectType::File, *std::move(content)); + if (not blob.has_value()) { + return std::nullopt; + } + return *std::move(blob); } /// \brief Convert `DirectoryTree` to `DirectoryNodeBundle`. diff --git a/src/buildtool/execution_api/common/TARGETS b/src/buildtool/execution_api/common/TARGETS index 6eeb3931..39ccc042 100644 --- a/src/buildtool/execution_api/common/TARGETS +++ b/src/buildtool/execution_api/common/TARGETS @@ -133,8 +133,7 @@ , ["src/buildtool/execution_api/bazel_msg", "directory_tree"] ] , "private-deps": - [ ["src/buildtool/common", "artifact_digest_factory"] - , ["src/buildtool/common", "common"] + [ ["src/buildtool/common", "common"] , ["src/buildtool/crypto", "hash_function"] , ["src/buildtool/file_system", "git_repo"] , ["src/buildtool/file_system", "object_type"] diff --git a/src/buildtool/execution_api/common/blob_tree.cpp b/src/buildtool/execution_api/common/blob_tree.cpp index 7224406a..e6d8fc2c 100644 --- a/src/buildtool/execution_api/common/blob_tree.cpp +++ b/src/buildtool/execution_api/common/blob_tree.cpp @@ -22,7 +22,6 @@ #include "src/buildtool/common/artifact.hpp" #include "src/buildtool/common/artifact_digest.hpp" -#include "src/buildtool/common/artifact_digest_factory.hpp" #include "src/buildtool/crypto/hash_function.hpp" #include "src/buildtool/file_system/git_repo.hpp" #include "src/buildtool/file_system/object_type.hpp" @@ -68,17 +67,13 @@ auto BlobTree::FromDirectoryTree(DirectoryTreePtr const& tree, } } if (auto git_tree = GitRepo::CreateShallowTree(entries)) { - auto digest = - ArtifactDigestFactory::Create(HashFunction::Type::GitSHA1, - ToHexString(git_tree->first), - git_tree->second.size(), - /*is_tree=*/true); - if (digest) { - return std::make_shared<BlobTree>( - ArtifactBlob{*std::move(digest), - git_tree->second, - /*is_exec=*/false}, - nodes); + auto blob = ArtifactBlob::FromMemory( + HashFunction{HashFunction::Type::GitSHA1}, + ObjectType::Tree, + std::move(git_tree)->second); + if (blob.has_value()) { + return std::make_shared<BlobTree>(*std::move(blob), + std::move(nodes)); } } } catch (...) { diff --git a/src/buildtool/execution_api/git/git_api.cpp b/src/buildtool/execution_api/git/git_api.cpp index 6021a913..231e717a 100644 --- a/src/buildtool/execution_api/git/git_api.cpp +++ b/src/buildtool/execution_api/git/git_api.cpp @@ -223,19 +223,16 @@ auto GitApi::RetrieveToCas( return false; } - ArtifactDigest digest = - IsTreeObject(info->type) - ? ArtifactDigestFactory::HashDataAs<ObjectType::Tree>( - hash_function, *content) - : ArtifactDigestFactory::HashDataAs<ObjectType::File>( - hash_function, *content); + auto blob = ArtifactBlob::FromMemory( + hash_function, info->type, *std::move(content)); + if (not blob.has_value()) { + return false; + } // Collect blob and upload to remote CAS if transfer size reached. if (not UpdateContainerAndUpload( &container, - ArtifactBlob{std::move(digest), - std::move(*content), - IsExecutableObject(info->type)}, + *std::move(blob), /*exception_is_fatal=*/true, [&api](std::unordered_set<ArtifactBlob>&& blobs) { return api.Upload(std::move(blobs), diff --git a/src/buildtool/execution_api/local/local_api.cpp b/src/buildtool/execution_api/local/local_api.cpp index 040b4be6..92953190 100644 --- a/src/buildtool/execution_api/local/local_api.cpp +++ b/src/buildtool/execution_api/local/local_api.cpp @@ -25,7 +25,6 @@ #include <grpcpp/support/status.h> -#include "src/buildtool/common/artifact_digest_factory.hpp" #include "src/buildtool/common/protocol_traits.hpp" #include "src/buildtool/execution_api/bazel_msg/directory_tree.hpp" #include "src/buildtool/execution_api/common/common_api.hpp" @@ -177,26 +176,23 @@ auto LocalApi::RetrieveToCas( } // Read artifact content (file or symlink). - auto const& content = FileSystemManager::ReadFile(*path); + auto content = FileSystemManager::ReadFile(*path); if (not content) { return false; } - // Regenerate digest since object infos read by - // storage_.ReadTreeInfos() will contain 0 as size. - ArtifactDigest digest = - IsTreeObject(info->type) - ? ArtifactDigestFactory::HashDataAs<ObjectType::Tree>( - local_context_.storage_config->hash_function, *content) - : ArtifactDigestFactory::HashDataAs<ObjectType::File>( - local_context_.storage_config->hash_function, *content); + auto blob = ArtifactBlob::FromMemory( + local_context_.storage_config->hash_function, + info->type, + *std::move(content)); + if (not blob.has_value()) { + return false; + } // Collect blob and upload to remote CAS if transfer size reached. if (not UpdateContainerAndUpload( &container, - ArtifactBlob{std::move(digest), - *content, - IsExecutableObject(info->type)}, + *std::move(blob), /*exception_is_fatal=*/true, [&api](std::unordered_set<ArtifactBlob>&& blobs) { return api.Upload(std::move(blobs), diff --git a/src/buildtool/execution_api/remote/TARGETS b/src/buildtool/execution_api/remote/TARGETS index 0dd5ccb0..a575edb0 100644 --- a/src/buildtool/execution_api/remote/TARGETS +++ b/src/buildtool/execution_api/remote/TARGETS @@ -33,6 +33,7 @@ , ["src/buildtool/execution_api/common", "ids"] , ["src/buildtool/execution_api/common", "message_limits"] , ["src/buildtool/file_system", "git_repo"] + , ["src/buildtool/file_system", "object_type"] , ["src/buildtool/logging", "log_level"] , ["src/buildtool/logging", "logging"] , ["src/utils/cpp", "expected"] @@ -54,7 +55,6 @@ , ["src/buildtool/common", "protocol_traits"] , ["src/buildtool/common/remote", "retry"] , ["src/buildtool/execution_api/bazel_msg", "bazel_msg_factory"] - , ["src/buildtool/file_system", "object_type"] , ["src/utils/cpp", "back_map"] , ["src/utils/cpp", "gsl"] , ["src/utils/cpp", "path"] diff --git a/src/buildtool/execution_api/remote/bazel/bazel_cas_client.cpp b/src/buildtool/execution_api/remote/bazel/bazel_cas_client.cpp index dd4ec919..a02b8d4c 100644 --- a/src/buildtool/execution_api/remote/bazel/bazel_cas_client.cpp +++ b/src/buildtool/execution_api/remote/bazel/bazel_cas_client.cpp @@ -36,6 +36,7 @@ #include "src/buildtool/file_system/object_type.hpp" #include "src/buildtool/logging/log_level.hpp" #include "src/utils/cpp/back_map.hpp" +#include "src/utils/cpp/expected.hpp" namespace { @@ -302,12 +303,19 @@ auto BazelCasClient::BatchReadBlobs( std::vector<ArtifactBlob>* v, bazel_re::BatchReadBlobsResponse_Response const& r) { - if (auto value = - back_map->GetReference(r.digest())) { - v->emplace_back(*value.value(), - r.data(), - /*is_exec=*/false); + auto ref = back_map->GetReference(r.digest()); + if (not ref.has_value()) { + return; } + auto blob = ArtifactBlob::FromMemory( + HashFunction{ref.value()->GetHashType()}, + ref.value()->IsTree() ? ObjectType::Tree + : ObjectType::File, + r.data()); + if (not blob.has_value()) { + return; + } + v->emplace_back(*std::move(blob)); }); if (batch_response.ok) { std::move(batch_response.result.begin(), diff --git a/src/buildtool/execution_api/remote/bazel/bazel_response.cpp b/src/buildtool/execution_api/remote/bazel/bazel_response.cpp index f06a4275..d0429cd7 100644 --- a/src/buildtool/execution_api/remote/bazel/bazel_response.cpp +++ b/src/buildtool/execution_api/remote/bazel/bazel_response.cpp @@ -50,12 +50,8 @@ auto ProcessDirectoryMessage(HashFunction hash_function, fmt::format("found invalid symlink at {}", link.name())}; } } - auto data = dir.SerializeAsString(); - auto digest = ArtifactDigestFactory::HashDataAs<ObjectType::File>( - hash_function, data); - return ArtifactBlob{std::move(digest), - std::move(data), - /*is_exec=*/false}; + return ArtifactBlob::FromMemory( + hash_function, ObjectType::File, dir.SerializeAsString()); } } // namespace diff --git a/src/buildtool/execution_api/remote/bazel/bytestream_client.hpp b/src/buildtool/execution_api/remote/bazel/bytestream_client.hpp index 23a296c9..cf85b769 100644 --- a/src/buildtool/execution_api/remote/bazel/bytestream_client.hpp +++ b/src/buildtool/execution_api/remote/bazel/bytestream_client.hpp @@ -33,8 +33,10 @@ #include "src/buildtool/common/artifact_digest.hpp" #include "src/buildtool/common/remote/client_common.hpp" #include "src/buildtool/common/remote/port.hpp" +#include "src/buildtool/crypto/hash_function.hpp" #include "src/buildtool/execution_api/common/bytestream_utils.hpp" #include "src/buildtool/execution_api/common/ids.hpp" +#include "src/buildtool/file_system/object_type.hpp" #include "src/buildtool/logging/log_level.hpp" #include "src/buildtool/logging/logger.hpp" #include "src/utils/cpp/expected.hpp" @@ -115,7 +117,15 @@ class ByteStreamClient { if (not data) { return std::nullopt; } - return ArtifactBlob{digest, std::move(output), /*is_exec=*/false}; + + auto blob = ArtifactBlob::FromMemory( + HashFunction{digest.GetHashType()}, + digest.IsTree() ? ObjectType::Tree : ObjectType::File, + std::move(output)); + if (not blob.has_value() or blob->GetDigest() != digest) { + return std::nullopt; + } + return *std::move(blob); } [[nodiscard]] auto Write(std::string const& instance_name, |