feat(sysctl): allow disabling cache via NOCACHE variable

This commit is contained in:
Orhun Parmaksız 2022-03-18 15:47:44 +03:00
parent fa7a805214
commit 584e060aa4
No known key found for this signature in database
GPG key ID: F83424824B3E4B90
3 changed files with 16 additions and 10 deletions

View file

@ -2,11 +2,12 @@ use crate::cache::{Cache, CacheData};
use crate::config::Config;
use crate::error::Result;
use crate::parsers::{parse_kernel_docs, KERNEL_DOCS_PATH};
use crate::sysctl::parameter::{Parameter, PARAMETERS_CACHE_LABEL};
use crate::sysctl::parameter::Parameter;
use crate::sysctl::section::Section;
use crate::sysctl::PROC_PATH;
use crate::sysctl::{DISABLE_CACHE_ENV, PARAMETERS_CACHE_LABEL, PROC_PATH};
use rayon::prelude::*;
use std::convert::TryFrom;
use std::env;
use std::path::{Path, PathBuf};
use std::result::Result as StdResult;
use sysctl::{CtlFlags, CtlIter, Sysctl as SysctlImpl};
@ -90,10 +91,12 @@ impl Sysctl {
}
}
self.update_docs(path)?;
cache.write(
CacheData::new(&self.parameters, path)?,
PARAMETERS_CACHE_LABEL,
)?;
if env::var(DISABLE_CACHE_ENV).is_err() {
cache.write(
CacheData::new(&self.parameters, path)?,
PARAMETERS_CACHE_LABEL,
)?;
}
} else {
eprintln!("warning: `Linux kernel documentation cannot be found. Please specify a path via '-D' argument`");
}

View file

@ -11,7 +11,7 @@ pub mod display;
pub mod parameter;
/// Path of the kernel parameters.
pub const PROC_PATH: &str = "/proc/sys/";
pub(crate) const PROC_PATH: &str = "/proc/sys/";
/// Default configuration file to preload values from.
pub const DEFAULT_PRELOAD: &str = "/etc/sysctl.conf";
@ -32,3 +32,9 @@ pub const DEPRECATED_PARAMS: &[&str] = &["base_reachable_time", "retrans_time"];
/// Environment variable for setting the path of the Linux kernel documentation.
pub const KERNEL_DOCS_ENV: &str = "KERNEL_DOCS";
/// Environment variable for disabling the cache.
pub(crate) const DISABLE_CACHE_ENV: &str = "NOCACHE";
/// Label for caching the kernel parameters.
pub(crate) const PARAMETERS_CACHE_LABEL: &str = "parameters";

View file

@ -8,9 +8,6 @@ use std::io::Write;
use std::path::PathBuf;
use sysctl::{Ctl, Sysctl as SysctlImpl};
/// Label for caching the kernel parameters.
pub(crate) const PARAMETERS_CACHE_LABEL: &str = "parameters";
/// Representation of a kernel parameter.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Parameter {