Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exported from_subsystem_sysname() #44

Merged
merged 2 commits into from
Sep 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 56 additions & 1 deletion src/device.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -112,6 +112,61 @@ 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<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 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))
}

/// 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
Loading