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:
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user