mirror of
https://github.com/wez/wezterm.git
synced 2024-08-17 10:10:23 +03:00
centralize/share some filedescriptor bits
This commit is contained in:
parent
ed4a605355
commit
0f83309b58
@ -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: IntoRawFileDescriptor>(f: F) -> Self {
|
||||
Self {
|
||||
handle: f.into_raw_file_descriptor(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn try_clone(&self) -> Fallible<Self> {
|
||||
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: IntoRawFileDescriptor>(f: F) -> Self {
|
||||
let handle = OwnedHandle::new(f);
|
||||
Self { handle }
|
||||
}
|
||||
|
||||
pub fn dup<F: AsRawFileDescriptor>(f: &F) -> Fallible<Self> {
|
||||
OwnedHandle::dup(f).map(|handle| Self { handle })
|
||||
}
|
||||
|
||||
pub fn try_clone(&self) -> Fallible<Self> {
|
||||
self.handle.try_clone().map(|handle| Self { handle })
|
||||
}
|
||||
}
|
||||
|
@ -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<T: FromRawFd> 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: IntoRawFileDescriptor>(f: F) -> Self {
|
||||
Self {
|
||||
handle: f.into_raw_file_descriptor(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn dup<F: AsRawFileDescriptor>(fd: &F) -> Fallible<Self> {
|
||||
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> {
|
||||
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: IntoRawFileDescriptor>(f: F) -> Self {
|
||||
let handle = OwnedHandle::new(f);
|
||||
Self { handle }
|
||||
}
|
||||
|
||||
pub fn dup<F: AsRawFileDescriptor>(f: &F) -> Fallible<Self> {
|
||||
OwnedHandle::dup(f).map(|handle| Self { handle })
|
||||
}
|
||||
|
||||
pub fn try_clone(&self) -> Fallible<Self> {
|
||||
self.handle.try_clone().map(|handle| Self { handle })
|
||||
}
|
||||
|
||||
pub fn pipe() -> Fallible<Pipes> {
|
||||
let mut fds = [-1i32; 2];
|
||||
let res = unsafe { libc::pipe(fds.as_mut_ptr()) };
|
||||
|
@ -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<T: FromRawHandle> 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: IntoRawFileDescriptor>(f: F) -> Self {
|
||||
let handle = f.into_raw_file_descriptor();
|
||||
Self { handle }
|
||||
}
|
||||
|
||||
pub fn dup<F: AsRawFileDescriptor>(f: &F) -> Fallible<Self> {
|
||||
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<Self> {
|
||||
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<OwnedHandle> {
|
||||
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: IntoRawFileDescriptor>(f: F) -> Self {
|
||||
let handle = OwnedHandle::new(f);
|
||||
Self { handle }
|
||||
}
|
||||
|
||||
pub fn try_clone(&self) -> Fallible<Self> {
|
||||
self.handle
|
||||
.try_clone()
|
||||
.map(|handle| FileDescriptor { handle })
|
||||
}
|
||||
|
||||
pub fn as_stdio(&self) -> Fallible<std::process::Stdio> {
|
||||
let duped = self.handle.try_clone()?;
|
||||
let handle = duped.into_raw_handle();
|
||||
@ -153,10 +115,6 @@ impl FileDescriptor {
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
pub fn dup<F: AsRawFileDescriptor>(f: &F) -> Fallible<Self> {
|
||||
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<usize, io::Error> {
|
||||
let mut num_read = 0;
|
||||
|
Loading…
Reference in New Issue
Block a user