Skip to content

Commit 6f28ea9

Browse files
committed
Use a declarative style to represent the disk partition layout
The next step is to add a map<string, ImageFile> argument which will be used to start moving file paths out of the config object, and avoid an excessive number of arguments to the function. Bug: b/470430427
1 parent 1781cb9 commit 6f28ea9

File tree

2 files changed

+85
-100
lines changed

2 files changed

+85
-100
lines changed

base/cvd/cuttlefish/host/commands/assemble_cvd/disk/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ cf_cc_library(
2929
"//cuttlefish/host/libs/config:cuttlefish_config",
3030
"//cuttlefish/host/libs/image_aggregator",
3131
"//libbase",
32+
"@abseil-cpp//absl/strings",
3233
],
3334
)
3435

base/cvd/cuttlefish/host/commands/assemble_cvd/disk/android_composite_disk_config.cc

Lines changed: 84 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
#include <string>
2323
#include <vector>
2424

25-
#include <android-base/strings.h>
25+
#include "absl/strings/str_cat.h"
26+
#include "android-base/strings.h"
2627

2728
#include "cuttlefish/common/libs/utils/files.h"
2829
#include "cuttlefish/common/libs/utils/result.h"
@@ -35,6 +36,22 @@
3536
namespace cuttlefish {
3637
namespace {
3738

39+
// Defined as constants to avoid typos in repeated names
40+
constexpr struct {
41+
std::string_view boot = "boot";
42+
std::string_view hibernation = "hibernation";
43+
std::string_view init_boot = "init_boot";
44+
std::string_view misc = "misc";
45+
std::string_view super = "super";
46+
std::string_view userdata = "userdata";
47+
std::string_view vbmeta = "vbmeta";
48+
std::string_view vbmeta_system = "vbmeta_system";
49+
std::string_view vbmeta_system_dlkm = "vbmeta_system_dlkm";
50+
std::string_view vbmeta_vendor_dlkm = "vbmeta_vendor_dlkm";
51+
std::string_view vendor_boot = "vendor_boot";
52+
std::string_view vvmtruststore = "vvmtruststore";
53+
} kPartitions;
54+
3855
std::optional<ImagePartition> HibernationImage(
3956
const SystemImageDirFlag& system_image_dir,
4057
const CuttlefishConfig::InstanceSpecific& instance) {
@@ -55,99 +72,74 @@ Result<std::vector<ImagePartition>> AndroidCompositeDiskConfig(
5572
const SystemImageDirFlag& system_image_dir) {
5673
std::vector<ImagePartition> partitions;
5774

58-
partitions.push_back(misc_image.Partition());
59-
partitions.push_back(ImagePartition{
60-
.label = "boot_a",
61-
.image_file_path = AbsolutePath(instance.new_boot_image()),
62-
});
63-
partitions.push_back(ImagePartition{
64-
.label = "boot_b",
65-
.image_file_path = AbsolutePath(instance.new_boot_image()),
66-
});
67-
const auto init_boot_path = instance.init_boot_image();
68-
if (FileExists(init_boot_path)) {
69-
partitions.push_back(ImagePartition{
70-
.label = "init_boot_a",
71-
.image_file_path = AbsolutePath(init_boot_path),
72-
});
73-
partitions.push_back(ImagePartition{
74-
.label = "init_boot_b",
75-
.image_file_path = AbsolutePath(init_boot_path),
76-
});
77-
}
78-
partitions.push_back(ImagePartition{
79-
.label = "vendor_boot_a",
80-
.image_file_path = AbsolutePath(instance.new_vendor_boot_image()),
81-
});
82-
partitions.push_back(ImagePartition{
83-
.label = "vendor_boot_b",
84-
.image_file_path = AbsolutePath(instance.new_vendor_boot_image()),
85-
});
86-
auto vbmeta_image = instance.new_vbmeta_image();
87-
if (!FileExists(vbmeta_image)) {
88-
vbmeta_image = instance.vbmeta_image();
89-
}
90-
partitions.push_back(ImagePartition{
91-
.label = "vbmeta_a",
92-
.image_file_path = AbsolutePath(vbmeta_image),
93-
});
94-
partitions.push_back(ImagePartition{
95-
.label = "vbmeta_b",
96-
.image_file_path = AbsolutePath(vbmeta_image),
97-
});
98-
partitions.push_back(ImagePartition{
99-
.label = "vbmeta_system_a",
100-
.image_file_path = AbsolutePath(instance.vbmeta_system_image()),
101-
});
102-
partitions.push_back(ImagePartition{
103-
.label = "vbmeta_system_b",
104-
.image_file_path = AbsolutePath(instance.vbmeta_system_image()),
105-
});
106-
auto vbmeta_vendor_dlkm_img = instance.new_vbmeta_vendor_dlkm_image();
107-
if (!FileExists(vbmeta_vendor_dlkm_img)) {
108-
vbmeta_vendor_dlkm_img = instance.vbmeta_vendor_dlkm_image();
109-
}
110-
if (FileExists(vbmeta_vendor_dlkm_img)) {
111-
partitions.push_back(ImagePartition{
112-
.label = "vbmeta_vendor_dlkm_a",
113-
.image_file_path = AbsolutePath(vbmeta_vendor_dlkm_img),
114-
});
115-
partitions.push_back(ImagePartition{
116-
.label = "vbmeta_vendor_dlkm_b",
117-
.image_file_path = AbsolutePath(vbmeta_vendor_dlkm_img),
118-
});
119-
}
120-
auto vbmeta_system_dlkm_img = instance.new_vbmeta_system_dlkm_image();
121-
if (!FileExists(vbmeta_system_dlkm_img)) {
122-
vbmeta_system_dlkm_img = instance.vbmeta_system_dlkm_image();
123-
}
124-
if (FileExists(vbmeta_system_dlkm_img)) {
125-
partitions.push_back(ImagePartition{
126-
.label = "vbmeta_system_dlkm_a",
127-
.image_file_path = AbsolutePath(vbmeta_system_dlkm_img),
128-
});
129-
partitions.push_back(ImagePartition{
130-
.label = "vbmeta_system_dlkm_b",
131-
.image_file_path = AbsolutePath(vbmeta_system_dlkm_img),
132-
});
133-
}
134-
auto super_image = instance.new_super_image();
135-
if (!FileExists(super_image)) {
136-
super_image = instance.super_image();
137-
}
138-
partitions.push_back(ImagePartition{
139-
.label = "super",
140-
.image_file_path = AbsolutePath(super_image),
141-
});
142-
auto data_image = instance.new_data_image();
143-
if (!FileExists(data_image)) {
144-
data_image = instance.data_image();
75+
const std::set<std::string_view> ab_partitions = {
76+
kPartitions.boot,
77+
kPartitions.init_boot,
78+
kPartitions.vbmeta,
79+
kPartitions.vbmeta_system,
80+
kPartitions.vbmeta_system_dlkm,
81+
kPartitions.vbmeta_vendor_dlkm,
82+
kPartitions.vendor_boot,
83+
};
84+
85+
const std::set<std::string_view> optional_partitions = {
86+
kPartitions.init_boot, kPartitions.vbmeta_vendor_dlkm,
87+
kPartitions.vbmeta_system_dlkm, kPartitions.hibernation,
88+
kPartitions.vvmtruststore,
89+
};
90+
91+
const std::map<std::string_view, std::string> primary_paths = {
92+
{kPartitions.boot, instance.new_boot_image()},
93+
{kPartitions.init_boot, instance.init_boot_image()},
94+
{kPartitions.super, instance.new_super_image()},
95+
{kPartitions.userdata, instance.new_data_image()},
96+
{kPartitions.vbmeta, instance.new_vbmeta_image()},
97+
{kPartitions.vbmeta_system, instance.vbmeta_system_image()},
98+
{kPartitions.vbmeta_system_dlkm, instance.new_vbmeta_system_dlkm_image()},
99+
{kPartitions.vbmeta_vendor_dlkm, instance.new_vbmeta_vendor_dlkm_image()},
100+
{kPartitions.vendor_boot, instance.new_vendor_boot_image()},
101+
{kPartitions.vvmtruststore, instance.vvmtruststore_path()},
102+
};
103+
104+
const std::map<std::string_view, std::string> fallback_paths = {
105+
{kPartitions.super, instance.super_image()},
106+
{kPartitions.vbmeta, instance.vbmeta_image()},
107+
{kPartitions.vbmeta_vendor_dlkm, instance.vbmeta_vendor_dlkm_image()},
108+
{kPartitions.vbmeta_system_dlkm, instance.vbmeta_system_dlkm_image()},
109+
{kPartitions.userdata, instance.data_image()},
110+
};
111+
112+
for (const auto& [partition, path] : primary_paths) {
113+
std::string path_used;
114+
if (FileExists(path)) {
115+
path_used = path;
116+
} else if (auto it = fallback_paths.find(partition);
117+
it != fallback_paths.end() && FileExists(it->second)) {
118+
path_used = it->second;
119+
} else if (optional_partitions.count(partition)) {
120+
continue;
121+
} else {
122+
return CF_ERRF("Could not find file for partition '{}'", partition);
123+
}
124+
125+
if (ab_partitions.count(partition)) {
126+
partitions.push_back(ImagePartition{
127+
.label = absl::StrCat(partition, "_a"),
128+
.image_file_path = AbsolutePath(path_used),
129+
});
130+
partitions.push_back(ImagePartition{
131+
.label = absl::StrCat(partition, "_b"),
132+
.image_file_path = AbsolutePath(path_used),
133+
});
134+
} else {
135+
partitions.push_back(ImagePartition{
136+
.label = std::string(partition),
137+
.image_file_path = AbsolutePath(path_used),
138+
});
139+
}
145140
}
146-
partitions.push_back(ImagePartition{
147-
.label = "userdata",
148-
.image_file_path = AbsolutePath(data_image),
149-
});
150141

142+
partitions.push_back(misc_image.Partition());
151143
partitions.push_back(metadata_image.Partition());
152144

153145
std::optional<ImagePartition> hibernation_partition =
@@ -156,14 +148,6 @@ Result<std::vector<ImagePartition>> AndroidCompositeDiskConfig(
156148
partitions.push_back(std::move(*hibernation_partition));
157149
}
158150

159-
const auto vvmtruststore_path = instance.vvmtruststore_path();
160-
if (!vvmtruststore_path.empty()) {
161-
partitions.push_back(ImagePartition{
162-
.label = "vvmtruststore",
163-
.image_file_path = AbsolutePath(vvmtruststore_path),
164-
});
165-
}
166-
167151
const auto custom_partition_path = instance.custom_partition_path();
168152
if (!custom_partition_path.empty()) {
169153
auto custom_partition_paths =

0 commit comments

Comments
 (0)