blob: 81e02b7b6172c781b435734547b7e1ba72965cd4 [file] [log] [blame]
Jooyung Han21e9b922021-06-26 04:14:16 +09001// Copyright 2021, The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Payload disk image
16
Jaewan Kim61f86142023-03-28 15:12:52 +090017use crate::debug_config::DebugConfig;
Andrew Walbrancc0db522021-07-12 17:03:42 +000018use android_system_virtualizationservice::aidl::android::system::virtualizationservice::{
Alan Stokes0d1ef782022-09-27 13:46:35 +010019 DiskImage::DiskImage,
20 Partition::Partition,
21 VirtualMachineAppConfig::DebugLevel::DebugLevel,
22 VirtualMachineAppConfig::{Payload::Payload, VirtualMachineAppConfig},
Andrew Walbrancc0db522021-07-12 17:03:42 +000023 VirtualMachineRawConfig::VirtualMachineRawConfig,
24};
Inseob Kima5a262f2021-11-17 19:41:03 +090025use anyhow::{anyhow, bail, Context, Result};
Alan Stokes0e82b502022-08-08 14:44:48 +010026use binder::{wait_for_interface, ParcelFileDescriptor};
Alan Stokesbf20c6a2022-01-04 12:30:50 +000027use log::{info, warn};
Alan Stokes0d1ef782022-09-27 13:46:35 +010028use microdroid_metadata::{ApexPayload, ApkPayload, Metadata, PayloadConfig, PayloadMetadata};
Jooyung Han5dc42172021-10-05 16:43:47 +090029use microdroid_payload_config::{ApexConfig, VmPayloadConfig};
Jooyung Han9900f3d2021-07-06 10:27:54 +090030use once_cell::sync::OnceCell;
Jooyung Han743e0d62022-11-07 20:57:48 +090031use packagemanager_aidl::aidl::android::content::pm::{
32 IPackageManagerNative::IPackageManagerNative, StagedApexInfo::StagedApexInfo,
33};
Alan Stokesbf20c6a2022-01-04 12:30:50 +000034use regex::Regex;
Jooyung Han44b02ab2021-07-16 03:19:13 +090035use serde::Deserialize;
36use serde_xml_rs::from_reader;
Alan Stokesbf20c6a2022-01-04 12:30:50 +000037use std::collections::HashSet;
Inseob Kimecde8c02024-09-03 13:11:08 +090038use std::ffi::OsStr;
Andrew Walbran40be9d52022-01-19 14:32:53 +000039use std::fs::{metadata, File, OpenOptions};
Jooyung Han21e9b922021-06-26 04:14:16 +090040use std::path::{Path, PathBuf};
Alan Stokesbf20c6a2022-01-04 12:30:50 +000041use std::process::Command;
Andrew Walbran40be9d52022-01-19 14:32:53 +000042use std::time::SystemTime;
Andrew Walbrancc0db522021-07-12 17:03:42 +000043use vmconfig::open_parcel_file;
44
Jooyung Han44b02ab2021-07-16 03:19:13 +090045const APEX_INFO_LIST_PATH: &str = "/apex/apex-info-list.xml";
46
Jooyung Han5dc42172021-10-05 16:43:47 +090047const PACKAGE_MANAGER_NATIVE_SERVICE: &str = "package_native";
48
Jooyung Han73bac242021-07-02 10:25:49 +090049/// Represents the list of APEXes
Jooyung Han743e0d62022-11-07 20:57:48 +090050#[derive(Clone, Debug, Deserialize, Eq, PartialEq)]
Jooyung Han9900f3d2021-07-06 10:27:54 +090051struct ApexInfoList {
Jooyung Han44b02ab2021-07-16 03:19:13 +090052 #[serde(rename = "apex-info")]
Jooyung Han73bac242021-07-02 10:25:49 +090053 list: Vec<ApexInfo>,
54}
55
Jooyung Han5ce867a2022-01-28 03:18:38 +090056#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq)]
Jooyung Han73bac242021-07-02 10:25:49 +090057struct ApexInfo {
Jooyung Han44b02ab2021-07-16 03:19:13 +090058 #[serde(rename = "moduleName")]
Jooyung Han73bac242021-07-02 10:25:49 +090059 name: String,
Jooyung Hancaa995c2022-11-08 16:35:50 +090060 #[serde(rename = "versionCode")]
61 version: u64,
Jooyung Han44b02ab2021-07-16 03:19:13 +090062 #[serde(rename = "modulePath")]
Jooyung Han73bac242021-07-02 10:25:49 +090063 path: PathBuf,
Jooyung Han9d7cd7d2021-10-12 17:44:14 +090064
65 #[serde(default)]
Alan Stokes46ac3862021-12-21 15:31:47 +000066 has_classpath_jar: bool,
Andrew Walbran40be9d52022-01-19 14:32:53 +000067
68 // The field claims to be milliseconds but is actually seconds.
69 #[serde(rename = "lastUpdateMillis")]
70 last_update_seconds: u64,
Jiyong Parkd6502352022-01-27 01:07:30 +090071
72 #[serde(rename = "isFactory")]
73 is_factory: bool,
Jooyung Hanec788042022-01-27 22:28:37 +090074
75 #[serde(rename = "isActive")]
76 is_active: bool,
Jooyung Han5ce867a2022-01-28 03:18:38 +090077
78 #[serde(rename = "provideSharedApexLibs")]
79 provide_shared_apex_libs: bool,
Nikita Ioffed4551e12023-07-14 16:01:03 +010080
81 #[serde(rename = "preinstalledModulePath")]
82 preinstalled_path: PathBuf,
Jooyung Han73bac242021-07-02 10:25:49 +090083}
84
85impl ApexInfoList {
86 /// Loads ApexInfoList
Jooyung Han9900f3d2021-07-06 10:27:54 +090087 fn load() -> Result<&'static ApexInfoList> {
88 static INSTANCE: OnceCell<ApexInfoList> = OnceCell::new();
89 INSTANCE.get_or_try_init(|| {
Jooyung Han44b02ab2021-07-16 03:19:13 +090090 let apex_info_list = File::open(APEX_INFO_LIST_PATH)
91 .context(format!("Failed to open {}", APEX_INFO_LIST_PATH))?;
Jooyung Han9d7cd7d2021-10-12 17:44:14 +090092 let mut apex_info_list: ApexInfoList = from_reader(apex_info_list)
Jooyung Han44b02ab2021-07-16 03:19:13 +090093 .context(format!("Failed to parse {}", APEX_INFO_LIST_PATH))?;
Jooyung Han9d7cd7d2021-10-12 17:44:14 +090094
Alan Stokesbf20c6a2022-01-04 12:30:50 +000095 // For active APEXes, we run derive_classpath and parse its output to see if it
96 // contributes to the classpath(s). (This allows us to handle any new classpath env
97 // vars seamlessly.)
Inseob Kimecde8c02024-09-03 13:11:08 +090098 if !cfg!(early) {
99 let classpath_vars = run_derive_classpath()?;
100 let classpath_apexes = find_apex_names_in_classpath(&classpath_vars)?;
Alan Stokesbf20c6a2022-01-04 12:30:50 +0000101
Inseob Kimecde8c02024-09-03 13:11:08 +0900102 for apex_info in apex_info_list.list.iter_mut() {
103 apex_info.has_classpath_jar = classpath_apexes.contains(&apex_info.name);
104 }
Jooyung Han9d7cd7d2021-10-12 17:44:14 +0900105 }
Alan Stokesbf20c6a2022-01-04 12:30:50 +0000106
Jooyung Han44b02ab2021-07-16 03:19:13 +0900107 Ok(apex_info_list)
Jooyung Han9900f3d2021-07-06 10:27:54 +0900108 })
Jooyung Han73bac242021-07-02 10:25:49 +0900109 }
Jooyung Han743e0d62022-11-07 20:57:48 +0900110
111 // Override apex info with the staged one
112 fn override_staged_apex(&mut self, staged_apex_info: &StagedApexInfo) -> Result<()> {
113 let mut need_to_add: Option<ApexInfo> = None;
114 for apex_info in self.list.iter_mut() {
115 if staged_apex_info.moduleName == apex_info.name {
116 if apex_info.is_active && apex_info.is_factory {
117 // Copy the entry to the end as factory/non-active after the loop
118 // to keep the factory version. Typically this step is unncessary,
119 // but some apexes (like sharedlibs) need to be kept even if it's inactive.
120 need_to_add.replace(ApexInfo { is_active: false, ..apex_info.clone() });
121 // And make this one as non-factory. Note that this one is still active
122 // and overridden right below.
123 apex_info.is_factory = false;
124 }
125 // Active one is overridden with the staged one.
126 if apex_info.is_active {
Jooyung Hancaa995c2022-11-08 16:35:50 +0900127 apex_info.version = staged_apex_info.versionCode as u64;
Jooyung Han743e0d62022-11-07 20:57:48 +0900128 apex_info.path = PathBuf::from(&staged_apex_info.diskImagePath);
129 apex_info.has_classpath_jar = staged_apex_info.hasClassPathJars;
130 apex_info.last_update_seconds = last_updated(&apex_info.path)?;
131 }
132 }
133 }
134 if let Some(info) = need_to_add {
135 self.list.push(info);
136 }
137 Ok(())
138 }
139}
140
141fn last_updated<P: AsRef<Path>>(path: P) -> Result<u64> {
142 let metadata = metadata(path)?;
143 Ok(metadata.modified()?.duration_since(SystemTime::UNIX_EPOCH)?.as_secs())
Jooyung Hanec788042022-01-27 22:28:37 +0900144}
Jooyung Han73bac242021-07-02 10:25:49 +0900145
Jooyung Hanec788042022-01-27 22:28:37 +0900146impl ApexInfo {
147 fn matches(&self, apex_config: &ApexConfig) -> bool {
148 // Match with pseudo name "{CLASSPATH}" which represents APEXes contributing
149 // to any derive_classpath environment variable
150 if apex_config.name == "{CLASSPATH}" && self.has_classpath_jar {
151 return true;
152 }
153 if apex_config.name == self.name {
154 return true;
155 }
156 false
Jooyung Han73bac242021-07-02 10:25:49 +0900157 }
Jooyung Han21e9b922021-06-26 04:14:16 +0900158}
159
Jooyung Han5dc42172021-10-05 16:43:47 +0900160struct PackageManager {
Jooyung Han5dc42172021-10-05 16:43:47 +0900161 apex_info_list: &'static ApexInfoList,
162}
163
164impl PackageManager {
165 fn new() -> Result<Self> {
Jooyung Han5dc42172021-10-05 16:43:47 +0900166 let apex_info_list = ApexInfoList::load()?;
Jooyung Han53cf7992021-10-18 19:38:41 +0900167 Ok(Self { apex_info_list })
Jooyung Han5dc42172021-10-05 16:43:47 +0900168 }
169
Jooyung Han9d7cd7d2021-10-12 17:44:14 +0900170 fn get_apex_list(&self, prefer_staged: bool) -> Result<ApexInfoList> {
Jooyung Han53cf7992021-10-18 19:38:41 +0900171 // get the list of active apexes
Jooyung Han9d7cd7d2021-10-12 17:44:14 +0900172 let mut list = self.apex_info_list.clone();
Jooyung Han53cf7992021-10-18 19:38:41 +0900173 // When prefer_staged, we override ApexInfo by consulting "package_native"
Jooyung Han5dc42172021-10-05 16:43:47 +0900174 if prefer_staged {
Inseob Kimecde8c02024-09-03 13:11:08 +0900175 if cfg!(early) {
176 return Err(anyhow!("Can't turn on prefer_staged on early boot VMs"));
177 }
Jooyung Han53cf7992021-10-18 19:38:41 +0900178 let pm =
179 wait_for_interface::<dyn IPackageManagerNative>(PACKAGE_MANAGER_NATIVE_SERVICE)
180 .context("Failed to get service when prefer_staged is set.")?;
Alan Stokes70ccf162022-07-08 11:05:03 +0100181 let staged =
182 pm.getStagedApexModuleNames().context("getStagedApexModuleNames failed")?;
Jooyung Han743e0d62022-11-07 20:57:48 +0900183 for name in staged {
184 if let Some(staged_apex_info) =
185 pm.getStagedApexInfo(&name).context("getStagedApexInfo failed")?
186 {
187 list.override_staged_apex(&staged_apex_info)?;
Jooyung Han9d7cd7d2021-10-12 17:44:14 +0900188 }
Jooyung Han5dc42172021-10-05 16:43:47 +0900189 }
190 }
Jooyung Han9d7cd7d2021-10-12 17:44:14 +0900191 Ok(list)
Jooyung Han5dc42172021-10-05 16:43:47 +0900192 }
193}
194
Andrew Walbrancc0db522021-07-12 17:03:42 +0000195fn make_metadata_file(
Alan Stokes0d1ef782022-09-27 13:46:35 +0100196 app_config: &VirtualMachineAppConfig,
Jooyung Hanec788042022-01-27 22:28:37 +0900197 apex_infos: &[&ApexInfo],
Jooyung Han21e9b922021-06-26 04:14:16 +0900198 temporary_directory: &Path,
Andrew Walbrancc0db522021-07-12 17:03:42 +0000199) -> Result<ParcelFileDescriptor> {
Alan Stokes0d1ef782022-09-27 13:46:35 +0100200 let payload_metadata = match &app_config.payload {
Ludovic Barman93ee3082023-06-20 12:18:43 +0000201 Payload::PayloadConfig(payload_config) => PayloadMetadata::Config(PayloadConfig {
Alan Stokes8f12f2b2023-01-09 09:19:20 +0000202 payload_binary_name: payload_config.payloadBinaryName.clone(),
Alan Stokesfda70842023-12-20 17:50:14 +0000203 extra_apk_count: payload_config.extraApks.len().try_into()?,
204 special_fields: Default::default(),
Alan Stokes0d1ef782022-09-27 13:46:35 +0100205 }),
206 Payload::ConfigPath(config_path) => {
Ludovic Barman93ee3082023-06-20 12:18:43 +0000207 PayloadMetadata::ConfigPath(format!("/mnt/apk/{}", config_path))
Alan Stokes0d1ef782022-09-27 13:46:35 +0100208 }
209 };
210
Jooyung Han21e9b922021-06-26 04:14:16 +0900211 let metadata = Metadata {
Andrew Walbrancc0db522021-07-12 17:03:42 +0000212 version: 1,
Jooyung Hanec788042022-01-27 22:28:37 +0900213 apexes: apex_infos
Jooyung Han21e9b922021-06-26 04:14:16 +0900214 .iter()
Jooyung Han19c1d6c2021-08-06 14:08:16 +0900215 .enumerate()
Jooyung Hanec788042022-01-27 22:28:37 +0900216 .map(|(i, apex_info)| {
Andrew Walbran40be9d52022-01-19 14:32:53 +0000217 Ok(ApexPayload {
Jooyung Hanec788042022-01-27 22:28:37 +0900218 name: apex_info.name.clone(),
Andrew Walbran40be9d52022-01-19 14:32:53 +0000219 partition_name: format!("microdroid-apex-{}", i),
Jiyong Parkd6502352022-01-27 01:07:30 +0900220 last_update_seconds: apex_info.last_update_seconds,
221 is_factory: apex_info.is_factory,
Andrew Walbran40be9d52022-01-19 14:32:53 +0000222 ..Default::default()
223 })
Jooyung Han19c1d6c2021-08-06 14:08:16 +0900224 })
Andrew Walbran40be9d52022-01-19 14:32:53 +0000225 .collect::<Result<_>>()?,
Jooyung Han21e9b922021-06-26 04:14:16 +0900226 apk: Some(ApkPayload {
Jooyung Han35edb8f2021-07-01 16:17:16 +0900227 name: "apk".to_owned(),
228 payload_partition_name: "microdroid-apk".to_owned(),
229 idsig_partition_name: "microdroid-apk-idsig".to_owned(),
Jooyung Han21e9b922021-06-26 04:14:16 +0900230 ..Default::default()
231 })
232 .into(),
Alan Stokes0d1ef782022-09-27 13:46:35 +0100233 payload: Some(payload_metadata),
Jooyung Han21e9b922021-06-26 04:14:16 +0900234 ..Default::default()
235 };
Andrew Walbrancc0db522021-07-12 17:03:42 +0000236
237 // Write metadata to file.
Alan Stokes0d1ef782022-09-27 13:46:35 +0100238 let metadata_path = temporary_directory.join("metadata");
Andrew Walbrancc0db522021-07-12 17:03:42 +0000239 let mut metadata_file = OpenOptions::new()
240 .create_new(true)
241 .read(true)
242 .write(true)
243 .open(&metadata_path)
244 .with_context(|| format!("Failed to open metadata file {:?}", metadata_path))?;
Jooyung Han21e9b922021-06-26 04:14:16 +0900245 microdroid_metadata::write_metadata(&metadata, &mut metadata_file)?;
246
Andrew Walbrancc0db522021-07-12 17:03:42 +0000247 // Re-open the metadata file as read-only.
248 open_parcel_file(&metadata_path, false)
249}
250
251/// Creates a DiskImage with partitions:
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100252/// payload-metadata: metadata
Andrew Walbrancc0db522021-07-12 17:03:42 +0000253/// microdroid-apex-0: apex 0
254/// microdroid-apex-1: apex 1
255/// ..
256/// microdroid-apk: apk
257/// microdroid-apk-idsig: idsig
Inseob Kima5a262f2021-11-17 19:41:03 +0900258/// extra-apk-0: additional apk 0
259/// extra-idsig-0: additional idsig 0
260/// extra-apk-1: additional apk 1
261/// extra-idsig-1: additional idsig 1
262/// ..
Andrew Walbrancc0db522021-07-12 17:03:42 +0000263fn make_payload_disk(
Inseob Kima5a262f2021-11-17 19:41:03 +0900264 app_config: &VirtualMachineAppConfig,
Jaewan Kim61f86142023-03-28 15:12:52 +0900265 debug_config: &DebugConfig,
Andrew Walbrancc0db522021-07-12 17:03:42 +0000266 apk_file: File,
267 idsig_file: File,
Alan Stokesfda70842023-12-20 17:50:14 +0000268 extra_apk_files: Vec<File>,
Jooyung Han9d7cd7d2021-10-12 17:44:14 +0900269 vm_payload_config: &VmPayloadConfig,
Andrew Walbrancc0db522021-07-12 17:03:42 +0000270 temporary_directory: &Path,
271) -> Result<DiskImage> {
Alan Stokesfda70842023-12-20 17:50:14 +0000272 if extra_apk_files.len() != app_config.extraIdsigs.len() {
Inseob Kima5a262f2021-11-17 19:41:03 +0900273 bail!(
274 "payload config has {} apks, but app config has {} idsigs",
275 vm_payload_config.extra_apks.len(),
276 app_config.extraIdsigs.len()
277 );
278 }
279
Jooyung Han9d7cd7d2021-10-12 17:44:14 +0900280 let pm = PackageManager::new()?;
281 let apex_list = pm.get_apex_list(vm_payload_config.prefer_staged)?;
282
Jooyung Hanec788042022-01-27 22:28:37 +0900283 // collect APEXes from config
Nikita Ioffed4551e12023-07-14 16:01:03 +0100284 let mut apex_infos = collect_apex_infos(&apex_list, &vm_payload_config.apexes, debug_config)?;
Jooyung Hancaa995c2022-11-08 16:35:50 +0900285
286 // Pass sorted list of apexes. Sorting key shouldn't use `path` because it will change after
287 // reboot with prefer_staged. `last_update_seconds` is added to distinguish "samegrade"
288 // update.
289 apex_infos.sort_by_key(|info| (&info.name, &info.version, &info.last_update_seconds));
Jooyung Hanec788042022-01-27 22:28:37 +0900290 info!("Microdroid payload APEXes: {:?}", apex_infos.iter().map(|ai| &ai.name));
Jooyung Han9d7cd7d2021-10-12 17:44:14 +0900291
Alan Stokes0d1ef782022-09-27 13:46:35 +0100292 let metadata_file = make_metadata_file(app_config, &apex_infos, temporary_directory)?;
Jooyung Han21e9b922021-06-26 04:14:16 +0900293 // put metadata at the first partition
294 let mut partitions = vec![Partition {
Jooyung Han14e5a8e2021-07-06 20:48:38 +0900295 label: "payload-metadata".to_owned(),
Jooyung Han631d5882021-07-29 06:34:05 +0900296 image: Some(metadata_file),
Jooyung Han21e9b922021-06-26 04:14:16 +0900297 writable: false,
Jiyong Park3f9b5092024-07-10 13:38:29 +0900298 guid: None,
Jooyung Han21e9b922021-06-26 04:14:16 +0900299 }];
300
Jooyung Hanec788042022-01-27 22:28:37 +0900301 for (i, apex_info) in apex_infos.iter().enumerate() {
Inseob Kimecde8c02024-09-03 13:11:08 +0900302 let path = if cfg!(early) {
303 let path = &apex_info.preinstalled_path;
304 if path.extension().and_then(OsStr::to_str).unwrap_or("") != "apex" {
305 bail!("compressed APEX {} not supported", path.display());
306 }
307 path
308 } else {
309 &apex_info.path
310 };
311 let apex_file = open_parcel_file(path, false)?;
Jooyung Han95884632021-07-06 22:27:54 +0900312 partitions.push(Partition {
313 label: format!("microdroid-apex-{}", i),
Jooyung Han631d5882021-07-29 06:34:05 +0900314 image: Some(apex_file),
Jooyung Han95884632021-07-06 22:27:54 +0900315 writable: false,
Jiyong Park3f9b5092024-07-10 13:38:29 +0900316 guid: None,
Jooyung Han95884632021-07-06 22:27:54 +0900317 });
Jooyung Han21e9b922021-06-26 04:14:16 +0900318 }
Jooyung Han95884632021-07-06 22:27:54 +0900319 partitions.push(Partition {
320 label: "microdroid-apk".to_owned(),
Jooyung Han631d5882021-07-29 06:34:05 +0900321 image: Some(ParcelFileDescriptor::new(apk_file)),
Jooyung Han95884632021-07-06 22:27:54 +0900322 writable: false,
Jiyong Park3f9b5092024-07-10 13:38:29 +0900323 guid: None,
Jooyung Han95884632021-07-06 22:27:54 +0900324 });
325 partitions.push(Partition {
326 label: "microdroid-apk-idsig".to_owned(),
Jooyung Han631d5882021-07-29 06:34:05 +0900327 image: Some(ParcelFileDescriptor::new(idsig_file)),
Jooyung Han95884632021-07-06 22:27:54 +0900328 writable: false,
Jiyong Park3f9b5092024-07-10 13:38:29 +0900329 guid: None,
Jooyung Han95884632021-07-06 22:27:54 +0900330 });
Jooyung Han21e9b922021-06-26 04:14:16 +0900331
Inseob Kima5a262f2021-11-17 19:41:03 +0900332 // we've already checked that extra_apks and extraIdsigs are in the same size.
Inseob Kima5a262f2021-11-17 19:41:03 +0900333 let extra_idsigs = &app_config.extraIdsigs;
Alan Stokesfda70842023-12-20 17:50:14 +0000334 for (i, (extra_apk_file, extra_idsig)) in
335 extra_apk_files.into_iter().zip(extra_idsigs.iter()).enumerate()
336 {
Inseob Kima5a262f2021-11-17 19:41:03 +0900337 partitions.push(Partition {
Alan Stokesfda70842023-12-20 17:50:14 +0000338 label: format!("extra-apk-{i}"),
339 image: Some(ParcelFileDescriptor::new(extra_apk_file)),
Inseob Kima5a262f2021-11-17 19:41:03 +0900340 writable: false,
Jiyong Park3f9b5092024-07-10 13:38:29 +0900341 guid: None,
Inseob Kima5a262f2021-11-17 19:41:03 +0900342 });
343
344 partitions.push(Partition {
Alan Stokesfda70842023-12-20 17:50:14 +0000345 label: format!("extra-idsig-{i}"),
Victor Hsieh14497f02022-09-21 10:10:16 -0700346 image: Some(ParcelFileDescriptor::new(
347 extra_idsig
348 .as_ref()
349 .try_clone()
Alan Stokesfda70842023-12-20 17:50:14 +0000350 .with_context(|| format!("Failed to clone the extra idsig #{i}"))?,
Victor Hsieh14497f02022-09-21 10:10:16 -0700351 )),
Inseob Kima5a262f2021-11-17 19:41:03 +0900352 writable: false,
Jiyong Park3f9b5092024-07-10 13:38:29 +0900353 guid: None,
Inseob Kima5a262f2021-11-17 19:41:03 +0900354 });
355 }
356
Jooyung Han21e9b922021-06-26 04:14:16 +0900357 Ok(DiskImage { image: None, partitions, writable: false })
358}
Andrew Walbrancc0db522021-07-12 17:03:42 +0000359
Alan Stokesbf20c6a2022-01-04 12:30:50 +0000360fn run_derive_classpath() -> Result<String> {
361 let result = Command::new("/apex/com.android.sdkext/bin/derive_classpath")
362 .arg("/proc/self/fd/1")
363 .output()
364 .context("Failed to run derive_classpath")?;
365
366 if !result.status.success() {
367 bail!("derive_classpath returned {}", result.status);
368 }
369
370 String::from_utf8(result.stdout).context("Converting derive_classpath output")
371}
372
373fn find_apex_names_in_classpath(classpath_vars: &str) -> Result<HashSet<String>> {
374 // Each line should be in the format "export <var name> <paths>", where <paths> is a
375 // colon-separated list of paths to JARs. We don't care about the var names, and we're only
376 // interested in paths that look like "/apex/<apex name>/<anything>" so we know which APEXes
377 // contribute to at least one var.
378 let mut apexes = HashSet::new();
379
380 let pattern = Regex::new(r"^export [^ ]+ ([^ ]+)$").context("Failed to construct Regex")?;
381 for line in classpath_vars.lines() {
382 if let Some(captures) = pattern.captures(line) {
383 if let Some(paths) = captures.get(1) {
384 apexes.extend(paths.as_str().split(':').filter_map(|path| {
385 let path = path.strip_prefix("/apex/")?;
386 Some(path[..path.find('/')?].to_owned())
387 }));
388 continue;
389 }
390 }
391 warn!("Malformed line from derive_classpath: {}", line);
392 }
393
394 Ok(apexes)
Jooyung Han5e0f2062021-10-12 14:00:46 +0900395}
396
Nikita Ioffed4551e12023-07-14 16:01:03 +0100397fn check_apexes_are_from_allowed_partitions(requested_apexes: &Vec<&ApexInfo>) -> Result<()> {
398 const ALLOWED_PARTITIONS: [&str; 2] = ["/system", "/system_ext"];
399 for apex in requested_apexes {
400 if !ALLOWED_PARTITIONS.iter().any(|p| apex.preinstalled_path.starts_with(p)) {
401 bail!("Non-system APEX {} is not supported in Microdroid", apex.name);
402 }
403 }
404 Ok(())
405}
406
Jooyung Hanec788042022-01-27 22:28:37 +0900407// Collect ApexInfos from VM config
408fn collect_apex_infos<'a>(
409 apex_list: &'a ApexInfoList,
410 apex_configs: &[ApexConfig],
Jaewan Kim61f86142023-03-28 15:12:52 +0900411 debug_config: &DebugConfig,
Nikita Ioffed4551e12023-07-14 16:01:03 +0100412) -> Result<Vec<&'a ApexInfo>> {
Alan Stokesf7260f12023-08-30 17:25:21 +0100413 // APEXes which any Microdroid VM needs.
414 // TODO(b/192200378) move this to microdroid.json?
415 let required_apexes: &[_] =
416 if debug_config.should_include_debug_apexes() { &["com.android.adbd"] } else { &[] };
Jooyung Hanec788042022-01-27 22:28:37 +0900417
Nikita Ioffed4551e12023-07-14 16:01:03 +0100418 let apex_infos = apex_list
Jooyung Hanec788042022-01-27 22:28:37 +0900419 .list
420 .iter()
421 .filter(|ai| {
422 apex_configs.iter().any(|cfg| ai.matches(cfg) && ai.is_active)
Alan Stokesf7260f12023-08-30 17:25:21 +0100423 || required_apexes.iter().any(|name| name == &ai.name && ai.is_active)
Jooyung Han5ce867a2022-01-28 03:18:38 +0900424 || ai.provide_shared_apex_libs
Jooyung Hanec788042022-01-27 22:28:37 +0900425 })
Nikita Ioffed4551e12023-07-14 16:01:03 +0100426 .collect();
427
428 check_apexes_are_from_allowed_partitions(&apex_infos)?;
429 Ok(apex_infos)
Jooyung Han5e0f2062021-10-12 14:00:46 +0900430}
431
Nikita Ioffe5dfddf22023-06-29 16:11:26 +0100432pub fn add_microdroid_vendor_image(vendor_image: File, vm_config: &mut VirtualMachineRawConfig) {
433 vm_config.disks.push(DiskImage {
434 image: None,
435 writable: false,
436 partitions: vec![Partition {
437 label: "microdroid-vendor".to_owned(),
438 image: Some(ParcelFileDescriptor::new(vendor_image)),
439 writable: false,
Jiyong Park3f9b5092024-07-10 13:38:29 +0900440 guid: None,
Nikita Ioffe5dfddf22023-06-29 16:11:26 +0100441 }],
442 })
443}
444
Shikha Panwar22e70452022-10-10 18:32:55 +0000445pub fn add_microdroid_system_images(
Andrew Walbrancc0db522021-07-12 17:03:42 +0000446 config: &VirtualMachineAppConfig,
Jiyong Park8d081812021-07-23 17:45:04 +0900447 instance_file: File,
Shikha Panwar22e70452022-10-10 18:32:55 +0000448 storage_image: Option<File>,
Inseob Kim172f9eb2023-11-06 17:02:08 +0900449 os_name: &str,
Andrew Walbrancc0db522021-07-12 17:03:42 +0000450 vm_config: &mut VirtualMachineRawConfig,
451) -> Result<()> {
Shikha Panwared8ace42022-09-28 12:52:16 +0000452 let debug_suffix = match config.debugLevel {
453 DebugLevel::NONE => "normal",
Seungjae Yooe85831e2022-12-12 09:34:58 +0900454 DebugLevel::FULL => "debuggable",
Shikha Panwared8ace42022-09-28 12:52:16 +0000455 _ => return Err(anyhow!("unsupported debug level: {:?}", config.debugLevel)),
456 };
Inseob Kim172f9eb2023-11-06 17:02:08 +0900457 let initrd = format!("/apex/com.android.virt/etc/{os_name}_initrd_{debug_suffix}.img");
Shikha Panwared8ace42022-09-28 12:52:16 +0000458 vm_config.initrd = Some(open_parcel_file(Path::new(&initrd), false)?);
459
Shikha Panwar22e70452022-10-10 18:32:55 +0000460 let mut writable_partitions = vec![Partition {
Shikha Panwared8ace42022-09-28 12:52:16 +0000461 label: "vm-instance".to_owned(),
462 image: Some(ParcelFileDescriptor::new(instance_file)),
463 writable: true,
Jiyong Park3f9b5092024-07-10 13:38:29 +0900464 guid: None,
Shikha Panwar22e70452022-10-10 18:32:55 +0000465 }];
466
467 if let Some(file) = storage_image {
468 writable_partitions.push(Partition {
Shikha Panwar566c9672022-11-15 14:39:58 +0000469 label: "encryptedstore".to_owned(),
Shikha Panwar22e70452022-10-10 18:32:55 +0000470 image: Some(ParcelFileDescriptor::new(file)),
471 writable: true,
Jiyong Park3f9b5092024-07-10 13:38:29 +0900472 guid: None,
Shikha Panwar22e70452022-10-10 18:32:55 +0000473 });
474 }
475
476 vm_config.disks.push(DiskImage {
477 image: None,
478 partitions: writable_partitions,
479 writable: true,
480 });
481
482 Ok(())
483}
484
Alan Stokesfda70842023-12-20 17:50:14 +0000485#[allow(clippy::too_many_arguments)] // TODO: Fewer arguments
Shikha Panwar22e70452022-10-10 18:32:55 +0000486pub fn add_microdroid_payload_images(
487 config: &VirtualMachineAppConfig,
Jaewan Kim61f86142023-03-28 15:12:52 +0900488 debug_config: &DebugConfig,
Shikha Panwar22e70452022-10-10 18:32:55 +0000489 temporary_directory: &Path,
490 apk_file: File,
491 idsig_file: File,
Alan Stokesfda70842023-12-20 17:50:14 +0000492 extra_apk_files: Vec<File>,
Shikha Panwar22e70452022-10-10 18:32:55 +0000493 vm_payload_config: &VmPayloadConfig,
494 vm_config: &mut VirtualMachineRawConfig,
495) -> Result<()> {
Andrew Walbrancc0db522021-07-12 17:03:42 +0000496 vm_config.disks.push(make_payload_disk(
Inseob Kima5a262f2021-11-17 19:41:03 +0900497 config,
Jaewan Kim61f86142023-03-28 15:12:52 +0900498 debug_config,
Andrew Walbrancc0db522021-07-12 17:03:42 +0000499 apk_file,
500 idsig_file,
Alan Stokesfda70842023-12-20 17:50:14 +0000501 extra_apk_files,
Jooyung Han9d7cd7d2021-10-12 17:44:14 +0900502 vm_payload_config,
Andrew Walbrancc0db522021-07-12 17:03:42 +0000503 temporary_directory,
504 )?);
505
Andrew Walbrancc0db522021-07-12 17:03:42 +0000506 Ok(())
507}
Jooyung Han5e0f2062021-10-12 14:00:46 +0900508
509#[cfg(test)]
510mod tests {
511 use super::*;
Alan Stokesf7260f12023-08-30 17:25:21 +0100512 use std::collections::HashMap;
Jooyung Han743e0d62022-11-07 20:57:48 +0900513 use tempfile::NamedTempFile;
Andrew Walbranc1a5f5a2022-01-19 13:38:13 +0000514
Jooyung Han5e0f2062021-10-12 14:00:46 +0900515 #[test]
Alan Stokesbf20c6a2022-01-04 12:30:50 +0000516 fn test_find_apex_names_in_classpath() {
517 let vars = r#"
518export FOO /apex/unterminated
519export BAR /apex/valid.apex/something
520wrong
521export EMPTY
522export OTHER /foo/bar:/baz:/apex/second.valid.apex/:gibberish:"#;
523 let expected = vec!["valid.apex", "second.valid.apex"];
524 let expected: HashSet<_> = expected.into_iter().map(ToString::to_string).collect();
525
526 assert_eq!(find_apex_names_in_classpath(vars).unwrap(), expected);
Jooyung Han5e0f2062021-10-12 14:00:46 +0900527 }
Andrew Walbranc1a5f5a2022-01-19 13:38:13 +0000528
529 #[test]
Nikita Ioffed4551e12023-07-14 16:01:03 +0100530 fn test_collect_apexes() -> Result<()> {
Alan Stokesf7260f12023-08-30 17:25:21 +0100531 let apex_infos_for_test = [
532 (
533 "adbd",
Andrew Walbranc1a5f5a2022-01-19 13:38:13 +0000534 ApexInfo {
Jooyung Hanec788042022-01-27 22:28:37 +0900535 name: "com.android.adbd".to_string(),
536 path: PathBuf::from("adbd"),
Nikita Ioffed4551e12023-07-14 16:01:03 +0100537 preinstalled_path: PathBuf::from("/system/adbd"),
Andrew Walbranc1a5f5a2022-01-19 13:38:13 +0000538 has_classpath_jar: false,
Andrew Walbran40be9d52022-01-19 14:32:53 +0000539 last_update_seconds: 12345678,
Jiyong Parkd6502352022-01-27 01:07:30 +0900540 is_factory: true,
Jooyung Hanec788042022-01-27 22:28:37 +0900541 is_active: false,
Jooyung Han5ce867a2022-01-28 03:18:38 +0900542 ..Default::default()
Jooyung Hanec788042022-01-27 22:28:37 +0900543 },
Alan Stokesf7260f12023-08-30 17:25:21 +0100544 ),
545 (
546 "adbd_updated",
Jooyung Hanec788042022-01-27 22:28:37 +0900547 ApexInfo {
Alan Stokesf7260f12023-08-30 17:25:21 +0100548 name: "com.android.adbd".to_string(),
549 path: PathBuf::from("adbd"),
550 preinstalled_path: PathBuf::from("/system/adbd"),
Jooyung Hanec788042022-01-27 22:28:37 +0900551 has_classpath_jar: false,
552 last_update_seconds: 12345678 + 1,
553 is_factory: false,
554 is_active: true,
Jooyung Han5ce867a2022-01-28 03:18:38 +0900555 ..Default::default()
Jooyung Hanec788042022-01-27 22:28:37 +0900556 },
Alan Stokesf7260f12023-08-30 17:25:21 +0100557 ),
558 (
559 "no_classpath",
Jooyung Hanec788042022-01-27 22:28:37 +0900560 ApexInfo {
Jooyung Hanec788042022-01-27 22:28:37 +0900561 name: "no_classpath".to_string(),
562 path: PathBuf::from("no_classpath"),
563 has_classpath_jar: false,
564 last_update_seconds: 12345678,
565 is_factory: true,
566 is_active: true,
Jooyung Han5ce867a2022-01-28 03:18:38 +0900567 ..Default::default()
Jooyung Hanec788042022-01-27 22:28:37 +0900568 },
Alan Stokesf7260f12023-08-30 17:25:21 +0100569 ),
570 (
571 "has_classpath",
Jooyung Hanec788042022-01-27 22:28:37 +0900572 ApexInfo {
Andrew Walbranc1a5f5a2022-01-19 13:38:13 +0000573 name: "has_classpath".to_string(),
Jooyung Hanec788042022-01-27 22:28:37 +0900574 path: PathBuf::from("has_classpath"),
Andrew Walbranc1a5f5a2022-01-19 13:38:13 +0000575 has_classpath_jar: true,
Andrew Walbran40be9d52022-01-19 14:32:53 +0000576 last_update_seconds: 87654321,
Jooyung Hanec788042022-01-27 22:28:37 +0900577 is_factory: true,
578 is_active: false,
Jooyung Han5ce867a2022-01-28 03:18:38 +0900579 ..Default::default()
Jooyung Hanec788042022-01-27 22:28:37 +0900580 },
Alan Stokesf7260f12023-08-30 17:25:21 +0100581 ),
582 (
583 "has_classpath_updated",
Jooyung Hanec788042022-01-27 22:28:37 +0900584 ApexInfo {
Jooyung Hanec788042022-01-27 22:28:37 +0900585 name: "has_classpath".to_string(),
586 path: PathBuf::from("has_classpath/updated"),
Nikita Ioffed4551e12023-07-14 16:01:03 +0100587 preinstalled_path: PathBuf::from("/system/has_classpath"),
Jooyung Hanec788042022-01-27 22:28:37 +0900588 has_classpath_jar: true,
589 last_update_seconds: 87654321 + 1,
Jiyong Parkd6502352022-01-27 01:07:30 +0900590 is_factory: false,
Jooyung Hanec788042022-01-27 22:28:37 +0900591 is_active: true,
Jooyung Han5ce867a2022-01-28 03:18:38 +0900592 ..Default::default()
Jooyung Hanec788042022-01-27 22:28:37 +0900593 },
Alan Stokesf7260f12023-08-30 17:25:21 +0100594 ),
595 (
596 "apex-foo",
Jooyung Hanec788042022-01-27 22:28:37 +0900597 ApexInfo {
Jooyung Hanec788042022-01-27 22:28:37 +0900598 name: "apex-foo".to_string(),
599 path: PathBuf::from("apex-foo"),
Nikita Ioffed4551e12023-07-14 16:01:03 +0100600 preinstalled_path: PathBuf::from("/system/apex-foo"),
Jooyung Hanec788042022-01-27 22:28:37 +0900601 has_classpath_jar: false,
602 last_update_seconds: 87654321,
603 is_factory: true,
604 is_active: false,
Jooyung Han5ce867a2022-01-28 03:18:38 +0900605 ..Default::default()
Jooyung Hanec788042022-01-27 22:28:37 +0900606 },
Alan Stokesf7260f12023-08-30 17:25:21 +0100607 ),
608 (
609 "apex-foo-updated",
Jooyung Hanec788042022-01-27 22:28:37 +0900610 ApexInfo {
Jooyung Hanec788042022-01-27 22:28:37 +0900611 name: "apex-foo".to_string(),
612 path: PathBuf::from("apex-foo/updated"),
Nikita Ioffed4551e12023-07-14 16:01:03 +0100613 preinstalled_path: PathBuf::from("/system/apex-foo"),
Jooyung Hanec788042022-01-27 22:28:37 +0900614 has_classpath_jar: false,
615 last_update_seconds: 87654321 + 1,
616 is_factory: false,
617 is_active: true,
Jooyung Han5ce867a2022-01-28 03:18:38 +0900618 ..Default::default()
619 },
Alan Stokesf7260f12023-08-30 17:25:21 +0100620 ),
621 (
622 "sharedlibs",
Jooyung Han5ce867a2022-01-28 03:18:38 +0900623 ApexInfo {
Jooyung Han5ce867a2022-01-28 03:18:38 +0900624 name: "sharedlibs".to_string(),
625 path: PathBuf::from("apex-foo"),
Nikita Ioffed4551e12023-07-14 16:01:03 +0100626 preinstalled_path: PathBuf::from("/system/apex-foo"),
Jooyung Han5ce867a2022-01-28 03:18:38 +0900627 last_update_seconds: 87654321,
628 is_factory: true,
629 provide_shared_apex_libs: true,
630 ..Default::default()
631 },
Alan Stokesf7260f12023-08-30 17:25:21 +0100632 ),
633 (
634 "sharedlibs-updated",
Jooyung Han5ce867a2022-01-28 03:18:38 +0900635 ApexInfo {
Jooyung Han5ce867a2022-01-28 03:18:38 +0900636 name: "sharedlibs".to_string(),
637 path: PathBuf::from("apex-foo/updated"),
Nikita Ioffed4551e12023-07-14 16:01:03 +0100638 preinstalled_path: PathBuf::from("/system/apex-foo"),
Jooyung Han5ce867a2022-01-28 03:18:38 +0900639 last_update_seconds: 87654321 + 1,
640 is_active: true,
641 provide_shared_apex_libs: true,
642 ..Default::default()
Andrew Walbranc1a5f5a2022-01-19 13:38:13 +0000643 },
Alan Stokesf7260f12023-08-30 17:25:21 +0100644 ),
645 ];
646 let apex_info_list = ApexInfoList {
647 list: apex_infos_for_test.iter().map(|(_, info)| info).cloned().collect(),
Andrew Walbranc1a5f5a2022-01-19 13:38:13 +0000648 };
Alan Stokesf7260f12023-08-30 17:25:21 +0100649 let apex_info_map = HashMap::from(apex_infos_for_test);
Jooyung Hanec788042022-01-27 22:28:37 +0900650 let apex_configs = vec![
651 ApexConfig { name: "apex-foo".to_string() },
Andrew Walbranc1a5f5a2022-01-19 13:38:13 +0000652 ApexConfig { name: "{CLASSPATH}".to_string() },
653 ];
654 assert_eq!(
Nikita Ioffed4551e12023-07-14 16:01:03 +0100655 collect_apex_infos(
656 &apex_info_list,
657 &apex_configs,
Jaewan Kimf3143242024-03-15 06:56:31 +0000658 &DebugConfig::new_with_debug_level(DebugLevel::FULL)
Nikita Ioffed4551e12023-07-14 16:01:03 +0100659 )?,
Andrew Walbranc1a5f5a2022-01-19 13:38:13 +0000660 vec![
Jooyung Han5ce867a2022-01-28 03:18:38 +0900661 // Pass active/required APEXes
Alan Stokesf7260f12023-08-30 17:25:21 +0100662 &apex_info_map["adbd_updated"],
Jooyung Han5ce867a2022-01-28 03:18:38 +0900663 // Pass active APEXes specified in the config
Alan Stokesf7260f12023-08-30 17:25:21 +0100664 &apex_info_map["has_classpath_updated"],
665 &apex_info_map["apex-foo-updated"],
Jooyung Han5ce867a2022-01-28 03:18:38 +0900666 // Pass both preinstalled(inactive) and updated(active) for "sharedlibs" APEXes
Alan Stokesf7260f12023-08-30 17:25:21 +0100667 &apex_info_map["sharedlibs"],
668 &apex_info_map["sharedlibs-updated"],
Andrew Walbranc1a5f5a2022-01-19 13:38:13 +0000669 ]
670 );
Nikita Ioffed4551e12023-07-14 16:01:03 +0100671 Ok(())
672 }
673
674 #[test]
675 fn test_check_allowed_partitions_vendor_not_allowed() -> Result<()> {
676 let apex_info_list = ApexInfoList {
677 list: vec![ApexInfo {
678 name: "apex-vendor".to_string(),
679 path: PathBuf::from("apex-vendor"),
680 preinstalled_path: PathBuf::from("/vendor/apex-vendor"),
681 is_active: true,
682 ..Default::default()
683 }],
684 };
685 let apex_configs = vec![ApexConfig { name: "apex-vendor".to_string() }];
686
Jaewan Kimf3143242024-03-15 06:56:31 +0000687 let ret = collect_apex_infos(
688 &apex_info_list,
689 &apex_configs,
690 &DebugConfig::new_with_debug_level(DebugLevel::NONE),
691 );
Nikita Ioffed4551e12023-07-14 16:01:03 +0100692 assert!(ret
693 .is_err_and(|ret| ret.to_string()
694 == "Non-system APEX apex-vendor is not supported in Microdroid"));
695
696 Ok(())
697 }
698
699 #[test]
700 fn test_check_allowed_partitions_system_ext_allowed() -> Result<()> {
701 let apex_info_list = ApexInfoList {
702 list: vec![ApexInfo {
703 name: "apex-system_ext".to_string(),
704 path: PathBuf::from("apex-system_ext"),
705 preinstalled_path: PathBuf::from("/system_ext/apex-system_ext"),
706 is_active: true,
707 ..Default::default()
708 }],
709 };
710
711 let apex_configs = vec![ApexConfig { name: "apex-system_ext".to_string() }];
712
713 assert_eq!(
714 collect_apex_infos(
715 &apex_info_list,
716 &apex_configs,
Jaewan Kimf3143242024-03-15 06:56:31 +0000717 &DebugConfig::new_with_debug_level(DebugLevel::NONE)
Nikita Ioffed4551e12023-07-14 16:01:03 +0100718 )?,
719 vec![&apex_info_list.list[0]]
720 );
721
722 Ok(())
Andrew Walbranc1a5f5a2022-01-19 13:38:13 +0000723 }
Jooyung Han743e0d62022-11-07 20:57:48 +0900724
725 #[test]
726 fn test_prefer_staged_apex_with_factory_active_apex() {
727 let single_apex = ApexInfo {
728 name: "foo".to_string(),
Jooyung Hancaa995c2022-11-08 16:35:50 +0900729 version: 1,
Jooyung Han743e0d62022-11-07 20:57:48 +0900730 path: PathBuf::from("foo.apex"),
731 is_factory: true,
732 is_active: true,
733 ..Default::default()
734 };
735 let mut apex_info_list = ApexInfoList { list: vec![single_apex.clone()] };
736
737 let staged = NamedTempFile::new().unwrap();
738 apex_info_list
739 .override_staged_apex(&StagedApexInfo {
740 moduleName: "foo".to_string(),
Jooyung Hancaa995c2022-11-08 16:35:50 +0900741 versionCode: 2,
Jooyung Han743e0d62022-11-07 20:57:48 +0900742 diskImagePath: staged.path().to_string_lossy().to_string(),
743 ..Default::default()
744 })
745 .expect("should be ok");
746
747 assert_eq!(
748 apex_info_list,
749 ApexInfoList {
750 list: vec![
751 ApexInfo {
Jooyung Hancaa995c2022-11-08 16:35:50 +0900752 version: 2,
Jooyung Han743e0d62022-11-07 20:57:48 +0900753 is_factory: false,
754 path: staged.path().to_owned(),
755 last_update_seconds: last_updated(staged.path()).unwrap(),
756 ..single_apex.clone()
757 },
758 ApexInfo { is_active: false, ..single_apex },
759 ],
760 }
761 );
762 }
763
764 #[test]
765 fn test_prefer_staged_apex_with_factory_and_inactive_apex() {
766 let factory_apex = ApexInfo {
767 name: "foo".to_string(),
Jooyung Hancaa995c2022-11-08 16:35:50 +0900768 version: 1,
Jooyung Han743e0d62022-11-07 20:57:48 +0900769 path: PathBuf::from("foo.apex"),
770 is_factory: true,
771 ..Default::default()
772 };
773 let active_apex = ApexInfo {
774 name: "foo".to_string(),
Jooyung Hancaa995c2022-11-08 16:35:50 +0900775 version: 2,
Jooyung Han743e0d62022-11-07 20:57:48 +0900776 path: PathBuf::from("foo.downloaded.apex"),
777 is_active: true,
778 ..Default::default()
779 };
780 let mut apex_info_list =
781 ApexInfoList { list: vec![factory_apex.clone(), active_apex.clone()] };
782
783 let staged = NamedTempFile::new().unwrap();
784 apex_info_list
785 .override_staged_apex(&StagedApexInfo {
786 moduleName: "foo".to_string(),
Jooyung Hancaa995c2022-11-08 16:35:50 +0900787 versionCode: 3,
Jooyung Han743e0d62022-11-07 20:57:48 +0900788 diskImagePath: staged.path().to_string_lossy().to_string(),
789 ..Default::default()
790 })
791 .expect("should be ok");
792
793 assert_eq!(
794 apex_info_list,
795 ApexInfoList {
796 list: vec![
797 // factory apex isn't touched
798 factory_apex,
799 // update active one
800 ApexInfo {
Jooyung Hancaa995c2022-11-08 16:35:50 +0900801 version: 3,
Jooyung Han743e0d62022-11-07 20:57:48 +0900802 path: staged.path().to_owned(),
803 last_update_seconds: last_updated(staged.path()).unwrap(),
804 ..active_apex
805 },
806 ],
807 }
808 );
809 }
Jooyung Han5e0f2062021-10-12 14:00:46 +0900810}