From de0a8458f0030feff22abeb59c0d771e708003c5 Mon Sep 17 00:00:00 2001 From: Andrew Chin Date: Sat, 2 Oct 2021 18:31:53 -0400 Subject: [PATCH] Make the flate2 dependency optional --- Cargo.toml | 4 ++-- src/lib.rs | 23 +++++++++++++++++------ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ac916936..0916fbec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ license = "MIT OR Apache-2.0" edition = "2018" [features] -default = ["chrono"] +default = ["chrono", "flate2"] [dependencies] libc = "0.2" @@ -21,7 +21,7 @@ lazy_static = "1" chrono = {version = "0.4", optional = true } byteorder = {version="1", features=["i128"]} hex = "0.4" -flate2 = "1" +flate2 = { version = "1", optional = true } backtrace = { version = "0.3", optional = true } [dev-dependencies] diff --git a/src/lib.rs b/src/lib.rs index f7950e0e..bac9f7a5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -671,13 +671,24 @@ pub enum ConfigSetting { /// /// If CONFIG_KCONFIG_PROC is available, the config is read from `/proc/config.gz`. /// Else look in `/boot/config-$(uname -r)` or `/boot/config` (in that order). +/// +/// # Notes +/// Reading the compress `/proc/config.gz` is only supported if the `flate2` feature is enabled +/// (which it is by default). +#[cfg_attr(feature = "flate2", doc = "The flate2 feature is currently enabled")] +#[cfg_attr(not(feature = "flate2"), doc = "The flate2 feature is NOT currently enabled")] pub fn kernel_config() -> ProcResult> { - use flate2::read::GzDecoder; - - let reader: Box = if Path::new(PROC_CONFIG_GZ).exists() { - let file = FileWrapper::open(PROC_CONFIG_GZ)?; - let decoder = GzDecoder::new(file); - Box::new(BufReader::new(decoder)) + let reader: Box = if Path::new(PROC_CONFIG_GZ).exists() && cfg!(feature = "flate2") { + #[cfg(feature = "flate2")] + { + let file = FileWrapper::open(PROC_CONFIG_GZ)?; + let decoder = flate2::read::GzDecoder::new(file); + Box::new(BufReader::new(decoder)) + } + #[cfg(not(feature = "flate2"))] + { + unreachable!("flate2 feature not enabled") + } } else { let mut kernel: libc::utsname = unsafe { mem::zeroed() };