|
| 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 | +#pragma once |
| 17 | + |
| 18 | +#include <functional> |
| 19 | +#include <optional> |
| 20 | +#include <ostream> |
| 21 | +#include <set> |
| 22 | +#include <string_view> |
| 23 | + |
| 24 | +#include "absl/strings/str_format.h" |
| 25 | +#include "fmt/ostream.h" |
| 26 | + |
| 27 | +#include "cuttlefish/common/libs/utils/result.h" |
| 28 | + |
| 29 | +namespace cuttlefish { |
| 30 | + |
| 31 | +/** |
| 32 | + * Represents an Android build, as defined by image files and some of the |
| 33 | + * metadata text files produced by the build system. |
| 34 | + * |
| 35 | + * The build system produces a subset of these files with faster `m` builds, and |
| 36 | + * does the complete set and packages them up into zip files in slower `m dist` |
| 37 | + * builds. The zip files are uploaded to the Android Build server, and the `cvd |
| 38 | + * fetch` tool can download subsets of them to the local filesystem. |
| 39 | + * |
| 40 | + * Image files also contain some duplication: the 'super' image contains logical |
| 41 | + * partitions that may also be present as standalone image files, depending on |
| 42 | + * the file subset available. |
| 43 | + * |
| 44 | + * Note the distinction between "images" and "partitions" in methods. Image |
| 45 | + * files may contain zero or more partitions. |
| 46 | + * |
| 47 | + * Instances of this class present a subset of the files produced by the build |
| 48 | + * system. This may be complete or incomplete subset. |
| 49 | + */ |
| 50 | +class AndroidBuild { |
| 51 | + public: |
| 52 | + virtual ~AndroidBuild() = default; |
| 53 | + |
| 54 | + /** |
| 55 | + * Image information, as reported by the Android build system. |
| 56 | + * |
| 57 | + * An image may be one of three different categories: |
| 58 | + * - A partition in the top-level GPT, such as the the `super` partition. |
| 59 | + * - A logical partition stored inside the GPT `super` partition. |
| 60 | + * - A `super_empty` pseudo-partition file that reports what should be in the |
| 61 | + * `super` partition, but without the logical partition contents. |
| 62 | + */ |
| 63 | + virtual Result<std::set<std::string, std::less<void>>> Images(); |
| 64 | + /* |
| 65 | + * A file on the host that represents an image. If the file is not already |
| 66 | + * stored in a distinct file on the host, it is first saved to `extract_dir` |
| 67 | + * and returned from there. If the file needs to be extracted and |
| 68 | + * `extract_dir` is not provided, returns an error. |
| 69 | + * |
| 70 | + * It's possible for there to be an image file in `Images()` that cannot be |
| 71 | + * extracted to the filesystem, if a metadata file reports that an image or |
| 72 | + * partition should exist, but it's not actually present anywhere. |
| 73 | + */ |
| 74 | + virtual Result<std::string> ImageFile( |
| 75 | + std::string_view name, std::optional<std::string_view> extract_dir = {}); |
| 76 | + |
| 77 | + virtual Result<std::set<std::string, std::less<void>>> AbPartitions(); |
| 78 | + |
| 79 | + /** |
| 80 | + * If this build is combined with another build by mixing system and vendor |
| 81 | + * from different places, reports which partitions this build expects to |
| 82 | + * contribute to a particular side of the mix. System and vendor partition |
| 83 | + * sets should be disjoint. |
| 84 | + */ |
| 85 | + virtual Result<std::set<std::string, std::less<void>>> SystemPartitions(); |
| 86 | + virtual Result<std::set<std::string, std::less<void>>> VendorPartitions(); |
| 87 | + |
| 88 | + /** Partitions in the super image. Disjoint from GPT entries. */ |
| 89 | + virtual Result<std::set<std::string, std::less<void>>> LogicalPartitions(); |
| 90 | + /** Entries in the GPT. Disjoint from logical partitions. */ |
| 91 | + virtual Result<std::set<std::string, std::less<void>>> PhysicalPartitions(); |
| 92 | + |
| 93 | + private: |
| 94 | + virtual std::ostream& Format(std::ostream&) const = 0; |
| 95 | + |
| 96 | + template <typename Sink> |
| 97 | + friend void AbslStringify(Sink& sink, const AndroidBuild& provider) { |
| 98 | + sink.Append(absl::FormatStreamed(provider)); |
| 99 | + } |
| 100 | + |
| 101 | + friend std::ostream& operator<<(std::ostream&, const AndroidBuild&); |
| 102 | +}; |
| 103 | + |
| 104 | +} // namespace cuttlefish |
| 105 | + |
| 106 | +namespace fmt { |
| 107 | + |
| 108 | +template <> |
| 109 | +struct formatter<::cuttlefish::AndroidBuild> : ostream_formatter {}; |
| 110 | + |
| 111 | +} // namespace fmt |
0 commit comments