diff options
Diffstat (limited to 'src/buildtool/execution_api/local')
-rw-r--r-- | src/buildtool/execution_api/local/TARGETS | 12 | ||||
-rw-r--r-- | src/buildtool/execution_api/local/local_action.cpp | 12 | ||||
-rw-r--r-- | src/buildtool/execution_api/local/local_api.hpp | 16 | ||||
-rw-r--r-- | src/buildtool/execution_api/local/local_cas_reader.cpp | 66 | ||||
-rw-r--r-- | src/buildtool/execution_api/local/local_cas_reader.hpp | 39 |
5 files changed, 132 insertions, 13 deletions
diff --git a/src/buildtool/execution_api/local/TARGETS b/src/buildtool/execution_api/local/TARGETS index b1b7c08c..b192b402 100644 --- a/src/buildtool/execution_api/local/TARGETS +++ b/src/buildtool/execution_api/local/TARGETS @@ -19,8 +19,13 @@ , "local": { "type": ["@", "rules", "CC", "library"] , "name": ["local"] - , "hdrs": ["local_api.hpp", "local_action.hpp", "local_response.hpp"] - , "srcs": ["local_action.cpp"] + , "hdrs": + [ "local_api.hpp" + , "local_action.hpp" + , "local_response.hpp" + , "local_cas_reader.hpp" + ] + , "srcs": ["local_action.cpp", "local_cas_reader.cpp"] , "deps": [ ["@", "fmt", "", "fmt"] , ["@", "gsl", "", "gsl"] @@ -38,6 +43,7 @@ , ["src/buildtool/logging", "log_level"] , ["src/buildtool/logging", "logging"] , ["src/buildtool/execution_api/execution_service", "cas_utils"] + , ["src/buildtool/file_system", "git_repo"] ] , "stage": ["src", "buildtool", "execution_api", "local"] , "private-deps": @@ -48,6 +54,8 @@ , ["src/buildtool/common", "bazel_types"] , ["src/buildtool/file_system", "file_system_manager"] , ["src/buildtool/execution_api/utils", "outputscheck"] + , ["src/buildtool/crypto", "hash_function"] + , ["src/utils/cpp", "path"] ] } } diff --git a/src/buildtool/execution_api/local/local_action.cpp b/src/buildtool/execution_api/local/local_action.cpp index 52b513f0..cb3e3563 100644 --- a/src/buildtool/execution_api/local/local_action.cpp +++ b/src/buildtool/execution_api/local/local_action.cpp @@ -21,7 +21,9 @@ #include "src/buildtool/common/bazel_types.hpp" #include "src/buildtool/compatibility/native_support.hpp" +#include "src/buildtool/execution_api/common/tree_reader.hpp" #include "src/buildtool/execution_api/local/config.hpp" +#include "src/buildtool/execution_api/local/local_cas_reader.hpp" #include "src/buildtool/execution_api/local/local_response.hpp" #include "src/buildtool/execution_api/utils/outputscheck.hpp" #include "src/buildtool/file_system/file_system_manager.hpp" @@ -216,14 +218,14 @@ auto LocalAction::StageInputs( if (FileSystemManager::IsRelativePath(exec_path)) { return false; } - - auto infos = storage_->CAS().RecursivelyReadTreeLeafs( + auto reader = TreeReader<LocalCasReader>{storage_->CAS()}; + auto result = reader.RecursivelyReadTreeLeafs( root_digest_, exec_path, /*include_trees=*/true); - if (not infos) { + if (not result) { return false; } - for (std::size_t i{}; i < infos->first.size(); ++i) { - if (not StageInput(infos->first.at(i), infos->second.at(i))) { + for (std::size_t i{}; i < result->paths.size(); ++i) { + if (not StageInput(result->paths.at(i), result->infos.at(i))) { return false; } } diff --git a/src/buildtool/execution_api/local/local_api.hpp b/src/buildtool/execution_api/local/local_api.hpp index 53cee482..c81b75c1 100644 --- a/src/buildtool/execution_api/local/local_api.hpp +++ b/src/buildtool/execution_api/local/local_api.hpp @@ -37,9 +37,11 @@ #include "src/buildtool/execution_api/bazel_msg/blob_tree.hpp" #include "src/buildtool/execution_api/common/common_api.hpp" #include "src/buildtool/execution_api/common/execution_api.hpp" +#include "src/buildtool/execution_api/common/tree_reader.hpp" #include "src/buildtool/execution_api/execution_service/cas_utils.hpp" #include "src/buildtool/execution_api/git/git_api.hpp" #include "src/buildtool/execution_api/local/local_action.hpp" +#include "src/buildtool/execution_api/local/local_cas_reader.hpp" #include "src/buildtool/file_system/file_system_manager.hpp" #include "src/buildtool/logging/log_level.hpp" #include "src/buildtool/logging/logger.hpp" @@ -85,11 +87,12 @@ class LocalApi final : public IExecutionApi { auto const& info = artifacts_info[i]; if (IsTreeObject(info.type)) { // read object infos from sub tree and call retrieve recursively - auto const infos = storage_->CAS().RecursivelyReadTreeLeafs( + auto reader = TreeReader<LocalCasReader>{storage_->CAS()}; + auto const result = reader.RecursivelyReadTreeLeafs( info.digest, output_paths[i]); - if (not infos) { + if (not result) { if (Compatibility::IsCompatible()) { - // infos not available, and in compatible mode cannot + // result not available, and in compatible mode cannot // fall back to git return false; } @@ -99,7 +102,7 @@ class LocalApi final : public IExecutionApi { return false; } } - else if (not RetrieveToPaths(infos->second, infos->first)) { + else if (not RetrieveToPaths(result->infos, result->paths)) { return false; } } @@ -192,9 +195,10 @@ class LocalApi final : public IExecutionApi { auto const& info = missing_artifacts_info->back_map[dgst]; // Recursively process trees. if (IsTreeObject(info.type)) { - auto const& infos = storage_->CAS().ReadDirectTreeEntries( + auto reader = TreeReader<LocalCasReader>{storage_->CAS()}; + auto const& result = reader.ReadDirectTreeEntries( info.digest, std::filesystem::path{}); - if (not infos or not RetrieveToCas(infos->second, api)) { + if (not result or not RetrieveToCas(result->infos, api)) { return false; } } diff --git a/src/buildtool/execution_api/local/local_cas_reader.cpp b/src/buildtool/execution_api/local/local_cas_reader.cpp new file mode 100644 index 00000000..411bc5d7 --- /dev/null +++ b/src/buildtool/execution_api/local/local_cas_reader.cpp @@ -0,0 +1,66 @@ +// Copyright 2024 Huawei Cloud Computing Technology Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "src/buildtool/execution_api/local/local_cas_reader.hpp" + +#include "src/buildtool/crypto/hash_function.hpp" +#include "src/buildtool/execution_api/bazel_msg/bazel_msg_factory.hpp" +#include "src/buildtool/file_system/file_system_manager.hpp" +#include "src/buildtool/logging/log_level.hpp" +#include "src/buildtool/logging/logger.hpp" +#include "src/utils/cpp/path.hpp" + +auto LocalCasReader::ReadDirectory(ArtifactDigest const& digest) const noexcept + -> std::optional<bazel_re::Directory> { + if (auto const path = cas_.TreePath(digest)) { + if (auto const content = FileSystemManager::ReadFile(*path)) { + return BazelMsgFactory::MessageFromString<bazel_re::Directory>( + *content); + } + } + Logger::Log( + LogLevel::Error, "Directory {} not found in CAS", digest.hash()); + return std::nullopt; +} + +auto LocalCasReader::ReadGitTree(ArtifactDigest const& digest) const noexcept + -> std::optional<GitRepo::tree_entries_t> { + if (auto const path = cas_.TreePath(digest)) { + if (auto const content = FileSystemManager::ReadFile(*path)) { + auto check_symlinks = + [this](std::vector<bazel_re::Digest> const& ids) { + for (auto const& id : ids) { + auto link_path = cas_.BlobPath(id, + /*is_executable=*/false); + if (not link_path) { + return false; + } + // in the local CAS we store as files + auto content = FileSystemManager::ReadFile(*link_path); + if (not content or not PathIsNonUpwards(*content)) { + return false; + } + } + return true; + }; + return GitRepo::ReadTreeData( + *content, + HashFunction::ComputeTreeHash(*content).Bytes(), + check_symlinks, + /*is_hex_id=*/false); + } + } + Logger::Log(LogLevel::Debug, "Tree {} not found in CAS", digest.hash()); + return std::nullopt; +} diff --git a/src/buildtool/execution_api/local/local_cas_reader.hpp b/src/buildtool/execution_api/local/local_cas_reader.hpp new file mode 100644 index 00000000..69baf3a9 --- /dev/null +++ b/src/buildtool/execution_api/local/local_cas_reader.hpp @@ -0,0 +1,39 @@ +// Copyright 2024 Huawei Cloud Computing Technology Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef INCLUDED_SRC_BUILDTOOL_EXECUTION_API_LOCAL_LOCAL_CAS_READER_HPP +#define INCLUDED_SRC_BUILDTOOL_EXECUTION_API_LOCAL_LOCAL_CAS_READER_HPP + +#include <optional> + +#include "src/buildtool/common/artifact_digest.hpp" +#include "src/buildtool/common/bazel_types.hpp" +#include "src/buildtool/file_system/git_repo.hpp" +#include "src/buildtool/storage/local_cas.hpp" + +class LocalCasReader final { + public: + explicit LocalCasReader(LocalCAS<true> const& cas) noexcept : cas_(cas) {} + + [[nodiscard]] auto ReadDirectory(ArtifactDigest const& digest) + const noexcept -> std::optional<bazel_re::Directory>; + + [[nodiscard]] auto ReadGitTree(ArtifactDigest const& digest) const noexcept + -> std::optional<GitRepo::tree_entries_t>; + + private: + LocalCAS<true> const& cas_; +}; + +#endif // INCLUDED_SRC_BUILDTOOL_EXECUTION_API_LOCAL_LOCAL_CAS_READER_HPP
\ No newline at end of file |