auto merge of #10804 : alexcrichton/rust/less-dup, r=pcwalton
This is just an implementation detail of using libuv, so move the libuv-specific logic into librustuv.
This commit is contained in:
commit
10c8409c78
@ -44,6 +44,14 @@ impl TtyWatcher {
|
|||||||
return Err(UvError(uvll::EBADF));
|
return Err(UvError(uvll::EBADF));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// libuv was recently changed to not close the stdio file descriptors,
|
||||||
|
// but it did not change the behavior for windows. Until this issue is
|
||||||
|
// fixed, we need to dup the stdio file descriptors because otherwise
|
||||||
|
// uv_close will close them
|
||||||
|
let fd = if cfg!(windows) && fd <= libc::STDERR_FILENO {
|
||||||
|
unsafe { libc::dup(fd) }
|
||||||
|
} else { fd };
|
||||||
|
|
||||||
// If this file descriptor is indeed guessed to be a tty, then go ahead
|
// If this file descriptor is indeed guessed to be a tty, then go ahead
|
||||||
// with attempting to open it as a tty.
|
// with attempting to open it as a tty.
|
||||||
let handle = UvHandle::alloc(None::<TtyWatcher>, uvll::UV_TTY);
|
let handle = UvHandle::alloc(None::<TtyWatcher>, uvll::UV_TTY);
|
||||||
|
@ -206,7 +206,9 @@ impl rtio::IoFactory for IoFactory {
|
|||||||
}
|
}
|
||||||
fn tty_open(&mut self, fd: c_int, _readable: bool) -> IoResult<~RtioTTY> {
|
fn tty_open(&mut self, fd: c_int, _readable: bool) -> IoResult<~RtioTTY> {
|
||||||
if unsafe { libc::isatty(fd) } != 0 {
|
if unsafe { libc::isatty(fd) } != 0 {
|
||||||
Ok(~file::FileDesc::new(fd, true) as ~RtioTTY)
|
// Don't ever close the stdio file descriptors, nothing good really
|
||||||
|
// comes of that.
|
||||||
|
Ok(~file::FileDesc::new(fd, fd > libc::STDERR_FILENO) as ~RtioTTY)
|
||||||
} else {
|
} else {
|
||||||
Err(IoError {
|
Err(IoError {
|
||||||
kind: io::MismatchedFileTypeForOperation,
|
kind: io::MismatchedFileTypeForOperation,
|
||||||
|
@ -31,8 +31,7 @@ use libc;
|
|||||||
use option::{Option, Some, None};
|
use option::{Option, Some, None};
|
||||||
use result::{Ok, Err};
|
use result::{Ok, Err};
|
||||||
use io::buffered::LineBufferedWriter;
|
use io::buffered::LineBufferedWriter;
|
||||||
use rt::rtio::{IoFactory, RtioTTY, RtioFileStream, with_local_io,
|
use rt::rtio::{IoFactory, RtioTTY, RtioFileStream, with_local_io, DontClose};
|
||||||
CloseAsynchronously};
|
|
||||||
use super::{Reader, Writer, io_error, IoError, OtherIoError,
|
use super::{Reader, Writer, io_error, IoError, OtherIoError,
|
||||||
standard_error, EndOfFile};
|
standard_error, EndOfFile};
|
||||||
|
|
||||||
@ -71,18 +70,9 @@ enum StdSource {
|
|||||||
|
|
||||||
fn src<T>(fd: libc::c_int, readable: bool, f: |StdSource| -> T) -> T {
|
fn src<T>(fd: libc::c_int, readable: bool, f: |StdSource| -> T) -> T {
|
||||||
with_local_io(|io| {
|
with_local_io(|io| {
|
||||||
let fd = unsafe { libc::dup(fd) };
|
|
||||||
match io.tty_open(fd, readable) {
|
match io.tty_open(fd, readable) {
|
||||||
Ok(tty) => Some(f(TTY(tty))),
|
Ok(tty) => Some(f(TTY(tty))),
|
||||||
Err(_) => {
|
Err(_) => Some(f(File(io.fs_from_raw_fd(fd, DontClose)))),
|
||||||
// It's not really that desirable if these handles are closed
|
|
||||||
// synchronously, and because they're squirreled away in a task
|
|
||||||
// structure the destructors will be run when the task is
|
|
||||||
// attempted to get destroyed. This means that if we run a
|
|
||||||
// synchronous destructor we'll attempt to do some scheduling
|
|
||||||
// operations which will just result in sadness.
|
|
||||||
Some(f(File(io.fs_from_raw_fd(fd, CloseAsynchronously))))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}).unwrap()
|
}).unwrap()
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user