summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPaul Cristian Sarbu <paul.cristian.sarbu@huawei.com>2024-09-26 18:28:30 +0200
committerPaul Cristian Sarbu <paul.cristian.sarbu@huawei.com>2024-10-25 13:00:43 +0200
commit9dfa79780768c43292673b7d60c69aec5bfdd12c (patch)
tree2c30753ac3ea985e461afa3c96d3d1fa749cba2a /src
parent78da0fa2d1aa9986105889eb3d639a153ed509ec (diff)
downloadjustbuild-9dfa79780768c43292673b7d60c69aec5bfdd12c.tar.gz
Add utility methods for digest mappings
These allow to read and write file associations between known digests in different CAS instances.
Diffstat (limited to 'src')
-rw-r--r--src/buildtool/execution_api/serve/TARGETS19
-rw-r--r--src/buildtool/execution_api/serve/utils.cpp117
-rw-r--r--src/buildtool/execution_api/serve/utils.hpp64
3 files changed, 200 insertions, 0 deletions
diff --git a/src/buildtool/execution_api/serve/TARGETS b/src/buildtool/execution_api/serve/TARGETS
new file mode 100644
index 00000000..087c32a7
--- /dev/null
+++ b/src/buildtool/execution_api/serve/TARGETS
@@ -0,0 +1,19 @@
+{ "utils":
+ { "type": ["@", "rules", "CC", "library"]
+ , "name": ["utils"]
+ , "hdrs": ["utils.hpp"]
+ , "srcs": ["utils.cpp"]
+ , "deps":
+ [ ["src/buildtool/common", "common"]
+ , ["src/buildtool/file_system", "object_type"]
+ , ["src/buildtool/storage", "config"]
+ , ["src/utils/cpp", "expected"]
+ ]
+ , "stage": ["src", "buildtool", "execution_api", "serve"]
+ , "private-deps":
+ [ ["@", "fmt", "", "fmt"]
+ , ["src/buildtool/file_system", "file_system_manager"]
+ , ["src/buildtool/storage", "fs_utils"]
+ ]
+ }
+}
diff --git a/src/buildtool/execution_api/serve/utils.cpp b/src/buildtool/execution_api/serve/utils.cpp
new file mode 100644
index 00000000..2796d3b1
--- /dev/null
+++ b/src/buildtool/execution_api/serve/utils.cpp
@@ -0,0 +1,117 @@
+// 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/serve/utils.hpp"
+
+#include <cstddef> // std::size_t
+#include <filesystem>
+#include <system_error>
+#include <utility> // std::move
+
+#include "fmt/core.h"
+#include "src/buildtool/file_system/file_system_manager.hpp"
+#include "src/buildtool/storage/fs_utils.hpp"
+
+namespace MRApiUtils {
+
+auto ReadRehashedDigest(ArtifactDigest const& digest,
+ StorageConfig const& source_config,
+ StorageConfig const& target_config,
+ bool from_git) noexcept
+ -> expected<std::optional<Artifact::ObjectInfo>, std::string> {
+ // check for mapping file in all generations
+ std::size_t generation = 0;
+ std::optional<std::filesystem::path> rehash_id_file = std::nullopt;
+ auto const compat_hash_type = target_config.hash_function.GetType();
+ for (; generation < source_config.num_generations; ++generation) {
+ auto path = StorageUtils::GetRehashIDFile(source_config,
+ compat_hash_type,
+ digest.hash(),
+ from_git,
+ generation);
+ if (FileSystemManager::Exists(path)) {
+ rehash_id_file = std::move(path);
+ break; // found the generation
+ }
+ }
+ if (rehash_id_file) {
+ // read id file
+ auto compat_obj_str = FileSystemManager::ReadFile(*rehash_id_file);
+ if (not compat_obj_str) {
+ return unexpected{fmt::format("failed to read rehash id file {}",
+ rehash_id_file->string())};
+ }
+ // get artifact object from content
+ auto compat_obj =
+ Artifact::ObjectInfo::FromString(compat_hash_type, *compat_obj_str);
+ if (not compat_obj) {
+ // handle nullopt value explicitly
+ return unexpected{
+ fmt::format("failed to read rehashed artifact from id file {}",
+ rehash_id_file->string())};
+ }
+ // ensure the id file is in generation 0 for future calls
+ if (generation != 0) {
+ auto dest_id_file = StorageUtils::GetRehashIDFile(
+ source_config, compat_hash_type, digest.hash(), from_git);
+ auto ok = FileSystemManager::CreateFileHardlink(*rehash_id_file,
+ dest_id_file);
+ if (not ok) {
+ auto const& err = ok.error();
+ if (err != std::errc::too_many_links) {
+ return unexpected{
+ fmt::format("failed to link rehash id file {}:\n{} {}",
+ dest_id_file.string(),
+ err.value(),
+ err.message())};
+ }
+ // if too many links reported, write id file ourselves
+ if (not StorageUtils::WriteTreeIDFile(dest_id_file,
+ *compat_obj_str)) {
+ return unexpected{
+ fmt::format("failed to write rehash id file {}",
+ dest_id_file.string())};
+ }
+ }
+ }
+ return std::move(compat_obj); // not dereferenced to assist type
+ // deduction in variant
+ }
+ // no mapping file found
+ return std::optional<Artifact::ObjectInfo>{std::nullopt};
+}
+
+auto StoreRehashedDigest(ArtifactDigest const& source_digest,
+ ArtifactDigest const& target_digest,
+ ObjectType obj_type,
+ StorageConfig const& source_config,
+ StorageConfig const& target_config,
+ bool from_git) noexcept -> std::optional<std::string> {
+ // write mapping
+ auto const rehash_id_file =
+ StorageUtils::GetRehashIDFile(source_config,
+ target_config.hash_function.GetType(),
+ source_digest.hash(),
+ from_git);
+ if (not StorageUtils::WriteTreeIDFile(
+ rehash_id_file,
+ Artifact::ObjectInfo{.digest = target_digest, .type = obj_type}
+ .ToString())) {
+ return fmt::format("failed to write rehash id to file {}",
+ rehash_id_file.string());
+ }
+ return std::nullopt; // a-ok
+}
+
+} // namespace MRApiUtils
diff --git a/src/buildtool/execution_api/serve/utils.hpp b/src/buildtool/execution_api/serve/utils.hpp
new file mode 100644
index 00000000..3b588cf3
--- /dev/null
+++ b/src/buildtool/execution_api/serve/utils.hpp
@@ -0,0 +1,64 @@
+// 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_SERVE_UTILS_HPP
+#define INCLUDED_SRC_BUILDTOOL_EXECUTION_API_SERVE_UTILS_HPP
+
+#include <optional>
+#include <string>
+
+#include "src/buildtool/common/artifact.hpp"
+#include "src/buildtool/common/artifact_digest.hpp"
+#include "src/buildtool/file_system/object_type.hpp"
+#include "src/buildtool/storage/config.hpp"
+#include "src/utils/cpp/expected.hpp"
+
+namespace MRApiUtils {
+
+/// \brief Get a corresponding known object from a different local CAS, as
+/// stored in a mapping file, if exists.
+/// \param digest Source digest.
+/// \param source_config Storage config corresponding to source digest.
+/// \param target_config Storage config corresponding to target digest.
+/// \param from_git Specify if source digest comes from a Git location instead
+/// of CAS.
+/// \returns The target artifact info on successfully reading an existing
+/// mapping file, nullopt if no mapping file exists, or the error message on
+/// failure.
+[[nodiscard]] auto ReadRehashedDigest(ArtifactDigest const& digest,
+ StorageConfig const& source_config,
+ StorageConfig const& target_config,
+ bool from_git = false) noexcept
+ -> expected<std::optional<Artifact::ObjectInfo>, std::string>;
+
+/// \brief Write the mapping file linking two digests hashing the same content.
+/// \param source_digest Source digest.
+/// \param target_digest Target digest.
+/// \param obj_type Object type of the content represented by the two digests.
+/// \param source_config Storage config corresponding to source digest.
+/// \param target_config Storage config corresponding to target digest.
+/// \param from_git Specify if source digest comes from a Git location instead
+/// of CAS.
+/// \returns nullopt on success, error message on failure.
+[[nodiscard]] auto StoreRehashedDigest(ArtifactDigest const& source_digest,
+ ArtifactDigest const& target_digest,
+ ObjectType obj_type,
+ StorageConfig const& source_config,
+ StorageConfig const& target_config,
+ bool from_git = false) noexcept
+ -> std::optional<std::string>;
+
+} // namespace MRApiUtils
+
+#endif // INCLUDED_SRC_BUILDTOOL_EXECUTION_API_SERVE_UTILS_HPP