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
|
use libc;
use std;
use errno::Errno;
pub type NixResult<T> = Result<T, NixError>;
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum NixError {
Sys(Errno),
InvalidPath
}
pub trait NixPath {
fn with_nix_path<T, F>(&self, f: F) -> Result<T, NixError>
where F: FnOnce(*const libc::c_char) -> T;
}
impl<'a> NixPath for &'a [u8] {
fn with_nix_path<T, F>(&self, f: F) -> Result<T, NixError>
where F: FnOnce(*const libc::c_char) -> T
{
// TODO: Extract this size as a const
let mut buf = [0u8; 4096];
if self.len() >= 4096 {
return Err(NixError::InvalidPath);
}
match self.position_elem(&0) {
Some(_) => Err(NixError::InvalidPath),
None => {
std::slice::bytes::copy_memory(&mut buf, self);
Ok(f(buf.as_ptr() as *const libc::c_char))
}
}
}
}
impl<P: NixPath> NixPath for Option<P> {
fn with_nix_path<T, F>(&self, f: F) -> Result<T, NixError>
where F: FnOnce(*const libc::c_char) -> T
{
match *self {
Some(ref some) => some.with_nix_path(f),
None => b"".with_nix_path(f)
}
}
}
#[inline]
pub fn from_ffi(res: libc::c_int) -> NixResult<()> {
if res != 0 {
return Err(NixError::Sys(Errno::last()));
}
Ok(())
}
|