Skip to content

Commit e8a58c8

Browse files
committed
Further SockAddr & NixPath cleanup
1 parent 8a7cd56 commit e8a58c8

File tree

11 files changed

+450
-317
lines changed

11 files changed

+450
-317
lines changed

src/fcntl.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use libc::mode_t;
1+
use {NixError, NixResult, NixPath, AsExtStr};
22
use errno::Errno;
3-
use {NixError, NixResult, NixPath};
3+
use libc::mode_t;
44
use sys::stat::Mode;
55

66
pub use self::consts::*;
@@ -71,11 +71,9 @@ mod ffi {
7171
}
7272
}
7373

74-
pub fn open<P: NixPath>(path: P, oflag: OFlag, mode: Mode) -> NixResult<Fd> {
75-
let fd = try!(path.with_nix_path(|ptr| {
76-
unsafe {
77-
ffi::open(ptr, oflag.bits(), mode.bits() as mode_t)
78-
}
74+
pub fn open<P: ?Sized + NixPath>(path: &P, oflag: OFlag, mode: Mode) -> NixResult<Fd> {
75+
let fd = try!(path.with_nix_path(|osstr| {
76+
unsafe { ffi::open(osstr.as_ext_str(), oflag.bits(), mode.bits() as mode_t) }
7977
}));
8078

8179
if fd < 0 {

src/lib.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,23 @@ pub mod sys;
4343

4444
#[cfg(unix)]
4545
pub mod unistd;
46+
47+
/*
48+
*
49+
* ===== Impl utilities =====
50+
*
51+
*/
52+
53+
use std::ffi::OsStr;
54+
use std::os::unix::OsStrExt;
55+
56+
/// Converts a value to an external (FFI) string representation
57+
trait AsExtStr {
58+
fn as_ext_str(&self) -> *const libc::c_char;
59+
}
60+
61+
impl AsExtStr for OsStr {
62+
fn as_ext_str(&self) -> *const libc::c_char {
63+
self.as_bytes().as_ptr() as *const libc::c_char
64+
}
65+
}

src/mount.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ mod ffi {
6666
}
6767

6868
// XXX: Should `data` be a `NixPath` here?
69-
pub fn mount<P1: NixPath, P2: NixPath, P3: NixPath, P4: NixPath>(
69+
pub fn mount<P1: ?Sized + NixPath, P2: ?Sized + NixPath, P3: ?Sized + NixPath, P4: ?Sized + NixPath>(
7070
source: Option<P1>,
7171
target: P2,
7272
fstype: Option<P3>,
@@ -94,15 +94,15 @@ pub fn mount<P1: NixPath, P2: NixPath, P3: NixPath, P4: NixPath>(
9494
return from_ffi(res);
9595
}
9696

97-
pub fn umount<P: NixPath>(target: P) -> NixResult<()> {
97+
pub fn umount<P: ?Sized + NixPath>(target: P) -> NixResult<()> {
9898
let res = try!(target.with_nix_path(|ptr| {
9999
unsafe { ffi::umount(ptr) }
100100
}));
101101

102102
from_ffi(res)
103103
}
104104

105-
pub fn umount2<P: NixPath>(target: P, flags: MntFlags) -> NixResult<()> {
105+
pub fn umount2<P: ?Sized + NixPath>(target: P, flags: MntFlags) -> NixResult<()> {
106106
let res = try!(target.with_nix_path(|ptr| {
107107
unsafe { ffi::umount2(ptr, flags.bits) }
108108
}));

src/nix.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use libc;
2-
use std;
2+
use std::ffi::{OsStr, AsOsStr};
3+
use std::os::unix::OsStrExt;
4+
use std::path::{Path, PathBuf};
5+
use std::slice::bytes;
36

47
use errno::{Errno, EINVAL};
58

@@ -23,13 +26,12 @@ impl NixError {
2326

2427
pub trait NixPath {
2528
fn with_nix_path<T, F>(&self, f: F) -> Result<T, NixError>
26-
where F: FnOnce(*const libc::c_char) -> T;
29+
where F: FnOnce(&OsStr) -> T;
2730
}
2831

29-
impl<'a> NixPath for &'a [u8] {
32+
impl NixPath for [u8] {
3033
fn with_nix_path<T, F>(&self, f: F) -> Result<T, NixError>
31-
where F: FnOnce(*const libc::c_char) -> T
32-
{
34+
where F: FnOnce(&OsStr) -> T {
3335
// TODO: Extract this size as a const
3436
let mut buf = [0u8; 4096];
3537

@@ -40,21 +42,24 @@ impl<'a> NixPath for &'a [u8] {
4042
match self.position_elem(&0) {
4143
Some(_) => Err(NixError::InvalidPath),
4244
None => {
43-
std::slice::bytes::copy_memory(&mut buf, self);
44-
Ok(f(buf.as_ptr() as *const libc::c_char))
45+
bytes::copy_memory(&mut buf, self);
46+
Ok(f(<OsStr as OsStrExt>::from_bytes(&buf[..self.len()])))
4547
}
4648
}
4749
}
4850
}
4951

50-
impl<P: NixPath> NixPath for Option<P> {
52+
impl NixPath for Path {
5153
fn with_nix_path<T, F>(&self, f: F) -> Result<T, NixError>
52-
where F: FnOnce(*const libc::c_char) -> T
53-
{
54-
match *self {
55-
Some(ref some) => some.with_nix_path(f),
56-
None => b"".with_nix_path(f)
57-
}
54+
where F: FnOnce(&OsStr) -> T {
55+
Ok(f(self.as_os_str()))
56+
}
57+
}
58+
59+
impl NixPath for PathBuf {
60+
fn with_nix_path<T, F>(&self, f: F) -> Result<T, NixError>
61+
where F: FnOnce(&OsStr) -> T {
62+
Ok(f(self.as_os_str()))
5863
}
5964
}
6065

@@ -63,5 +68,6 @@ pub fn from_ffi(res: libc::c_int) -> NixResult<()> {
6368
if res != 0 {
6469
return Err(NixError::Sys(Errno::last()));
6570
}
71+
6672
Ok(())
6773
}

src/sys/mman.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
use {NixError, NixResult, NixPath, AsExtStr};
12
use errno::Errno;
23
use fcntl::{Fd, OFlag};
34
use libc::{c_void, size_t, off_t, mode_t};
45
use sys::stat::Mode;
5-
use {NixError, NixResult, NixPath};
66

77
pub use self::consts::*;
88

@@ -175,10 +175,10 @@ pub fn msync(addr: *const c_void, length: size_t, flags: MmapSync) -> NixResult<
175175
}
176176
}
177177

178-
pub fn shm_open<P: NixPath>(name: P, flag: OFlag, mode: Mode) -> NixResult<Fd> {
179-
let ret = try!(name.with_nix_path(|ptr| {
178+
pub fn shm_open<P: ?Sized + NixPath>(name: &P, flag: OFlag, mode: Mode) -> NixResult<Fd> {
179+
let ret = try!(name.with_nix_path(|osstr| {
180180
unsafe {
181-
ffi::shm_open(ptr, flag.bits(), mode.bits() as mode_t)
181+
ffi::shm_open(osstr.as_ext_str(), flag.bits(), mode.bits() as mode_t)
182182
}
183183
}));
184184

@@ -189,9 +189,9 @@ pub fn shm_open<P: NixPath>(name: P, flag: OFlag, mode: Mode) -> NixResult<Fd> {
189189
}
190190
}
191191

192-
pub fn shm_unlink<P: NixPath>(name: P) -> NixResult<()> {
193-
let ret = try!(name.with_nix_path(|ptr| {
194-
unsafe { ffi::shm_unlink(ptr) }
192+
pub fn shm_unlink<P: ?Sized + NixPath>(name: &P) -> NixResult<()> {
193+
let ret = try!(name.with_nix_path(|osstr| {
194+
unsafe { ffi::shm_unlink(osstr.as_ext_str()) }
195195
}));
196196

197197
if ret < 0 {

0 commit comments

Comments
 (0)