Skip to content

Commit

Permalink
🔧 Support <includedir>
Browse files Browse the repository at this point in the history
A little less efficient than @elmarco 's implementation over in dbus2#23
  • Loading branch information
jokeyrhyme committed Nov 17, 2024
1 parent f204c7d commit 25baa78
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 5 deletions.
7 changes: 6 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,12 @@ impl TryFrom<Document> for Config {
bc.auth.insert(s);
}
Element::Fork => bc.fork = true,
Element::Include(_) => { /* no-op */ }
Element::Include(_) => {
// NO-OP: removed during `Document::resolve_includes`
}
Element::Includedir(_) => {
// NO-OP: removed during `Document::resolve_includedirs`
}
Element::KeepUmask => bc.keep_umask = true,
Element::Limit => {
warn!("warning: busd does not implement `<limit>`");
Expand Down
47 changes: 44 additions & 3 deletions src/config/xml.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{
env::current_dir,
fs::read_to_string,
ffi::OsString,
fs::{read_dir, read_to_string},
path::{Path, PathBuf},
str::FromStr,
};
Expand Down Expand Up @@ -36,7 +37,47 @@ impl Document {

let mut doc = Document::from_str(&text)?;
doc.file_path = Some(file_path.as_ref().to_path_buf());
doc.resolve_includes()
doc.resolve_includedirs()?.resolve_includes()
}

fn resolve_includedirs(self) -> Result<Document> {
let base_path = self.base_path()?;
let Document {
busconfig,
file_path,
} = self;

let mut doc = Document {
busconfig: vec![],
file_path: None,
};

for el in busconfig {
match el {
Element::Includedir(dir_path) => {
let dir_path = match resolve_include_path(&base_path, &dir_path) {
Ok(ok) => ok,
Err(err) => {
error!("'{}' should be a valid file path", dir_path.display());
return Err(err);
}
};
for entry in read_dir(dir_path)? {
let path = entry?.path();
if path.extension() == Some(&OsString::from("conf")) && path.is_file() {
doc.busconfig.push(Element::Include(IncludeElement {
file_path: path,
..Default::default()
}));
}
}
}
_ => doc.busconfig.push(el),
}
}

doc.file_path = file_path;
Ok(doc)
}

fn resolve_includes(self) -> Result<Document> {
Expand Down Expand Up @@ -117,7 +158,7 @@ pub enum Element {
Auth(String),
Fork,
Include(IncludeElement),
// TODO: support `<includedir>`
Includedir(PathBuf),
KeepUmask,
Listen(String),
Limit,
Expand Down
12 changes: 11 additions & 1 deletion tests/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ fn config_read_file_with_includes_ok() {
Config {
auth: HashSet::from_iter(vec![String::from("ANONYMOUS"), String::from("EXTERNAL"),]),
listen: HashSet::from_iter(vec![
String::from("unix:path=/tmp/a"), // via <includedir>
String::from("unix:path=/tmp/b"), // via <includedir>
// should be no "unix:path=/tmp/not_included" as that file ends in .xml
String::from("unix:path=/tmp/foo"),
String::from("tcp:host=localhost,port=1234"),
String::from("tcp:host=localhost,port=1234"), // via <include>
]),
policies: vec![
Policy::DefaultContext(vec![
Expand Down Expand Up @@ -63,3 +66,10 @@ fn config_read_file_with_transitive_missing_include_err() {
Config::read_file("./tests/fixtures/transitive_missing_include.conf")
.expect("should read and parse XML input");
}

#[should_panic]
#[test]
fn config_read_file_with_missing_includedir_err() {
Config::read_file("./tests/fixtures/missing_includedir.conf")
.expect("should read and parse XML input");
}
5 changes: 5 additions & 0 deletions tests/fixtures/includedir/a.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-Bus Bus Configuration 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>
<listen>unix:path=/tmp/a</listen>
</busconfig>
5 changes: 5 additions & 0 deletions tests/fixtures/includedir/b.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-Bus Bus Configuration 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>
<listen>unix:path=/tmp/b</listen>
</busconfig>
5 changes: 5 additions & 0 deletions tests/fixtures/includedir/not_included.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-Bus Bus Configuration 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>
<listen>unix:path=/tmp/not_included</listen>
</busconfig>
5 changes: 5 additions & 0 deletions tests/fixtures/missing_includedir.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-Bus Bus Configuration 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>
<includedir>./missing</includedir>
</busconfig>
1 change: 1 addition & 0 deletions tests/fixtures/valid.conf
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
</policy>
<include>./valid_included.conf</include>
<include ignore_missing="yes">./valid_missing.conf</include>
<includedir>./includedir</includedir>
</busconfig>

0 comments on commit 25baa78

Please sign in to comment.