Skip to content

Commit 5fd49af

Browse files
committed
Allow using virtual files for stdin/out/err
This adds the ability to use virtual files not only for the file system in WASI, but also for stdin, stdout and stderr.
1 parent 1873c0a commit 5fd49af

1 file changed

Lines changed: 33 additions & 21 deletions

File tree

crates/wasi-common/src/ctx.rs

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ use crate::entry::{Entry, EntryHandle};
22
use crate::fdpool::FdPool;
33
use crate::handle::Handle;
44
use crate::sys::oshandle::{OsHandle, OsHandleExt};
5-
use crate::virtfs::{VirtualDir, VirtualDirEntry};
6-
use crate::wasi::types;
7-
use crate::wasi::{Errno, Result};
5+
use crate::virtfs::{FileContents, InMemoryFile, VirtualDir, VirtualDirEntry};
6+
use crate::wasi::{types, Errno, Result};
87
use std::borrow::Borrow;
98
use std::cell::RefCell;
109
use std::collections::HashMap;
@@ -45,6 +44,7 @@ type WasiCtxBuilderResult<T> = std::result::Result<T, WasiCtxBuilderError>;
4544
enum PendingEntry {
4645
Thunk(fn() -> io::Result<OsHandle>),
4746
File(File),
47+
Virtual(Box<dyn FileContents>),
4848
}
4949

5050
impl std::fmt::Debug for PendingEntry {
@@ -56,6 +56,7 @@ impl std::fmt::Debug for PendingEntry {
5656
f as *const fn() -> io::Result<OsHandle>
5757
),
5858
Self::File(f) => write!(fmt, "PendingEntry::File({:?})", f),
59+
Self::Virtual(_) => write!(fmt, "PendingEntry::Virtual(...)"),
5960
}
6061
}
6162
}
@@ -229,24 +230,42 @@ impl WasiCtxBuilder {
229230
self
230231
}
231232

232-
/// Provide a File to use as stdin
233+
/// Provide a File to use as stdin.
233234
pub fn stdin(&mut self, file: File) -> &mut Self {
234235
self.stdin = Some(PendingEntry::File(file));
235236
self
236237
}
237238

238-
/// Provide a File to use as stdout
239+
/// Provide a File to use as stdout.
239240
pub fn stdout(&mut self, file: File) -> &mut Self {
240241
self.stdout = Some(PendingEntry::File(file));
241242
self
242243
}
243244

244-
/// Provide a File to use as stderr
245+
/// Provide a File to use as stderr.
245246
pub fn stderr(&mut self, file: File) -> &mut Self {
246247
self.stderr = Some(PendingEntry::File(file));
247248
self
248249
}
249250

251+
/// Provide a virtual file to use as stdin.
252+
pub fn stdin_virt(&mut self, file: Box<dyn FileContents>) -> &mut Self {
253+
self.stdin = Some(PendingEntry::Virtual(file));
254+
self
255+
}
256+
257+
/// Provide a virtual file to use as stdout.
258+
pub fn stdout_virt(&mut self, file: Box<dyn FileContents>) -> &mut Self {
259+
self.stdout = Some(PendingEntry::Virtual(file));
260+
self
261+
}
262+
263+
/// Provide a virtual file to use as stderr.
264+
pub fn stderr_virt(&mut self, file: Box<dyn FileContents>) -> &mut Self {
265+
self.stderr = Some(PendingEntry::Virtual(file));
266+
self
267+
}
268+
250269
/// Add a preopened directory.
251270
pub fn preopened_dir<P: AsRef<Path>>(&mut self, dir: File, guest_path: P) -> &mut Self {
252271
self.preopens.as_mut().unwrap().push((
@@ -334,22 +353,15 @@ impl WasiCtxBuilder {
334353
self.stderr.take().unwrap(),
335354
] {
336355
log::debug!("WasiCtx inserting entry {:?}", pending);
337-
let fd = match pending {
338-
PendingEntry::Thunk(f) => {
339-
let handle = EntryHandle::new(f()?);
340-
let entry = Entry::from(handle)?;
341-
entries
342-
.insert(entry)
343-
.ok_or(WasiCtxBuilderError::TooManyFilesOpen)?
344-
}
345-
PendingEntry::File(f) => {
346-
let handle = EntryHandle::new(OsHandle::from(f));
347-
let entry = Entry::from(handle)?;
348-
entries
349-
.insert(entry)
350-
.ok_or(WasiCtxBuilderError::TooManyFilesOpen)?
351-
}
356+
let handle = match pending {
357+
PendingEntry::Thunk(f) => EntryHandle::new(f()?),
358+
PendingEntry::File(f) => EntryHandle::new(OsHandle::from(f)),
359+
PendingEntry::Virtual(f) => EntryHandle::new(InMemoryFile::new(f)),
352360
};
361+
let entry = Entry::from(handle)?;
362+
let fd = entries
363+
.insert(entry)
364+
.ok_or(WasiCtxBuilderError::TooManyFilesOpen)?;
353365
log::debug!("WasiCtx inserted at {:?}", fd);
354366
}
355367
// Then add the preopen entries.

0 commit comments

Comments
 (0)