|
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; |
13 | 15 | use panicking::PanicOutput; |
14 | 16 |
|
15 | | -pub struct Stdin; |
16 | | -pub struct Stdout; |
17 | | -pub struct Stderr; |
| 17 | +pub struct Stdin(()); |
| 18 | +pub struct Stdout(()); |
| 19 | +pub struct Stderr(()); |
| 20 | + |
| 21 | +fn with_std_fd<F: FnOnce(&FileDesc) -> R, R>(fd: abi::Fd, f: F) -> R { |
| 22 | + let fd = FileDesc::new(fd); |
| 23 | + let ret = f(&fd); |
| 24 | + fd.into_raw(); |
| 25 | + ret |
| 26 | +} |
18 | 27 |
|
19 | 28 | impl Stdin { |
20 | | - pub fn new() -> io::Result<Stdin> { |
21 | | - Ok(Stdin) |
22 | | - } |
| 29 | + pub fn new() -> io::Result<Stdin> { Ok(Stdin(())) } |
23 | 30 |
|
24 | | - pub fn read(&self, _data: &mut [u8]) -> io::Result<usize> { |
25 | | - unsupported() |
| 31 | + pub fn read(&self, data: &mut [u8]) -> io::Result<usize> { |
| 32 | + with_std_fd(abi::FD_STDIN, |fd| fd.read(data)) |
26 | 33 | } |
27 | 34 | } |
28 | 35 |
|
29 | 36 | impl Stdout { |
30 | | - pub fn new() -> io::Result<Stdout> { |
31 | | - Ok(Stdout) |
32 | | - } |
| 37 | + pub fn new() -> io::Result<Stdout> { Ok(Stdout(())) } |
33 | 38 |
|
34 | | - pub fn write(&self, _data: &[u8]) -> io::Result<usize> { |
35 | | - unsupported() |
| 39 | + pub fn write(&self, data: &[u8]) -> io::Result<usize> { |
| 40 | + with_std_fd(abi::FD_STDOUT, |fd| fd.write(data)) |
36 | 41 | } |
37 | 42 |
|
38 | 43 | pub fn flush(&self) -> io::Result<()> { |
39 | | - Ok(()) |
| 44 | + with_std_fd(abi::FD_STDOUT, |fd| fd.flush()) |
40 | 45 | } |
41 | 46 | } |
42 | 47 |
|
43 | 48 | impl Stderr { |
44 | | - pub fn new() -> io::Result<Stderr> { |
45 | | - Ok(Stderr) |
46 | | - } |
| 49 | + pub fn new() -> io::Result<Stderr> { Ok(Stderr(())) } |
47 | 50 |
|
48 | | - pub fn write(&self, _data: &[u8]) -> io::Result<usize> { |
49 | | - unsupported() |
| 51 | + pub fn write(&self, data: &[u8]) -> io::Result<usize> { |
| 52 | + with_std_fd(abi::FD_STDERR, |fd| fd.write(data)) |
50 | 53 | } |
51 | 54 |
|
52 | 55 | pub fn flush(&self) -> io::Result<()> { |
53 | | - Ok(()) |
| 56 | + with_std_fd(abi::FD_STDERR, |fd| fd.flush()) |
54 | 57 | } |
55 | 58 | } |
56 | 59 |
|
| 60 | +// FIXME: right now this raw stderr handle is used in a few places because |
| 61 | +// std::io::stderr_raw isn't exposed, but once that's exposed this impl |
| 62 | +// should go away |
57 | 63 | impl io::Write for Stderr { |
58 | 64 | fn write(&mut self, data: &[u8]) -> io::Result<usize> { |
59 | | - (&*self).write(data) |
| 65 | + Stderr::write(self, data) |
60 | 66 | } |
| 67 | + |
61 | 68 | fn flush(&mut self) -> io::Result<()> { |
62 | | - (&*self).flush() |
| 69 | + Stderr::flush(self) |
63 | 70 | } |
64 | 71 | } |
65 | 72 |
|
66 | | -pub const STDIN_BUF_SIZE: usize = 0; |
67 | | - |
68 | | -pub fn is_ebadf(_err: &io::Error) -> bool { |
69 | | - true |
| 73 | +pub fn is_ebadf(err: &io::Error) -> bool { |
| 74 | + // FIXME: Rust normally maps Unix EBADF to `Other` |
| 75 | + err.raw_os_error() == Some(abi::Error::BrokenPipe as _) |
70 | 76 | } |
71 | 77 |
|
| 78 | +pub const STDIN_BUF_SIZE: usize = ::sys_common::io::DEFAULT_BUF_SIZE; |
| 79 | + |
72 | 80 | pub fn panic_output() -> Option<PanicOutput<super::abi::panic::SgxPanicOutput>> { |
73 | 81 | extern { |
74 | 82 | static DEBUG: u8; |
|
0 commit comments