diff options
author | Oliver Reiche <oliver.reiche@huawei.com> | 2022-07-29 14:47:50 +0200 |
---|---|---|
committer | Sascha Roloff <sascha.roloff@huawei.com> | 2022-08-05 14:41:31 +0200 |
commit | baa073d46cae264183f576c0acae8df57f42e759 (patch) | |
tree | a6cc74f0ddf0dbe319b13cfb0b0edc400534a2f2 /src/buildtool/main/install_cas.cpp | |
parent | 406ee1b8c0f6c2ca3e3cd1281eee6dd59e473a68 (diff) | |
download | justbuild-baa073d46cae264183f576c0acae8df57f42e759.tar.gz |
InstallCas: Moved install-cas code to separate library
Diffstat (limited to 'src/buildtool/main/install_cas.cpp')
-rw-r--r-- | src/buildtool/main/install_cas.cpp | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/src/buildtool/main/install_cas.cpp b/src/buildtool/main/install_cas.cpp new file mode 100644 index 00000000..dbe3b5ec --- /dev/null +++ b/src/buildtool/main/install_cas.cpp @@ -0,0 +1,62 @@ +#include "src/buildtool/main/install_cas.hpp" + +#include "src/buildtool/compatibility/compatibility.hpp" +#include "src/buildtool/crypto/hash_function.hpp" +#include "src/buildtool/execution_api/remote/config.hpp" + +auto ObjectInfoFromLiberalString(std::string const& s) noexcept + -> Artifact::ObjectInfo { + std::istringstream iss(s); + std::string id{}; + std::string size_str{"0"}; + std::string type{"f"}; + if (iss.peek() == '[') { + (void)iss.get(); + } + std::getline(iss, id, ':'); + if (not iss.eof()) { + std::getline(iss, size_str, ':'); + } + if (not iss.eof()) { + std::getline(iss, type, ']'); + } + auto size = static_cast<std::size_t>( + size_str.empty() ? 0 : std::atol(size_str.c_str())); + auto const& object_type = FromChar(*type.c_str()); + return Artifact::ObjectInfo{ + ArtifactDigest{id, size, IsTreeObject(object_type)}, object_type}; +} + +#ifndef BOOTSTRAP_BUILD_TOOL +auto FetchAndInstallArtifacts(gsl::not_null<IExecutionApi*> const& api, + FetchArguments const& clargs) -> bool { + auto object_info = ObjectInfoFromLiberalString(clargs.object_id); + + if (clargs.output_path) { + auto output_path = (*clargs.output_path / "").parent_path(); + if (FileSystemManager::IsDirectory(output_path)) { + output_path /= object_info.digest.hash(); + } + + if (not FileSystemManager::CreateDirectory(output_path.parent_path()) or + not api->RetrieveToPaths({object_info}, {output_path})) { + Logger::Log(LogLevel::Error, "failed to retrieve artifact."); + return false; + } + + Logger::Log(LogLevel::Info, + "artifact {} was installed to {}", + object_info.ToString(), + output_path.string()); + } + else { // dump to stdout + if (not api->RetrieveToFds( + {object_info}, {dup(fileno(stdout))}, clargs.raw_tree)) { + Logger::Log(LogLevel::Error, "failed to dump artifact."); + return false; + } + } + + return true; +} +#endif |