From 197eb7da5ffe32e12bfd2c44735325a350124747 Mon Sep 17 00:00:00 2001 From: Rene Harder Date: Sun, 27 Aug 2023 10:59:02 -0500 Subject: [PATCH 1/2] Exported from_subsystem_sysname() --- src/device.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/device.rs b/src/device.rs index 3a0b279..648bc11 100644 --- a/src/device.rs +++ b/src/device.rs @@ -1,6 +1,6 @@ use std::str; -use std::ffi::{CStr, OsStr}; +use std::ffi::{CStr, CString, OsStr}; use std::io::Result; use std::marker::PhantomData; use std::path::Path; @@ -112,6 +112,31 @@ impl Device { Ok(Self::from_raw(udev, ptr)) } + /// Create new udev device, and fill in information from the sys device + /// and the udev database entry. The device is looked up by the `subsystem` + /// and `sysname` string of the device, like "mem" / "zero", or "block" / "sda". + pub fn from_subsystem_sysname(subsystem: String, sysname: String) -> Result { + let subsystem = CString::new(subsystem.as_bytes()) + .ok() + .ok_or(std::io::Error::from_raw_os_error(libc::EINVAL))?; + + let sysname = CString::new(sysname.as_bytes()) + .ok() + .ok_or(std::io::Error::from_raw_os_error(libc::EINVAL))?; + + let udev = Udev::new()?; + + let ptr = try_alloc!(unsafe { + ffi::udev_device_new_from_subsystem_sysname( + udev.as_raw(), + subsystem.as_ptr(), + sysname.as_ptr(), + ) + }); + + Ok(Self::from_raw(udev, ptr)) + } + /// Creates a rust udev `Device` for a given UNIX device "special file" type and number. /// /// The `dev_type` parameter indicates which of the historical UNIX file-like I/O paradigms the From 73cc15a59970d32b892a0e2389b4d156be5cb605 Mon Sep 17 00:00:00 2001 From: Victoria Brekenfeld Date: Fri, 22 Sep 2023 20:39:34 +0200 Subject: [PATCH 2/2] device: Add context variant for `from_subsystem_sysname` --- src/device.rs | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/device.rs b/src/device.rs index 648bc11..f141c97 100644 --- a/src/device.rs +++ b/src/device.rs @@ -113,8 +113,9 @@ impl Device { } /// Create new udev device, and fill in information from the sys device - /// and the udev database entry. The device is looked up by the `subsystem` - /// and `sysname` string of the device, like "mem" / "zero", or "block" / "sda". + /// and the udev database entry. + /// + /// The device is looked up by the `subsystem` and `sysname` string of the device, like "mem" / "zero", or "block" / "sda". pub fn from_subsystem_sysname(subsystem: String, sysname: String) -> Result { let subsystem = CString::new(subsystem.as_bytes()) .ok() @@ -137,6 +138,35 @@ impl Device { Ok(Self::from_raw(udev, ptr)) } + /// Create new udev device, and fill in information from the sys device + /// and the udev database entry, using an existing `Udev` instance rather than + /// creating a new one. + /// + /// The device is looked up by the `subsystem` and `sysname` string of the device, like "mem" / "zero", or "block" / "sda". + pub fn from_subsystem_sysname_with_context( + udev: Udev, + subsystem: String, + sysname: String, + ) -> Result { + let subsystem = CString::new(subsystem.as_bytes()) + .ok() + .ok_or(std::io::Error::from_raw_os_error(libc::EINVAL))?; + + let sysname = CString::new(sysname.as_bytes()) + .ok() + .ok_or(std::io::Error::from_raw_os_error(libc::EINVAL))?; + + let ptr = try_alloc!(unsafe { + ffi::udev_device_new_from_subsystem_sysname( + udev.as_raw(), + subsystem.as_ptr(), + sysname.as_ptr(), + ) + }); + + Ok(Self::from_raw(udev, ptr)) + } + /// Creates a rust udev `Device` for a given UNIX device "special file" type and number. /// /// The `dev_type` parameter indicates which of the historical UNIX file-like I/O paradigms the