-
Notifications
You must be signed in to change notification settings - Fork 650
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
NixPath
, NixError
, and NixResult
.
- Loading branch information
1 parent
51becf7
commit effb423
Showing
19 changed files
with
618 additions
and
554 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.