From a28dcc4004e5bbcc84a0c7b774d20f88bf211e84 Mon Sep 17 00:00:00 2001 From: Maksim Denisov Date: Wed, 18 Sep 2024 15:49:15 +0200 Subject: Add LocalCAS::CheckTreeInvariant that works with file paths --- src/buildtool/storage/local_cas.hpp | 16 ++++++++++++++++ src/buildtool/storage/local_cas.tpp | 23 +++++++++++++++-------- 2 files changed, 31 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/buildtool/storage/local_cas.hpp b/src/buildtool/storage/local_cas.hpp index ee5a6ea2..8a95c234 100644 --- a/src/buildtool/storage/local_cas.hpp +++ b/src/buildtool/storage/local_cas.hpp @@ -213,6 +213,14 @@ class LocalCAS { std::string const& tree_data) const noexcept -> std::optional; + /// \brief Check whether all parts of the tree are in the storage. + /// \param tree_digest Digest of the tree to be checked. + /// \param file Content of the tree. + /// \return An error on fail. + [[nodiscard]] auto CheckTreeInvariant(ArtifactDigest const& tree_digest, + std::filesystem::path const& file) + const noexcept -> std::optional; + /// \brief Uplink blob from this generation to latest LocalCAS generation. /// Performs a synchronization if requested and if blob is only available /// with inverse x-bit. This function is only available for instances that @@ -354,6 +362,14 @@ auto LocalCAS::CheckTreeInvariant( return std::nullopt; } +template +auto LocalCAS::CheckTreeInvariant( + ArtifactDigest const& tree_digest, + std::filesystem::path const& file) const noexcept + -> std::optional { + return std::nullopt; +} + template template auto LocalCAS::Splice(ArtifactDigest const& digest, diff --git a/src/buildtool/storage/local_cas.tpp b/src/buildtool/storage/local_cas.tpp index a7f81b1a..e47cffa4 100644 --- a/src/buildtool/storage/local_cas.tpp +++ b/src/buildtool/storage/local_cas.tpp @@ -354,6 +354,20 @@ auto LocalCAS::CheckTreeInvariant( return std::nullopt; } +template +auto LocalCAS::CheckTreeInvariant( + ArtifactDigest const& tree_digest, + std::filesystem::path const& file) const noexcept + -> std::optional { + auto const tree_data = FileSystemManager::ReadFile(file); + if (not tree_data) { + return LargeObjectError{ + LargeObjectErrorCode::Internal, + fmt::format("could not read tree {}", tree_digest.hash())}; + } + return CheckTreeInvariant(tree_digest, *tree_data); +} + template template auto LocalCAS::Splice(ArtifactDigest const& digest, @@ -400,14 +414,7 @@ auto LocalCAS::Splice(ArtifactDigest const& digest, // Check tree invariants: if constexpr (kIsTree) { if (ProtocolTraits::IsNative(hash_function_.GetType())) { - // Read tree entries: - auto const tree_data = FileSystemManager::ReadFile(file_path); - if (not tree_data) { - return unexpected{LargeObjectError{ - LargeObjectErrorCode::Internal, - fmt::format("could not read tree {}", digest.hash())}}; - } - if (auto error = CheckTreeInvariant(digest, *tree_data)) { + if (auto error = CheckTreeInvariant(digest, file_path)) { return unexpected{std::move(*error)}; } } -- cgit v1.2.3