|
| 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/misc_info_metadata.h" |
| 17 | + |
| 18 | +#include <functional> |
| 19 | +#include <map> |
| 20 | +#include <memory> |
| 21 | +#include <ostream> |
| 22 | +#include <set> |
| 23 | +#include <string> |
| 24 | +#include <string_view> |
| 25 | +#include <utility> |
| 26 | + |
| 27 | +#include "absl/strings/match.h" |
| 28 | +#include "absl/strings/str_cat.h" |
| 29 | +#include "absl/strings/str_split.h" |
| 30 | +#include "fmt/ostream.h" |
| 31 | + |
| 32 | +#include "cuttlefish/host/commands/assemble_cvd/android_build/android_build.h" |
| 33 | +#include "cuttlefish/result/result.h" |
| 34 | + |
| 35 | +namespace cuttlefish { |
| 36 | +namespace { |
| 37 | + |
| 38 | +class MetadataFromMiscInfo : public AndroidBuild { |
| 39 | + public: |
| 40 | + MetadataFromMiscInfo(std::map<std::string, std::string> misc_info) |
| 41 | + : misc_info_(std::move(misc_info)) {} |
| 42 | + |
| 43 | + Result<std::set<std::string, std::less<void>>> SystemPartitions() override { |
| 44 | + return CF_EXPECT(PartitionsMatchingGroup("system")); |
| 45 | + } |
| 46 | + |
| 47 | + Result<std::set<std::string, std::less<void>>> VendorPartitions() override { |
| 48 | + return CF_EXPECT(PartitionsMatchingGroup("vendor")); |
| 49 | + } |
| 50 | + |
| 51 | + Result<std::set<std::string, std::less<void>>> LogicalPartitions() override { |
| 52 | + std::set<std::string, std::less<void>> partitions; |
| 53 | + partitions.merge(CF_EXPECT(SystemPartitions())); |
| 54 | + partitions.merge(CF_EXPECT(VendorPartitions())); |
| 55 | + return partitions; |
| 56 | + } |
| 57 | + |
| 58 | + private: |
| 59 | + Result<std::set<std::string, std::less<void>>> PartitionsMatchingGroup( |
| 60 | + std::string_view match) { |
| 61 | + std::string groups_key = "super_partition_groups"; |
| 62 | + auto groups_it = misc_info_.find(groups_key); |
| 63 | + CF_EXPECTF(groups_it != misc_info_.end(), "Could not find entry for '{}'", |
| 64 | + groups_key); |
| 65 | + |
| 66 | + std::string_view matching_group; |
| 67 | + for (std::string_view group : |
| 68 | + absl::StrSplit(groups_it->second, " ", absl::SkipEmpty())) { |
| 69 | + if (absl::StrContains(group, match)) { |
| 70 | + matching_group = group; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + CF_EXPECTF(!matching_group.empty(), "No '{}' group", match); |
| 75 | + |
| 76 | + std::string key = absl::StrCat("super_", matching_group, "_partition_list"); |
| 77 | + |
| 78 | + auto group_it = misc_info_.find(key); |
| 79 | + CF_EXPECTF(group_it != misc_info_.end(), "Could not find entry for '{}'", |
| 80 | + key); |
| 81 | + |
| 82 | + return absl::StrSplit(group_it->second, " ", absl::SkipEmpty()); |
| 83 | + } |
| 84 | + |
| 85 | + std::ostream& Format(std::ostream& out) const override { |
| 86 | + out << "MetadataFromMiscInfo { "; |
| 87 | + for (auto& [key, value] : misc_info_) { |
| 88 | + fmt::print(out, "'{}' => '{}', ", key, value); |
| 89 | + } |
| 90 | + return out << "}"; |
| 91 | + } |
| 92 | + |
| 93 | + std::map<std::string, std::string> misc_info_; |
| 94 | +}; |
| 95 | + |
| 96 | +} // namespace |
| 97 | + |
| 98 | +Result<std::unique_ptr<AndroidBuild>> AndroidBuildFromMiscInfo( |
| 99 | + std::map<std::string, std::string> misc_info) { |
| 100 | + auto build = std::make_unique<MetadataFromMiscInfo>(std::move(misc_info)); |
| 101 | + |
| 102 | + std::set<std::string, std::less<void>> system = |
| 103 | + CF_EXPECT(build->SystemPartitions()); |
| 104 | + CF_EXPECT(!system.empty()); |
| 105 | + |
| 106 | + std::set<std::string, std::less<void>> vendor = |
| 107 | + CF_EXPECT(build->VendorPartitions()); |
| 108 | + CF_EXPECT(!vendor.empty()); |
| 109 | + |
| 110 | + return build; |
| 111 | +} |
| 112 | + |
| 113 | +} // namespace cuttlefish |
0 commit comments