Skip to content

Commit f76e7c4

Browse files
author
Jakub Konka
committed
Remove unnecessary check for TTY for Stdio handle type
1 parent 5b7e54e commit f76e7c4

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

crates/wasi-common/src/sys/stdio.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,23 @@ pub(crate) trait StdioExt: Sized {
1616
fn stderr() -> io::Result<Box<dyn Handle>>;
1717
}
1818

19+
// The reason we have a separate Stdio type is to correctly facilitate redirects on Windows.
20+
// To elaborate further, in POSIX, we can get a stdio handle by opening a specific fd {0,1,2}.
21+
// On Windows however, we need to issue a syscall that's separate from standard Windows "open"
22+
// to get a console handle, and this is GetStdHandle. This is exactly what Rust does and what
23+
// is wrapped inside their Stdio object in the libstd. We wrap it here as well because of this
24+
// nuance on Windows:
25+
//
26+
// The standard handles of a process may be redirected by a call to SetStdHandle, in which
27+
// case GetStdHandle returns the redirected handle.
28+
//
29+
// The MSDN also says this however:
30+
//
31+
// If the standard handles have been redirected, you can specify the CONIN$ value in a call
32+
// to the CreateFile function to get a handle to a console's input buffer. Similarly, you
33+
// can specify the CONOUT$ value to get a handle to a console's active screen buffer.
34+
//
35+
// TODO it might worth re-investigating the suitability of this type on Windows.
1936
#[derive(Debug, Clone)]
2037
#[allow(dead_code)]
2138
pub(crate) enum Stdio {
@@ -75,11 +92,7 @@ impl Handle for Stdio {
7592
// lock for the duration of the scope
7693
let stdout = io::stdout();
7794
let mut stdout = stdout.lock();
78-
let nwritten = if self.is_tty() {
79-
SandboxedTTYWriter::new(&mut stdout).write_vectored(&iovs)?
80-
} else {
81-
stdout.write_vectored(&iovs)?
82-
};
95+
let nwritten = SandboxedTTYWriter::new(&mut stdout).write_vectored(&iovs)?;
8396
stdout.flush()?;
8497
nwritten
8598
}

0 commit comments

Comments
 (0)