summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/buildtool/common/TARGETS8
-rw-r--r--src/buildtool/common/artifact_blob.cpp27
-rw-r--r--src/buildtool/common/artifact_blob.hpp22
3 files changed, 55 insertions, 2 deletions
diff --git a/src/buildtool/common/TARGETS b/src/buildtool/common/TARGETS
index 292fb597..33e88829 100644
--- a/src/buildtool/common/TARGETS
+++ b/src/buildtool/common/TARGETS
@@ -86,10 +86,16 @@
, "srcs": ["artifact_blob.cpp"]
, "deps":
[ "common"
+ , ["src/buildtool/crypto", "hash_function"]
+ , ["src/buildtool/file_system", "object_type"]
, ["src/utils/cpp", "expected"]
, ["src/utils/cpp", "incremental_reader"]
]
- , "private-deps": [["src/utils/cpp", "hash_combine"]]
+ , "private-deps":
+ [ "artifact_digest_factory"
+ , ["@", "fmt", "", "fmt"]
+ , ["src/utils/cpp", "hash_combine"]
+ ]
, "stage": ["src", "buildtool", "common"]
}
, "common":
diff --git a/src/buildtool/common/artifact_blob.cpp b/src/buildtool/common/artifact_blob.cpp
index 3b199e5e..c25e1db1 100644
--- a/src/buildtool/common/artifact_blob.cpp
+++ b/src/buildtool/common/artifact_blob.cpp
@@ -14,8 +14,35 @@
#include "src/buildtool/common/artifact_blob.hpp"
+#include <exception>
+
+#include "fmt/core.h"
+#include "src/buildtool/common/artifact_digest_factory.hpp"
#include "src/utils/cpp/hash_combine.hpp"
+auto ArtifactBlob::FromMemory(HashFunction hash_function,
+ ObjectType type,
+ std::string content) noexcept
+ -> expected<ArtifactBlob, std::string> {
+ try {
+ auto digest = IsTreeObject(type)
+ ? ArtifactDigestFactory::HashDataAs<ObjectType::Tree>(
+ hash_function, content)
+ : ArtifactDigestFactory::HashDataAs<ObjectType::File>(
+ hash_function, content);
+ return ArtifactBlob{
+ std::move(digest),
+ std::make_shared<std::string const>(std::move(content)),
+ IsExecutableObject(type)};
+ } catch (const std::exception& e) {
+ return unexpected{fmt::format(
+ "ArtifactBlob::FromMemory: Got an exception:\n{}", e.what())};
+ } catch (...) {
+ return unexpected<std::string>{
+ "ArtifactBlob::FromMemory: Got an unknown exception"};
+ }
+}
+
auto ArtifactBlob::ReadContent() const noexcept
-> std::shared_ptr<std::string const> {
return content_;
diff --git a/src/buildtool/common/artifact_blob.hpp b/src/buildtool/common/artifact_blob.hpp
index 612f851f..42a7778c 100644
--- a/src/buildtool/common/artifact_blob.hpp
+++ b/src/buildtool/common/artifact_blob.hpp
@@ -22,6 +22,8 @@
#include <utility>
#include "src/buildtool/common/artifact_digest.hpp"
+#include "src/buildtool/crypto/hash_function.hpp"
+#include "src/buildtool/file_system/object_type.hpp"
#include "src/utils/cpp/expected.hpp"
#include "src/utils/cpp/incremental_reader.hpp"
@@ -34,6 +36,17 @@ class ArtifactBlob final {
content_{std::make_shared<std::string const>(std::move(content))},
is_executable_{is_exec} {}
+ /// \brief Create ArtifactBlob and keep the given content in memory. The
+ /// content is hashed based on the given hash function and ObjectType.
+ /// \param hash_function Hash function that must be used for hashing.
+ /// \param type Type of the content.
+ /// \param content String to be stored
+ /// \return Valid ArtifactBlob on success or an error message on failure.
+ [[nodiscard]] static auto FromMemory(HashFunction hash_function,
+ ObjectType type,
+ std::string content) noexcept
+ -> expected<ArtifactBlob, std::string>;
+
[[nodiscard]] auto operator==(ArtifactBlob const& other) const noexcept
-> bool {
return digest_ == other.digest_ and
@@ -47,7 +60,7 @@ class ArtifactBlob final {
/// \brief Obtain the size of the content.
[[nodiscard]] auto GetContentSize() const noexcept -> std::size_t {
- return content_->size();
+ return digest_.size();
}
/// \brief Read the content from source.
@@ -76,6 +89,13 @@ class ArtifactBlob final {
ArtifactDigest digest_;
std::shared_ptr<std::string const> content_;
bool is_executable_;
+
+ explicit ArtifactBlob(ArtifactDigest digest,
+ std::shared_ptr<std::string const> content,
+ bool is_executable) noexcept
+ : digest_{std::move(digest)},
+ content_{std::move(content)},
+ is_executable_{is_executable} {}
};
namespace std {