From 0f83309b585ed6a0a382f478e3caa11cedeba2c4 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 19 May 2019 08:40:38 -0700 Subject: [PATCH] centralize/share some filedescriptor bits --- filedescriptor/src/lib.rs | 38 ++++++++++++ filedescriptor/src/unix.rs | 38 ++---------- filedescriptor/src/windows.rs | 106 ++++++++++++---------------------- 3 files changed, 78 insertions(+), 104 deletions(-) diff --git a/filedescriptor/src/lib.rs b/filedescriptor/src/lib.rs index 80ba2d3bf..b49ddc347 100644 --- a/filedescriptor/src/lib.rs +++ b/filedescriptor/src/lib.rs @@ -1,3 +1,4 @@ +use failure::Fallible; #[cfg(unix)] mod unix; #[cfg(unix)] @@ -20,7 +21,44 @@ pub trait FromRawFileDescriptor { unsafe fn from_raw_file_descrptor(fd: RawFileDescriptor) -> Self; } +#[derive(Debug)] +pub struct OwnedHandle { + handle: RawFileDescriptor, +} + +impl OwnedHandle { + pub fn new(f: F) -> Self { + Self { + handle: f.into_raw_file_descriptor(), + } + } + + pub fn try_clone(&self) -> Fallible { + Self::dup(self) + } +} + +#[derive(Debug)] +pub struct FileDescriptor { + handle: OwnedHandle, +} + pub struct Pipes { pub read: FileDescriptor, pub write: FileDescriptor, } + +impl FileDescriptor { + pub fn new(f: F) -> Self { + let handle = OwnedHandle::new(f); + Self { handle } + } + + pub fn dup(f: &F) -> Fallible { + OwnedHandle::dup(f).map(|handle| Self { handle }) + } + + pub fn try_clone(&self) -> Fallible { + self.handle.try_clone().map(|handle| Self { handle }) + } +} diff --git a/filedescriptor/src/unix.rs b/filedescriptor/src/unix.rs index 88aab8c39..4627ebb0b 100644 --- a/filedescriptor/src/unix.rs +++ b/filedescriptor/src/unix.rs @@ -1,4 +1,7 @@ -use crate::{AsRawFileDescriptor, FromRawFileDescriptor, IntoRawFileDescriptor, Pipes}; +use crate::{ + AsRawFileDescriptor, FileDescriptor, FromRawFileDescriptor, IntoRawFileDescriptor, OwnedHandle, + Pipes, +}; use failure::{bail, Fallible}; use std::os::unix::prelude::*; @@ -22,11 +25,6 @@ impl FromRawFileDescriptor for T { } } -#[derive(Debug)] -pub struct OwnedHandle { - handle: RawFileDescriptor, -} - impl Drop for OwnedHandle { fn drop(&mut self) { unsafe { @@ -75,12 +73,6 @@ impl OwnedHandle { Ok(()) } - pub fn new(f: F) -> Self { - Self { - handle: f.into_raw_file_descriptor(), - } - } - pub fn dup(fd: &F) -> Fallible { let fd = fd.as_raw_file_descriptor(); let duped = unsafe { libc::dup(fd) }; @@ -96,15 +88,6 @@ impl OwnedHandle { Ok(owned) } } - - pub fn try_clone(&self) -> Fallible { - Self::dup(self) - } -} - -#[derive(Debug)] -pub struct FileDescriptor { - handle: OwnedHandle, } impl std::io::Read for FileDescriptor { @@ -153,19 +136,6 @@ impl FromRawFd for FileDescriptor { } impl FileDescriptor { - pub fn new(f: F) -> Self { - let handle = OwnedHandle::new(f); - Self { handle } - } - - pub fn dup(f: &F) -> Fallible { - OwnedHandle::dup(f).map(|handle| Self { handle }) - } - - pub fn try_clone(&self) -> Fallible { - self.handle.try_clone().map(|handle| Self { handle }) - } - pub fn pipe() -> Fallible { let mut fds = [-1i32; 2]; let res = unsafe { libc::pipe(fds.as_mut_ptr()) }; diff --git a/filedescriptor/src/windows.rs b/filedescriptor/src/windows.rs index 392ee5d75..b9ead60f3 100644 --- a/filedescriptor/src/windows.rs +++ b/filedescriptor/src/windows.rs @@ -1,4 +1,7 @@ -use crate::{AsRawFileDescriptor, FromRawFileDescriptor, IntoRawFileDescriptor, Pipes}; +use crate::{ + AsRawFileDescriptor, FileDescriptor, FromRawFileDescriptor, IntoRawFileDescriptor, OwnedHandle, + Pipes, +}; use failure::{bail, Fallible}; use std::io::{self, Error as IoError}; use std::os::windows::prelude::*; @@ -29,11 +32,6 @@ impl FromRawFileDescriptor for T { } } -#[derive(Debug)] -pub struct OwnedHandle { - handle: RawHandle, -} - unsafe impl Send for OwnedHandle {} impl Drop for OwnedHandle { @@ -51,17 +49,32 @@ impl FromRawHandle for OwnedHandle { } impl OwnedHandle { - pub fn new(f: F) -> Self { - let handle = f.into_raw_file_descriptor(); - Self { handle } - } - pub fn dup(f: &F) -> Fallible { - dup_handle(f.as_raw_file_descriptor()) - } + let handle = f.as_raw_file_descriptor(); + if handle == INVALID_HANDLE_VALUE || handle.is_null() { + return Ok(OwnedHandle { handle }); + } - pub fn try_clone(&self) -> Fallible { - dup_handle(self.handle) + let proc = unsafe { GetCurrentProcess() }; + let mut duped = INVALID_HANDLE_VALUE; + let ok = unsafe { + DuplicateHandle( + proc, + handle as *mut _, + proc, + &mut duped, + 0, + 0, + winapi::um::winnt::DUPLICATE_SAME_ACCESS, + ) + }; + if ok == 0 { + Err(IoError::last_os_error().into()) + } else { + Ok(OwnedHandle { + handle: duped as *mut _, + }) + } } } @@ -79,58 +92,7 @@ impl IntoRawHandle for OwnedHandle { } } -#[derive(Debug)] -pub struct FileDescriptor { - handle: OwnedHandle, -} - -fn dup_handle(handle: HANDLE) -> Fallible { - if handle == INVALID_HANDLE_VALUE || handle.is_null() { - return Ok(OwnedHandle { handle }); - } - - let proc = unsafe { GetCurrentProcess() }; - let mut duped = INVALID_HANDLE_VALUE; - let ok = unsafe { - DuplicateHandle( - proc, - handle as *mut _, - proc, - &mut duped, - 0, - 0, - winapi::um::winnt::DUPLICATE_SAME_ACCESS, - ) - }; - if ok == 0 { - Err(IoError::last_os_error().into()) - } else { - Ok(OwnedHandle { - handle: duped as *mut _, - }) - } -} - -impl FromRawHandle for FileDescriptor { - unsafe fn from_raw_handle(handle: RawHandle) -> FileDescriptor { - Self { - handle: OwnedHandle::from_raw_handle(handle), - } - } -} - impl FileDescriptor { - pub fn new(f: F) -> Self { - let handle = OwnedHandle::new(f); - Self { handle } - } - - pub fn try_clone(&self) -> Fallible { - self.handle - .try_clone() - .map(|handle| FileDescriptor { handle }) - } - pub fn as_stdio(&self) -> Fallible { let duped = self.handle.try_clone()?; let handle = duped.into_raw_handle(); @@ -153,10 +115,6 @@ impl FileDescriptor { }, }) } - - pub fn dup(f: &F) -> Fallible { - dup_handle(f.as_raw_file_descriptor()).map(|handle| FileDescriptor { handle }) - } } impl IntoRawHandle for FileDescriptor { @@ -171,6 +129,14 @@ impl AsRawHandle for FileDescriptor { } } +impl FromRawHandle for FileDescriptor { + unsafe fn from_raw_handle(handle: RawHandle) -> FileDescriptor { + Self { + handle: OwnedHandle::from_raw_handle(handle), + } + } +} + impl io::Read for FileDescriptor { fn read(&mut self, buf: &mut [u8]) -> Result { let mut num_read = 0;