wasi_process2/
stdio.rs

1use serde::{Deserialize, Serialize};
2use std::io::{prelude::*, SeekFrom};
3use tokio::io::{self, AsyncReadExt, AsyncWriteExt};
4use tokio::runtime::Handle;
5use wasmer_wasi::{types as wasi_types, WasiFile, WasiFsError};
6
7use super::{STDERR, STDIN, STDOUT};
8
9/// The stdin pseudo-file for wasi processes.
10#[derive(Debug, Serialize, Deserialize)]
11pub struct Stdin;
12impl Read for Stdin {
13    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
14        STDIN.with(|stdin| Handle::current().block_on((&*stdin).read(buf)))
15    }
16}
17impl Seek for Stdin {
18    fn seek(&mut self, _pos: SeekFrom) -> io::Result<u64> {
19        Err(io::Error::new(io::ErrorKind::Other, "can not seek stdin"))
20    }
21}
22impl Write for Stdin {
23    fn write(&mut self, _buf: &[u8]) -> io::Result<usize> {
24        Err(io::Error::new(
25            io::ErrorKind::Other,
26            "can not write to stdin",
27        ))
28    }
29    fn flush(&mut self) -> io::Result<()> {
30        Err(io::Error::new(
31            io::ErrorKind::Other,
32            "can not write to stdin",
33        ))
34    }
35    fn write_all(&mut self, _buf: &[u8]) -> io::Result<()> {
36        Err(io::Error::new(
37            io::ErrorKind::Other,
38            "can not write to stdin",
39        ))
40    }
41    fn write_fmt(&mut self, _fmt: ::std::fmt::Arguments) -> io::Result<()> {
42        Err(io::Error::new(
43            io::ErrorKind::Other,
44            "can not write to stdin",
45        ))
46    }
47}
48
49impl WasiFile for Stdin {
50    fn last_accessed(&self) -> u64 {
51        0
52    }
53    fn last_modified(&self) -> u64 {
54        0
55    }
56    fn created_time(&self) -> u64 {
57        0
58    }
59    fn size(&self) -> u64 {
60        0
61    }
62    fn set_len(&mut self, _new_size: wasi_types::__wasi_filesize_t) -> Result<(), WasiFsError> {
63        Err(WasiFsError::PermissionDenied)
64    }
65
66    fn unlink(&mut self) -> Result<(), WasiFsError> {
67        Ok(())
68    }
69
70    fn bytes_available(&self) -> Result<usize, WasiFsError> {
71        Ok(0)
72    }
73}
74
75/// The stdout pseudo-file for wasi processes.
76#[derive(Debug, Serialize, Deserialize)]
77pub struct Stdout;
78impl Read for Stdout {
79    fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> {
80        Err(io::Error::new(
81            io::ErrorKind::Other,
82            "can not read from stdout",
83        ))
84    }
85    fn read_to_end(&mut self, _buf: &mut Vec<u8>) -> io::Result<usize> {
86        Err(io::Error::new(
87            io::ErrorKind::Other,
88            "can not read from stdout",
89        ))
90    }
91    fn read_to_string(&mut self, _buf: &mut String) -> io::Result<usize> {
92        Err(io::Error::new(
93            io::ErrorKind::Other,
94            "can not read from stdout",
95        ))
96    }
97    fn read_exact(&mut self, _buf: &mut [u8]) -> io::Result<()> {
98        Err(io::Error::new(
99            io::ErrorKind::Other,
100            "can not read from stdout",
101        ))
102    }
103}
104impl Seek for Stdout {
105    fn seek(&mut self, _pos: SeekFrom) -> io::Result<u64> {
106        Err(io::Error::new(io::ErrorKind::Other, "can not seek stdout"))
107    }
108}
109impl Write for Stdout {
110    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
111        STDOUT.with(|stdout| Handle::current().block_on((&*stdout).write(buf)))
112    }
113    fn flush(&mut self) -> io::Result<()> {
114        Ok(())
115    }
116}
117
118impl WasiFile for Stdout {
119    fn last_accessed(&self) -> u64 {
120        0
121    }
122    fn last_modified(&self) -> u64 {
123        0
124    }
125    fn created_time(&self) -> u64 {
126        0
127    }
128    fn size(&self) -> u64 {
129        0
130    }
131    fn set_len(&mut self, _new_size: wasi_types::__wasi_filesize_t) -> Result<(), WasiFsError> {
132        Err(WasiFsError::PermissionDenied)
133    }
134    fn unlink(&mut self) -> Result<(), WasiFsError> {
135        Ok(())
136    }
137
138    fn bytes_available(&self) -> Result<usize, WasiFsError> {
139        Err(WasiFsError::InvalidInput)
140    }
141}
142
143/// The stderr pseudo-file for wasi processes.
144#[derive(Debug, Serialize, Deserialize)]
145pub struct Stderr;
146impl Read for Stderr {
147    fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> {
148        Err(io::Error::new(
149            io::ErrorKind::Other,
150            "can not read from stderr",
151        ))
152    }
153    fn read_to_end(&mut self, _buf: &mut Vec<u8>) -> io::Result<usize> {
154        Err(io::Error::new(
155            io::ErrorKind::Other,
156            "can not read from stderr",
157        ))
158    }
159    fn read_to_string(&mut self, _buf: &mut String) -> io::Result<usize> {
160        Err(io::Error::new(
161            io::ErrorKind::Other,
162            "can not read from stderr",
163        ))
164    }
165    fn read_exact(&mut self, _buf: &mut [u8]) -> io::Result<()> {
166        Err(io::Error::new(
167            io::ErrorKind::Other,
168            "can not read from stderr",
169        ))
170    }
171}
172impl Seek for Stderr {
173    fn seek(&mut self, _pos: SeekFrom) -> io::Result<u64> {
174        Err(io::Error::new(io::ErrorKind::Other, "can not seek stderr"))
175    }
176}
177impl Write for Stderr {
178    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
179        STDERR.with(|stderr| Handle::current().block_on((&*stderr).write(buf)))
180    }
181    fn flush(&mut self) -> io::Result<()> {
182        Ok(())
183    }
184}
185
186impl WasiFile for Stderr {
187    fn last_accessed(&self) -> u64 {
188        0
189    }
190    fn last_modified(&self) -> u64 {
191        0
192    }
193    fn created_time(&self) -> u64 {
194        0
195    }
196    fn size(&self) -> u64 {
197        0
198    }
199    fn set_len(&mut self, _new_size: wasi_types::__wasi_filesize_t) -> Result<(), WasiFsError> {
200        Err(WasiFsError::PermissionDenied)
201    }
202    fn unlink(&mut self) -> Result<(), WasiFsError> {
203        Ok(())
204    }
205
206    fn bytes_available(&self) -> Result<usize, WasiFsError> {
207        Err(WasiFsError::InvalidInput)
208    }
209}