Skip to content

Commit

Permalink
device: Add context variant for from_subsystem_sysname
Browse files Browse the repository at this point in the history
  • Loading branch information
Drakulix committed Sep 22, 2023
1 parent 197eb7d commit 73cc15a
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
let subsystem = CString::new(subsystem.as_bytes())
.ok()
Expand All @@ -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<Self> {
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
Expand Down

0 comments on commit 73cc15a

Please sign in to comment.