restructure StringArrayWriter trait into StringArray struct
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use crate::entry::{Entry, EntryHandle};
|
||||
use crate::fdpool::FdPool;
|
||||
use crate::handle::Handle;
|
||||
use crate::string_array_writer::StringArrayWriter;
|
||||
use crate::string_array::{StringArray, StringArrayError};
|
||||
use crate::sys::osdir::OsDir;
|
||||
use crate::sys::stdio::NullDevice;
|
||||
use crate::sys::stdio::{Stderr, StderrExt, Stdin, StdinExt, Stdout, StdoutExt};
|
||||
@@ -12,7 +12,7 @@ use std::borrow::Borrow;
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
use std::convert::TryFrom;
|
||||
use std::ffi::{self, CString, OsString};
|
||||
use std::ffi::OsString;
|
||||
use std::fs::File;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::rc::Rc;
|
||||
@@ -33,9 +33,12 @@ pub enum WasiCtxBuilderError {
|
||||
/// This error is expected to only occur on Windows hosts.
|
||||
#[error("provided sequence is not valid UTF-16: {0}")]
|
||||
InvalidUtf16(#[from] string::FromUtf16Error),
|
||||
/// Provided sequence of bytes contained an unexpected NUL byte.
|
||||
#[error("provided sequence contained an unexpected NUL byte")]
|
||||
UnexpectedNul(#[from] ffi::NulError),
|
||||
/// Error constructing arguments
|
||||
#[error("while constructing arguments: {0}")]
|
||||
Args(#[source] StringArrayError),
|
||||
/// Error constructing environment
|
||||
#[error("while constructing environment: {0}")]
|
||||
Env(#[source] StringArrayError),
|
||||
/// The root of a VirtualDirEntry tree must be a VirtualDirEntry::Directory.
|
||||
#[error("the root of a VirtualDirEntry tree at {} must be a VirtualDirEntry::Directory", .0.display())]
|
||||
VirtualDirEntryRootNotADirectory(PathBuf),
|
||||
@@ -65,24 +68,24 @@ impl std::fmt::Debug for PendingEntry {
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, Hash, PartialEq)]
|
||||
enum PendingCString {
|
||||
enum PendingString {
|
||||
Bytes(Vec<u8>),
|
||||
OsString(OsString),
|
||||
}
|
||||
|
||||
impl From<Vec<u8>> for PendingCString {
|
||||
impl From<Vec<u8>> for PendingString {
|
||||
fn from(bytes: Vec<u8>) -> Self {
|
||||
Self::Bytes(bytes)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<OsString> for PendingCString {
|
||||
impl From<OsString> for PendingString {
|
||||
fn from(s: OsString) -> Self {
|
||||
Self::OsString(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl PendingCString {
|
||||
impl PendingString {
|
||||
fn into_string(self) -> WasiCtxBuilderResult<String> {
|
||||
let res = match self {
|
||||
Self::Bytes(v) => String::from_utf8(v)?,
|
||||
@@ -100,13 +103,6 @@ impl PendingCString {
|
||||
};
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
/// Create a `CString` containing valid UTF-8.
|
||||
fn into_utf8_cstring(self) -> WasiCtxBuilderResult<CString> {
|
||||
let s = self.into_string()?;
|
||||
let s = CString::new(s)?;
|
||||
Ok(s)
|
||||
}
|
||||
}
|
||||
|
||||
struct PendingPreopen(Box<dyn FnOnce() -> WasiCtxBuilderResult<Box<dyn Handle>>>);
|
||||
@@ -130,8 +126,8 @@ pub struct WasiCtxBuilder {
|
||||
stdout: Option<PendingEntry>,
|
||||
stderr: Option<PendingEntry>,
|
||||
preopens: Option<Vec<(PathBuf, PendingPreopen)>>,
|
||||
args: Option<Vec<PendingCString>>,
|
||||
env: Option<HashMap<PendingCString, PendingCString>>,
|
||||
args: Option<Vec<PendingString>>,
|
||||
env: Option<HashMap<PendingString, PendingString>>,
|
||||
}
|
||||
|
||||
impl WasiCtxBuilder {
|
||||
@@ -180,7 +176,7 @@ impl WasiCtxBuilder {
|
||||
pub fn inherit_args(&mut self) -> &mut Self {
|
||||
let args = self.args.as_mut().unwrap();
|
||||
args.clear();
|
||||
args.extend(env::args_os().map(PendingCString::OsString));
|
||||
args.extend(env::args_os().map(PendingString::OsString));
|
||||
self
|
||||
}
|
||||
|
||||
@@ -324,16 +320,16 @@ impl WasiCtxBuilder {
|
||||
/// If any of the arguments or environment variables in this builder cannot be converted into
|
||||
/// `CString`s, either due to NUL bytes or Unicode conversions, this will fail.
|
||||
pub fn build(&mut self) -> WasiCtxBuilderResult<WasiCtx> {
|
||||
// Process arguments and environment variables into `CString`s, failing quickly if they
|
||||
// Process arguments and environment variables into `String`s, failing quickly if they
|
||||
// contain any NUL bytes, or if conversion from `OsString` fails.
|
||||
let args = self
|
||||
.args
|
||||
.take()
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.map(|arg| arg.into_utf8_cstring())
|
||||
.collect::<WasiCtxBuilderResult<Vec<CString>>>()?;
|
||||
|
||||
.map(|arg| arg.into_string())
|
||||
.collect::<WasiCtxBuilderResult<Vec<String>>>()?;
|
||||
let args = StringArray::new(args).map_err(WasiCtxBuilderError::Args)?;
|
||||
let env = self
|
||||
.env
|
||||
.take()
|
||||
@@ -344,14 +340,12 @@ impl WasiCtxBuilder {
|
||||
v.into_string().and_then(|v| {
|
||||
pair.push('=');
|
||||
pair.push_str(v.as_str());
|
||||
// We have valid UTF-8, but the keys and values have not yet been checked
|
||||
// for NULs, so we do a final check here.
|
||||
let s = CString::new(pair)?;
|
||||
Ok(s)
|
||||
Ok(pair)
|
||||
})
|
||||
})
|
||||
})
|
||||
.collect::<WasiCtxBuilderResult<Vec<CString>>>()?;
|
||||
.collect::<WasiCtxBuilderResult<Vec<String>>>()?;
|
||||
let env = StringArray::new(env).map_err(WasiCtxBuilderError::Env)?;
|
||||
|
||||
let mut entries = EntryTable::new();
|
||||
// Populate the non-preopen entries.
|
||||
@@ -441,8 +435,8 @@ impl EntryTable {
|
||||
|
||||
pub struct WasiCtx {
|
||||
entries: RefCell<EntryTable>,
|
||||
pub(crate) args: Vec<CString>,
|
||||
pub(crate) env: Vec<CString>,
|
||||
pub(crate) args: StringArray,
|
||||
pub(crate) env: StringArray,
|
||||
}
|
||||
|
||||
impl WasiCtx {
|
||||
@@ -493,6 +487,7 @@ impl WasiCtx {
|
||||
self.entries.borrow_mut().remove(fd).ok_or(Error::Badf)
|
||||
}
|
||||
|
||||
/*
|
||||
pub(crate) fn args(&self) -> &impl StringArrayWriter {
|
||||
&self.args
|
||||
}
|
||||
@@ -500,4 +495,5 @@ impl WasiCtx {
|
||||
pub(crate) fn env(&self) -> &impl StringArrayWriter {
|
||||
&self.env
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user