Skip to content

Commit fe4eb7c

Browse files
committed
Extract ImageFile creation to a common location.
This code was duplicated. Bug: b/472532039
1 parent 8d6c354 commit fe4eb7c

File tree

5 files changed

+110
-17
lines changed

5 files changed

+110
-17
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ cf_cc_binary(
3737
"//cuttlefish/host/commands/assemble_cvd:flag_feature",
3838
"//cuttlefish/host/commands/assemble_cvd:flags",
3939
"//cuttlefish/host/commands/assemble_cvd:flags_defaults",
40+
"//cuttlefish/host/commands/assemble_cvd:instance_image_files",
4041
"//cuttlefish/host/commands/assemble_cvd:resolve_instance_files",
4142
"//cuttlefish/host/commands/assemble_cvd:touchpad",
4243
"//cuttlefish/host/commands/assemble_cvd/disk:ap_composite_disk",
@@ -151,6 +152,7 @@ cf_cc_library(
151152
"//cuttlefish/host/commands/assemble_cvd:boot_image_utils",
152153
"//cuttlefish/host/commands/assemble_cvd:disk_builder",
153154
"//cuttlefish/host/commands/assemble_cvd:flags_defaults",
155+
"//cuttlefish/host/commands/assemble_cvd:instance_image_files",
154156
"//cuttlefish/host/commands/assemble_cvd:super_image_mixer",
155157
"//cuttlefish/host/commands/assemble_cvd/disk:access_kregistry",
156158
"//cuttlefish/host/commands/assemble_cvd/disk:ap_composite_disk",
@@ -164,8 +166,6 @@ cf_cc_library(
164166
"//cuttlefish/host/commands/assemble_cvd/disk:image_file",
165167
"//cuttlefish/host/commands/assemble_cvd/disk:initialize_instance_composite_disk",
166168
"//cuttlefish/host/commands/assemble_cvd/disk:kernel_ramdisk_repacker",
167-
"//cuttlefish/host/commands/assemble_cvd/disk:metadata_image",
168-
"//cuttlefish/host/commands/assemble_cvd/disk:misc_image",
169169
"//cuttlefish/host/commands/assemble_cvd/disk:os_composite_disk",
170170
"//cuttlefish/host/commands/assemble_cvd/disk:pflash",
171171
"//cuttlefish/host/commands/assemble_cvd/disk:pstore",
@@ -410,6 +410,18 @@ cc_proto_library(
410410
deps = [":guest_config_proto"],
411411
)
412412

413+
cf_cc_library(
414+
name = "instance_image_files",
415+
srcs = ["instance_image_files.cc"],
416+
hdrs = ["instance_image_files.h"],
417+
deps = [
418+
"//cuttlefish/host/commands/assemble_cvd/disk:image_file",
419+
"//cuttlefish/host/commands/assemble_cvd/disk:metadata_image",
420+
"//cuttlefish/host/commands/assemble_cvd/disk:misc_image",
421+
"//cuttlefish/host/libs/config:cuttlefish_config",
422+
],
423+
)
424+
413425
cf_cc_library(
414426
name = "misc_info",
415427
srcs = ["misc_info.cc"],

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#include "cuttlefish/host/commands/assemble_cvd/flags/vendor_boot_image.h"
5353
#include "cuttlefish/host/commands/assemble_cvd/flags/vm_manager.h"
5454
#include "cuttlefish/host/commands/assemble_cvd/flags_defaults.h"
55+
#include "cuttlefish/host/commands/assemble_cvd/instance_image_files.h"
5556
#include "cuttlefish/host/commands/assemble_cvd/resolve_instance_files.h"
5657
#include "cuttlefish/host/commands/assemble_cvd/touchpad.h"
5758
#include "cuttlefish/host/libs/command_util/snapshot_utils.h"
@@ -338,17 +339,22 @@ Result<const CuttlefishConfig*> InitFilesystemAndCreateConfig(
338339
bool creating_os_disk = false;
339340
// if any device needs to rebuild its composite disk,
340341
// then don't preserve any files and delete everything.
341-
for (const auto& instance : config.Instances()) {
342-
std::vector<std::unique_ptr<ImageFile>> image_files;
343342

344-
image_files.emplace_back(std::make_unique<MetadataImage>(instance));
345-
image_files.emplace_back(std::make_unique<MiscImage>(instance));
343+
std::vector<std::vector<std::unique_ptr<ImageFile>>> image_files =
344+
InstanceImageFiles(config);
345+
346+
size_t index = 0;
347+
for (const auto& instance : config.Instances()) {
348+
CF_EXPECT_LE(index, image_files.size());
349+
const std::vector<std::unique_ptr<ImageFile>>& instance_image_files =
350+
image_files[index];
346351

347352
Result<std::optional<ChromeOsStateImage>> chrome_os_state =
348353
CF_EXPECT(ChromeOsStateImage::Reuse(instance));
349354
if (chrome_os_state.ok()) {
350-
Result<DiskBuilder> os_builder = OsCompositeDiskBuilder(
351-
config, instance, *chrome_os_state, image_files, system_image_dir);
355+
Result<DiskBuilder> os_builder =
356+
OsCompositeDiskBuilder(config, instance, *chrome_os_state,
357+
instance_image_files, system_image_dir);
352358
if (!os_builder.ok()) {
353359
creating_os_disk = true;
354360
} else {
@@ -365,6 +371,7 @@ Result<const CuttlefishConfig*> InitFilesystemAndCreateConfig(
365371
if (instance.modem_simulator_instance_number() > modem_simulator_count) {
366372
modem_simulator_count = instance.modem_simulator_instance_number();
367373
}
374+
index++;
368375
}
369376
// TODO(schuffelen): Add smarter decision for when to delete runtime files.
370377
// Files like NVChip are tightly bound to Android keymint and should be

base/cvd/cuttlefish/host/commands/assemble_cvd/create_dynamic_disk_files.cc

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,14 @@
4343
#include "cuttlefish/host/commands/assemble_cvd/disk/image_file.h"
4444
#include "cuttlefish/host/commands/assemble_cvd/disk/initialize_instance_composite_disk.h"
4545
#include "cuttlefish/host/commands/assemble_cvd/disk/kernel_ramdisk_repacker.h"
46-
#include "cuttlefish/host/commands/assemble_cvd/disk/metadata_image.h"
47-
#include "cuttlefish/host/commands/assemble_cvd/disk/misc_image.h"
4846
#include "cuttlefish/host/commands/assemble_cvd/disk/os_composite_disk.h"
4947
#include "cuttlefish/host/commands/assemble_cvd/disk/pflash.h"
5048
#include "cuttlefish/host/commands/assemble_cvd/disk/pstore.h"
5149
#include "cuttlefish/host/commands/assemble_cvd/disk/sd_card.h"
5250
#include "cuttlefish/host/commands/assemble_cvd/disk/vbmeta_enforce_minimum_size.h"
5351
#include "cuttlefish/host/commands/assemble_cvd/disk_builder.h"
5452
#include "cuttlefish/host/commands/assemble_cvd/flags/system_image_dir.h"
53+
#include "cuttlefish/host/commands/assemble_cvd/instance_image_files.h"
5554
#include "cuttlefish/host/commands/assemble_cvd/super_image_mixer.h"
5655
#include "cuttlefish/host/libs/avb/avb.h"
5756
#include "cuttlefish/host/libs/config/ap_boot_flow.h"
@@ -99,6 +98,8 @@ Result<BuildArchive> FindImgZip(const FetcherConfig& fetcher_config,
9998
Result<void> CreateDynamicDiskFiles(
10099
const FetcherConfigs& fetcher_configs, const CuttlefishConfig& config,
101100
const SystemImageDirFlag& system_image_dirs) {
101+
std::vector<std::vector<std::unique_ptr<ImageFile>>> image_files =
102+
InstanceImageFiles(config);
102103
size_t instance_index = 0;
103104
for (const auto& instance : config.Instances()) {
104105
const FetcherConfig& fetcher_config =
@@ -160,17 +161,17 @@ Result<void> CreateDynamicDiskFiles(
160161
}
161162
}
162163

163-
std::vector<std::unique_ptr<ImageFile>> image_files;
164+
CF_EXPECT_LE(instance_index, image_files.size());
165+
const std::vector<std::unique_ptr<ImageFile>>& instance_image_files =
166+
image_files[instance_index];
164167

165-
image_files.emplace_back(std::make_unique<MetadataImage>(instance));
166-
image_files.emplace_back(std::make_unique<MiscImage>(instance));
167-
168-
for (auto& image_file : image_files) {
168+
for (auto& image_file : instance_image_files) {
169169
CF_EXPECT(image_file->Generate());
170170
}
171171

172-
DiskBuilder os_disk_builder = CF_EXPECT(OsCompositeDiskBuilder(
173-
config, instance, chrome_os_state, image_files, system_image_dirs));
172+
DiskBuilder os_disk_builder = CF_EXPECT(
173+
OsCompositeDiskBuilder(config, instance, chrome_os_state,
174+
instance_image_files, system_image_dirs));
174175
const auto os_built_composite =
175176
CF_EXPECT(os_disk_builder.BuildCompositeDiskIfNecessary());
176177

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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/instance_image_files.h"
17+
18+
#include <memory>
19+
#include <vector>
20+
21+
#include "cuttlefish/host/commands/assemble_cvd/disk/image_file.h"
22+
#include "cuttlefish/host/commands/assemble_cvd/disk/metadata_image.h"
23+
#include "cuttlefish/host/commands/assemble_cvd/disk/misc_image.h"
24+
#include "cuttlefish/host/libs/config/cuttlefish_config.h"
25+
26+
namespace cuttlefish {
27+
28+
std::vector<std::vector<std::unique_ptr<ImageFile>>> InstanceImageFiles(
29+
const CuttlefishConfig& config) {
30+
std::vector<std::vector<std::unique_ptr<ImageFile>>> image_files;
31+
32+
for (const auto& instance : config.Instances()) {
33+
std::vector<std::unique_ptr<ImageFile>>& instance_image_files =
34+
image_files.emplace_back();
35+
36+
instance_image_files.emplace_back(
37+
std::make_unique<MetadataImage>(instance));
38+
instance_image_files.emplace_back(std::make_unique<MiscImage>(instance));
39+
}
40+
41+
return image_files;
42+
}
43+
44+
} // namespace cuttlefish
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 <memory>
19+
#include <vector>
20+
21+
#include "cuttlefish/host/commands/assemble_cvd/disk/image_file.h"
22+
#include "cuttlefish/host/libs/config/cuttlefish_config.h"
23+
24+
namespace cuttlefish {
25+
26+
std::vector<std::vector<std::unique_ptr<ImageFile>>> InstanceImageFiles(
27+
const CuttlefishConfig&);
28+
29+
} // namespace cuttlefish

0 commit comments

Comments
 (0)