From de0a8458f0030feff22abeb59c0d771e708003c5 Mon Sep 17 00:00:00 2001 From: Andrew Chin Date: Sat, 2 Oct 2021 18:31:53 -0400 Subject: [PATCH 1/2] 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() }; From 14757fa894614209eb5029a00784c53813bbcc7c Mon Sep 17 00:00:00 2001 From: Andrew Chin Date: Sat, 20 Nov 2021 16:51:08 -0500 Subject: [PATCH 2/2] Updated README and lib.rs about the `flate2` feature --- README.md | 1 + src/lib.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/README.md b/README.md index 2209e80d..fe9fcb8a 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,7 @@ details. The following cargo features are available: * `chrono` -- Default. Optional. This feature enables a few methods that return values as `DateTime` objects. +* `flate2` -- Default. Optional. This feature enables parsing gzip compressed `/proc/config.gz` file via the `procfs::kernel_config` method. * `backtrace` -- Optional. This feature lets you get a stack trace whenever an `InternalError` is raised. ## Minimum Rust Version diff --git a/src/lib.rs b/src/lib.rs index bac9f7a5..d9f79aca 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -41,6 +41,7 @@ //! The following cargo features are available: //! //! * `chrono` -- Default. Optional. This feature enables a few methods that return values as `DateTime` objects. +//! * `flate2` -- Default. Optional. This feature enables parsing gzip compressed `/proc/config.gz` file via the `procfs::kernel_config` method. //! * `backtrace` -- Optional. This feature lets you get a stack trace whenever an `InternalError` is raised. //! //! # Examples