From c8724482c2f06d88947f83a16935445b7561afd1 Mon Sep 17 00:00:00 2001 From: Paul Cristian Sarbu Date: Fri, 22 Sep 2023 15:56:32 +0200 Subject: Move content archive helper functions in own library --- src/other_tools/utils/TARGETS | 11 +++++++++ src/other_tools/utils/content.hpp | 49 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 src/other_tools/utils/content.hpp (limited to 'src/other_tools/utils') diff --git a/src/other_tools/utils/TARGETS b/src/other_tools/utils/TARGETS index df08d551..35685ba7 100644 --- a/src/other_tools/utils/TARGETS +++ b/src/other_tools/utils/TARGETS @@ -28,4 +28,15 @@ , "stage": ["src", "other_tools", "utils"] , "private-deps": [["src/buildtool/logging", "logging"], ["", "libcurl"]] } +, "content": + { "type": ["@", "rules", "CC", "library"] + , "name": ["content"] + , "hdrs": ["content.hpp"] + , "deps": + [ "curl_easy_handle" + , ["src/buildtool/common", "user_structs"] + , ["src/buildtool/crypto", "hasher"] + ] + , "stage": ["src", "other_tools", "utils"] + } } diff --git a/src/other_tools/utils/content.hpp b/src/other_tools/utils/content.hpp new file mode 100644 index 00000000..e7cb7501 --- /dev/null +++ b/src/other_tools/utils/content.hpp @@ -0,0 +1,49 @@ +// Copyright 2023 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_OTHER_TOOLS_UTILS_CONTENT_HPP +#define INCLUDED_SRC_OTHER_TOOLS_UTILS_CONTENT_HPP + +#include +#include + +#include "src/buildtool/common/user_structs.hpp" +#include "src/buildtool/crypto/hasher.hpp" +#include "src/other_tools/utils/curl_easy_handle.hpp" + +// Utilities related to the content of an archive + +/// \brief Fetches a file from the internet and stores its content in memory. +/// Returns the content. +[[nodiscard]] static auto NetworkFetch(std::string const& fetch_url, + CAInfoPtr const& ca_info) noexcept + -> std::optional { + auto curl_handle = + CurlEasyHandle::Create(ca_info->no_ssl_verify, ca_info->ca_bundle); + if (not curl_handle) { + return std::nullopt; + } + return curl_handle->DownloadToString(fetch_url); +} + +template +[[nodiscard]] static auto GetContentHash(std::string const& data) noexcept + -> std::string { + Hasher hasher{type}; + hasher.Update(data); + auto digest = std::move(hasher).Finalize(); + return digest.HexString(); +} + +#endif // INCLUDED_SRC_OTHER_TOOLS_UTILS_CONTENT_HPP -- cgit v1.2.3