From 7ec03631daefec3eb11f1102bd850a7e1747e6a6 Mon Sep 17 00:00:00 2001 From: Pat Hickey Date: Wed, 20 Jan 2021 20:53:44 -0800 Subject: [PATCH] inline only what we need of cap-std/system-interface traits --- crates/wasi-c2/cap-std-sync/src/dir.rs | 8 +- crates/wasi-c2/cap-std-sync/src/file.rs | 153 +++++++------- crates/wasi-c2/cap-std-sync/src/sched.rs | 33 ++- crates/wasi-c2/cap-std-sync/src/stdio.rs | 207 +++++++++++++------ crates/wasi-c2/src/file.rs | 52 +++-- crates/wasi-c2/src/pipe.rs | 232 ++++++++-------------- crates/wasi-c2/src/snapshots/preview_1.rs | 6 +- 7 files changed, 354 insertions(+), 337 deletions(-) diff --git a/crates/wasi-c2/cap-std-sync/src/dir.rs b/crates/wasi-c2/cap-std-sync/src/dir.rs index bf9990f3db..50d61fe1e9 100644 --- a/crates/wasi-c2/cap-std-sync/src/dir.rs +++ b/crates/wasi-c2/cap-std-sync/src/dir.rs @@ -1,4 +1,4 @@ -use crate::file::File; +use crate::file::{filetype_from, File}; use cap_fs_ext::{DirExt, MetadataExt, SystemTimeSpec}; use std::any::Any; use std::convert::TryInto; @@ -117,7 +117,7 @@ impl WasiDir for Dir { let entry = entry?; let meta = entry.metadata()?; let inode = meta.ino(); - let filetype = FileType::from(&meta.file_type()); + let filetype = filetype_from(&meta.file_type()); let name = entry.file_name().into_string().map_err(|_| Error::Ilseq)?; let namelen = name.as_bytes().len().try_into()?; Ok((filetype, inode, namelen, name)) @@ -164,7 +164,7 @@ impl WasiDir for Dir { Ok(Filestat { device_id: meta.dev(), inode: meta.ino(), - filetype: FileType::from(&meta.file_type()), + filetype: filetype_from(&meta.file_type()), nlink: meta.nlink(), size: meta.len(), atim: meta.accessed().map(|t| Some(t.into_std())).unwrap_or(None), @@ -177,7 +177,7 @@ impl WasiDir for Dir { Ok(Filestat { device_id: meta.dev(), inode: meta.ino(), - filetype: FileType::from(&meta.file_type()), + filetype: filetype_from(&meta.file_type()), nlink: meta.nlink(), size: meta.len(), atim: meta.accessed().map(|t| Some(t.into_std())).unwrap_or(None), diff --git a/crates/wasi-c2/cap-std-sync/src/file.rs b/crates/wasi-c2/cap-std-sync/src/file.rs index d29804c1a6..3fb8c90f3b 100644 --- a/crates/wasi-c2/cap-std-sync/src/file.rs +++ b/crates/wasi-c2/cap-std-sync/src/file.rs @@ -1,6 +1,7 @@ use cap_fs_ext::MetadataExt; -use fs_set_times::SetTimes; +use fs_set_times::{SetTimes, SystemTimeSpec}; use std::any::Any; +use std::convert::TryInto; use std::io; use system_interface::fs::{Advice, FileIoExt}; use system_interface::io::ReadReady; @@ -31,7 +32,7 @@ impl WasiFile for File { } fn get_filetype(&self) -> Result { let meta = self.0.metadata()?; - Ok(FileType::from(&meta.file_type())) + Ok(filetype_from(&meta.file_type())) } fn get_fdflags(&self) -> Result { // XXX get_fdflags is not implemented but lets lie rather than panic: @@ -45,7 +46,7 @@ impl WasiFile for File { Ok(Filestat { device_id: meta.dev(), inode: meta.ino(), - filetype: FileType::from(&meta.file_type()), + filetype: filetype_from(&meta.file_type()), nlink: meta.nlink(), size: meta.len(), atim: meta.accessed().map(|t| Some(t.into_std())).unwrap_or(None), @@ -57,80 +58,90 @@ impl WasiFile for File { self.0.set_len(size)?; Ok(()) } -} - -impl FileIoExt for File { - fn advise(&self, offset: u64, len: u64, advice: Advice) -> io::Result<()> { - self.0.advise(offset, len, advice) + fn advise(&self, offset: u64, len: u64, advice: Advice) -> Result<(), Error> { + self.0.advise(offset, len, advice)?; + Ok(()) } - fn allocate(&self, offset: u64, len: u64) -> io::Result<()> { - self.0.allocate(offset, len) + fn allocate(&self, offset: u64, len: u64) -> Result<(), Error> { + self.0.allocate(offset, len)?; + Ok(()) } - fn read(&self, buf: &mut [u8]) -> io::Result { - self.0.read(buf) - } - fn read_exact(&self, buf: &mut [u8]) -> io::Result<()> { - self.0.read_exact(buf) - } - fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result { - self.0.read_at(buf, offset) - } - fn read_exact_at(&self, buf: &mut [u8], offset: u64) -> io::Result<()> { - self.0.read_exact_at(buf, offset) - } - fn read_vectored(&self, bufs: &mut [io::IoSliceMut]) -> io::Result { - self.0.read_vectored(bufs) - } - fn read_to_end(&self, buf: &mut Vec) -> io::Result { - self.0.read_to_end(buf) - } - fn read_to_string(&self, buf: &mut String) -> io::Result { - self.0.read_to_string(buf) - } - fn write(&self, buf: &[u8]) -> io::Result { - self.0.write(buf) - } - fn write_all(&self, buf: &[u8]) -> io::Result<()> { - self.0.write_all(buf) - } - fn write_at(&self, buf: &[u8], offset: u64) -> io::Result { - self.0.write_at(buf, offset) - } - fn write_all_at(&self, buf: &[u8], offset: u64) -> io::Result<()> { - self.0.write_all_at(buf, offset) - } - fn write_vectored(&self, bufs: &[io::IoSlice]) -> io::Result { - self.0.write_vectored(bufs) - } - fn write_fmt(&self, fmt: std::fmt::Arguments) -> io::Result<()> { - self.0.write_fmt(fmt) - } - fn flush(&self) -> io::Result<()> { - self.0.flush() - } - fn seek(&self, pos: std::io::SeekFrom) -> io::Result { - self.0.seek(pos) - } - fn stream_position(&self) -> io::Result { - self.0.stream_position() - } - fn peek(&self, buf: &mut [u8]) -> io::Result { - self.0.peek(buf) - } -} - -impl SetTimes for File { fn set_times( &self, - atime: Option, - mtime: Option, - ) -> io::Result<()> { - self.0.set_times(atime, mtime) + atime: Option, + mtime: Option, + ) -> Result<(), Error> { + self.0.set_times(atime, mtime)?; + Ok(()) + } + fn read_vectored(&self, bufs: &mut [io::IoSliceMut]) -> Result { + let n = self.0.read_vectored(bufs)?; + Ok(n.try_into().map_err(|_| Error::Overflow)?) + } + fn read_vectored_at(&self, bufs: &mut [io::IoSliceMut], offset: u64) -> Result { + let n = self.0.read_vectored_at(bufs, offset)?; + Ok(n.try_into().map_err(|_| Error::Overflow)?) + } + fn write_vectored(&self, bufs: &[io::IoSlice]) -> Result { + let n = self.0.write_vectored(bufs)?; + Ok(n.try_into().map_err(|_| Error::Overflow)?) + } + fn write_vectored_at(&self, bufs: &[io::IoSlice], offset: u64) -> Result { + let n = self.0.write_vectored_at(bufs, offset)?; + Ok(n.try_into().map_err(|_| Error::Overflow)?) + } + fn seek(&self, pos: std::io::SeekFrom) -> Result { + Ok(self.0.seek(pos)?) + } + fn stream_position(&self) -> Result { + Ok(self.0.stream_position()?) + } + fn peek(&self, buf: &mut [u8]) -> Result { + let n = self.0.peek(buf)?; + Ok(n.try_into().map_err(|_| Error::Overflow)?) + } + fn num_ready_bytes(&self) -> Result { + Ok(self.0.num_ready_bytes()?) } } -impl ReadReady for File { - fn num_ready_bytes(&self) -> io::Result { - self.0.num_ready_bytes() +pub fn filetype_from(ft: &cap_std::fs::FileType) -> FileType { + use cap_fs_ext::FileTypeExt; + if ft.is_dir() { + FileType::Directory + } else if ft.is_symlink() { + FileType::SymbolicLink + } else if ft.is_socket() { + if ft.is_block_device() { + FileType::SocketDgram + } else { + FileType::SocketStream + } + } else if ft.is_block_device() { + FileType::BlockDevice + } else if ft.is_char_device() { + FileType::CharacterDevice + } else if ft.is_file() { + FileType::RegularFile + } else { + FileType::Unknown + } +} + +#[cfg(windows)] +use std::os::windows::io::{AsRawHandle, RawHandle}; +#[cfg(windows)] +impl AsRawHandle for File { + fn as_raw_handle(&self) -> RawHandle { + self.0.as_raw_handle() + } +} + +#[cfg(unix)] +use std::os::unix::io::{AsRawFd, RawFd}; +#[cfg(unix)] +impl AsRawFd for File { + fn as_raw_fd(&self) -> RawFd { + self.0.as_raw_fd() } } diff --git a/crates/wasi-c2/cap-std-sync/src/sched.rs b/crates/wasi-c2/cap-std-sync/src/sched.rs index 1fd930f937..587984788e 100644 --- a/crates/wasi-c2/cap-std-sync/src/sched.rs +++ b/crates/wasi-c2/cap-std-sync/src/sched.rs @@ -106,24 +106,21 @@ mod unix { fn wasi_file_raw_fd(f: &dyn WasiFile) -> Option { let a = f.as_any(); if a.is::() { - /* DISABLED UNTIL AsRawFd can be implemented properly - Some(a.downcast_ref::().unwrap().as_raw_fd()) - } else if a.is::() { - Some(a.downcast_ref::().unwrap().as_raw_fd()) - } else if a.is::() { - Some( - a.downcast_ref::() - .unwrap() - .as_raw_fd(), - ) - } else if a.is::() { - Some( - a.downcast_ref::() - .unwrap() - .as_raw_fd(), - ) - */ - None + Some(a.downcast_ref::().unwrap().as_raw_fd()) + } else if a.is::() { + Some(a.downcast_ref::().unwrap().as_raw_fd()) + } else if a.is::() { + Some( + a.downcast_ref::() + .unwrap() + .as_raw_fd(), + ) + } else if a.is::() { + Some( + a.downcast_ref::() + .unwrap() + .as_raw_fd(), + ) } else { None } diff --git a/crates/wasi-c2/cap-std-sync/src/stdio.rs b/crates/wasi-c2/cap-std-sync/src/stdio.rs index 46fe2b19d6..ad6e61cb03 100644 --- a/crates/wasi-c2/cap-std-sync/src/stdio.rs +++ b/crates/wasi-c2/cap-std-sync/src/stdio.rs @@ -1,63 +1,156 @@ -use wasi_c2::pipe::{ReadPipe, WritePipe}; - -pub type Stdin = ReadPipe; - -pub fn stdin() -> Stdin { - ReadPipe::new(std::io::stdin()) -} - -pub type Stdout = WritePipe; - -pub fn stdout() -> Stdout { - WritePipe::new(std::io::stdout()) -} - -pub type Stderr = WritePipe; - -pub fn stderr() -> Stderr { - WritePipe::new(std::io::stderr()) -} - -/* -#[cfg(windows)] -mod windows { - use super::*; - use std::os::windows::io::{AsRawHandle, RawHandle}; - impl AsRawHandle for Stdin { - fn as_raw_handle(&self) -> RawHandle { - self.borrow().as_raw_handle() - } - } - impl AsRawHandle for Stdout { - fn as_raw_handle(&self) -> RawHandle { - self.borrow().as_raw_handle() - } - } - impl AsRawHandle for Stderr { - fn as_raw_handle(&self) -> RawHandle { - self.borrow().as_raw_handle() - } - } -} +use fs_set_times::SetTimes; +use std::any::Any; +use std::convert::TryInto; +use std::io; +use system_interface::{ + fs::{Advice, FileIoExt}, + io::ReadReady, +}; +use wasi_c2::{ + file::{FdFlags, FileType, Filestat, WasiFile}, + Error, +}; #[cfg(unix)] -mod unix { - use super::*; - use std::os::unix::io::{AsRawFd, RawFd}; - impl AsRawFd for Stdin { - fn as_raw_fd(&self) -> RawFd { - self.borrow().as_raw_fd() +use std::os::unix::io::{AsRawFd, RawFd}; +#[cfg(windows)] +use std::os::windows::io::{AsRawHandle, RawHandle}; + +macro_rules! wasi_file_impl { + ($ty:ty, $additional:item) => { + impl WasiFile for $ty { + fn as_any(&self) -> &dyn Any { + self + } + fn datasync(&self) -> Result<(), Error> { + Ok(()) + } + fn sync(&self) -> Result<(), Error> { + Ok(()) + } + fn get_filetype(&self) -> Result { + Ok(FileType::CharacterDevice) // XXX wrong + } + fn get_fdflags(&self) -> Result { + // XXX get_fdflags is not implemented but lets lie rather than panic: + Ok(FdFlags::empty()) + } + fn set_fdflags(&self, _fdflags: FdFlags) -> Result<(), Error> { + // XXX + Err(Error::Perm) + } + fn get_filestat(&self) -> Result { + // XXX can unsafe-io give a way to get metadata? + Ok(Filestat { + device_id: 0, + inode: 0, + filetype: self.get_filetype()?, + nlink: 0, + size: 0, + atim: None, + mtim: None, + ctim: None, + }) + } + fn set_filestat_size(&self, _size: u64) -> Result<(), Error> { + // XXX is this the right error? + Err(Error::Perm) + } + fn advise(&self, offset: u64, len: u64, advice: Advice) -> Result<(), Error> { + self.0.advise(offset, len, advice)?; + Ok(()) + } + fn allocate(&self, offset: u64, len: u64) -> Result<(), Error> { + self.0.allocate(offset, len)?; + Ok(()) + } + fn read_vectored(&self, bufs: &mut [io::IoSliceMut]) -> Result { + let n = self.0.read_vectored(bufs)?; + Ok(n.try_into().map_err(|_| Error::Overflow)?) + } + fn read_vectored_at( + &self, + bufs: &mut [io::IoSliceMut], + offset: u64, + ) -> Result { + let n = self.0.read_vectored_at(bufs, offset)?; + Ok(n.try_into().map_err(|_| Error::Overflow)?) + } + fn write_vectored(&self, bufs: &[io::IoSlice]) -> Result { + let n = self.0.write_vectored(bufs)?; + Ok(n.try_into().map_err(|_| Error::Overflow)?) + } + fn write_vectored_at(&self, bufs: &[io::IoSlice], offset: u64) -> Result { + let n = self.0.write_vectored_at(bufs, offset)?; + Ok(n.try_into().map_err(|_| Error::Overflow)?) + } + fn seek(&self, pos: std::io::SeekFrom) -> Result { + Ok(self.0.seek(pos)?) + } + fn stream_position(&self) -> Result { + Ok(self.0.stream_position()?) + } + fn peek(&self, buf: &mut [u8]) -> Result { + let n = self.0.peek(buf)?; + Ok(n.try_into().map_err(|_| Error::Overflow)?) + } + fn set_times( + &self, + atime: Option, + mtime: Option, + ) -> Result<(), Error> { + self.0.set_times(atime, mtime)?; + Ok(()) + } + $additional } - } - impl AsRawFd for Stdout { - fn as_raw_fd(&self) -> RawFd { - self.borrow().as_raw_fd() + #[cfg(windows)] + impl AsRawHandle for $ty { + fn as_raw_handle(&self) -> RawHandle { + self.0.as_raw_handle() + } } - } - impl AsRawFd for Stderr { - fn as_raw_fd(&self) -> RawFd { - self.borrow().as_raw_fd() + #[cfg(unix)] + impl AsRawFd for $ty { + fn as_raw_fd(&self) -> RawFd { + self.0.as_raw_fd() + } } - } + }; } -*/ + +pub struct Stdin(std::io::Stdin); + +pub fn stdin() -> Stdin { + Stdin(std::io::stdin()) +} +wasi_file_impl!( + Stdin, + fn num_ready_bytes(&self) -> Result { + Ok(self.0.num_ready_bytes()?) + } +); + +pub struct Stdout(std::io::Stdout); + +pub fn stdout() -> Stdout { + Stdout(std::io::stdout()) +} +wasi_file_impl!( + Stdout, + fn num_ready_bytes(&self) -> Result { + Ok(0) + } +); + +pub struct Stderr(std::io::Stderr); + +pub fn stderr() -> Stderr { + Stderr(std::io::stderr()) +} +wasi_file_impl!( + Stderr, + fn num_ready_bytes(&self) -> Result { + Ok(0) + } +); diff --git a/crates/wasi-c2/src/file.rs b/crates/wasi-c2/src/file.rs index c94dd6a6dd..3521d21b53 100644 --- a/crates/wasi-c2/src/file.rs +++ b/crates/wasi-c2/src/file.rs @@ -1,13 +1,11 @@ use crate::Error; use bitflags::bitflags; -use fs_set_times::SetTimes; +use fs_set_times::SystemTimeSpec; use std::any::Any; use std::cell::Ref; use std::ops::Deref; -use system_interface::fs::FileIoExt; -use system_interface::io::ReadReady; -pub trait WasiFile: FileIoExt + SetTimes + ReadReady { +pub trait WasiFile { fn as_any(&self) -> &dyn Any; fn datasync(&self) -> Result<(), Error>; fn sync(&self) -> Result<(), Error>; @@ -16,6 +14,27 @@ pub trait WasiFile: FileIoExt + SetTimes + ReadReady { fn set_fdflags(&self, _flags: FdFlags) -> Result<(), Error>; fn get_filestat(&self) -> Result; fn set_filestat_size(&self, _size: u64) -> Result<(), Error>; + fn advise( + &self, + offset: u64, + len: u64, + advice: system_interface::fs::Advice, + ) -> Result<(), Error>; + fn allocate(&self, offset: u64, len: u64) -> Result<(), Error>; + fn set_times( + &self, + atime: Option, + mtime: Option, + ) -> Result<(), Error>; + fn read_vectored(&self, bufs: &mut [std::io::IoSliceMut]) -> Result; + fn read_vectored_at(&self, bufs: &mut [std::io::IoSliceMut], offset: u64) + -> Result; + fn write_vectored(&self, bufs: &[std::io::IoSlice]) -> Result; + fn write_vectored_at(&self, bufs: &[std::io::IoSlice], offset: u64) -> Result; + fn seek(&self, pos: std::io::SeekFrom) -> Result; + fn stream_position(&self) -> Result; + fn peek(&self, buf: &mut [u8]) -> Result; + fn num_ready_bytes(&self) -> Result; } #[derive(Debug, Copy, Clone)] @@ -30,31 +49,6 @@ pub enum FileType { Unknown, } -impl From<&cap_std::fs::FileType> for FileType { - fn from(ft: &cap_std::fs::FileType) -> FileType { - use cap_fs_ext::FileTypeExt; - if ft.is_dir() { - FileType::Directory - } else if ft.is_symlink() { - FileType::SymbolicLink - } else if ft.is_socket() { - if ft.is_block_device() { - FileType::SocketDgram - } else { - FileType::SocketStream - } - } else if ft.is_block_device() { - FileType::BlockDevice - } else if ft.is_char_device() { - FileType::CharacterDevice - } else if ft.is_file() { - FileType::RegularFile - } else { - FileType::Unknown - } - } -} - bitflags! { pub struct FdFlags: u32 { const APPEND = 0b1; diff --git a/crates/wasi-c2/src/pipe.rs b/crates/wasi-c2/src/pipe.rs index aebf84de9f..55c6c6ce08 100644 --- a/crates/wasi-c2/src/pipe.rs +++ b/crates/wasi-c2/src/pipe.rs @@ -12,10 +12,10 @@ use crate::file::{FdFlags, FileType, Filestat, WasiFile}; use crate::Error; use std::any::Any; +use std::convert::TryInto; use std::io::{self, Read, Write}; use std::sync::{Arc, RwLock}; -use system_interface::fs::{Advice, FileIoExt}; -use system_interface::io::ReadReady; +use system_interface::fs::Advice; /// A virtual pipe read end. /// @@ -96,82 +96,6 @@ impl From<&str> for ReadPipe> { } } -impl FileIoExt for ReadPipe { - fn advise(&self, offset: u64, len: u64, advice: Advice) -> io::Result<()> { - Err(std::io::Error::from_raw_os_error(libc::EBADF)) - } - fn allocate(&self, offset: u64, len: u64) -> io::Result<()> { - Err(std::io::Error::from_raw_os_error(libc::EBADF)) - } - fn read(&self, buf: &mut [u8]) -> io::Result { - self.borrow().read(buf) - } - fn read_exact(&self, buf: &mut [u8]) -> io::Result<()> { - self.borrow().read_exact(buf) - } - fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result { - Err(std::io::Error::from_raw_os_error(libc::EBADF)) - } - fn read_exact_at(&self, buf: &mut [u8], offset: u64) -> io::Result<()> { - Err(std::io::Error::from_raw_os_error(libc::EBADF)) - } - fn read_vectored(&self, bufs: &mut [io::IoSliceMut]) -> io::Result { - self.borrow().read_vectored(bufs) - } - fn read_to_end(&self, buf: &mut Vec) -> io::Result { - self.borrow().read_to_end(buf) - } - fn read_to_string(&self, buf: &mut String) -> io::Result { - self.borrow().read_to_string(buf) - } - fn write(&self, buf: &[u8]) -> io::Result { - Err(std::io::Error::from_raw_os_error(libc::EBADF)) - } - fn write_all(&self, buf: &[u8]) -> io::Result<()> { - Err(std::io::Error::from_raw_os_error(libc::EBADF)) - } - fn write_at(&self, buf: &[u8], offset: u64) -> io::Result { - Err(std::io::Error::from_raw_os_error(libc::EBADF)) - } - fn write_all_at(&self, buf: &[u8], offset: u64) -> io::Result<()> { - Err(std::io::Error::from_raw_os_error(libc::EBADF)) - } - fn write_vectored(&self, bufs: &[io::IoSlice]) -> io::Result { - Err(std::io::Error::from_raw_os_error(libc::EBADF)) - } - fn write_fmt(&self, fmt: std::fmt::Arguments) -> io::Result<()> { - Err(std::io::Error::from_raw_os_error(libc::EBADF)) - } - fn flush(&self) -> io::Result<()> { - Err(std::io::Error::from_raw_os_error(libc::EBADF)) - } - fn seek(&self, _pos: std::io::SeekFrom) -> io::Result { - Err(std::io::Error::from_raw_os_error(libc::ESPIPE)) - } - fn stream_position(&self) -> io::Result { - Err(std::io::Error::from_raw_os_error(libc::ESPIPE)) - } - fn peek(&self, _buf: &mut [u8]) -> io::Result { - Err(std::io::Error::from_raw_os_error(libc::EBADF)) // XXX is this right? or do we have to implement this faithfully, and add a buffer of peeked values to handle during reads? - } -} - -impl fs_set_times::SetTimes for ReadPipe { - fn set_times( - &self, - _: Option, - _: Option, - ) -> io::Result<()> { - todo!() - } -} - -impl ReadReady for ReadPipe { - fn num_ready_bytes(&self) -> Result { - todo!() - } -} - impl WasiFile for ReadPipe { fn as_any(&self) -> &dyn Any { self @@ -206,6 +130,44 @@ impl WasiFile for ReadPipe { fn set_filestat_size(&self, _size: u64) -> Result<(), Error> { Err(Error::Perm) } + fn advise(&self, offset: u64, len: u64, advice: Advice) -> Result<(), Error> { + Err(Error::Badf) + } + fn allocate(&self, offset: u64, len: u64) -> Result<(), Error> { + Err(Error::Badf) + } + fn read_vectored(&self, bufs: &mut [io::IoSliceMut]) -> Result { + let n = self.borrow().read_vectored(bufs)?; + Ok(n.try_into().map_err(|_| Error::Overflow)?) + } + fn read_vectored_at(&self, bufs: &mut [io::IoSliceMut], offset: u64) -> Result { + Err(Error::Badf) + } + fn write_vectored(&self, bufs: &[io::IoSlice]) -> Result { + Err(Error::Badf) + } + fn write_vectored_at(&self, bufs: &[io::IoSlice], offset: u64) -> Result { + Err(Error::Badf) + } + fn seek(&self, pos: std::io::SeekFrom) -> Result { + Err(Error::Badf) + } + fn stream_position(&self) -> Result { + Err(Error::Badf) + } + fn peek(&self, buf: &mut [u8]) -> Result { + Err(Error::Badf) + } + fn set_times( + &self, + atime: Option, + mtime: Option, + ) -> Result<(), Error> { + Err(Error::Badf) + } + fn num_ready_bytes(&self) -> Result { + Ok(0) + } } /// A virtual pipe write end. @@ -273,82 +235,6 @@ impl WritePipe>> { } } -impl FileIoExt for WritePipe { - fn advise(&self, offset: u64, len: u64, advice: Advice) -> io::Result<()> { - Err(std::io::Error::from_raw_os_error(libc::EBADF)) - } - fn allocate(&self, offset: u64, len: u64) -> io::Result<()> { - Err(std::io::Error::from_raw_os_error(libc::EBADF)) - } - fn read(&self, buf: &mut [u8]) -> io::Result { - Err(std::io::Error::from_raw_os_error(libc::EBADF)) - } - fn read_exact(&self, buf: &mut [u8]) -> io::Result<()> { - Err(std::io::Error::from_raw_os_error(libc::EBADF)) - } - fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result { - Err(std::io::Error::from_raw_os_error(libc::EBADF)) - } - fn read_exact_at(&self, buf: &mut [u8], offset: u64) -> io::Result<()> { - Err(std::io::Error::from_raw_os_error(libc::EBADF)) - } - fn read_vectored(&self, bufs: &mut [io::IoSliceMut]) -> io::Result { - Err(std::io::Error::from_raw_os_error(libc::EBADF)) - } - fn read_to_end(&self, buf: &mut Vec) -> io::Result { - Err(std::io::Error::from_raw_os_error(libc::EBADF)) - } - fn read_to_string(&self, buf: &mut String) -> io::Result { - Err(std::io::Error::from_raw_os_error(libc::EBADF)) - } - fn write(&self, buf: &[u8]) -> io::Result { - self.borrow().write(buf) - } - fn write_all(&self, buf: &[u8]) -> io::Result<()> { - self.borrow().write_all(buf) - } - fn write_at(&self, buf: &[u8], offset: u64) -> io::Result { - Err(std::io::Error::from_raw_os_error(libc::EBADF)) - } - fn write_all_at(&self, buf: &[u8], offset: u64) -> io::Result<()> { - Err(std::io::Error::from_raw_os_error(libc::EBADF)) - } - fn write_vectored(&self, bufs: &[io::IoSlice]) -> io::Result { - self.borrow().write_vectored(bufs) - } - fn write_fmt(&self, fmt: std::fmt::Arguments) -> io::Result<()> { - self.borrow().write_fmt(fmt) - } - fn flush(&self) -> io::Result<()> { - self.borrow().flush() - } - fn seek(&self, pos: std::io::SeekFrom) -> io::Result { - Err(std::io::Error::from_raw_os_error(libc::ESPIPE)) - } - fn stream_position(&self) -> io::Result { - Err(std::io::Error::from_raw_os_error(libc::ESPIPE)) - } - fn peek(&self, _buf: &mut [u8]) -> io::Result { - Err(std::io::Error::from_raw_os_error(libc::EBADF)) - } -} - -impl fs_set_times::SetTimes for WritePipe { - fn set_times( - &self, - _: Option, - _: Option, - ) -> io::Result<()> { - todo!() // - } -} - -impl ReadReady for WritePipe { - fn num_ready_bytes(&self) -> Result { - Ok(0) - } -} - impl WasiFile for WritePipe { fn as_any(&self) -> &dyn Any { self @@ -383,4 +269,42 @@ impl WasiFile for WritePipe { fn set_filestat_size(&self, _size: u64) -> Result<(), Error> { Err(Error::Perm) } + fn advise(&self, offset: u64, len: u64, advice: Advice) -> Result<(), Error> { + Err(Error::Badf) + } + fn allocate(&self, offset: u64, len: u64) -> Result<(), Error> { + Err(Error::Badf) + } + fn read_vectored(&self, bufs: &mut [io::IoSliceMut]) -> Result { + Err(Error::Badf) + } + fn read_vectored_at(&self, bufs: &mut [io::IoSliceMut], offset: u64) -> Result { + Err(Error::Badf) + } + fn write_vectored(&self, bufs: &[io::IoSlice]) -> Result { + let n = self.borrow().write_vectored(bufs)?; + Ok(n.try_into().map_err(|_| Error::Overflow)?) + } + fn write_vectored_at(&self, bufs: &[io::IoSlice], offset: u64) -> Result { + Err(Error::Badf) + } + fn seek(&self, pos: std::io::SeekFrom) -> Result { + Err(Error::Badf) + } + fn stream_position(&self) -> Result { + Err(Error::Badf) + } + fn peek(&self, buf: &mut [u8]) -> Result { + Err(Error::Badf) + } + fn set_times( + &self, + atime: Option, + mtime: Option, + ) -> Result<(), Error> { + Err(Error::Badf) + } + fn num_ready_bytes(&self) -> Result { + Ok(0) + } } diff --git a/crates/wasi-c2/src/snapshots/preview_1.rs b/crates/wasi-c2/src/snapshots/preview_1.rs index dbe4d076b0..53d973a834 100644 --- a/crates/wasi-c2/src/snapshots/preview_1.rs +++ b/crates/wasi-c2/src/snapshots/preview_1.rs @@ -369,8 +369,7 @@ impl<'a> wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx { .get_file(fd) .expect("checked that entry is file") .get_cap(FileCaps::FILESTAT_SET_TIMES)? - .set_times(atim, mtim)?; - Ok(()) + .set_times(atim, mtim) } else if table.is::(fd) { use cap_std::time::{Duration, SystemClock}; @@ -398,8 +397,7 @@ impl<'a> wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx { .get_dir(fd) .expect("checked that entry is dir") .get_cap(DirCaps::FILESTAT_SET_TIMES)? - .set_times(".", atim, mtim)?; - Ok(()) + .set_times(".", atim, mtim) } else { Err(Error::Badf) }