diff options
-rw-r--r-- | src/fcntl.rs | 6 | ||||
-rw-r--r-- | src/lib.rs | 53 | ||||
-rw-r--r-- | src/mount.rs | 10 | ||||
-rw-r--r-- | src/sys/mman.rs | 10 | ||||
-rw-r--r-- | src/sys/socket/addr.rs | 16 | ||||
-rw-r--r-- | src/sys/stat.rs | 14 | ||||
-rw-r--r-- | src/unistd.rs | 16 |
7 files changed, 59 insertions, 66 deletions
diff --git a/src/fcntl.rs b/src/fcntl.rs index 73c0619c..76ea9f65 100644 --- a/src/fcntl.rs +++ b/src/fcntl.rs @@ -1,4 +1,4 @@ -use {Error, Result, NixPath, AsExtStr}; +use {Error, Result, NixPath}; use errno::Errno; use libc::mode_t; use sys::stat::Mode; @@ -72,8 +72,8 @@ mod ffi { } pub fn open<P: ?Sized + NixPath>(path: &P, oflag: OFlag, mode: Mode) -> Result<Fd> { - let fd = try!(path.with_nix_path(|osstr| { - unsafe { ffi::open(osstr.as_ext_str(), oflag.bits(), mode.bits() as mode_t) } + let fd = try!(path.with_nix_path(|cstr| { + unsafe { ffi::open(cstr.as_ptr(), oflag.bits(), mode.bits() as mode_t) } })); if fd < 0 { @@ -48,8 +48,11 @@ pub mod unistd; * */ +use libc::c_char; use std::{ptr, result}; +use std::ffi::CStr; use std::path::{Path, PathBuf}; +use std::os::unix::ffi::OsStrExt; pub type Result<T> = result::Result<T, Error>; @@ -81,13 +84,19 @@ impl Error { } pub trait NixPath { + fn len(&self) -> usize; + fn with_nix_path<T, F>(&self, f: F) -> Result<T> - where F: FnOnce(&OsStr) -> T; + where F: FnOnce(&CStr) -> T; } impl NixPath for [u8] { + fn len(&self) -> usize { + self.len() + } + fn with_nix_path<T, F>(&self, f: F) -> Result<T> - where F: FnOnce(&OsStr) -> T { + where F: FnOnce(&CStr) -> T { // TODO: Extract this size as a const let mut buf = [0u8; 4096]; @@ -101,25 +110,31 @@ impl NixPath for [u8] { unsafe { // TODO: Replace with bytes::copy_memory. rust-lang/rust#24028 ptr::copy_nonoverlapping(self.as_ptr(), buf.as_mut_ptr(), self.len()); + Ok(f(CStr::from_ptr(buf.as_ptr() as *const c_char))) } - Ok(f(<OsStr as OsStrExt>::from_bytes(&buf[..self.len()]))) } } } } impl NixPath for Path { - fn with_nix_path<T, F>(&self, f: F) -> Result<T> - where F: FnOnce(&OsStr) -> T { - Ok(f(self.as_os_str())) + fn len(&self) -> usize { + self.as_os_str().as_bytes().len() + } + + fn with_nix_path<T, F>(&self, f: F) -> Result<T> where F: FnOnce(&CStr) -> T { + self.as_os_str().as_bytes().with_nix_path(f) } } impl NixPath for PathBuf { - fn with_nix_path<T, F>(&self, f: F) -> Result<T> - where F: FnOnce(&OsStr) -> T { - Ok(f(self.as_os_str())) + fn len(&self) -> usize { + self.as_os_str().as_bytes().len() + } + + fn with_nix_path<T, F>(&self, f: F) -> Result<T> where F: FnOnce(&CStr) -> T { + self.as_os_str().as_bytes().with_nix_path(f) } } @@ -131,23 +146,3 @@ pub fn from_ffi(res: libc::c_int) -> Result<()> { Ok(()) } - -/* - * - * ===== Impl utilities ===== - * - */ - -use std::ffi::OsStr; -use std::os::unix::ffi::OsStrExt; - -/// Converts a value to an external (FFI) string representation -trait AsExtStr { - fn as_ext_str(&self) -> *const libc::c_char; -} - -impl AsExtStr for OsStr { - fn as_ext_str(&self) -> *const libc::c_char { - self.as_bytes().as_ptr() as *const libc::c_char - } -} diff --git a/src/mount.rs b/src/mount.rs index d92e18bd..1da894e4 100644 --- a/src/mount.rs +++ b/src/mount.rs @@ -1,5 +1,5 @@ use libc::{c_ulong, c_int}; -use {Result, NixPath, AsExtStr, from_ffi}; +use {Result, NixPath, from_ffi}; bitflags!( flags MsFlags: c_ulong { @@ -101,16 +101,16 @@ pub fn mount<P1: ?Sized + NixPath, P2: ?Sized + NixPath, P3: ?Sized + NixPath, P */ pub fn umount<P: ?Sized + NixPath>(target: &P) -> Result<()> { - let res = try!(target.with_nix_path(|ptr| { - unsafe { ffi::umount(ptr.as_ext_str()) } + let res = try!(target.with_nix_path(|cstr| { + unsafe { ffi::umount(cstr.as_ptr()) } })); from_ffi(res) } pub fn umount2<P: ?Sized + NixPath>(target: &P, flags: MntFlags) -> Result<()> { - let res = try!(target.with_nix_path(|ptr| { - unsafe { ffi::umount2(ptr.as_ext_str(), flags.bits) } + let res = try!(target.with_nix_path(|cstr| { + unsafe { ffi::umount2(cstr.as_ptr(), flags.bits) } })); from_ffi(res) diff --git a/src/sys/mman.rs b/src/sys/mman.rs index d3f3b7ae..04664fd0 100644 --- a/src/sys/mman.rs +++ b/src/sys/mman.rs @@ -1,4 +1,4 @@ -use {Error, Result, NixPath, AsExtStr}; +use {Error, Result, NixPath}; use errno::Errno; use fcntl::{Fd, OFlag}; use libc::{c_void, size_t, off_t, mode_t}; @@ -225,9 +225,9 @@ pub fn msync(addr: *const c_void, length: size_t, flags: MmapSync) -> Result<()> } pub fn shm_open<P: ?Sized + NixPath>(name: &P, flag: OFlag, mode: Mode) -> Result<Fd> { - let ret = try!(name.with_nix_path(|osstr| { + let ret = try!(name.with_nix_path(|cstr| { unsafe { - ffi::shm_open(osstr.as_ext_str(), flag.bits(), mode.bits() as mode_t) + ffi::shm_open(cstr.as_ptr(), flag.bits(), mode.bits() as mode_t) } })); @@ -239,8 +239,8 @@ pub fn shm_open<P: ?Sized + NixPath>(name: &P, flag: OFlag, mode: Mode) -> Resul } pub fn shm_unlink<P: ?Sized + NixPath>(name: &P) -> Result<()> { - let ret = try!(name.with_nix_path(|osstr| { - unsafe { ffi::shm_unlink(osstr.as_ext_str()) } + let ret = try!(name.with_nix_path(|cstr| { + unsafe { ffi::shm_unlink(cstr.as_ptr()) } })); if ret < 0 { diff --git a/src/sys/socket/addr.rs b/src/sys/socket/addr.rs index 91052c77..29c216eb 100644 --- a/src/sys/socket/addr.rs +++ b/src/sys/socket/addr.rs @@ -2,7 +2,7 @@ use {Result, Error, NixPath}; use super::{consts, sa_family_t}; use errno::Errno; use libc; -use std::{fmt, hash, mem, net, ptr}; +use std::{fmt, hash, mem, net}; use std::ffi::{CStr, OsStr}; use std::path::Path; use std::os::unix::ffi::OsStrExt; @@ -334,23 +334,21 @@ pub struct UnixAddr(pub libc::sockaddr_un); impl UnixAddr { pub fn new<P: ?Sized + NixPath>(path: &P) -> Result<UnixAddr> { - try!(path.with_nix_path(|osstr| { - unsafe { - let bytes = osstr.as_bytes(); + use libc::strcpy; + try!(path.with_nix_path(|cstr| { + unsafe { let mut ret = libc::sockaddr_un { sun_family: AddressFamily::Unix as sa_family_t, .. mem::zeroed() }; - if bytes.len() >= ret.sun_path.len() { + // Must be smaller to account for the null byte + if path.len() >= ret.sun_path.len() { return Err(Error::Sys(Errno::ENAMETOOLONG)); } - ptr::copy( - bytes.as_ptr() as *const i8, - ret.sun_path.as_mut_ptr(), - bytes.len()); + strcpy(ret.sun_path.as_mut_ptr(), cstr.as_ptr()); Ok(UnixAddr(ret)) } diff --git a/src/sys/stat.rs b/src/sys/stat.rs index cafb36d9..60153a61 100644 --- a/src/sys/stat.rs +++ b/src/sys/stat.rs @@ -1,7 +1,7 @@ pub use libc::dev_t; pub use libc::stat as FileStat; -use {Error, Result, NixPath, AsExtStr, from_ffi}; +use {Error, Result, NixPath, from_ffi}; use errno::Errno; use fcntl::Fd; use libc::mode_t; @@ -58,9 +58,9 @@ impl fmt::Debug for SFlag { } pub fn mknod<P: ?Sized + NixPath>(path: &P, kind: SFlag, perm: Mode, dev: dev_t) -> Result<()> { - let res = try!(path.with_nix_path(|osstr| { + let res = try!(path.with_nix_path(|cstr| { unsafe { - ffi::mknod(osstr.as_ext_str(), kind.bits | perm.bits() as mode_t, dev) + ffi::mknod(cstr.as_ptr(), kind.bits | perm.bits() as mode_t, dev) } })); from_ffi(res) @@ -81,9 +81,9 @@ pub fn umask(mode: Mode) -> Mode { pub fn stat<P: ?Sized + NixPath>(path: &P) -> Result<FileStat> { let mut dst = unsafe { mem::uninitialized() }; - let res = try!(path.with_nix_path(|osstr| { + let res = try!(path.with_nix_path(|cstr| { unsafe { - ffi::stat(osstr.as_ext_str(), &mut dst as *mut FileStat) + ffi::stat(cstr.as_ptr(), &mut dst as *mut FileStat) } })); @@ -96,9 +96,9 @@ pub fn stat<P: ?Sized + NixPath>(path: &P) -> Result<FileStat> { pub fn lstat<P: ?Sized + NixPath>(path: &P) -> Result<FileStat> { let mut dst = unsafe { mem::uninitialized() }; - let res = try!(path.with_nix_path(|osstr| { + let res = try!(path.with_nix_path(|cstr| { unsafe { - ffi::lstat(osstr.as_ext_str(), &mut dst as *mut FileStat) + ffi::lstat(cstr.as_ptr(), &mut dst as *mut FileStat) } })); diff --git a/src/unistd.rs b/src/unistd.rs index 1852721d..68447f5b 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -1,6 +1,6 @@ //! Standard symbolic constants and types //! -use {Error, Result, NixPath, AsExtStr, from_ffi}; +use {Error, Result, NixPath, from_ffi}; use errno::Errno; use fcntl::{fcntl, Fd, OFlag, O_NONBLOCK, O_CLOEXEC, FD_CLOEXEC}; use fcntl::FcntlArg::{F_SETFD, F_SETFL}; @@ -144,8 +144,8 @@ fn dup3_polyfill(oldfd: Fd, newfd: Fd, flags: OFlag) -> Result<Fd> { #[inline] pub fn chdir<P: ?Sized + NixPath>(path: &P) -> Result<()> { - let res = try!(path.with_nix_path(|osstr| { - unsafe { ffi::chdir(osstr.as_ext_str()) } + let res = try!(path.with_nix_path(|cstr| { + unsafe { ffi::chdir(cstr.as_ptr()) } })); if res != 0 { @@ -301,9 +301,9 @@ pub fn isatty(fd: Fd) -> Result<bool> { } pub fn unlink<P: ?Sized + NixPath>(path: &P) -> Result<()> { - let res = try!(path.with_nix_path(|osstr| { + let res = try!(path.with_nix_path(|cstr| { unsafe { - ffi::unlink(osstr.as_ext_str()) + ffi::unlink(cstr.as_ptr()) } })); from_ffi(res) @@ -311,8 +311,8 @@ pub fn unlink<P: ?Sized + NixPath>(path: &P) -> Result<()> { #[inline] pub fn chroot<P: ?Sized + NixPath>(path: &P) -> Result<()> { - let res = try!(path.with_nix_path(|osstr| { - unsafe { ffi::chroot(osstr.as_ext_str()) } + let res = try!(path.with_nix_path(|cstr| { + unsafe { ffi::chroot(cstr.as_ptr()) } })); if res != 0 { @@ -336,7 +336,7 @@ mod linux { let res = try!(try!(new_root.with_nix_path(|new_root| { put_old.with_nix_path(|put_old| { unsafe { - syscall(SYSPIVOTROOT, new_root, put_old) + syscall(SYSPIVOTROOT, new_root.as_ptr(), put_old.as_ptr()) } }) }))); |