disk: Turn Android Sparse format into a feature

This is a follow-up to
https://chromium-review.googlesource.com/c/crosvm/crosvm/+/3864926,
which implements this for QCOW.

Same rationale: allow to turn off support for disk formats that projects
do not need/want.

BUG=None
TEST=./tools/presubmit; cd crosvm-fuzz && cargo build

Change-Id: I401bad4d4ccb1a00a303ed86f1156f153b70b562
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/3872278
Commit-Queue: Christian Blichmann <cblichmann@google.com>
Reviewed-by: David Stevens <stevensd@chromium.org>
Tested-by: Christian Blichmann <cblichmann@google.com>
This commit is contained in:
Christian Blichmann 2022-09-05 14:29:55 +02:00 committed by crosvm LUCI
parent 8cef8b7e64
commit 44762216cf
4 changed files with 11 additions and 0 deletions

View file

@ -8,6 +8,7 @@ edition = "2021"
path = "src/disk.rs"
[features]
android-sparse = []
composite-disk = ["crc32fast", "protos", "protobuf", "uuid"]
qcow = []

View file

@ -34,6 +34,7 @@ pub struct AsyncDiskFileWrapper<T: DiskFile + Send> {
}
impl<T: DiskFile + Send> AsyncDiskFileWrapper<T> {
#[allow(dead_code)] // Only used if qcow or android-sparse features are enabled
pub fn new(disk_file: T, _ex: &Executor) -> Self {
Self {
blocking_pool: BlockingPool::new(1, Duration::from_secs(10)),

View file

@ -31,6 +31,7 @@ use cros_async::IoSourceExt;
use thiserror::Error as ThisError;
mod asynchronous;
#[allow(unused)]
pub(crate) use asynchronous::AsyncDiskFileWrapper;
#[cfg(feature = "qcow")]
mod qcow;
@ -63,8 +64,11 @@ pub use composite::PartitionInfo;
#[cfg(feature = "composite-disk")]
pub use gpt::Error as GptError;
#[cfg(feature = "android-sparse")]
mod android_sparse;
#[cfg(feature = "android-sparse")]
use android_sparse::AndroidSparse;
#[cfg(feature = "android-sparse")]
use android_sparse::SPARSE_HEADER_MAGIC;
/// Nesting depth limit for disk formats that can open other disk files.
@ -76,6 +80,7 @@ pub enum Error {
BlockDeviceNew(base::Error),
#[error("requested file conversion not supported")]
ConversionNotSupported,
#[cfg(feature = "android-sparse")]
#[error("failure in android sparse disk: {0}")]
CreateAndroidSparseDisk(android_sparse::Error),
#[cfg(feature = "composite-disk")]
@ -242,11 +247,13 @@ pub fn detect_image_type(file: &File) -> Result<ImageType> {
}
}
#[allow(unused_variables)] // magic4 is only used with the qcow or android-sparse features.
if let Some(magic4) = magic.data.get(0..4) {
#[cfg(feature = "qcow")]
if magic4 == QCOW_MAGIC.to_be_bytes() {
return Ok(ImageType::Qcow2);
}
#[cfg(feature = "android-sparse")]
if magic4 == SPARSE_HEADER_MAGIC.to_le_bytes() {
return Ok(ImageType::AndroidSparse);
}
@ -296,6 +303,7 @@ pub fn create_disk_file(
.map_err(Error::CreateCompositeDisk)?,
) as Box<dyn DiskFile>
}
#[cfg(feature = "android-sparse")]
ImageType::AndroidSparse => {
Box::new(AndroidSparse::from_file(raw_image).map_err(Error::CreateAndroidSparseDisk)?)
as Box<dyn DiskFile>

View file

@ -80,6 +80,7 @@ mod tests {
}
#[test]
#[cfg(feature = "android-sparse")]
fn detect_image_type_android_sparse() {
let mut t = tempfile::tempfile().unwrap();
// Write the Android sparse magic signature. The rest of the header is not filled in, so if