Skip to content

Commit

Permalink
🔧 Add XDG_... <standard_session_servicedirs>
Browse files Browse the repository at this point in the history
  • Loading branch information
jokeyrhyme committed Nov 17, 2024
1 parent e582891 commit fc10870
Showing 1 changed file with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -124,9 +126,23 @@ impl TryFrom<Document> 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
Expand Down Expand Up @@ -548,6 +564,15 @@ impl From<RuleAttributes> for SendOperation {
}
}

const DEFAULT_DATA_DIRS: &[&str] = &["/usr/local/share", "/usr/share"];

fn xdg_data_dirs() -> Vec<PathBuf> {
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",
Expand Down Expand Up @@ -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"))
);
}

Expand Down

0 comments on commit fc10870

Please sign in to comment.