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
29
30namespace cuttlefish {
31
32inline constexpr char kKeyDownloaderPath[] = "downloader-path";
33inline constexpr char kKeyFlags[] = "flags";
34
35inline constexpr char kFlagDigest[] = "digest";
36inline constexpr char kFlagDir[] = "dir";
37inline constexpr char kFlagDumpJson[] = "dump-json";
38inline constexpr char kFlagDisableCache[] = "disable-cache";
39inline constexpr char kFlagCasInstance[] = "cas-instance";
40inline constexpr char kFlagCasAddr[] = "cas-addr";
41inline constexpr char kFlagServiceAccountJson[] = "service-account-json";
42inline constexpr char kFlagUseAdc[] = "use-adc";
43
44// Identifies an artifact in CAS.
45// The digest of an artifact is unique in a CAS instance. To identify the CAS
46// instance, the cas_instance and cas_addr are required. An artifact can only be
47// downloaded from the CAS instance it is uploaded to. This info is available in
48// cas_digests.json from AB.
50 std::string cas_instance;
51 std::string cas_addr;
52 std::string digest;
53 // The actual filename in CAS, can be different from the the artifact_name.
54 std::string filename;
55};
56
57// A callback function provided by the caller of CasDownloader::DownloadFile to
58// fetch digests or other artifacts not available on cas. The callback function
59// takes the path of the artifact on AB and returns the local path of the
60// downloaded file.
61using DigestsFetcher = std::function<Result<std::string>(std::string)>;
62
63// A c++ wrapper for the CAS downloader binary.
64// Example:
65// std::unique_ptr<CasDownloader> casdownloader =
66// CF_EXPECT(CasDownloader::Create(cas_downloader_flags,
67// service_account_filepath),
68// "Failed to create CasDownloader.");
69// CF_EXPECT(casClient->DownloadFile(device_build, artifact_name,
70// target_dir, digests_fetcher),
71// "Failed to download file from CAS.");
72//
74 public:
75 CasDownloader(std::string downloader_path, std::vector<std::string> flags,
76 bool prefer_uncompressed = false);
78 const CasDownloaderFlags& cas_downloader_flags,
79 const std::string& service_account_filepath);
80 virtual ~CasDownloader() = default;
82 const DeviceBuild& build, const std::string& artifact_name,
83 const std::string& target_directory,
84 const DigestsFetcher& digests_fetcher,
85 const std::optional<std::string>& stats_filepath = std::nullopt);
86
87 private:
89 const CasDownloaderFlags& cas_downloader_flags,
90 const std::string& service_account_filepath);
91
92 Result<CasIdentifier> GetCasIdentifier(const std::string& build_id,
93 const std::string& build_target,
94 const std::string& artifact_name,
95 const DigestsFetcher& digests_fetcher);
96
97 std::string downloader_path_;
98 std::vector<std::string> flags_;
100 std::string build_desc_; // e.g. "build_id:build_target"
101 Json::Value cas_digests_;
102};
103
104} // namespace cuttlefish
Definition: cas_downloader.h:73
static Result< std::unique_ptr< CasDownloader > > Create(const CasDownloaderFlags &cas_downloader_flags, const std::string &service_account_filepath)
Definition: cas_downloader.cpp:291
Json::Value cas_digests_
Definition: cas_downloader.h:101
virtual ~CasDownloader()=default
static Result< std::unique_ptr< CasDownloader > > CreateImpl(const CasDownloaderFlags &cas_downloader_flags, const std::string &service_account_filepath)
Definition: cas_downloader.cpp:306
bool prefer_uncompressed_
Definition: cas_downloader.h:99
std::string downloader_path_
Definition: cas_downloader.h:97
std::string build_desc_
Definition: cas_downloader.h:100
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:449
virtual Result< void > DownloadFile(const DeviceBuild &build, 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:415
std::vector< std::string > flags_
Definition: cas_downloader.h:98
CasDownloader(std::string downloader_path, std::vector< std::string > flags, bool prefer_uncompressed=false)
Definition: cas_downloader.cpp:408
Definition: alloc_driver.h:20
constexpr char kFlagDumpJson[]
Definition: cas_downloader.h:37
constexpr char kKeyDownloaderPath[]
Definition: cas_downloader.h:32
constexpr char kFlagDigest[]
Definition: cas_downloader.h:35
constexpr char kFlagCasInstance[]
Definition: cas_downloader.h:39
constexpr char kFlagDisableCache[]
Definition: cas_downloader.h:38
constexpr char kFlagUseAdc[]
Definition: cas_downloader.h:42
constexpr char kFlagCasAddr[]
Definition: cas_downloader.h:40
constexpr char kFlagDir[]
Definition: cas_downloader.h:36
constexpr char kKeyFlags[]
Definition: cas_downloader.h:33
constexpr char kFlagServiceAccountJson[]
Definition: cas_downloader.h:41
tl::expected< T, StackTraceError > Result
Definition: result_type.h:30
std::function< Result< std::string >(std::string)> DigestsFetcher
Definition: cas_downloader.h:61
Definition: cas_flags.h:71
Definition: cas_downloader.h:49
std::string filename
Definition: cas_downloader.h:54
std::string cas_instance
Definition: cas_downloader.h:50
std::string digest
Definition: cas_downloader.h:52
std::string cas_addr
Definition: cas_downloader.h:51
Definition: android_build.h:28