Skip to content

Commit

Permalink
Merge pull request #566 from zeenix/random-fixes-and-improvs
Browse files Browse the repository at this point in the history
A few fixes and improvements
  • Loading branch information
zeenix authored Feb 4, 2024
2 parents 09b6950 + e838840 commit 185ed64
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 81 deletions.
20 changes: 10 additions & 10 deletions zbus/src/address/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ mod tests {
#[cfg(windows)]
use crate::address::transport::{Autolaunch, AutolaunchScope};
use crate::{
address::transport::{Unix, UnixPath},
address::transport::{Unix, UnixSocket},
Error,
};
use std::str::FromStr;
Expand Down Expand Up @@ -240,17 +240,17 @@ mod tests {
}
assert_eq!(
Address::from_str("unix:path=/tmp/dbus-foo").unwrap(),
Transport::Unix(Unix::new(UnixPath::File("/tmp/dbus-foo".into()))).into(),
Transport::Unix(Unix::new(UnixSocket::File("/tmp/dbus-foo".into()))).into(),
);
#[cfg(target_os = "linux")]
assert_eq!(
Address::from_str("unix:abstract=/tmp/dbus-foo").unwrap(),
Transport::Unix(Unix::new(UnixPath::Abstract("/tmp/dbus-foo".into()))).into(),
Transport::Unix(Unix::new(UnixSocket::Abstract("/tmp/dbus-foo".into()))).into(),
);
let guid = crate::Guid::generate();
assert_eq!(
Address::from_str(&format!("unix:path=/tmp/dbus-foo,guid={guid}")).unwrap(),
Address::from(Transport::Unix(Unix::new(UnixPath::File(
Address::from(Transport::Unix(Unix::new(UnixSocket::File(
"/tmp/dbus-foo".into()
))))
.set_guid(guid.clone())
Expand Down Expand Up @@ -320,32 +320,32 @@ mod tests {
);
assert_eq!(
Address::from_str("unix:dir=/some/dir").unwrap(),
Transport::Unix(Unix::new(UnixPath::Dir("/some/dir".into()))).into(),
Transport::Unix(Unix::new(UnixSocket::Dir("/some/dir".into()))).into(),
);
assert_eq!(
Address::from_str("unix:tmpdir=/some/dir").unwrap(),
Transport::Unix(Unix::new(UnixPath::TmpDir("/some/dir".into()))).into(),
Transport::Unix(Unix::new(UnixSocket::TmpDir("/some/dir".into()))).into(),
);
}

#[test]
fn stringify_dbus_addresses() {
assert_eq!(
Address::from(Transport::Unix(Unix::new(UnixPath::File(
Address::from(Transport::Unix(Unix::new(UnixSocket::File(
"/tmp/dbus-foo".into()
))))
.to_string(),
"unix:path=/tmp/dbus-foo",
);
assert_eq!(
Address::from(Transport::Unix(Unix::new(UnixPath::Dir(
Address::from(Transport::Unix(Unix::new(UnixSocket::Dir(
"/tmp/dbus-foo".into()
))))
.to_string(),
"unix:dir=/tmp/dbus-foo",
);
assert_eq!(
Address::from(Transport::Unix(Unix::new(UnixPath::TmpDir(
Address::from(Transport::Unix(Unix::new(UnixSocket::TmpDir(
"/tmp/dbus-foo".into()
))))
.to_string(),
Expand All @@ -354,7 +354,7 @@ mod tests {
// FIXME: figure out how to handle abstract on Windows
#[cfg(target_os = "linux")]
assert_eq!(
Address::from(Transport::Unix(Unix::new(UnixPath::Abstract(
Address::from(Transport::Unix(Unix::new(UnixSocket::Abstract(
"/tmp/dbus-foo".into()
))))
.to_string(),
Expand Down
4 changes: 2 additions & 2 deletions zbus/src/address/transport/launchd.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{Transport, Unix, UnixPath};
use super::{Transport, Unix, UnixSocket};
use crate::{process::run, Result};
use std::collections::HashMap;

Expand Down Expand Up @@ -39,7 +39,7 @@ impl Launchd {
crate::Error::Address(format!("Unable to parse launchctl output as UTF-8: {}", e))
})?;

Ok(Transport::Unix(Unix::new(UnixPath::File(
Ok(Transport::Unix(Unix::new(UnixSocket::File(
addr.trim().into(),
))))
}
Expand Down
10 changes: 5 additions & 5 deletions zbus/src/address/transport/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use std::{
};

mod unix;
pub use unix::{Unix, UnixPath};
pub use unix::{Unix, UnixSocket};
mod tcp;
pub use tcp::{Tcp, TcpTransportFamily};
#[cfg(windows)]
Expand Down Expand Up @@ -88,14 +88,14 @@ impl Transport {
// https://github.com/haraldh/rust_uds_windows/issues/14
let addr = match unix.take_path() {
#[cfg(unix)]
UnixPath::File(path) => SocketAddr::from_pathname(path)?,
UnixSocket::File(path) => SocketAddr::from_pathname(path)?,
#[cfg(windows)]
UnixPath::File(path) => path,
UnixSocket::File(path) => path,
#[cfg(target_os = "linux")]
UnixPath::Abstract(name) => {
UnixSocket::Abstract(name) => {
SocketAddr::from_abstract_name(name.as_encoded_bytes())?
}
UnixPath::Dir(_) | UnixPath::TmpDir(_) => {
UnixSocket::Dir(_) | UnixSocket::TmpDir(_) => {
// you can't connect to a unix:dir
return Err(Error::Unsupported);
}
Expand Down
45 changes: 24 additions & 21 deletions zbus/src/address/transport/unix.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#[cfg(target_os = "linux")]
use std::ffi::OsString;
use std::{
collections::HashMap,
ffi::{OsStr, OsString},
ffi::OsStr,
fmt::{Display, Formatter},
path::PathBuf,
};

#[cfg(unix)]
Expand All @@ -10,22 +13,22 @@ use super::encode_percents;
/// A Unix domain socket transport in a D-Bus address.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Unix {
path: UnixPath,
path: UnixSocket,
}

impl Unix {
/// Create a new Unix transport with the given path.
pub fn new(path: UnixPath) -> Self {
pub fn new(path: UnixSocket) -> Self {
Self { path }
}

/// The path.
pub fn path(&self) -> &UnixPath {
pub fn path(&self) -> &UnixSocket {
&self.path
}

/// Take the path, consuming `self`.
pub fn take_path(self) -> UnixPath {
pub fn take_path(self) -> UnixSocket {
self.path
}

Expand All @@ -36,17 +39,17 @@ impl Unix {
let dir = opts.get("dir");
let tmpdir = opts.get("tmpdir");
let path = match (path, abs, dir, tmpdir) {
(Some(p), None, None, None) => UnixPath::File(OsString::from(p)),
(Some(p), None, None, None) => UnixSocket::File(PathBuf::from(p)),
#[cfg(target_os = "linux")]
(None, Some(p), None, None) => UnixPath::Abstract(OsString::from(p)),
(None, Some(p), None, None) => UnixSocket::Abstract(OsString::from(p)),
#[cfg(not(target_os = "linux"))]
(None, Some(_), None, None) => {
return Err(crate::Error::Address(
"abstract sockets currently Linux-only".to_owned(),
));
}
(None, None, Some(p), None) => UnixPath::Dir(OsString::from(p)),
(None, None, None, Some(p)) => UnixPath::TmpDir(OsString::from(p)),
(None, None, Some(p), None) => UnixSocket::Dir(PathBuf::from(p)),
(None, None, None, Some(p)) => UnixSocket::TmpDir(PathBuf::from(p)),
_ => {
return Err(crate::Error::Address("unix: address is invalid".to_owned()));
}
Expand All @@ -65,9 +68,9 @@ impl Display for Unix {
/// A Unix domain socket path in a D-Bus address.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum UnixPath {
pub enum UnixSocket {
/// A path to a unix domain socket on the filesystem.
File(OsString),
File(PathBuf),
/// A abstract unix domain socket name.
#[cfg(target_os = "linux")]
Abstract(OsString),
Expand All @@ -78,16 +81,16 @@ pub enum UnixPath {
/// This address is mostly relevant to server (typically bus broker) implementations.
///
/// [UNIX domain socket address]: https://dbus.freedesktop.org/doc/dbus-specification.html#transports-unix-domain-sockets-addresses
Dir(OsString),
Dir(PathBuf),
/// The same as UnixDir, except that on platforms with abstract sockets, the server may attempt
/// to create an abstract socket whose name starts with this directory instead of a path-based
/// socket.
///
/// This address is mostly relevant to server (typically bus broker) implementations.
TmpDir(OsString),
TmpDir(PathBuf),
}

impl Display for UnixPath {
impl Display for UnixSocket {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn fmt_unix_path(f: &mut Formatter<'_>, path: &OsStr) -> std::fmt::Result {
#[cfg(unix)]
Expand All @@ -104,22 +107,22 @@ impl Display for UnixPath {
}

match self {
UnixPath::File(path) => {
UnixSocket::File(path) => {
f.write_str("path=")?;
fmt_unix_path(f, path)?;
fmt_unix_path(f, path.as_os_str())?;
}
#[cfg(target_os = "linux")]
UnixPath::Abstract(name) => {
UnixSocket::Abstract(name) => {
f.write_str("abstract=")?;
fmt_unix_path(f, name)?;
}
UnixPath::Dir(path) => {
UnixSocket::Dir(path) => {
f.write_str("dir=")?;
fmt_unix_path(f, path)?;
fmt_unix_path(f, path.as_os_str())?;
}
UnixPath::TmpDir(path) => {
UnixSocket::TmpDir(path) => {
f.write_str("tmpdir=")?;
fmt_unix_path(f, path)?;
fmt_unix_path(f, path.as_os_str())?;
}
}

Expand Down
6 changes: 6 additions & 0 deletions zbus/src/blocking/connection/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use zvariant::{ObjectPath, Str};
use crate::{
address::Address,
blocking::Connection,
connection::socket::BoxedSplit,
names::{UniqueName, WellKnownName},
object_server::Interface,
utils::block_on,
Expand Down Expand Up @@ -68,6 +69,11 @@ impl<'a> Builder<'a> {
Self(crate::connection::Builder::tcp_stream(stream))
}

/// Create a builder for connection that will use the given socket.
pub fn socket<S: Into<BoxedSplit>>(socket: S) -> Self {
Self(crate::connection::Builder::socket(socket))
}

/// Specify the mechanisms to use during authentication.
pub fn auth_mechanisms(self, auth_mechanisms: &[AuthMechanism]) -> Self {
Self(self.0.auth_mechanisms(auth_mechanisms))
Expand Down
14 changes: 7 additions & 7 deletions zbus/src/blocking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,27 @@ pub use object_server::ObjectServer;
pub mod proxy;
pub use proxy::Proxy;

#[deprecated(note = "Use `proxy::Builder` instead")]
#[deprecated(since = "4.0.0", note = "Use `proxy::Builder` instead")]
#[doc(hidden)]
pub use proxy::Builder as ProxyBuilder;
#[deprecated(note = "Use `proxy::OwnerChangedIterator` instead")]
#[deprecated(since = "4.0.0", note = "Use `proxy::OwnerChangedIterator` instead")]
#[doc(hidden)]
pub use proxy::OwnerChangedIterator;
#[deprecated(note = "Use `proxy::PropertyChanged` instead")]
#[deprecated(since = "4.0.0", note = "Use `proxy::PropertyChanged` instead")]
#[doc(hidden)]
pub use proxy::PropertyChanged;
#[deprecated(note = "Use `proxy::PropertyIterator` instead")]
#[deprecated(since = "4.0.0", note = "Use `proxy::PropertyIterator` instead")]
#[doc(hidden)]
pub use proxy::PropertyIterator;
#[deprecated(note = "Use `proxy::SignalIterator` instead")]
#[deprecated(since = "4.0.0", note = "Use `proxy::SignalIterator` instead")]
#[doc(hidden)]
pub use proxy::SignalIterator;

#[deprecated(note = "Use `object_server::InterfaceRef` instead")]
#[deprecated(since = "4.0.0", note = "Use `object_server::InterfaceRef` instead")]
#[doc(hidden)]
pub use object_server::InterfaceRef;

#[deprecated(note = "Use `connection::Builder` instead")]
#[deprecated(since = "4.0.0", note = "Use `connection::Builder` instead")]
#[doc(hidden)]
pub use connection::Builder;

Expand Down
4 changes: 2 additions & 2 deletions zbus/src/connection/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use crate::{

use super::{
handshake::{AuthMechanism, Authenticated},
socket::{BoxedSplit, ReadHalf, Socket, Split, WriteHalf},
socket::{BoxedSplit, ReadHalf, Split, WriteHalf},
};

const DEFAULT_MAX_QUEUED: usize = 64;
Expand Down Expand Up @@ -160,7 +160,7 @@ impl<'a> Builder<'a> {
}

/// Create a builder for connection that will use the given socket.
pub fn socket<S: Socket + 'static>(socket: S) -> Self {
pub fn socket<S: Into<BoxedSplit>>(socket: S) -> Self {
Self::new(Target::Socket(socket.into()))
}

Expand Down
Loading

0 comments on commit 185ed64

Please sign in to comment.