Skip to content

Commit

Permalink
Merge pull request #157 from eminence/flate_optional
Browse files Browse the repository at this point in the history
Make the flate2 dependency optional
  • Loading branch information
eminence committed Nov 26, 2021
2 parents 5d84fcd + 14757fa commit 8081e99
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Expand Up @@ -12,7 +12,7 @@ license = "MIT OR Apache-2.0"
edition = "2018"

[features]
default = ["chrono"]
default = ["chrono", "flate2"]

[dependencies]
libc = "0.2"
Expand All @@ -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]
Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -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
Expand Down
24 changes: 18 additions & 6 deletions src/lib.rs
Expand Up @@ -44,6 +44,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
Expand Down Expand Up @@ -675,13 +676,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<HashMap<String, ConfigSetting>> {
use flate2::read::GzDecoder;

let reader: Box<dyn BufRead> = 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<dyn BufRead> = 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() };

Expand Down

0 comments on commit 8081e99

Please sign in to comment.