1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use std::ffi::OsStr;
use std::marker::PhantomData;
use std::path::Path;
use ::context::Context;
use ::device::Device;
use ::handle::Handle;
pub struct Enumerator {
enumerator: *mut ::ffi::udev_enumerate,
}
impl Drop for Enumerator {
fn drop(&mut self) {
unsafe {
let udev = ::ffi::udev_enumerate_get_udev(self.enumerator);
::ffi::udev_enumerate_unref(self.enumerator);
::ffi::udev_unref(udev);
};
}
}
impl Enumerator {
pub fn new(context: &Context) -> ::Result<Self> {
unsafe {
let ptr = try_alloc!(
::ffi::udev_enumerate_new(context.as_ptr())
);
::ffi::udev_ref(context.as_ptr());
Ok(Enumerator { enumerator: ptr })
}
}
pub fn match_is_initialized(&mut self) -> ::Result<()> {
::util::errno_to_result(unsafe {
::ffi::udev_enumerate_add_match_is_initialized(self.enumerator)
})
}
pub fn match_subsystem<T: AsRef<OsStr>>(&mut self, subsystem: T) -> ::Result<()> {
let subsystem = try!(::util::os_str_to_cstring(subsystem));
::util::errno_to_result(unsafe {
::ffi::udev_enumerate_add_match_subsystem(self.enumerator, subsystem.as_ptr())
})
}
pub fn match_attribute<T: AsRef<OsStr>, U: AsRef<OsStr>>(&mut self, attribute: T, value: U) -> ::Result<()> {
let attribute = try!(::util::os_str_to_cstring(attribute));
let value = try!(::util::os_str_to_cstring(value));
::util::errno_to_result(unsafe {
::ffi::udev_enumerate_add_match_sysattr(self.enumerator, attribute.as_ptr(), value.as_ptr())
})
}
pub fn match_sysname<T: AsRef<OsStr>>(&mut self, sysname: T) -> ::Result<()> {
let sysname = try!(::util::os_str_to_cstring(sysname));
::util::errno_to_result(unsafe {
::ffi::udev_enumerate_add_match_sysname(self.enumerator, sysname.as_ptr())
})
}
pub fn match_property<T: AsRef<OsStr>, U: AsRef<OsStr>>(&mut self, property: T, value: U) -> ::Result<()> {
let property = try!(::util::os_str_to_cstring(property));
let value = try!(::util::os_str_to_cstring(value));
::util::errno_to_result(unsafe {
::ffi::udev_enumerate_add_match_property(self.enumerator, property.as_ptr(), value.as_ptr())
})
}
pub fn match_tag<T: AsRef<OsStr>>(&mut self, tag: T) -> ::Result<()> {
let tag = try!(::util::os_str_to_cstring(tag));
::util::errno_to_result(unsafe {
::ffi::udev_enumerate_add_match_tag(self.enumerator, tag.as_ptr())
})
}
pub fn match_parent(&mut self, parent: &Device) -> ::Result<()> {
::util::errno_to_result(unsafe {
::ffi::udev_enumerate_add_match_parent(self.enumerator, parent.as_ptr())
})
}
pub fn nomatch_subsystem<T: AsRef<OsStr>>(&mut self, subsystem: T) -> ::Result<()> {
let subsystem = try!(::util::os_str_to_cstring(subsystem));
::util::errno_to_result(unsafe {
::ffi::udev_enumerate_add_nomatch_subsystem(self.enumerator, subsystem.as_ptr())
})
}
pub fn nomatch_attribute<T: AsRef<OsStr>, U: AsRef<OsStr>>(&mut self, attribute: T, value: U) -> ::Result<()> {
let attribute = try!(::util::os_str_to_cstring(attribute));
let value = try!(::util::os_str_to_cstring(value));
::util::errno_to_result(unsafe {
::ffi::udev_enumerate_add_nomatch_sysattr(self.enumerator, attribute.as_ptr(), value.as_ptr())
})
}
pub fn add_syspath(&mut self, syspath: &Path) -> ::Result<()> {
let syspath = try!(::util::os_str_to_cstring(syspath));
::util::errno_to_result(unsafe {
::ffi::udev_enumerate_add_syspath(self.enumerator, syspath.as_ptr())
})
}
pub fn scan_devices(&mut self) -> ::Result<Devices> {
try!(::util::errno_to_result(unsafe {
::ffi::udev_enumerate_scan_devices(self.enumerator)
}));
unsafe {
Ok(Devices {
_enumerator: PhantomData,
udev: ::ffi::udev_enumerate_get_udev(self.enumerator),
entry: ::ffi::udev_enumerate_get_list_entry(self.enumerator),
})
}
}
}
pub struct Devices<'a> {
_enumerator: PhantomData<&'a Enumerator>,
udev: *mut ::ffi::udev,
entry: *mut ::ffi::udev_list_entry,
}
impl<'a> Iterator for Devices<'a> {
type Item = Device;
fn next(&mut self) -> Option<Device> {
while !self.entry.is_null() {
unsafe {
let syspath = ::ffi::udev_list_entry_get_name(self.entry);
self.entry = ::ffi::udev_list_entry_get_next(self.entry);
let device = ::ffi::udev_device_new_from_syspath(self.udev, syspath);
if !device.is_null() {
return Some(::device::from_raw(device));
}
else {
continue;
}
};
}
None
}
fn size_hint(&self) -> (usize, Option<usize>) {
(0, None)
}
}