From a439c9149fc0e45b76978053b2d8f55d889f8041 Mon Sep 17 00:00:00 2001 From: Maksim Denisov Date: Mon, 8 Jul 2024 16:40:23 +0200 Subject: Move ObjectCAS::CreateDigest to the private space --- src/buildtool/common/artifact_digest.hpp | 13 +++++++++++++ src/buildtool/file_system/object_cas.hpp | 23 +++++++---------------- src/buildtool/storage/compactifier.cpp | 4 ++-- src/buildtool/storage/local_cas.hpp | 1 + src/buildtool/storage/local_cas.tpp | 2 +- 5 files changed, 24 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/buildtool/common/artifact_digest.hpp b/src/buildtool/common/artifact_digest.hpp index 2ad94c27..8cbf6ccc 100644 --- a/src/buildtool/common/artifact_digest.hpp +++ b/src/buildtool/common/artifact_digest.hpp @@ -90,6 +90,19 @@ class ArtifactDigest { } } + template + [[nodiscard]] static auto CreateFromFile( + std::filesystem::path const& path) noexcept + -> std::optional { + static constexpr bool kIsTree = IsTreeObject(kType); + auto hash = HashFunction::ComputeHashFile(path, kIsTree); + if (hash) { + return ArtifactDigest{ + hash->first.HexString(), hash->second, kIsTree}; + } + return std::nullopt; + } + [[nodiscard]] auto operator<(ArtifactDigest const& other) const -> bool { return (hash_ < other.hash_) or ((hash_ == other.hash_) and (static_cast(is_tree_) < diff --git a/src/buildtool/file_system/object_cas.hpp b/src/buildtool/file_system/object_cas.hpp index 185dc26a..aa521e07 100644 --- a/src/buildtool/file_system/object_cas.hpp +++ b/src/buildtool/file_system/object_cas.hpp @@ -20,7 +20,7 @@ #include #include // std::move -#include "src/buildtool/common/artifact.hpp" +#include "src/buildtool/common/artifact_digest.hpp" #include "src/buildtool/common/bazel_types.hpp" #include "src/buildtool/file_system/file_storage.hpp" #include "src/buildtool/file_system/file_system_manager.hpp" @@ -103,21 +103,6 @@ class ObjectCAS { return blob_path; } - /// \brief Calculate the digest for a file. - /// \param file_path File for which the digest needs to be calculated. - /// \return File digest. - [[nodiscard]] static auto CreateDigest( - std::filesystem::path const& file_path) noexcept - -> std::optional { - bool is_tree = kType == ObjectType::Tree; - auto hash = HashFunction::ComputeHashFile(file_path, is_tree); - if (hash) { - return ArtifactDigest( - hash->first.HexString(), hash->second, is_tree); - } - return std::nullopt; - } - private: // For `Tree` the underlying storage type is non-executable file. static constexpr auto kStorageType = @@ -134,6 +119,12 @@ class ObjectCAS { return ArtifactDigest::Create(bytes); } + [[nodiscard]] static auto CreateDigest( + std::filesystem::path const& file_path) noexcept + -> std::optional { + return ArtifactDigest::CreateFromFile(file_path); + } + [[nodiscard]] auto IsAvailable( bazel_re::Digest const& digest, std::filesystem::path const& path) const noexcept -> bool { diff --git a/src/buildtool/storage/compactifier.cpp b/src/buildtool/storage/compactifier.cpp index e9997efb..7dac4a06 100644 --- a/src/buildtool/storage/compactifier.cpp +++ b/src/buildtool/storage/compactifier.cpp @@ -21,12 +21,12 @@ #include #include +#include "src/buildtool/common/artifact_digest.hpp" #include "src/buildtool/common/bazel_types.hpp" #include "src/buildtool/crypto/hash_function.hpp" #include "src/buildtool/crypto/hasher.hpp" #include "src/buildtool/file_system/file_storage.hpp" #include "src/buildtool/file_system/file_system_manager.hpp" -#include "src/buildtool/file_system/object_cas.hpp" #include "src/buildtool/file_system/object_type.hpp" #include "src/buildtool/logging/log_level.hpp" #include "src/buildtool/logging/logger.hpp" @@ -271,7 +271,7 @@ template } // Calculate the digest for the entry: - auto const digest = ObjectCAS::CreateDigest(path); + auto const digest = ArtifactDigest::CreateFromFile(path); if (not digest) { task.Log(LogLevel::Error, "Failed to calculate digest for {}", diff --git a/src/buildtool/storage/local_cas.hpp b/src/buildtool/storage/local_cas.hpp index cbaa0439..aedf5838 100644 --- a/src/buildtool/storage/local_cas.hpp +++ b/src/buildtool/storage/local_cas.hpp @@ -21,6 +21,7 @@ #include #include "gsl/gsl" +#include "src/buildtool/common/artifact_digest.hpp" #include "src/buildtool/file_system/git_repo.hpp" #include "src/buildtool/file_system/object_cas.hpp" #include "src/buildtool/storage/config.hpp" diff --git a/src/buildtool/storage/local_cas.tpp b/src/buildtool/storage/local_cas.tpp index 97ecf1a3..8bb04049 100644 --- a/src/buildtool/storage/local_cas.tpp +++ b/src/buildtool/storage/local_cas.tpp @@ -374,7 +374,7 @@ auto LocalCAS::Splice( // methods can refer to a file that existed before. The direct hash // calculation is done instead. auto const& file_path = large_object.GetPath(); - auto spliced_digest = ObjectCAS::CreateDigest(file_path); + auto spliced_digest = ArtifactDigest::CreateFromFile(file_path); if (not spliced_digest) { return unexpected{LargeObjectError{LargeObjectErrorCode::Internal, "could not calculate digest"}}; -- cgit v1.2.3