Debian Codegraph @

third_party/rust/libudev/src/enumerator.rs

raw
1 use std::ffi::{OsStr};
2 use std::path::Path;
3
4 use ::handle::prelude::*;
5
6 pub use context::{Context};
7 pub use device::{Device};
8
9
10 /// An enumeration context.
11 ///
12 /// An Enumerator scans `/sys` for devices matching its filters. Filters are added to an Enumerator
13 /// by calling its `match_*` and `nomatch_*` methods. After the filters are setup, the
14 /// `scan_devices()` method finds devices in `/sys` that match the filters.
15 pub struct Enumerator<'a> {
16 context: &'a Context,
17 enumerator: *mut ::ffi::udev_enumerate
18 }
19
20 impl<'a> Drop for Enumerator<'a> {
21 fn drop(&mut self) {
22 unsafe { ::ffi::udev_enumerate_unref(self.enumerator) };
23 }
24 }
25
26 impl<'a> Enumerator<'a> {
27 /// Creates a new Enumerator.
28 pub fn new(context: &'a Context) -> ::Result<Self> {
29 let ptr = try_alloc!(unsafe { ::ffi::udev_enumerate_new(context.as_ptr()) });
30
31 Ok(Enumerator {
32 context: context,
33 enumerator: ptr
34 })
35 }
36
37 /// Adds a filter that matches only initialized devices.
38 pub fn match_is_initialized(&mut self) -> ::Result<()> {
39 ::util::errno_to_result(unsafe {
40 ::ffi::udev_enumerate_add_match_is_initialized(self.enumerator)
41 })
42 }
43
44 /// Adds a filter that matches only devices that belong to the given kernel subsystem.
45 pub fn match_subsystem<T: AsRef<OsStr>>(&mut self, subsystem: T) -> ::Result<()> {
46 let subsystem = try!(::util::os_str_to_cstring(subsystem));
47
48 ::util::errno_to_result(unsafe {
49 ::ffi::udev_enumerate_add_match_subsystem(self.enumerator, subsystem.as_ptr())
50 })
51 }
52
53 /// Adds a filter that matches only devices with the given attribute value.
54 pub fn match_attribute<T: AsRef<OsStr>, U: AsRef<OsStr>>(&mut self, attribute: T, value: U) -> ::Result<()> {
55 let attribute = try!(::util::os_str_to_cstring(attribute));
56 let value = try!(::util::os_str_to_cstring(value));
57
58 ::util::errno_to_result(unsafe {
59 ::ffi::udev_enumerate_add_match_sysattr(self.enumerator, attribute.as_ptr(), value.as_ptr())
60 })
61 }
62
63 /// Adds a filter that matches only devices with the given kernel device name.
64 pub fn match_sysname<T: AsRef<OsStr>>(&mut self, sysname: T) -> ::Result<()> {
65 let sysname = try!(::util::os_str_to_cstring(sysname));
66
67 ::util::errno_to_result(unsafe {
68 ::ffi::udev_enumerate_add_match_sysname(self.enumerator, sysname.as_ptr())
69 })
70 }
71
72 /// Adds a filter that matches only devices with the given property value.
73 pub fn match_property<T: AsRef<OsStr>, U: AsRef<OsStr>>(&mut self, property: T, value: U) -> ::Result<()> {
74 let property = try!(::util::os_str_to_cstring(property));
75 let value = try!(::util::os_str_to_cstring(value));
76
77 ::util::errno_to_result(unsafe {
78 ::ffi::udev_enumerate_add_match_property(self.enumerator, property.as_ptr(), value.as_ptr())
79 })
80 }
81
82 /// Adds a filter that matches only devices with the given tag.
83 pub fn match_tag<T: AsRef<OsStr>>(&mut self, tag: T) -> ::Result<()> {
84 let tag = try!(::util::os_str_to_cstring(tag));
85
86 ::util::errno_to_result(unsafe {
87 ::ffi::udev_enumerate_add_match_tag(self.enumerator, tag.as_ptr())
88 })
89 }
90
91 /// Includes the parent device and all devices in the subtree of the parent device.
92 pub fn match_parent(&mut self, parent: &Device) -> ::Result<()> {
93 ::util::errno_to_result(unsafe {
94 ::ffi::udev_enumerate_add_match_parent(self.enumerator, parent.as_ptr())
95 })
96 }
97
98 /// Adds a filter that matches only devices that don't belong to the given kernel subsystem.
99 pub fn nomatch_subsystem<T: AsRef<OsStr>>(&mut self, subsystem: T) -> ::Result<()> {
100 let subsystem = try!(::util::os_str_to_cstring(subsystem));
101
102 ::util::errno_to_result(unsafe {
103 ::ffi::udev_enumerate_add_nomatch_subsystem(self.enumerator, subsystem.as_ptr())
104 })
105 }
106
107 /// Adds a filter that matches only devices that don't have the the given attribute value.
108 pub fn nomatch_attribute<T: AsRef<OsStr>, U: AsRef<OsStr>>(&mut self, attribute: T, value: U) -> ::Result<()> {
109 let attribute = try!(::util::os_str_to_cstring(attribute));
110 let value = try!(::util::os_str_to_cstring(value));
111
112 ::util::errno_to_result(unsafe {
113 ::ffi::udev_enumerate_add_nomatch_sysattr(self.enumerator, attribute.as_ptr(), value.as_ptr())
114 })
115 }
116
117 /// Includes the device with the given syspath.
118 pub fn add_syspath(&mut self, syspath: &Path) -> ::Result<()> {
119 let syspath = try!(::util::os_str_to_cstring(syspath));
120
121 ::util::errno_to_result(unsafe {
122 ::ffi::udev_enumerate_add_syspath(self.enumerator, syspath.as_ptr())
123 })
124 }
125
126 /// Scans `/sys` for devices matching the attached filters.
127 ///
128 /// The devices will be sorted in dependency order.
129 pub fn scan_devices(&mut self) -> ::Result<Devices> {
130 try!(::util::errno_to_result(unsafe {
131 ::ffi::udev_enumerate_scan_devices(self.enumerator)
132 }));
133
134 Ok(Devices {
135 enumerator: self,
136 entry: unsafe { ::ffi::udev_enumerate_get_list_entry(self.enumerator) }
137 })
138 }
139 }
140
141
142 /// Iterator over devices.
143 pub struct Devices<'a> {
144 enumerator: &'a Enumerator<'a>,
145 entry: *mut ::ffi::udev_list_entry
146 }
147
148 impl<'a> Iterator for Devices<'a> {
149 type Item = Device<'a>;
150
151 fn next(&mut self) -> Option<Device<'a>> {
152 while !self.entry.is_null() {
153 let syspath = Path::new(unsafe {
154 ::util::ptr_to_os_str_unchecked(::ffi::udev_list_entry_get_name(self.entry))
155 });
156
157 self.entry = unsafe { ::ffi::udev_list_entry_get_next(self.entry) };
158
159 match self.enumerator.context.device_from_syspath(syspath) {
160 Ok(d) => return Some(d),
161 Err(_) => continue
162 };
163 }
164
165 None
166 }
167
168 fn size_hint(&self) -> (usize, Option<usize>) {
169 (0, None)
170 }
171 }