diff --git a/src/config.rs b/src/config.rs index fa603c8..00ae0a9 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,9 +1,11 @@ use std::{ collections::HashSet, + env::var, path::{Path, PathBuf}, }; use anyhow::{Error, Result}; +use directories::BaseDirs; use serde::Deserialize; pub mod limits; @@ -124,9 +126,23 @@ impl TryFrom for Config { Element::StandardSessionServicedirs => { // NOTE: because servicedir sequence is important, we cannot de-duplicate this // TODO: warn and then ignore if we aren't reading: /etc/dbus-1/session.conf - // TODO: include XDG_...-based directories + if let Some(base) = BaseDirs::new() { + if let Some(runtime_dir) = base.runtime_dir() { + bc.servicedir + .push(runtime_dir.join("dbus-1/services").to_path_buf()); + } + bc.servicedir + .push(base.data_dir().join("dbus-1/services").to_path_buf()); + } + let mut servicedirs_in_data_dirs = xdg_data_dirs() + .iter() + .map(|p| p.join("dbus-1/services")) + .map(PathBuf::from) + .collect(); + bc.servicedir.append(&mut servicedirs_in_data_dirs); bc.servicedir .push(PathBuf::from("/usr/share/dbus-1/services")); + // TODO: add Windows-specific session directories } Element::StandardSystemServicedirs => { // NOTE: because servicedir sequence is important, we cannot de-duplicate this @@ -548,6 +564,15 @@ impl From for SendOperation { } } +const DEFAULT_DATA_DIRS: &[&str] = &["/usr/local/share", "/usr/share"]; + +fn xdg_data_dirs() -> Vec { + if let Ok(ok) = var("XDG_DATA_DIRS") { + return ok.split(":").map(PathBuf::from).collect(); + } + DEFAULT_DATA_DIRS.iter().map(PathBuf::from).collect() +} + const STANDARD_SYSTEM_SERVICEDIRS: &[&str] = &[ "/usr/local/share/dbus-1/system-services", "/usr/share/dbus-1/system-services", @@ -1087,17 +1112,14 @@ mod tests { let busconfig = Config::parse(input).expect("should parse XML input"); + // TODO: improve test: contents are dynamic depending upon environment variables assert_eq!( - busconfig, - Config { - servicedir: vec![ - PathBuf::from("/example"), - PathBuf::from("/usr/share/dbus-1/services"), - PathBuf::from("/anotherexample"), - PathBuf::from("/usr/share/dbus-1/services"), - ], - ..Default::default() - } + busconfig.servicedir.first(), + Some(&PathBuf::from("/example")) + ); + assert_eq!( + busconfig.servicedir.last(), + Some(&PathBuf::from("/usr/share/dbus-1/services")) ); }