summaryrefslogtreecommitdiff
path: root/src/other_tools/utils
diff options
context:
space:
mode:
authorPaul Cristian Sarbu <paul.cristian.sarbu@huawei.com>2023-09-22 15:56:32 +0200
committerPaul Cristian Sarbu <paul.cristian.sarbu@huawei.com>2023-11-02 12:06:50 +0100
commitc8724482c2f06d88947f83a16935445b7561afd1 (patch)
treea33e7044390d902a2c98a735c442070d890e9305 /src/other_tools/utils
parentdab6ee07fe6d4523d7fb0f6b352e23a608721b56 (diff)
downloadjustbuild-c8724482c2f06d88947f83a16935445b7561afd1.tar.gz
Move content archive helper functions in own library
Diffstat (limited to 'src/other_tools/utils')
-rw-r--r--src/other_tools/utils/TARGETS11
-rw-r--r--src/other_tools/utils/content.hpp49
2 files changed, 60 insertions, 0 deletions
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 <optional>
+#include <string>
+
+#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<std::string> {
+ 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 <Hasher::HashType type>
+[[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