Android-cuttlefish cvd tool
cas_downloader.h
Go to the documentation of this file.
1//
2// Copyright (C) 2024 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16#pragma once
17
18#include <functional>
19#include <memory>
20#include <optional>
21#include <string>
22#include <vector>
23
24#include <json/value.h>
25
28
29namespace cuttlefish {
30
31inline constexpr char kKeyDownloaderPath[] = "downloader-path";
32inline constexpr char kKeyFlags[] = "flags";
33
34inline constexpr char kFlagDigest[] = "digest";
35inline constexpr char kFlagDir[] = "dir";
36inline constexpr char kFlagDumpJson[] = "dump-json";
37inline constexpr char kFlagDisableCache[] = "disable-cache";
38inline constexpr char kFlagCasInstance[] = "cas-instance";
39inline constexpr char kFlagCasAddr[] = "cas-addr";
40inline constexpr char kFlagServiceAccountJson[] = "service-account-json";
41inline constexpr char kFlagUseAdc[] = "use-adc";
42
43// Identifies an artifact in CAS.
44// The digest of an artifact is unique in a CAS instance. To identify the CAS
45// instance, the cas_instance and cas_addr are required. An artifact can only be
46// downloaded from the CAS instance it is uploaded to. This info is available in
47// cas_digests.json from AB.
49 std::string cas_instance;
50 std::string cas_addr;
51 std::string digest;
52 // The actual filename in CAS, can be different from the the artifact_name.
53 std::string filename;
54};
55
56// A callback function provided by the caller of CasDownloader::DownloadFile to
57// fetch digests or other artifacts not available on cas. The callback function
58// takes the path of the artifact on AB and returns the local path of the
59// downloaded file.
60using DigestsFetcher = std::function<Result<std::string>(std::string)>;
61
62// A c++ wrapper for the CAS downloader binary.
63// Example:
64// std::unique_ptr<CasDownloader> casdownloader =
65// CF_EXPECT(CasDownloader::Create(cas_downloader_flags,
66// service_account_filepath),
67// "Failed to create CasDownloader.");
68// CF_EXPECT(casClient->DownloadFile(build_id, build_target, artifact_name,
69// target_dir, digests_fetcher),
70// "Failed to download file from CAS.");
71//
73 public:
74 CasDownloader(std::string downloader_path, std::vector<std::string> flags,
75 bool prefer_uncompressed = false);
77 const CasDownloaderFlags& cas_downloader_flags,
78 const std::string& service_account_filepath);
79 virtual ~CasDownloader() = default;
81 const std::string& build_id, const std::string& build_target,
82 const std::string& artifact_name, const std::string& target_directory,
83 const DigestsFetcher& digests_fetcher,
84 const std::optional<std::string>& stats_filepath = std::nullopt);
85
86 private:
87 Result<CasIdentifier> GetCasIdentifier(const std::string& build_id,
88 const std::string& build_target,
89 const std::string& artifact_name,
90 const DigestsFetcher& digests_fetcher);
91
92 std::string downloader_path_;
93 std::vector<std::string> flags_;
95 std::string build_desc_; // e.g. "build_id:build_target"
96 Json::Value cas_digests_;
97};
98
99} // namespace cuttlefish
Definition: expected.h:86
Definition: cas_downloader.h:72
static Result< std::unique_ptr< CasDownloader > > Create(const CasDownloaderFlags &cas_downloader_flags, const std::string &service_account_filepath)
Definition: cas_downloader.cpp:194
Json::Value cas_digests_
Definition: cas_downloader.h:96
virtual ~CasDownloader()=default
bool prefer_uncompressed_
Definition: cas_downloader.h:94
std::string downloader_path_
Definition: cas_downloader.h:92
std::string build_desc_
Definition: cas_downloader.h:95
Result< CasIdentifier > GetCasIdentifier(const std::string &build_id, const std::string &build_target, const std::string &artifact_name, const DigestsFetcher &digests_fetcher)
Definition: cas_downloader.cpp:268
std::vector< std::string > flags_
Definition: cas_downloader.h:93
CasDownloader(std::string downloader_path, std::vector< std::string > flags, bool prefer_uncompressed=false)
Definition: cas_downloader.cpp:230
virtual Result< void > DownloadFile(const std::string &build_id, const std::string &build_target, const std::string &artifact_name, const std::string &target_directory, const DigestsFetcher &digests_fetcher, const std::optional< std::string > &stats_filepath=std::nullopt)
Definition: cas_downloader.cpp:237
Definition: alloc_utils.cpp:23
constexpr char kFlagDumpJson[]
Definition: cas_downloader.h:36
constexpr char kKeyDownloaderPath[]
Definition: cas_downloader.h:31
constexpr char kFlagDigest[]
Definition: cas_downloader.h:34
constexpr char kFlagCasInstance[]
Definition: cas_downloader.h:38
constexpr char kFlagDisableCache[]
Definition: cas_downloader.h:37
constexpr char kFlagUseAdc[]
Definition: cas_downloader.h:41
constexpr char kFlagCasAddr[]
Definition: cas_downloader.h:39
constexpr char kFlagDir[]
Definition: cas_downloader.h:35
constexpr char kKeyFlags[]
Definition: cas_downloader.h:32
constexpr char kFlagServiceAccountJson[]
Definition: cas_downloader.h:40
std::function< Result< std::string >(std::string)> DigestsFetcher
Definition: cas_downloader.h:60
Definition: cas_flags.h:36
Definition: cas_downloader.h:48
std::string filename
Definition: cas_downloader.h:53
std::string cas_instance
Definition: cas_downloader.h:49
std::string digest
Definition: cas_downloader.h:51
std::string cas_addr
Definition: cas_downloader.h:50