Skip to content

Commit

Permalink
Rework plugins_loading/search_dirs config option (#1278)
Browse files Browse the repository at this point in the history
* Rework `plugins_loading/search_dirs` config option

* Add license header in `lib_search_dirs.rs`

* Address review comment

* Remove `LIB_DEFAULT_SEARCH_PATHS`
  • Loading branch information
fuzzypixelz authored Aug 1, 2024
1 parent f394867 commit 5d09cf7
Show file tree
Hide file tree
Showing 13 changed files with 293 additions and 73 deletions.
9 changes: 7 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ rustls-native-certs = "0.7.0"
rustls-pemfile = "2.0.0"
rustls-webpki = "0.102.0"
rustls-pki-types = "1.1.0"
schemars = "0.8.12"
schemars = { version = "0.8.12", features = ["either"] }
secrecy = { version = "0.8.0", features = ["serde", "alloc"] }
serde = { version = "1.0.154", default-features = false, features = [
"derive",
Expand Down Expand Up @@ -188,6 +188,7 @@ webpki-roots = "0.26.0"
winapi = { version = "0.3.9", features = ["iphlpapi"] }
x509-parser = "0.16.0"
z-serial = "0.2.3"
either = "1.13.0"
zenoh-ext = { version = "0.11.0-dev", path = "zenoh-ext" }
zenoh-shm = { version = "0.11.0-dev", path = "commons/zenoh-shm" }
zenoh-result = { version = "0.11.0-dev", path = "commons/zenoh-result", default-features = false }
Expand Down
14 changes: 9 additions & 5 deletions DEFAULT_CONFIG.json5
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@
// /// Id has to be unique within the rule set
// "id": "rule1",
// "messages": [
// "put", "delete", "declare_subscriber",
// "put", "delete", "declare_subscriber",
// "query", "reply", "declare_queryable",
// ],
// "flows":["egress","ingress"],
Expand All @@ -211,7 +211,7 @@
// {
// "id": "rule2",
// "messages": [
// "put", "delete", "declare_subscriber",
// "put", "delete", "declare_subscriber",
// "query", "reply", "declare_queryable",
// ],
// "flows":["ingress"],
Expand Down Expand Up @@ -462,11 +462,15 @@
///
//
// plugins_loading: {
// // Enable plugins loading.
// /// Enable plugins loading.
// enabled: false,
// /// Directories where plugins configured by name should be looked for. Plugins configured by __path__ are not subject to lookup.
// /// If `enabled: true` and `search_dirs` is not specified then `search_dirs` falls back to the default value: ".:~/.zenoh/lib:/opt/homebrew/lib:/usr/local/lib:/usr/lib"
// search_dirs: [],
// /// Directories are specified as object with fields `kind` and `value` is accepted.
// /// 1. If `kind` is `current_exe_parent`, then the parent of the current executable's directory is searched and `value` should be `null`.
// /// In Bash notation, `{ "kind": "current_exe_parent" }` equals `$(dirname $(which zenohd))` while `"."` equals `$PWD`.
// /// 2. If `kind` is `path`, then `value` is interpreted as a filesystem path. Simply supplying a string instead of a object is equivalent to this.
// /// If `enabled: true` and `search_dirs` is not specified then `search_dirs` falls back to the default value:
// search_dirs: [{ "kind": "current_exe_parent" }, ".", "~/.zenoh/lib", "/opt/homebrew/lib", "/usr/local/lib", "/usr/lib"],
// },
// /// Plugins are only loaded if `plugins_loading: { enabled: true }` and present in the configuration when starting.
// /// Once loaded, they may react to changes in the configuration made through the zenoh instance's adminspace.
Expand Down
22 changes: 3 additions & 19 deletions commons/zenoh-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use zenoh_protocol::{
transport::{BatchSize, TransportSn},
};
use zenoh_result::{bail, zerror, ZResult};
use zenoh_util::LibLoader;
use zenoh_util::{LibLoader, LibSearchDirs};

pub mod mode_dependent;
pub use mode_dependent::*;
Expand Down Expand Up @@ -547,7 +547,7 @@ validated_struct::validator! {
pub plugins_loading: #[derive(Default)]
PluginsLoading {
pub enabled: bool,
pub search_dirs: Option<Vec<String>>, // TODO (low-prio): Switch this String to a PathBuf? (applies to other paths in the config as well)
pub search_dirs: LibSearchDirs,
},
#[validated(recursive_accessors)]
/// The configuration for plugins.
Expand All @@ -573,19 +573,6 @@ fn set_false() -> bool {
false
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PluginSearchDirs(Vec<String>);
impl Default for PluginSearchDirs {
fn default() -> Self {
Self(
(*zenoh_util::LIB_DEFAULT_SEARCH_PATHS)
.split(':')
.map(|c| c.to_string())
.collect(),
)
}
}

#[test]
fn config_deser() {
let config = Config::from_deserializer(
Expand Down Expand Up @@ -763,10 +750,7 @@ impl Config {

pub fn libloader(&self) -> LibLoader {
if self.plugins_loading.enabled {
match self.plugins_loading.search_dirs() {
Some(dirs) => LibLoader::new(dirs, true),
None => LibLoader::default(),
}
LibLoader::new(self.plugins_loading.search_dirs().clone())
} else {
LibLoader::empty()
}
Expand Down
2 changes: 2 additions & 0 deletions commons/zenoh-util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ shellexpand = { workspace = true }
zenoh-core = { workspace = true }
zenoh-result = { workspace = true, features = ["default"] }
const_format = { workspace = true }
serde = { workspace = true, features = ["default"] }
serde_json = { workspace = true }

[target.'cfg(windows)'.dependencies]
winapi = { workspace = true }
Expand Down
2 changes: 2 additions & 0 deletions commons/zenoh-util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ use lazy_static::lazy_static;

pub mod ffi;
mod lib_loader;
pub mod lib_search_dirs;
pub mod net;
pub mod time_range;

pub use lib_loader::*;
pub mod timer;
pub use timer::*;
pub mod log;
pub use lib_search_dirs::*;
pub use log::*;

/// The "ZENOH_HOME" environment variable name
Expand Down
45 changes: 10 additions & 35 deletions commons/zenoh-util/src/lib_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ use tracing::{debug, warn};
use zenoh_core::{zconfigurable, zerror};
use zenoh_result::{bail, ZResult};

use crate::LibSearchDirs;

zconfigurable! {
/// The libraries prefix for the current platform (usually: `"lib"`)
pub static ref LIB_PREFIX: String = DLL_PREFIX.to_string();
/// The libraries suffix for the current platform (`".dll"` or `".so"` or `".dylib"`...)
pub static ref LIB_SUFFIX: String = DLL_SUFFIX.to_string();
/// The default list of paths where to search for libraries to load
pub static ref LIB_DEFAULT_SEARCH_PATHS: String = ".:~/.zenoh/lib:/opt/homebrew/lib:/usr/local/lib:/usr/lib".to_string();
}

/// LibLoader allows search for libraries and to load them.
Expand All @@ -44,40 +44,16 @@ impl LibLoader {
LibLoader { search_paths: None }
}

/// Returns the list of search paths used by `LibLoader::default()`
pub fn default_search_paths() -> &'static str {
&LIB_DEFAULT_SEARCH_PATHS
}

/// Creates a new [LibLoader] with a set of paths where the libraries will be searched for.
/// If `exe_parent_dir`is true, the parent directory of the current executable is also added
/// to the set of paths for search.
pub fn new<S>(search_dirs: &[S], exe_parent_dir: bool) -> LibLoader
where
S: AsRef<str>,
{
let mut search_paths: Vec<PathBuf> = vec![];
for s in search_dirs {
match shellexpand::full(s) {
Ok(cow_str) => match PathBuf::from(&*cow_str).canonicalize() {
Ok(path) => search_paths.push(path),
Err(err) => debug!("Cannot search for libraries in {}: {}", cow_str, err),
},
Err(err) => warn!("Cannot search for libraries in '{}': {} ", s.as_ref(), err),
}
}
Self::_new(search_paths, exe_parent_dir)
}
fn _new(mut search_paths: Vec<PathBuf>, exe_parent_dir: bool) -> Self {
if exe_parent_dir {
match std::env::current_exe() {
Ok(path) => match path.parent() {
Some(p) => if p.is_dir() {
search_paths.push(p.canonicalize().unwrap())
},
None => warn!("Can't search for plugins in executable parent directory: no parent directory for {}.", path.to_string_lossy()),
},
Err(e) => warn!("Can't search for plugins in executable parent directory: {}.", e),
pub fn new(dirs: LibSearchDirs) -> LibLoader {
let mut search_paths = Vec::new();

for path in dirs.into_iter() {
match path {
Ok(path) => search_paths.push(path),
Err(err) => tracing::error!("{err}"),
}
}

Expand Down Expand Up @@ -237,7 +213,6 @@ impl LibLoader {

impl Default for LibLoader {
fn default() -> Self {
let paths: Vec<&str> = (*LIB_DEFAULT_SEARCH_PATHS).split(':').collect();
LibLoader::new(&paths, true)
LibLoader::new(LibSearchDirs::default())
}
}
Loading

0 comments on commit 5d09cf7

Please sign in to comment.