summaryrefslogtreecommitdiff
path: root/src/buildtool/execution_api/remote/bazel/bytestream_client.hpp
diff options
context:
space:
mode:
authorMaksim Denisov <denisov.maksim@huawei.com>2025-02-28 17:49:38 +0100
committerMaksim Denisov <denisov.maksim@huawei.com>2025-03-24 09:33:46 +0100
commit88c25cccdeda497cb84de59e0a60f15fb3476e91 (patch)
treedf6234d83f1649bb911c765dfde89d0e827e3566 /src/buildtool/execution_api/remote/bazel/bytestream_client.hpp
parent4adc474d49d14472972a82f4a256c0346f722ce6 (diff)
downloadjustbuild-88c25cccdeda497cb84de59e0a60f15fb3476e91.tar.gz
BytestreamClient: Read to temporary files.
Diffstat (limited to 'src/buildtool/execution_api/remote/bazel/bytestream_client.hpp')
-rw-r--r--src/buildtool/execution_api/remote/bazel/bytestream_client.hpp33
1 files changed, 23 insertions, 10 deletions
diff --git a/src/buildtool/execution_api/remote/bazel/bytestream_client.hpp b/src/buildtool/execution_api/remote/bazel/bytestream_client.hpp
index cf85b769..89d61d1f 100644
--- a/src/buildtool/execution_api/remote/bazel/bytestream_client.hpp
+++ b/src/buildtool/execution_api/remote/bazel/bytestream_client.hpp
@@ -17,6 +17,7 @@
#include <cstddef>
#include <cstdint>
+#include <fstream>
#include <memory>
#include <optional>
#include <string>
@@ -41,6 +42,7 @@
#include "src/buildtool/logging/logger.hpp"
#include "src/utils/cpp/expected.hpp"
#include "src/utils/cpp/incremental_reader.hpp"
+#include "src/utils/cpp/tmp_dir.hpp"
/// Implements client side for google.bytestream.ByteStream service.
class ByteStreamClient {
@@ -105,23 +107,34 @@ class ByteStreamClient {
}
[[nodiscard]] auto Read(std::string const& instance_name,
- ArtifactDigest const& digest) const noexcept
+ ArtifactDigest const& digest,
+ TmpDir::Ptr const& temp_space) const noexcept
-> std::optional<ArtifactBlob> {
- auto reader = IncrementalRead(instance_name, digest);
- std::string output{};
- auto data = reader.Next();
- while (data and not data->empty()) {
- output.append(data->begin(), data->end());
- data = reader.Next();
+ auto temp_file = TmpDir::CreateFile(temp_space, digest.hash());
+ if (temp_file == nullptr) {
+ return std::nullopt;
}
- if (not data) {
+
+ auto reader = IncrementalRead(instance_name, digest);
+ try {
+ std::ofstream stream{temp_file->GetPath(), std::ios_base::binary};
+
+ auto data = reader.Next();
+ while (data.has_value() and not data->empty() and stream.good()) {
+ stream << *data;
+ data = reader.Next();
+ }
+ if (not stream.good() or not data.has_value()) {
+ return std::nullopt;
+ }
+ } catch (...) {
return std::nullopt;
}
- auto blob = ArtifactBlob::FromMemory(
+ auto blob = ArtifactBlob::FromTempFile(
HashFunction{digest.GetHashType()},
digest.IsTree() ? ObjectType::Tree : ObjectType::File,
- std::move(output));
+ std::move(temp_file));
if (not blob.has_value() or blob->GetDigest() != digest) {
return std::nullopt;
}