|
1 | | -// Copyright 2018 The Rust Project Developers. See the COPYRIGHT |
| 1 | +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT |
2 | 2 | // file at the top-level directory of this distribution and at |
3 | 3 | // http://rust-lang.org/COPYRIGHT. |
4 | 4 | // |
|
8 | 8 | // option. This file may not be copied, modified, or distributed |
9 | 9 | // except according to those terms. |
10 | 10 |
|
| 11 | +use fortanix_sgx_abi as abi; |
| 12 | + |
11 | 13 | use io; |
12 | | -use sys::unsupported; |
| 14 | +use sys::fd::FileDesc; |
| 15 | + |
| 16 | +pub struct Stdin(()); |
| 17 | +pub struct Stdout(()); |
| 18 | +pub struct Stderr(()); |
13 | 19 |
|
14 | | -pub struct Stdin; |
15 | | -pub struct Stdout; |
16 | | -pub struct Stderr; |
| 20 | +fn with_std_fd<F: FnOnce(&FileDesc) -> R, R>(fd: abi::Fd, f: F) -> R { |
| 21 | + let fd = FileDesc::new(fd); |
| 22 | + let ret = f(&fd); |
| 23 | + fd.into_raw(); |
| 24 | + ret |
| 25 | +} |
17 | 26 |
|
18 | 27 | impl Stdin { |
19 | | - pub fn new() -> io::Result<Stdin> { |
20 | | - Ok(Stdin) |
21 | | - } |
| 28 | + pub fn new() -> io::Result<Stdin> { Ok(Stdin(())) } |
22 | 29 |
|
23 | | - pub fn read(&self, _data: &mut [u8]) -> io::Result<usize> { |
24 | | - unsupported() |
| 30 | + pub fn read(&self, data: &mut [u8]) -> io::Result<usize> { |
| 31 | + with_std_fd(abi::FD_STDIN, |fd| fd.read(data)) |
25 | 32 | } |
26 | 33 | } |
27 | 34 |
|
28 | 35 | impl Stdout { |
29 | | - pub fn new() -> io::Result<Stdout> { |
30 | | - Ok(Stdout) |
31 | | - } |
| 36 | + pub fn new() -> io::Result<Stdout> { Ok(Stdout(())) } |
32 | 37 |
|
33 | | - pub fn write(&self, _data: &[u8]) -> io::Result<usize> { |
34 | | - unsupported() |
| 38 | + pub fn write(&self, data: &[u8]) -> io::Result<usize> { |
| 39 | + with_std_fd(abi::FD_STDOUT, |fd| fd.write(data)) |
35 | 40 | } |
36 | 41 |
|
37 | 42 | pub fn flush(&self) -> io::Result<()> { |
38 | | - Ok(()) |
| 43 | + with_std_fd(abi::FD_STDOUT, |fd| fd.flush()) |
39 | 44 | } |
40 | 45 | } |
41 | 46 |
|
42 | 47 | impl Stderr { |
43 | | - pub fn new() -> io::Result<Stderr> { |
44 | | - Ok(Stderr) |
45 | | - } |
| 48 | + pub fn new() -> io::Result<Stderr> { Ok(Stderr(())) } |
46 | 49 |
|
47 | | - pub fn write(&self, _data: &[u8]) -> io::Result<usize> { |
48 | | - unsupported() |
| 50 | + pub fn write(&self, data: &[u8]) -> io::Result<usize> { |
| 51 | + with_std_fd(abi::FD_STDERR, |fd| fd.write(data)) |
49 | 52 | } |
50 | 53 |
|
51 | 54 | pub fn flush(&self) -> io::Result<()> { |
52 | | - Ok(()) |
| 55 | + with_std_fd(abi::FD_STDERR, |fd| fd.flush()) |
53 | 56 | } |
54 | 57 | } |
55 | 58 |
|
| 59 | +// FIXME: right now this raw stderr handle is used in a few places because |
| 60 | +// std::io::stderr_raw isn't exposed, but once that's exposed this impl |
| 61 | +// should go away |
56 | 62 | impl io::Write for Stderr { |
57 | 63 | fn write(&mut self, data: &[u8]) -> io::Result<usize> { |
58 | | - (&*self).write(data) |
| 64 | + Stderr::write(self, data) |
59 | 65 | } |
| 66 | + |
60 | 67 | fn flush(&mut self) -> io::Result<()> { |
61 | | - (&*self).flush() |
| 68 | + Stderr::flush(self) |
62 | 69 | } |
63 | 70 | } |
64 | 71 |
|
65 | | -pub const STDIN_BUF_SIZE: usize = 0; |
| 72 | +pub const STDIN_BUF_SIZE: usize = ::sys_common::io::DEFAULT_BUF_SIZE; |
66 | 73 |
|
67 | | -pub fn is_ebadf(_err: &io::Error) -> bool { |
68 | | - true |
| 74 | +pub fn is_ebadf(err: &io::Error) -> bool { |
| 75 | + // FIXME: Rust normally maps Unix EBADF to `Other` |
| 76 | + err.raw_os_error() == Some(abi::Error::BrokenPipe as _) |
69 | 77 | } |
70 | 78 |
|
71 | 79 | pub fn panic_output() -> Option<impl io::Write> { |
|
0 commit comments