Rewrite majority of impl reusing libstd (#34)

* Rewrite FdEntry reusing as much libstd as possible

* Use the new FdEntry, FdObject, Descriptor struct in *nix impl

* Adapt Windows impl

* Remove unnecessary check in fd_read

Check `host_nread == 0` caused premature FdEntry closure and removal
which ultimately was resulting in an attempt at "double closing" of
the same file descriptor at the end of the Wasm program:
...
fd_close(fd=4)
    -> errno=WASI_ESUCCESS
fd_close(fd=4)
    -> errno=WASI_EBADF

* Use libstd vectored IO

* Use std:🧵:yield_now to implement sched_yield

* Add logging to integration tests

* Add preliminary support for host-specific errors

* Operate on std::fs::File in path_get on *nix

* Add cross-platform RawString type encapsulating OsStrExt

* Fix Windows build

* Update Travis and README to Rust v1.36

* Remove unused winx::handle::close helper

* Refactor Descriptor into raw handles/fds

* Strip readlinkat in prep for path_get host-independent

* Strip openat in prep for path_get host-independent

* Move ManuallyDrop up one level from Descriptor to FdObject

* Make (c)iovec host fns unsafe

* Swap unwraps/expects for Results in fdentry_impl on nix

* Rewrite fd_pread/write and implement for Win

* Use File::sync_all to impl fd_sync

* Use File::sync_data to impl fd_datasync

* Rewind file cursor after fd_p{read, write} on Windows

* Add fd_p{read, write} tests

* Handle errors instead of panicking in path_get

* Use File::set_len to impl fd_allocate

* Add test for fd_allocate

* Replace all panics with Results

* Document the point of RawString
This commit is contained in:
Jakub Konka
2019-07-16 00:34:28 +02:00
committed by Dan Gohman
parent 93e1657bae
commit 667f272edd
32 changed files with 977 additions and 1045 deletions

View File

@@ -1,81 +1,24 @@
use super::host_impl;
use crate::fdentry::Descriptor;
use crate::host;
use std::fs::File;
use std::os::windows::prelude::{AsRawHandle, FromRawHandle, IntoRawHandle, RawHandle};
use std::path::PathBuf;
use std::io;
use std::os::windows::prelude::{AsRawHandle, FromRawHandle, RawHandle};
#[derive(Clone, Debug)]
pub struct FdObject {
pub ty: host::__wasi_filetype_t,
pub raw_handle: RawHandle,
pub needs_close: bool,
// TODO: directories
}
#[derive(Clone, Debug)]
pub struct FdEntry {
pub fd_object: FdObject,
pub rights_base: host::__wasi_rights_t,
pub rights_inheriting: host::__wasi_rights_t,
pub preopen_path: Option<PathBuf>,
}
impl Drop for FdObject {
fn drop(&mut self) {
if self.needs_close {
winx::handle::close(self.raw_handle)
.unwrap_or_else(|e| eprintln!("FdObject::drop(): {}", e))
impl AsRawHandle for Descriptor {
fn as_raw_handle(&self) -> RawHandle {
match self {
Descriptor::File(f) => f.as_raw_handle(),
Descriptor::Stdin => io::stdin().as_raw_handle(),
Descriptor::Stdout => io::stdout().as_raw_handle(),
Descriptor::Stderr => io::stderr().as_raw_handle(),
}
}
}
impl FdEntry {
pub fn from_file(file: File) -> Self {
unsafe { Self::from_raw_handle(file.into_raw_handle()) }
}
pub fn duplicate<F: AsRawHandle>(fd: &F) -> Self {
unsafe { Self::from_raw_handle(winx::handle::dup(fd.as_raw_handle()).unwrap()) }
}
}
impl FromRawHandle for FdEntry {
unsafe fn from_raw_handle(raw_handle: RawHandle) -> Self {
use winx::file::{get_file_access_rights, AccessRight};
let (ty, mut rights_base, rights_inheriting) =
determine_type_rights(raw_handle).expect("can determine type rights");
if ty != host::__WASI_FILETYPE_CHARACTER_DEVICE {
// TODO: is there a way around this? On windows, it seems
// we cannot check access rights for stdout/in handles
let rights =
get_file_access_rights(raw_handle).expect("can determine file access rights");
let rights = AccessRight::from_bits_truncate(rights);
if rights.contains(AccessRight::FILE_GENERIC_READ) {
rights_base |= host::__WASI_RIGHT_FD_READ;
}
if rights.contains(AccessRight::FILE_GENERIC_WRITE) {
rights_base |= host::__WASI_RIGHT_FD_WRITE;
}
}
Self {
fd_object: FdObject {
ty,
raw_handle,
needs_close: true,
},
rights_base,
rights_inheriting,
preopen_path: None,
}
}
}
pub unsafe fn determine_type_rights(
raw_handle: RawHandle,
pub(crate) fn determine_type_and_access_rights<Handle: AsRawHandle>(
handle: &Handle,
) -> Result<
(
host::__wasi_filetype_t,
@@ -84,8 +27,44 @@ pub unsafe fn determine_type_rights(
),
host::__wasi_errno_t,
> {
let (ty, rights_base, rights_inheriting) = {
let file_type = winx::file::get_file_type(raw_handle).map_err(host_impl::errno_from_win)?;
use winx::file::{get_file_access_rights, AccessRight};
let (file_type, mut rights_base, rights_inheriting) = determine_type_rights(handle)?;
match file_type {
host::__WASI_FILETYPE_DIRECTORY | host::__WASI_FILETYPE_REGULAR_FILE => {
let rights = get_file_access_rights(handle.as_raw_handle())
.map_err(host_impl::errno_from_win)?;
let rights = AccessRight::from_bits_truncate(rights);
if rights.contains(AccessRight::FILE_GENERIC_READ) {
rights_base |= host::__WASI_RIGHT_FD_READ;
}
if rights.contains(AccessRight::FILE_GENERIC_WRITE) {
rights_base |= host::__WASI_RIGHT_FD_WRITE;
}
}
_ => {
// TODO: is there a way around this? On windows, it seems
// we cannot check access rights for anything but dirs and regular files
}
}
Ok((file_type, rights_base, rights_inheriting))
}
pub(crate) fn determine_type_rights<Handle: AsRawHandle>(
handle: &Handle,
) -> Result<
(
host::__wasi_filetype_t,
host::__wasi_rights_t,
host::__wasi_rights_t,
),
host::__wasi_errno_t,
> {
let (file_type, rights_base, rights_inheriting) = {
let file_type =
winx::file::get_file_type(handle.as_raw_handle()).map_err(host_impl::errno_from_win)?;
if file_type.is_char() {
// character file: LPT device or console
// TODO: rule out LPT device
@@ -96,7 +75,9 @@ pub unsafe fn determine_type_rights(
)
} else if file_type.is_disk() {
// disk file: file, dir or disk device
let file = std::mem::ManuallyDrop::new(File::from_raw_handle(raw_handle));
let file = std::mem::ManuallyDrop::new(unsafe {
File::from_raw_handle(handle.as_raw_handle())
});
let meta = file.metadata().map_err(|_| host::__WASI_EINVAL)?;
if meta.is_dir() {
(
@@ -125,5 +106,5 @@ pub unsafe fn determine_type_rights(
return Err(host::__WASI_EINVAL);
}
};
Ok((ty, rights_base, rights_inheriting))
Ok((file_type, rights_base, rights_inheriting))
}

View File

@@ -33,28 +33,6 @@ pub fn errno_from_win(error: winx::winerror::WinError) -> host::__wasi_errno_t {
}
}
pub unsafe fn ciovec_to_win<'a>(ciovec: &'a host::__wasi_ciovec_t) -> winx::io::IoVec<'a> {
let slice = slice::from_raw_parts(ciovec.buf as *const u8, ciovec.buf_len);
winx::io::IoVec::new(slice)
}
pub unsafe fn ciovec_to_win_mut<'a>(
ciovec: &'a mut host::__wasi_ciovec_t,
) -> winx::io::IoVecMut<'a> {
let slice = slice::from_raw_parts_mut(ciovec.buf as *mut u8, ciovec.buf_len);
winx::io::IoVecMut::new(slice)
}
pub unsafe fn iovec_to_win<'a>(iovec: &'a host::__wasi_iovec_t) -> winx::io::IoVec<'a> {
let slice = slice::from_raw_parts(iovec.buf as *const u8, iovec.buf_len);
winx::io::IoVec::new(slice)
}
pub unsafe fn iovec_to_win_mut<'a>(iovec: &'a mut host::__wasi_iovec_t) -> winx::io::IoVecMut<'a> {
let slice = slice::from_raw_parts_mut(iovec.buf as *mut u8, iovec.buf_len);
winx::io::IoVecMut::new(slice)
}
pub fn win_from_fdflags(
fdflags: host::__wasi_fdflags_t,
) -> (winx::file::AccessRight, winx::file::FlagsAndAttributes) {
@@ -129,16 +107,61 @@ pub fn win_from_oflags(
(win_disp, win_flags_attrs)
}
pub fn path_from_raw(raw_path: &[u8]) -> OsString {
OsString::from_wide(&raw_path.iter().map(|&x| x as u16).collect::<Vec<u16>>())
/// `RawString` wraps `OsString` with Windows specific extensions
/// enabling a common interface between different hosts for
/// WASI raw string manipulation.
#[derive(Debug, Clone)]
pub struct RawString {
s: OsString,
}
pub fn path_to_raw<P: AsRef<OsStr>>(path: P) -> Vec<u8> {
path.as_ref()
.encode_wide()
.map(u16::to_le_bytes)
.fold(Vec::new(), |mut acc, bytes| {
acc.extend_from_slice(&bytes);
acc
})
impl RawString {
pub fn new(s: OsString) -> Self {
Self { s }
}
pub fn from_bytes(slice: &[u8]) -> Self {
Self {
s: OsString::from_wide(&slice.iter().map(|&x| x as u16).collect::<Vec<u16>>()),
}
}
pub fn to_bytes(&self) -> Vec<u8> {
self.s
.encode_wide()
.map(u16::to_le_bytes)
.fold(Vec::new(), |mut acc, bytes| {
acc.extend_from_slice(&bytes);
acc
})
}
pub fn contains(&self, c: &u8) -> bool {
let c = u16::from_le_bytes([*c, 0u8]);
self.s.encode_wide().find(|&x| x == c).is_some()
}
pub fn ends_with(&self, cs: &[u8]) -> bool {
let cs = cs.iter().map(|c| u16::from_le_bytes([*c, 0u8])).rev();
let ss: Vec<u16> = self.s.encode_wide().collect();
ss.into_iter().rev().zip(cs).all(|(l, r)| l == r)
}
pub fn push<T: AsRef<OsStr>>(&mut self, s: T) {
self.s.push(s)
}
}
impl AsRef<OsStr> for RawString {
fn as_ref(&self) -> &OsStr {
&self.s
}
}
impl From<&OsStr> for RawString {
fn from(os_str: &OsStr) -> Self {
Self {
s: os_str.to_owned(),
}
}
}

View File

@@ -1,51 +1,54 @@
#![allow(non_camel_case_types)]
#![allow(unused)]
use super::fdentry::{determine_type_rights, FdEntry};
use super::fs_helpers::*;
use super::host_impl;
use crate::ctx::WasiCtx;
use crate::fdentry::FdEntry;
use crate::host;
use crate::sys::errno_from_host;
use crate::sys::fdentry_impl::determine_type_rights;
use crate::sys::host_impl::{self, RawString};
use std::ffi::OsStr;
use std::os::windows::prelude::FromRawHandle;
use std::fs::File;
use std::io::{self, Seek, SeekFrom};
use std::os::windows::fs::FileExt;
use std::os::windows::prelude::{AsRawHandle, FromRawHandle};
pub(crate) fn fd_close(fd_entry: FdEntry) -> Result<(), host::__wasi_errno_t> {
winx::handle::close(fd_entry.fd_object.raw_handle).map_err(|e| host_impl::errno_from_win(e))
fn read_at(mut file: &File, buf: &mut [u8], offset: u64) -> io::Result<usize> {
// get current cursor position
let cur_pos = file.seek(SeekFrom::Current(0))?;
// perform a seek read by a specified offset
let nread = file.seek_read(buf, offset)?;
// rewind the cursor back to the original position
file.seek(SeekFrom::Start(cur_pos))?;
Ok(nread)
}
pub(crate) fn fd_datasync(fd_entry: &FdEntry) -> Result<(), host::__wasi_errno_t> {
unimplemented!("fd_datasync")
fn write_at(mut file: &File, buf: &[u8], offset: u64) -> io::Result<usize> {
// get current cursor position
let cur_pos = file.seek(SeekFrom::Current(0))?;
// perform a seek write by a specified offset
let nwritten = file.seek_write(buf, offset)?;
// rewind the cursor back to the original position
file.seek(SeekFrom::Start(cur_pos))?;
Ok(nwritten)
}
pub(crate) fn fd_pread(
fd_entry: &FdEntry,
file: &File,
buf: &mut [u8],
offset: host::__wasi_filesize_t,
) -> Result<usize, host::__wasi_errno_t> {
unimplemented!("fd_pread")
read_at(file, buf, offset)
.map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))
}
pub(crate) fn fd_pwrite(
fd_entry: &FdEntry,
file: &File,
buf: &[u8],
offset: host::__wasi_filesize_t,
) -> Result<usize, host::__wasi_errno_t> {
unimplemented!("fd_pwrite")
}
pub(crate) fn fd_read(
fd_entry: &FdEntry,
iovs: &mut [host::__wasi_iovec_t],
) -> Result<usize, host::__wasi_errno_t> {
use winx::io::{readv, IoVecMut};
let mut iovs: Vec<IoVecMut> = iovs
.iter_mut()
.map(|iov| unsafe { host_impl::iovec_to_win_mut(iov) })
.collect();
readv(fd_entry.fd_object.raw_handle, &mut iovs).map_err(|e| host_impl::errno_from_win(e))
write_at(file, buf, offset)
.map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))
}
pub(crate) fn fd_renumber(
@@ -72,9 +75,9 @@ pub(crate) fn fd_fdstat_get(
fd_entry: &FdEntry,
) -> Result<host::__wasi_fdflags_t, host::__wasi_errno_t> {
use winx::file::AccessRight;
match winx::file::get_file_access_rights(fd_entry.fd_object.raw_handle)
.map(AccessRight::from_bits_truncate)
{
let raw_handle = fd_entry.fd_object.descriptor.as_raw_handle();
match winx::file::get_file_access_rights(raw_handle).map(AccessRight::from_bits_truncate) {
Ok(rights) => Ok(host_impl::fdflags_from_win(rights)),
Err(e) => Err(host_impl::errno_from_win(e)),
}
@@ -87,24 +90,6 @@ pub(crate) fn fd_fdstat_set_flags(
unimplemented!("fd_fdstat_set_flags")
}
pub(crate) fn fd_sync(fd_entry: &FdEntry) -> Result<(), host::__wasi_errno_t> {
unimplemented!("fd_sync")
}
pub(crate) fn fd_write(
fd_entry: &FdEntry,
iovs: &[host::__wasi_iovec_t],
) -> Result<usize, host::__wasi_errno_t> {
use winx::io::{writev, IoVec};
let iovs: Vec<IoVec> = iovs
.iter()
.map(|iov| unsafe { host_impl::iovec_to_win(iov) })
.collect();
writev(fd_entry.fd_object.raw_handle, &iovs).map_err(|e| host_impl::errno_from_win(e))
}
pub(crate) fn fd_advise(
fd_entry: &FdEntry,
advice: host::__wasi_advice_t,
@@ -114,18 +99,10 @@ pub(crate) fn fd_advise(
unimplemented!("fd_advise")
}
pub(crate) fn fd_allocate(
fd_entry: &FdEntry,
offset: host::__wasi_filesize_t,
len: host::__wasi_filesize_t,
) -> Result<(), host::__wasi_errno_t> {
unimplemented!("fd_allocate")
}
pub(crate) fn path_create_directory(
ctx: &WasiCtx,
dirfd: host::__wasi_fd_t,
path: &OsStr,
path: &RawString,
) -> Result<(), host::__wasi_errno_t> {
unimplemented!("path_create_directory")
}
@@ -134,8 +111,8 @@ pub(crate) fn path_link(
ctx: &WasiCtx,
old_dirfd: host::__wasi_fd_t,
new_dirfd: host::__wasi_fd_t,
old_path: &OsStr,
new_path: &OsStr,
old_path: &RawString,
new_path: &RawString,
source_rights: host::__wasi_rights_t,
target_rights: host::__wasi_rights_t,
) -> Result<(), host::__wasi_errno_t> {
@@ -146,7 +123,7 @@ pub(crate) fn path_open(
ctx: &WasiCtx,
dirfd: host::__wasi_fd_t,
dirflags: host::__wasi_lookupflags_t,
path: &OsStr,
path: &RawString,
oflags: host::__wasi_oflags_t,
read: bool,
write: bool,
@@ -196,23 +173,23 @@ pub(crate) fn path_open(
Err(e) => return Err(e),
};
let new_handle =
match winx::file::openat(dir, &path, win_rights, win_create_disp, win_flags_attrs) {
Ok(handle) => handle,
Err(e) => return Err(host_impl::errno_from_win(e)),
};
let new_handle = match winx::file::openat(
dir.as_raw_handle(),
&path,
win_rights,
win_create_disp,
win_flags_attrs,
) {
Ok(handle) => handle,
Err(e) => return Err(host_impl::errno_from_win(e)),
};
// Determine the type of the new file descriptor and which rights contradict with this type
match unsafe { determine_type_rights(new_handle) } {
Err(e) => {
// if `close` fails, note it but do not override the underlying errno
winx::handle::close(new_handle).unwrap_or_else(|e| {
dbg!(e);
});
Err(e)
}
let file = unsafe { File::from_raw_handle(new_handle) };
match determine_type_rights(&file) {
Err(e) => Err(e),
Ok((_ty, max_base, max_inheriting)) => {
let mut fe = unsafe { FdEntry::from_raw_handle(new_handle) };
let mut fe = FdEntry::from(file)?;
fe.rights_base &= max_base;
fe.rights_inheriting &= max_inheriting;
Ok(fe)
@@ -231,7 +208,7 @@ pub(crate) fn fd_readdir(
pub(crate) fn path_readlink(
wasi_ctx: &WasiCtx,
dirfd: host::__wasi_fd_t,
path: &OsStr,
path: &RawString,
rights: host::__wasi_rights_t,
buf: &mut [u8],
) -> Result<usize, host::__wasi_errno_t> {
@@ -241,10 +218,10 @@ pub(crate) fn path_readlink(
pub(crate) fn path_rename(
wasi_ctx: &WasiCtx,
old_dirfd: host::__wasi_fd_t,
old_path: &OsStr,
old_path: &RawString,
old_rights: host::__wasi_rights_t,
new_dirfd: host::__wasi_fd_t,
new_path: &OsStr,
new_path: &RawString,
new_rights: host::__wasi_rights_t,
) -> Result<(), host::__wasi_errno_t> {
unimplemented!("path_rename")
@@ -276,7 +253,7 @@ pub(crate) fn path_filestat_get(
wasi_ctx: &WasiCtx,
dirfd: host::__wasi_fd_t,
dirflags: host::__wasi_lookupflags_t,
path: &OsStr,
path: &RawString,
) -> Result<host::__wasi_filestat_t, host::__wasi_errno_t> {
unimplemented!("path_filestat_get")
}
@@ -285,7 +262,7 @@ pub(crate) fn path_filestat_set_times(
wasi_ctx: &WasiCtx,
dirfd: host::__wasi_fd_t,
dirflags: host::__wasi_lookupflags_t,
path: &OsStr,
path: &RawString,
rights: host::__wasi_rights_t,
st_atim: host::__wasi_timestamp_t,
mut st_mtim: host::__wasi_timestamp_t,
@@ -298,8 +275,8 @@ pub(crate) fn path_symlink(
wasi_ctx: &WasiCtx,
dirfd: host::__wasi_fd_t,
rights: host::__wasi_rights_t,
old_path: &OsStr,
new_path: &OsStr,
old_path: &RawString,
new_path: &RawString,
) -> Result<(), host::__wasi_errno_t> {
unimplemented!("path_symlink")
}
@@ -307,7 +284,7 @@ pub(crate) fn path_symlink(
pub(crate) fn path_unlink_file(
wasi_ctx: &WasiCtx,
dirfd: host::__wasi_fd_t,
path: &OsStr,
path: &RawString,
rights: host::__wasi_rights_t,
) -> Result<(), host::__wasi_errno_t> {
unimplemented!("path_unlink_file")
@@ -316,7 +293,7 @@ pub(crate) fn path_unlink_file(
pub(crate) fn path_remove_directory(
wasi_ctx: &WasiCtx,
dirfd: host::__wasi_fd_t,
path: &OsStr,
path: &RawString,
rights: host::__wasi_rights_t,
) -> Result<(), host::__wasi_errno_t> {
unimplemented!("path_remove_directory")

View File

@@ -1,85 +1,75 @@
#![allow(non_camel_case_types)]
#![allow(unused_unsafe)]
use super::host_impl;
use crate::ctx::WasiCtx;
use crate::fdentry::Descriptor;
use crate::host;
use crate::sys::errno_from_host;
use crate::sys::host_impl::{self, RawString};
use std::ffi::{OsStr, OsString};
use std::os::windows::prelude::RawHandle;
use std::path::{Component, Path, PathBuf};
use std::ffi::OsStr;
use std::fs::File;
use std::os::windows::prelude::{AsRawHandle, FromRawHandle};
use std::path::{Component, Path};
/// Normalizes a path to ensure that the target path is located under the directory provided.
pub fn path_get<P: AsRef<OsStr>>(
pub fn path_get(
wasi_ctx: &WasiCtx,
dirfd: host::__wasi_fd_t,
_dirflags: host::__wasi_lookupflags_t,
path: P,
path: &RawString,
needed_base: host::__wasi_rights_t,
needed_inheriting: host::__wasi_rights_t,
needs_final_component: bool,
) -> Result<(RawHandle, OsString), host::__wasi_errno_t> {
/// close all the intermediate handles, but make sure not to drop either the original
/// dirfd or the one we return (which may be the same dirfd)
fn ret_dir_success(dir_stack: &mut Vec<RawHandle>) -> RawHandle {
let ret_dir = dir_stack.pop().expect("there is always a dirfd to return");
if let Some(dirfds) = dir_stack.get(1..) {
for dirfd in dirfds {
winx::handle::close(*dirfd).unwrap_or_else(|e| {
dbg!(e);
});
}
}
ret_dir
}
/// close all file descriptors other than the base directory, and return the errno for
/// convenience with `return`
fn ret_error(
dir_stack: &mut Vec<RawHandle>,
errno: host::__wasi_errno_t,
) -> Result<(RawHandle, OsString), host::__wasi_errno_t> {
if let Some(dirfds) = dir_stack.get(1..) {
for dirfd in dirfds {
winx::handle::close(*dirfd).unwrap_or_else(|e| {
dbg!(e);
});
}
}
Err(errno)
) -> Result<(File, RawString), host::__wasi_errno_t> {
if path.contains(&b'\0') {
// if contains NUL, return EILSEQ
return Err(host::__WASI_EILSEQ);
}
let dirfe = wasi_ctx.get_fd_entry(dirfd, needed_base, needed_inheriting)?;
let dirfd = match &*dirfe.fd_object.descriptor {
Descriptor::File(f) => f.try_clone().map_err(|err| {
err.raw_os_error()
.map_or(host::__WASI_EBADF, errno_from_host)
})?,
_ => return Err(host::__WASI_EBADF),
};
// Stack of directory handles. Index 0 always corresponds with the directory provided
// to this function. Entering a directory causes a handle to be pushed, while handling
// ".." entries causes an entry to be popped. Index 0 cannot be popped, as this would imply
// escaping the base directory.
let mut dir_stack = vec![dirfe.fd_object.raw_handle];
let mut dir_stack = vec![dirfd];
// Stack of paths left to process. This is initially the `path` argument to this function, but
// any symlinks we encounter are processed by pushing them on the stack.
let mut path_stack = vec![PathBuf::from(path.as_ref())];
let mut path_stack = vec![path.clone()];
loop {
match path_stack.pop() {
Some(cur_path) => {
// dbg!(&cur_path);
let mut components = cur_path.components();
let ends_with_slash = cur_path.ends_with(b"/");
let mut components = Path::new(&cur_path).components();
let head = match components.next() {
None => return ret_error(&mut dir_stack, host::__WASI_ENOENT),
None => return Err(host::__WASI_ENOENT),
Some(p) => p,
};
let tail = components.as_path();
if tail.components().next().is_some() {
path_stack.push(PathBuf::from(tail));
let mut tail = RawString::from(tail.as_os_str());
if ends_with_slash {
tail.push("/");
}
path_stack.push(tail);
}
match head {
Component::Prefix(_) | Component::RootDir => {
// path is absolute!
return ret_error(&mut dir_stack, host::__WASI_ENOTCAPABLE);
return Err(host::__WASI_ENOTCAPABLE);
}
Component::CurDir => {
// "." so skip
@@ -87,41 +77,43 @@ pub fn path_get<P: AsRef<OsStr>>(
}
Component::ParentDir => {
// ".." so pop a dir
let dirfd = dir_stack.pop().expect("dir_stack is never empty");
let _ = dir_stack.pop().ok_or(host::__WASI_ENOTCAPABLE)?;
// we're not allowed to pop past the original directory
if dir_stack.is_empty() {
return ret_error(&mut dir_stack, host::__WASI_ENOTCAPABLE);
} else {
winx::handle::close(dirfd).unwrap_or_else(|e| {
dbg!(e);
});
return Err(host::__WASI_ENOTCAPABLE);
}
}
Component::Normal(head) => {
let mut head = RawString::from(head);
if ends_with_slash {
// preserve trailing slash
head.push("/");
}
// should the component be a directory? it should if there is more path left to process, or
// if it has a trailing slash and `needs_final_component` is not set
if !path_stack.is_empty()
|| (Path::new(head).is_dir() && !needs_final_component)
{
if !path_stack.is_empty() || (ends_with_slash && !needs_final_component) {
match winx::file::openat(
*dir_stack.last().expect("dir_stack is never empty"),
head,
dir_stack
.last()
.ok_or(host::__WASI_ENOTCAPABLE)?
.as_raw_handle(),
head.as_ref(),
winx::file::AccessRight::FILE_GENERIC_READ,
winx::file::CreationDisposition::OPEN_EXISTING,
winx::file::FlagsAndAttributes::FILE_FLAG_BACKUP_SEMANTICS,
) {
Ok(new_dir) => {
dir_stack.push(new_dir);
dir_stack.push(unsafe { File::from_raw_handle(new_dir) });
continue;
}
Err(e) => {
return ret_error(&mut dir_stack, host_impl::errno_from_win(e));
return Err(host_impl::errno_from_win(e));
}
}
} else {
// we're done
return Ok((ret_dir_success(&mut dir_stack), head.to_os_string()));
return Ok((dir_stack.pop().ok_or(host::__WASI_ENOTCAPABLE)?, head));
}
}
}
@@ -130,8 +122,8 @@ pub fn path_get<P: AsRef<OsStr>>(
// no further components to process. means we've hit a case like "." or "a/..", or if the
// input path has trailing slashes and `needs_final_component` is not set
return Ok((
ret_dir_success(&mut dir_stack),
OsStr::new(".").to_os_string(),
dir_stack.pop().ok_or(host::__WASI_ENOTCAPABLE)?,
RawString::from(OsStr::new(".")),
));
}
}

View File

@@ -1,8 +1,8 @@
#![allow(non_camel_case_types)]
#![allow(unused_unsafe)]
#![allow(unused)]
use super::host_impl;
use crate::memory::*;
use crate::sys::host_impl;
use crate::{host, wasm32};
use wasi_common_cbindgen::wasi_common_cbindgen;
@@ -25,7 +25,3 @@ pub(crate) fn poll_oneoff(
) -> Result<wasm32::size_t, host::__wasi_errno_t> {
unimplemented!("poll_oneoff")
}
pub(crate) fn sched_yield() -> Result<(), host::__wasi_errno_t> {
unimplemented!("sched_yield")
}

View File

@@ -4,8 +4,5 @@ mod fs;
mod fs_helpers;
mod misc;
use super::fdentry;
use super::host_impl;
pub(crate) use self::fs::*;
pub(crate) use self::misc::*;

View File

@@ -1,16 +1,17 @@
pub(crate) mod fdentry;
pub(crate) mod fdentry_impl;
pub(crate) mod host_impl;
pub(crate) mod hostcalls_impl;
use crate::host;
use crate::sys::errno_from_host;
use std::fs::File;
use std::io;
use std::path::Path;
pub(crate) fn dev_null() -> File {
File::open("NUL").expect("failed to open NUL")
pub(crate) fn dev_null() -> Result<File, host::__wasi_errno_t> {
File::open("NUL").map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))
}
pub fn preopen_dir<P: AsRef<Path>>(path: P) -> io::Result<File> {
pub fn preopen_dir<P: AsRef<Path>>(path: P) -> Result<File, host::__wasi_errno_t> {
use std::fs::OpenOptions;
use std::os::windows::fs::OpenOptionsExt;
use winapi::um::winbase::FILE_FLAG_BACKUP_SEMANTICS;
@@ -24,4 +25,5 @@ pub fn preopen_dir<P: AsRef<Path>>(path: P) -> io::Result<File> {
.read(true)
.attributes(FILE_FLAG_BACKUP_SEMANTICS)
.open(path)
.map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))
}