From 44762216cfcdcd6da28c99c598bd580b2f678945 Mon Sep 17 00:00:00 2001 From: Christian Blichmann Date: Mon, 5 Sep 2022 14:29:55 +0200 Subject: [PATCH] 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 Reviewed-by: David Stevens Tested-by: Christian Blichmann --- disk/Cargo.toml | 1 + disk/src/asynchronous.rs | 1 + disk/src/disk.rs | 8 ++++++++ disk/src/sys/unix.rs | 1 + 4 files changed, 11 insertions(+) diff --git a/disk/Cargo.toml b/disk/Cargo.toml index 0b4e28ae73..20f4fa1239 100644 --- a/disk/Cargo.toml +++ b/disk/Cargo.toml @@ -8,6 +8,7 @@ edition = "2021" path = "src/disk.rs" [features] +android-sparse = [] composite-disk = ["crc32fast", "protos", "protobuf", "uuid"] qcow = [] diff --git a/disk/src/asynchronous.rs b/disk/src/asynchronous.rs index 1f6f26e65a..e205ad4ddd 100644 --- a/disk/src/asynchronous.rs +++ b/disk/src/asynchronous.rs @@ -34,6 +34,7 @@ pub struct AsyncDiskFileWrapper { } impl AsyncDiskFileWrapper { + #[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)), diff --git a/disk/src/disk.rs b/disk/src/disk.rs index 2fe8cfb4ec..12e0e2e2cd 100644 --- a/disk/src/disk.rs +++ b/disk/src/disk.rs @@ -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 { } } + #[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 } + #[cfg(feature = "android-sparse")] ImageType::AndroidSparse => { Box::new(AndroidSparse::from_file(raw_image).map_err(Error::CreateAndroidSparseDisk)?) as Box diff --git a/disk/src/sys/unix.rs b/disk/src/sys/unix.rs index 60d16262de..b32bf4974d 100644 --- a/disk/src/sys/unix.rs +++ b/disk/src/sys/unix.rs @@ -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