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,68 +1,35 @@
|
||||
#![allow(non_camel_case_types)]
|
||||
#![allow(unused_unsafe)]
|
||||
use super::fdentry::{determine_type_rights, FdEntry};
|
||||
use super::fs_helpers::*;
|
||||
use super::host_impl;
|
||||
use crate::fdentry::FdEntry;
|
||||
use crate::sys::errno_from_host;
|
||||
use crate::sys::fdentry_impl::determine_type_rights;
|
||||
use crate::sys::host_impl::{self, RawString};
|
||||
|
||||
use crate::ctx::WasiCtx;
|
||||
use crate::{host, wasm32};
|
||||
|
||||
use nix::libc::{self, c_long, c_void, off_t};
|
||||
use std::ffi::OsStr;
|
||||
use std::os::unix::prelude::{FromRawFd, OsStrExt};
|
||||
|
||||
pub(crate) fn fd_close(fd_entry: FdEntry) -> Result<(), host::__wasi_errno_t> {
|
||||
nix::unistd::close(fd_entry.fd_object.rawfd)
|
||||
.map_err(|e| host_impl::errno_from_nix(e.as_errno().unwrap()))
|
||||
}
|
||||
|
||||
pub(crate) fn fd_datasync(fd_entry: &FdEntry) -> Result<(), host::__wasi_errno_t> {
|
||||
let res;
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
{
|
||||
res = nix::unistd::fdatasync(fd_entry.fd_object.rawfd);
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
{
|
||||
res = nix::unistd::fsync(fd_entry.fd_object.rawfd);
|
||||
}
|
||||
|
||||
res.map_err(|e| host_impl::errno_from_nix(e.as_errno().unwrap()))
|
||||
}
|
||||
use std::fs::File;
|
||||
use std::os::unix::fs::FileExt;
|
||||
use std::os::unix::prelude::{AsRawFd, FromRawFd};
|
||||
|
||||
pub(crate) fn fd_pread(
|
||||
fd_entry: &FdEntry,
|
||||
file: &File,
|
||||
buf: &mut [u8],
|
||||
offset: host::__wasi_filesize_t,
|
||||
) -> Result<usize, host::__wasi_errno_t> {
|
||||
nix::sys::uio::pread(fd_entry.fd_object.rawfd, buf, offset as off_t)
|
||||
.map_err(|e| host_impl::errno_from_nix(e.as_errno().unwrap()))
|
||||
file.read_at(buf, offset)
|
||||
.map_err(|e| e.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> {
|
||||
nix::sys::uio::pwrite(fd_entry.fd_object.rawfd, buf, offset as off_t)
|
||||
.map_err(|e| host_impl::errno_from_nix(e.as_errno().unwrap()))
|
||||
}
|
||||
|
||||
pub(crate) fn fd_read(
|
||||
fd_entry: &FdEntry,
|
||||
iovs: &mut [host::__wasi_iovec_t],
|
||||
) -> Result<usize, host::__wasi_errno_t> {
|
||||
use nix::sys::uio::{readv, IoVec};
|
||||
|
||||
let mut iovs: Vec<IoVec<&mut [u8]>> = iovs
|
||||
.iter_mut()
|
||||
.map(|iov| unsafe { host_impl::iovec_to_nix_mut(iov) })
|
||||
.collect();
|
||||
|
||||
readv(fd_entry.fd_object.rawfd, &mut iovs)
|
||||
.map_err(|e| host_impl::errno_from_nix(e.as_errno().unwrap()))
|
||||
file.write_at(buf, offset)
|
||||
.map_err(|e| e.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))
|
||||
}
|
||||
|
||||
pub(crate) fn fd_renumber(
|
||||
@@ -86,12 +53,14 @@ pub(crate) fn fd_renumber(
|
||||
return Err(host::__WASI_ENOTSUP);
|
||||
}
|
||||
|
||||
if let Err(e) = nix::unistd::dup2(fe_from.fd_object.rawfd, fe_to.fd_object.rawfd) {
|
||||
let rawfd_from = fe_from.fd_object.descriptor.as_raw_fd();
|
||||
let rawfd_to = fe_to.fd_object.descriptor.as_raw_fd();
|
||||
|
||||
if let Err(e) = nix::unistd::dup2(rawfd_from, rawfd_to) {
|
||||
return Err(host_impl::errno_from_nix(e.as_errno().unwrap()));
|
||||
}
|
||||
|
||||
let fe_from_rawfd = fe_from.fd_object.rawfd;
|
||||
wasi_ctx.fds.remove(&(fe_from_rawfd as host::__wasi_fd_t));
|
||||
let _ = wasi_ctx.fds.remove(&(rawfd_from as host::__wasi_fd_t));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -109,7 +78,9 @@ pub(crate) fn fd_seek(
|
||||
_ => return Err(host::__WASI_EINVAL),
|
||||
};
|
||||
|
||||
match lseek(fd_entry.fd_object.rawfd, offset, nwhence) {
|
||||
let rawfd = fd_entry.fd_object.descriptor.as_raw_fd();
|
||||
|
||||
match lseek(rawfd, offset, nwhence) {
|
||||
Ok(offset) => Ok(offset as u64),
|
||||
Err(e) => Err(host_impl::errno_from_nix(e.as_errno().unwrap())),
|
||||
}
|
||||
@@ -117,7 +88,9 @@ pub(crate) fn fd_seek(
|
||||
|
||||
pub(crate) fn fd_tell(fd_entry: &FdEntry) -> Result<u64, host::__wasi_errno_t> {
|
||||
use nix::unistd::{lseek, Whence};
|
||||
match lseek(fd_entry.fd_object.rawfd, 0, Whence::SeekCur) {
|
||||
|
||||
let rawfd = fd_entry.fd_object.descriptor.as_raw_fd();
|
||||
match lseek(rawfd, 0, Whence::SeekCur) {
|
||||
Ok(newoffset) => Ok(newoffset as u64),
|
||||
Err(e) => Err(host_impl::errno_from_nix(e.as_errno().unwrap())),
|
||||
}
|
||||
@@ -127,7 +100,9 @@ pub(crate) fn fd_fdstat_get(
|
||||
fd_entry: &FdEntry,
|
||||
) -> Result<host::__wasi_fdflags_t, host::__wasi_errno_t> {
|
||||
use nix::fcntl::{fcntl, OFlag, F_GETFL};
|
||||
match fcntl(fd_entry.fd_object.rawfd, F_GETFL).map(OFlag::from_bits_truncate) {
|
||||
|
||||
let rawfd = fd_entry.fd_object.descriptor.as_raw_fd();
|
||||
match fcntl(rawfd, F_GETFL).map(OFlag::from_bits_truncate) {
|
||||
Ok(flags) => Ok(host_impl::fdflags_from_nix(flags)),
|
||||
Err(e) => Err(host_impl::errno_from_nix(e.as_errno().unwrap())),
|
||||
}
|
||||
@@ -137,31 +112,14 @@ pub(crate) fn fd_fdstat_set_flags(
|
||||
fd_entry: &FdEntry,
|
||||
fdflags: host::__wasi_fdflags_t,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
let rawfd = fd_entry.fd_object.descriptor.as_raw_fd();
|
||||
let nix_flags = host_impl::nix_from_fdflags(fdflags);
|
||||
match nix::fcntl::fcntl(fd_entry.fd_object.rawfd, nix::fcntl::F_SETFL(nix_flags)) {
|
||||
match nix::fcntl::fcntl(rawfd, nix::fcntl::F_SETFL(nix_flags)) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(e) => Err(host_impl::errno_from_nix(e.as_errno().unwrap())),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn fd_sync(fd_entry: &FdEntry) -> Result<(), host::__wasi_errno_t> {
|
||||
nix::unistd::fsync(fd_entry.fd_object.rawfd)
|
||||
.map_err(|e| host_impl::errno_from_nix(e.as_errno().unwrap()))
|
||||
}
|
||||
|
||||
pub(crate) fn fd_write(
|
||||
fd_entry: &FdEntry,
|
||||
iovs: &[host::__wasi_iovec_t],
|
||||
) -> Result<usize, host::__wasi_errno_t> {
|
||||
use nix::sys::uio::{writev, IoVec};
|
||||
let iovs: Vec<IoVec<&[u8]>> = iovs
|
||||
.iter()
|
||||
.map(|iov| unsafe { host_impl::iovec_to_nix(iov) })
|
||||
.collect();
|
||||
writev(fd_entry.fd_object.rawfd, &iovs)
|
||||
.map_err(|e| host_impl::errno_from_nix(e.as_errno().unwrap()))
|
||||
}
|
||||
|
||||
pub(crate) fn fd_advise(
|
||||
fd_entry: &FdEntry,
|
||||
advice: host::__wasi_advice_t,
|
||||
@@ -179,14 +137,8 @@ pub(crate) fn fd_advise(
|
||||
host::__WASI_ADVICE_NORMAL => libc::POSIX_FADV_NORMAL,
|
||||
_ => return Err(host::__WASI_EINVAL),
|
||||
};
|
||||
let res = unsafe {
|
||||
libc::posix_fadvise(
|
||||
fd_entry.fd_object.rawfd,
|
||||
offset as off_t,
|
||||
len as off_t,
|
||||
host_advice,
|
||||
)
|
||||
};
|
||||
let rawfd = fd_entry.fd_object.descriptor.as_raw_fd();
|
||||
let res = unsafe { libc::posix_fadvise(rawfd, offset as off_t, len as off_t, host_advice) };
|
||||
if res != 0 {
|
||||
return Err(host_impl::errno_from_nix(nix::errno::Errno::last()));
|
||||
}
|
||||
@@ -209,53 +161,10 @@ pub(crate) fn fd_advise(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn fd_allocate(
|
||||
fd_entry: &FdEntry,
|
||||
offset: host::__wasi_filesize_t,
|
||||
len: host::__wasi_filesize_t,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
#[cfg(target_os = "linux")]
|
||||
{
|
||||
let res = unsafe {
|
||||
libc::posix_fallocate(fd_entry.fd_object.rawfd, offset as off_t, len as off_t)
|
||||
};
|
||||
if res != 0 {
|
||||
return Err(host_impl::errno_from_nix(nix::errno::Errno::last()));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
{
|
||||
use nix::sys::stat::fstat;
|
||||
use nix::unistd::ftruncate;
|
||||
|
||||
match fstat(fd_entry.fd_object.rawfd) {
|
||||
Err(e) => return Err(host_impl::errno_from_nix(e.as_errno().unwrap())),
|
||||
Ok(st) => {
|
||||
let current_size = st.st_size as u64;
|
||||
let wanted_size = match offset.checked_add(len) {
|
||||
Some(wanted_size) => wanted_size,
|
||||
None => return Err(host::__WASI_E2BIG),
|
||||
};
|
||||
if wanted_size > i64::max_value() as u64 {
|
||||
return Err(host::__WASI_E2BIG);
|
||||
}
|
||||
if wanted_size > current_size {
|
||||
if let Err(e) = ftruncate(fd_entry.fd_object.rawfd, wanted_size as off_t) {
|
||||
return Err(host_impl::errno_from_nix(e.as_errno().unwrap()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn path_create_directory(
|
||||
ctx: &WasiCtx,
|
||||
dirfd: host::__wasi_fd_t,
|
||||
path: &OsStr,
|
||||
path: &RawString,
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
use nix::libc::mkdirat;
|
||||
|
||||
@@ -271,12 +180,12 @@ pub(crate) fn path_create_directory(
|
||||
Ok((dir, path)) => (dir, path),
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
let path_cstr = match std::ffi::CString::new(path.as_os_str().as_bytes()) {
|
||||
let path_cstr = match std::ffi::CString::new(path.to_bytes()) {
|
||||
Ok(path_cstr) => path_cstr,
|
||||
Err(_) => return Err(host::__WASI_EINVAL),
|
||||
};
|
||||
// nix doesn't expose mkdirat() yet
|
||||
match unsafe { mkdirat(dir, path_cstr.as_ptr(), 0o777) } {
|
||||
match unsafe { mkdirat(dir.as_raw_fd(), path_cstr.as_ptr(), 0o777) } {
|
||||
0 => Ok(()),
|
||||
_ => Err(host_impl::errno_from_nix(nix::errno::Errno::last())),
|
||||
}
|
||||
@@ -286,8 +195,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> {
|
||||
@@ -300,11 +209,11 @@ pub(crate) fn path_link(
|
||||
Ok((dir, path)) => (dir, path),
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
let old_path_cstr = match std::ffi::CString::new(old_path.as_bytes()) {
|
||||
let old_path_cstr = match std::ffi::CString::new(old_path.to_bytes()) {
|
||||
Ok(old_path_cstr) => old_path_cstr,
|
||||
Err(_) => return Err(host::__WASI_EINVAL),
|
||||
};
|
||||
let new_path_cstr = match std::ffi::CString::new(new_path.as_bytes()) {
|
||||
let new_path_cstr = match std::ffi::CString::new(new_path.to_bytes()) {
|
||||
Ok(new_path_cstr) => new_path_cstr,
|
||||
Err(_) => return Err(host::__WASI_EINVAL),
|
||||
};
|
||||
@@ -313,9 +222,9 @@ pub(crate) fn path_link(
|
||||
let atflags = libc::AT_SYMLINK_FOLLOW;
|
||||
let res = unsafe {
|
||||
linkat(
|
||||
old_dir,
|
||||
old_dir.as_raw_fd(),
|
||||
old_path_cstr.as_ptr(),
|
||||
new_dir,
|
||||
new_dir.as_raw_fd(),
|
||||
new_path_cstr.as_ptr(),
|
||||
atflags,
|
||||
)
|
||||
@@ -331,7 +240,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,
|
||||
@@ -390,8 +299,8 @@ pub(crate) fn path_open(
|
||||
// umask is, but don't set the executable flag, because it isn't yet
|
||||
// meaningful for WASI programs to create executable files.
|
||||
let new_fd = match openat(
|
||||
dir,
|
||||
path.as_os_str(),
|
||||
dir.as_raw_fd(),
|
||||
path.as_ref(),
|
||||
nix_all_oflags,
|
||||
Mode::from_bits_truncate(0o666),
|
||||
) {
|
||||
@@ -400,7 +309,9 @@ pub(crate) fn path_open(
|
||||
match e.as_errno() {
|
||||
// Linux returns ENXIO instead of EOPNOTSUPP when opening a socket
|
||||
Some(Errno::ENXIO) => {
|
||||
if let Ok(stat) = fstatat(dir, path.as_os_str(), AtFlags::AT_SYMLINK_NOFOLLOW) {
|
||||
if let Ok(stat) =
|
||||
fstatat(dir.as_raw_fd(), path.as_ref(), AtFlags::AT_SYMLINK_NOFOLLOW)
|
||||
{
|
||||
if SFlag::from_bits_truncate(stat.st_mode).contains(SFlag::S_IFSOCK) {
|
||||
return Err(host::__WASI_ENOTSUP);
|
||||
} else {
|
||||
@@ -415,7 +326,9 @@ pub(crate) fn path_open(
|
||||
Some(Errno::ENOTDIR)
|
||||
if !(nix_all_oflags & (OFlag::O_NOFOLLOW | OFlag::O_DIRECTORY)).is_empty() =>
|
||||
{
|
||||
if let Ok(stat) = fstatat(dir, path.as_os_str(), AtFlags::AT_SYMLINK_NOFOLLOW) {
|
||||
if let Ok(stat) =
|
||||
fstatat(dir.as_raw_fd(), path.as_ref(), AtFlags::AT_SYMLINK_NOFOLLOW)
|
||||
{
|
||||
if SFlag::from_bits_truncate(stat.st_mode).contains(SFlag::S_IFLNK) {
|
||||
return Err(host::__WASI_ELOOP);
|
||||
}
|
||||
@@ -434,16 +347,11 @@ pub(crate) fn path_open(
|
||||
};
|
||||
|
||||
// Determine the type of the new file descriptor and which rights contradict with this type
|
||||
match unsafe { determine_type_rights(new_fd) } {
|
||||
Err(e) => {
|
||||
// if `close` fails, note it but do not override the underlying errno
|
||||
nix::unistd::close(new_fd).unwrap_or_else(|e| {
|
||||
dbg!(e);
|
||||
});
|
||||
Err(e)
|
||||
}
|
||||
let file = unsafe { File::from_raw_fd(new_fd) };
|
||||
match determine_type_rights(&file) {
|
||||
Err(e) => Err(e),
|
||||
Ok((_ty, max_base, max_inheriting)) => {
|
||||
let mut fe = unsafe { FdEntry::from_raw_fd(new_fd) };
|
||||
let mut fe = FdEntry::from(file)?;
|
||||
fe.rights_base &= max_base;
|
||||
fe.rights_inheriting &= max_inheriting;
|
||||
Ok(fe)
|
||||
@@ -458,9 +366,10 @@ pub(crate) fn fd_readdir(
|
||||
) -> Result<usize, host::__wasi_errno_t> {
|
||||
use libc::{dirent, fdopendir, memcpy, readdir_r, seekdir};
|
||||
|
||||
let rawfd = fd_entry.fd_object.descriptor.as_raw_fd();
|
||||
let host_buf_ptr = host_buf.as_mut_ptr();
|
||||
let host_buf_len = host_buf.len();
|
||||
let dir = unsafe { fdopendir(fd_entry.fd_object.rawfd) };
|
||||
let dir = unsafe { fdopendir(rawfd) };
|
||||
if dir.is_null() {
|
||||
return Err(host_impl::errno_from_nix(nix::errno::Errno::last()));
|
||||
}
|
||||
@@ -512,7 +421,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> {
|
||||
@@ -523,7 +432,7 @@ pub(crate) fn path_readlink(
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
|
||||
let path_cstr = match std::ffi::CString::new(path.as_bytes()) {
|
||||
let path_cstr = match std::ffi::CString::new(path.to_bytes()) {
|
||||
Ok(path_cstr) => path_cstr,
|
||||
Err(_) => return Err(host::__WASI_EINVAL),
|
||||
};
|
||||
@@ -536,7 +445,7 @@ pub(crate) fn path_readlink(
|
||||
let buf_len = buf.len();
|
||||
let len = unsafe {
|
||||
libc::readlinkat(
|
||||
dir,
|
||||
dir.as_raw_fd(),
|
||||
path_cstr.as_ptr() as *const libc::c_char,
|
||||
if buf_len == 0 {
|
||||
fakebuf.as_mut_ptr()
|
||||
@@ -558,10 +467,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> {
|
||||
use nix::libc::renameat;
|
||||
@@ -576,19 +485,19 @@ pub(crate) fn path_rename(
|
||||
Ok((dir, path)) => (dir, path),
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
let old_path_cstr = match std::ffi::CString::new(old_path.as_bytes()) {
|
||||
let old_path_cstr = match std::ffi::CString::new(old_path.to_bytes()) {
|
||||
Ok(old_path_cstr) => old_path_cstr,
|
||||
Err(_) => return Err(host::__WASI_EINVAL),
|
||||
};
|
||||
let new_path_cstr = match std::ffi::CString::new(new_path.as_bytes()) {
|
||||
let new_path_cstr = match std::ffi::CString::new(new_path.to_bytes()) {
|
||||
Ok(new_path_cstr) => new_path_cstr,
|
||||
Err(_) => return Err(host::__WASI_EINVAL),
|
||||
};
|
||||
let res = unsafe {
|
||||
renameat(
|
||||
old_dir,
|
||||
old_dir.as_raw_fd(),
|
||||
old_path_cstr.as_ptr(),
|
||||
new_dir,
|
||||
new_dir.as_raw_fd(),
|
||||
new_path_cstr.as_ptr(),
|
||||
)
|
||||
};
|
||||
@@ -604,9 +513,10 @@ pub(crate) fn fd_filestat_get(
|
||||
) -> Result<host::__wasi_filestat_t, host::__wasi_errno_t> {
|
||||
use nix::sys::stat::fstat;
|
||||
|
||||
match fstat(fd_entry.fd_object.rawfd) {
|
||||
let rawfd = fd_entry.fd_object.descriptor.as_raw_fd();
|
||||
match fstat(rawfd) {
|
||||
Err(e) => Err(host_impl::errno_from_nix(e.as_errno().unwrap())),
|
||||
Ok(filestat) => Ok(host_impl::filestat_from_nix(filestat)),
|
||||
Ok(filestat) => Ok(host_impl::filestat_from_nix(filestat)?),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -649,7 +559,8 @@ pub(crate) fn fd_filestat_set_times(
|
||||
};
|
||||
let ts_mtime = *TimeSpec::nanoseconds(st_mtim as i64).as_ref();
|
||||
let times = [ts_atime, ts_mtime];
|
||||
let res = unsafe { libc::futimens(fd_entry.fd_object.rawfd, times.as_ptr()) };
|
||||
let rawfd = fd_entry.fd_object.descriptor.as_raw_fd();
|
||||
let res = unsafe { libc::futimens(rawfd, times.as_ptr()) };
|
||||
if res != 0 {
|
||||
Err(host_impl::errno_from_nix(nix::errno::Errno::last()))
|
||||
} else {
|
||||
@@ -663,15 +574,15 @@ pub(crate) fn fd_filestat_set_size(
|
||||
) -> Result<(), host::__wasi_errno_t> {
|
||||
use nix::unistd::ftruncate;
|
||||
|
||||
ftruncate(fd_entry.fd_object.rawfd, st_size as off_t)
|
||||
.map_err(|e| host_impl::errno_from_nix(e.as_errno().unwrap()))
|
||||
let rawfd = fd_entry.fd_object.descriptor.as_raw_fd();
|
||||
ftruncate(rawfd, st_size as off_t).map_err(|e| host_impl::errno_from_nix(e.as_errno().unwrap()))
|
||||
}
|
||||
|
||||
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> {
|
||||
use nix::fcntl::AtFlags;
|
||||
use nix::sys::stat::fstatat;
|
||||
@@ -693,9 +604,9 @@ pub(crate) fn path_filestat_get(
|
||||
_ => AtFlags::AT_SYMLINK_NOFOLLOW,
|
||||
};
|
||||
|
||||
match fstatat(dir, path.as_os_str(), atflags) {
|
||||
match fstatat(dir.as_raw_fd(), path.as_ref(), atflags) {
|
||||
Err(e) => Err(host_impl::errno_from_nix(e.as_errno().unwrap())),
|
||||
Ok(filestat) => Ok(host_impl::filestat_from_nix(filestat)),
|
||||
Ok(filestat) => Ok(host_impl::filestat_from_nix(filestat)?),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -703,7 +614,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,
|
||||
@@ -750,11 +661,12 @@ pub(crate) fn path_filestat_set_times(
|
||||
};
|
||||
let ts_mtime = *TimeSpec::nanoseconds(st_mtim as i64).as_ref();
|
||||
let times = [ts_atime, ts_mtime];
|
||||
let path_cstr = match std::ffi::CString::new(path.as_os_str().as_bytes()) {
|
||||
let path_cstr = match std::ffi::CString::new(path.to_bytes()) {
|
||||
Ok(path_cstr) => path_cstr,
|
||||
Err(_) => return Err(host::__WASI_EINVAL),
|
||||
};
|
||||
let res = unsafe { libc::utimensat(dir, path_cstr.as_ptr(), times.as_ptr(), atflags) };
|
||||
let res =
|
||||
unsafe { libc::utimensat(dir.as_raw_fd(), path_cstr.as_ptr(), times.as_ptr(), atflags) };
|
||||
if res != 0 {
|
||||
Err(host_impl::errno_from_nix(nix::errno::Errno::last()))
|
||||
} else {
|
||||
@@ -766,8 +678,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> {
|
||||
use nix::libc::symlinkat;
|
||||
|
||||
@@ -775,15 +687,21 @@ pub(crate) fn path_symlink(
|
||||
Ok((dir, path)) => (dir, path),
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
let old_path_cstr = match std::ffi::CString::new(old_path.as_bytes()) {
|
||||
let old_path_cstr = match std::ffi::CString::new(old_path.to_bytes()) {
|
||||
Ok(old_path_cstr) => old_path_cstr,
|
||||
Err(_) => return Err(host::__WASI_EINVAL),
|
||||
};
|
||||
let new_path_cstr = match std::ffi::CString::new(new_path.as_bytes()) {
|
||||
let new_path_cstr = match std::ffi::CString::new(new_path.to_bytes()) {
|
||||
Ok(new_path_cstr) => new_path_cstr,
|
||||
Err(_) => return Err(host::__WASI_EINVAL),
|
||||
};
|
||||
let res = unsafe { symlinkat(old_path_cstr.as_ptr(), dir, new_path_cstr.as_ptr()) };
|
||||
let res = unsafe {
|
||||
symlinkat(
|
||||
old_path_cstr.as_ptr(),
|
||||
dir.as_raw_fd(),
|
||||
new_path_cstr.as_ptr(),
|
||||
)
|
||||
};
|
||||
if res != 0 {
|
||||
Err(host_impl::errno_from_nix(nix::errno::Errno::last()))
|
||||
} else {
|
||||
@@ -794,7 +712,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> {
|
||||
use nix::errno;
|
||||
@@ -804,12 +722,12 @@ pub(crate) fn path_unlink_file(
|
||||
Ok((dir, path)) => (dir, path),
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
let path_cstr = match std::ffi::CString::new(path.as_os_str().as_bytes()) {
|
||||
let path_cstr = match std::ffi::CString::new(path.to_bytes()) {
|
||||
Ok(path_cstr) => path_cstr,
|
||||
Err(_) => return Err(host::__WASI_EINVAL),
|
||||
};
|
||||
// nix doesn't expose unlinkat() yet
|
||||
match unsafe { unlinkat(dir, path_cstr.as_ptr(), 0) } {
|
||||
match unsafe { unlinkat(dir.as_raw_fd(), path_cstr.as_ptr(), 0) } {
|
||||
0 => Ok(()),
|
||||
_ => {
|
||||
let mut e = errno::Errno::last();
|
||||
@@ -827,7 +745,9 @@ pub(crate) fn path_unlink_file(
|
||||
use nix::sys::stat::{fstatat, SFlag};
|
||||
|
||||
if e == errno::Errno::EPERM {
|
||||
if let Ok(stat) = fstatat(dir, path.as_os_str(), AtFlags::AT_SYMLINK_NOFOLLOW) {
|
||||
if let Ok(stat) =
|
||||
fstatat(dir.as_raw_fd(), path.as_ref(), AtFlags::AT_SYMLINK_NOFOLLOW)
|
||||
{
|
||||
if SFlag::from_bits_truncate(stat.st_mode).contains(SFlag::S_IFDIR) {
|
||||
e = errno::Errno::EISDIR;
|
||||
}
|
||||
@@ -845,7 +765,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> {
|
||||
use nix::errno;
|
||||
@@ -855,12 +775,12 @@ pub(crate) fn path_remove_directory(
|
||||
Ok((dir, path)) => (dir, path),
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
let path_cstr = match std::ffi::CString::new(path.as_os_str().as_bytes()) {
|
||||
let path_cstr = match std::ffi::CString::new(path.to_bytes()) {
|
||||
Ok(path_cstr) => path_cstr,
|
||||
Err(_) => return Err(host::__WASI_EINVAL),
|
||||
};
|
||||
// nix doesn't expose unlinkat() yet
|
||||
match unsafe { unlinkat(dir, path_cstr.as_ptr(), AT_REMOVEDIR) } {
|
||||
match unsafe { unlinkat(dir.as_raw_fd(), path_cstr.as_ptr(), AT_REMOVEDIR) } {
|
||||
0 => Ok(()),
|
||||
_ => Err(host_impl::errno_from_nix(errno::Errno::last())),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user