|
| 1 | +// |
| 2 | +// Copyright (C) 2025 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 | +#include "cuttlefish/host/commands/assemble_cvd/android_build/img_zip.h" |
| 17 | + |
| 18 | +#include <functional> |
| 19 | +#include <map> |
| 20 | +#include <memory> |
| 21 | +#include <optional> |
| 22 | +#include <ostream> |
| 23 | +#include <set> |
| 24 | +#include <string> |
| 25 | +#include <string_view> |
| 26 | +#include <utility> |
| 27 | + |
| 28 | +#include "absl/strings/str_cat.h" |
| 29 | +#include "absl/strings/strip.h" |
| 30 | +#include "fmt/ostream.h" |
| 31 | +#include "google/protobuf/text_format.h" |
| 32 | + |
| 33 | +#include "cuttlefish/common/libs/key_equals_value/key_equals_value.h" |
| 34 | +#include "cuttlefish/common/libs/utils/result.h" |
| 35 | +#include "cuttlefish/host/commands/assemble_cvd/android_build/android_build.h" |
| 36 | +#include "cuttlefish/host/commands/assemble_cvd/android_build/find_build_archive.h" |
| 37 | +#include "cuttlefish/host/commands/assemble_cvd/proto/guest_config.pb.h" |
| 38 | +#include "cuttlefish/host/libs/config/build_archive.h" |
| 39 | +#include "cuttlefish/host/libs/config/fetcher_config.h" |
| 40 | +#include "cuttlefish/host/libs/config/file_source.h" |
| 41 | + |
| 42 | +namespace cuttlefish { |
| 43 | +namespace { |
| 44 | + |
| 45 | +static constexpr std::string_view kImgMatch = "-img-"; |
| 46 | +static constexpr std::string_view kImgSuffix = ".img"; |
| 47 | + |
| 48 | +class ImgZipImpl : public AndroidBuild { |
| 49 | + public: |
| 50 | + static Result<std::unique_ptr<ImgZipImpl>> FromBuildArchive( |
| 51 | + BuildArchive archive) { |
| 52 | + std::unique_ptr<ImgZipImpl> img_zip(new ImgZipImpl(std::move(archive))); |
| 53 | + |
| 54 | + CF_EXPECT(img_zip->Images()); |
| 55 | + |
| 56 | + return std::move(img_zip); |
| 57 | + } |
| 58 | + |
| 59 | + Result<std::set<std::string, std::less<void>>> Images() override { |
| 60 | + std::set<std::string, std::less<void>> partitions; |
| 61 | + for (std::string_view member : archive_.Members()) { |
| 62 | + if (!absl::ConsumeSuffix(&member, kImgSuffix)) { |
| 63 | + continue; |
| 64 | + } |
| 65 | + absl::ConsumePrefix(&member, "/"); |
| 66 | + partitions.emplace(member); |
| 67 | + } |
| 68 | + return partitions; |
| 69 | + } |
| 70 | + |
| 71 | + Result<std::string> ImageFile( |
| 72 | + std::string_view name, |
| 73 | + std::optional<std::string_view> extract_dir) override { |
| 74 | + std::string member_name = absl::StrCat(name, kImgSuffix); |
| 75 | + return CF_EXPECT(archive_.MemberFilepath(member_name, extract_dir)); |
| 76 | + } |
| 77 | + |
| 78 | + // TODO: schuffelen - put in AndroidBuild |
| 79 | + Result<std::map<std::string, std::string>> AndroidInfoTxt() { |
| 80 | + std::string contents = |
| 81 | + CF_EXPECT(archive_.MemberContents("android-info.txt")); |
| 82 | + return CF_EXPECT(ParseKeyEqualsValue(contents)); |
| 83 | + } |
| 84 | + |
| 85 | + // TODO: schuffelen - put in AndroidBuild |
| 86 | + Result<config::GuestConfigFile> GuestConfigProto() { |
| 87 | + std::string contents = |
| 88 | + CF_EXPECT(archive_.MemberContents("cuttlefish-guest-config.txtpb")); |
| 89 | + |
| 90 | + config::GuestConfigFile proto_config; |
| 91 | + CF_EXPECT( |
| 92 | + google::protobuf::TextFormat::ParseFromString(contents, &proto_config)); |
| 93 | + |
| 94 | + return proto_config; |
| 95 | + } |
| 96 | + |
| 97 | + private: |
| 98 | + ImgZipImpl(BuildArchive archive) : archive_(std::move(archive)) {} |
| 99 | + |
| 100 | + std::ostream& Format(std::ostream& out) const override { |
| 101 | + fmt::print(out, "ImgZip {{ {} }}", archive_); |
| 102 | + return out; |
| 103 | + } |
| 104 | + |
| 105 | + BuildArchive archive_; |
| 106 | +}; |
| 107 | + |
| 108 | +} // namespace |
| 109 | + |
| 110 | +Result<std::unique_ptr<AndroidBuild>> ImgZip(const FetcherConfig& config, |
| 111 | + FileSource source) { |
| 112 | + BuildArchive archive = CF_EXPECT(FindBuildArchive(config, source, kImgMatch)); |
| 113 | + return CF_EXPECT(ImgZipImpl::FromBuildArchive(std::move(archive))); |
| 114 | +} |
| 115 | + |
| 116 | +Result<std::unique_ptr<AndroidBuild>> ImgZip(const std::string& path) { |
| 117 | + BuildArchive archive = CF_EXPECT(FindBuildArchive(path, kImgMatch)); |
| 118 | + return CF_EXPECT(ImgZipImpl::FromBuildArchive(std::move(archive))); |
| 119 | +} |
| 120 | + |
| 121 | +} // namespace cuttlefish |
0 commit comments