feat(wasi): provide default implementations for WasiFile (#3904)

Additionally, as cleanup, remove duplicate implementations.
This commit is contained in:
Nathaniel McCallum
2022-03-09 17:38:10 -05:00
committed by GitHub
parent ae4d86804d
commit 44a435a43a
6 changed files with 99 additions and 435 deletions

View File

@@ -26,9 +26,6 @@ impl WasiFile for File {
fn as_any(&self) -> &dyn Any {
self
}
async fn sock_accept(&mut self, _fdflags: FdFlags) -> Result<Box<dyn WasiFile>, Error> {
Err(Error::badf())
}
async fn datasync(&self) -> Result<(), Error> {
self.0.sync_data()?;
Ok(())
@@ -128,12 +125,6 @@ impl WasiFile for File {
fn isatty(&self) -> bool {
self.0.is_terminal()
}
async fn readable(&self) -> Result<(), Error> {
Err(Error::badf())
}
async fn writable(&self) -> Result<(), Error> {
Err(Error::badf())
}
}
pub fn filetype_from(ft: &cap_std::fs::FileType) -> FileType {

View File

@@ -17,7 +17,7 @@ use system_interface::fs::GetSetFdFlags;
use system_interface::io::IsReadWrite;
use system_interface::io::ReadReady;
use wasi_common::{
file::{Advice, FdFlags, FileType, Filestat, WasiFile},
file::{FdFlags, FileType, WasiFile},
Error, ErrorExt,
};
@@ -91,12 +91,6 @@ macro_rules! wasi_listen_write_impl {
stream.set_fdflags(fdflags).await?;
Ok(Box::new(stream))
}
async fn datasync(&self) -> Result<(), Error> {
Err(Error::badf())
}
async fn sync(&self) -> Result<(), Error> {
Err(Error::badf())
}
async fn get_filetype(&self) -> Result<FileType, Error> {
Ok(FileType::SocketStream)
}
@@ -105,13 +99,6 @@ macro_rules! wasi_listen_write_impl {
let fdflags = self.0.as_filelike().get_fd_flags()?;
Ok(from_sysif_fdflags(fdflags))
}
#[cfg(windows)]
async fn get_fdflags(&self) -> Result<FdFlags, Error> {
// There does not seem to be a way for windows to call s.th. like `fcntl()`
// `rustix::fs::fcntl` is only available for Unix
// `rustix::io::ioctl_fionbio` only sets the flags, but does not read
Ok(FdFlags::empty())
}
async fn set_fdflags(&mut self, fdflags: FdFlags) -> Result<(), Error> {
if fdflags == wasi_common::file::FdFlags::NONBLOCK {
self.0.set_nonblocking(true)?;
@@ -124,66 +111,9 @@ macro_rules! wasi_listen_write_impl {
}
Ok(())
}
async fn get_filestat(&self) -> Result<Filestat, Error> {
Err(Error::badf())
}
async fn set_filestat_size(&self, _size: u64) -> Result<(), Error> {
Err(Error::badf())
}
async fn advise(&self, _offset: u64, _len: u64, _advice: Advice) -> Result<(), Error> {
Err(Error::badf())
}
async fn allocate(&self, _offset: u64, _len: u64) -> Result<(), Error> {
Err(Error::badf())
}
async fn set_times(
&self,
_atime: Option<wasi_common::SystemTimeSpec>,
_mtime: Option<wasi_common::SystemTimeSpec>,
) -> Result<(), Error> {
Err(Error::badf())
}
async fn read_vectored<'a>(
&self,
_bufs: &mut [io::IoSliceMut<'a>],
) -> Result<u64, Error> {
Err(Error::badf())
}
async fn read_vectored_at<'a>(
&self,
_bufs: &mut [io::IoSliceMut<'a>],
_offset: u64,
) -> Result<u64, Error> {
Err(Error::badf())
}
async fn write_vectored<'a>(&self, _bufs: &[io::IoSlice<'a>]) -> Result<u64, Error> {
Err(Error::badf())
}
async fn write_vectored_at<'a>(
&self,
_bufs: &[io::IoSlice<'a>],
_offset: u64,
) -> Result<u64, Error> {
Err(Error::badf())
}
async fn seek(&self, _pos: std::io::SeekFrom) -> Result<u64, Error> {
Err(Error::badf())
}
async fn peek(&self, _buf: &mut [u8]) -> Result<u64, Error> {
Err(Error::badf())
}
async fn num_ready_bytes(&self) -> Result<u64, Error> {
Ok(1)
}
fn isatty(&self) -> bool {
false
}
async fn readable(&self) -> Result<(), Error> {
Err(Error::badf())
}
async fn writable(&self) -> Result<(), Error> {
Err(Error::badf())
}
}
#[cfg(windows)]
@@ -240,15 +170,6 @@ macro_rules! wasi_stream_write_impl {
fn as_any(&self) -> &dyn Any {
self
}
async fn sock_accept(&mut self, _fdflags: FdFlags) -> Result<Box<dyn WasiFile>, Error> {
Err(Error::badf())
}
async fn datasync(&self) -> Result<(), Error> {
Err(Error::badf())
}
async fn sync(&self) -> Result<(), Error> {
Err(Error::badf())
}
async fn get_filetype(&self) -> Result<FileType, Error> {
Ok(FileType::SocketStream)
}
@@ -257,14 +178,6 @@ macro_rules! wasi_stream_write_impl {
let fdflags = self.0.as_filelike().get_fd_flags()?;
Ok(from_sysif_fdflags(fdflags))
}
#[cfg(windows)]
async fn get_fdflags(&self) -> Result<FdFlags, Error> {
// There does not seem to be a way for windows to call s.th. like `fcntl(fd, F_GETFL, 0)`
// on a socket.
// `rustix::fs::fcntl` is only available for Unix.
// `rustix::io::ioctl_fionbio` only sets the flags, but does not read.
Ok(FdFlags::empty())
}
async fn set_fdflags(&mut self, fdflags: FdFlags) -> Result<(), Error> {
if fdflags == wasi_common::file::FdFlags::NONBLOCK {
self.0.set_nonblocking(true)?;
@@ -277,25 +190,6 @@ macro_rules! wasi_stream_write_impl {
}
Ok(())
}
async fn get_filestat(&self) -> Result<Filestat, Error> {
Err(Error::badf())
}
async fn set_filestat_size(&self, _size: u64) -> Result<(), Error> {
Err(Error::badf())
}
async fn advise(&self, _offset: u64, _len: u64, _advice: Advice) -> Result<(), Error> {
Err(Error::badf())
}
async fn allocate(&self, _offset: u64, _len: u64) -> Result<(), Error> {
Err(Error::badf())
}
async fn set_times(
&self,
_atime: Option<wasi_common::SystemTimeSpec>,
_mtime: Option<wasi_common::SystemTimeSpec>,
) -> Result<(), Error> {
Err(Error::badf())
}
async fn read_vectored<'a>(
&self,
bufs: &mut [io::IoSliceMut<'a>],
@@ -304,28 +198,11 @@ macro_rules! wasi_stream_write_impl {
let n = Read::read_vectored(&mut *self.as_socketlike_view::<$std_ty>(), bufs)?;
Ok(n.try_into()?)
}
async fn read_vectored_at<'a>(
&self,
_bufs: &mut [io::IoSliceMut<'a>],
_offset: u64,
) -> Result<u64, Error> {
Err(Error::badf())
}
async fn write_vectored<'a>(&self, bufs: &[io::IoSlice<'a>]) -> Result<u64, Error> {
use std::io::Write;
let n = Write::write_vectored(&mut *self.as_socketlike_view::<$std_ty>(), bufs)?;
Ok(n.try_into()?)
}
async fn write_vectored_at<'a>(
&self,
_bufs: &[io::IoSlice<'a>],
_offset: u64,
) -> Result<u64, Error> {
Err(Error::badf())
}
async fn seek(&self, _pos: std::io::SeekFrom) -> Result<u64, Error> {
Err(Error::badf())
}
async fn peek(&self, buf: &mut [u8]) -> Result<u64, Error> {
let n = self.0.peek(buf)?;
Ok(n.try_into()?)
@@ -334,9 +211,6 @@ macro_rules! wasi_stream_write_impl {
let val = self.as_socketlike_view::<$std_ty>().num_ready_bytes()?;
Ok(val)
}
fn isatty(&self) -> bool {
false
}
async fn readable(&self) -> Result<(), Error> {
let (readable, _writeable) = self.0.is_read_write()?;
if readable {

View File

@@ -16,7 +16,7 @@ use io_lifetimes::{AsFd, BorrowedFd};
#[cfg(windows)]
use io_lifetimes::{AsHandle, BorrowedHandle};
use wasi_common::{
file::{Advice, FdFlags, FileType, Filestat, WasiFile},
file::{FdFlags, FileType, Filestat, WasiFile},
Error, ErrorExt,
};
@@ -31,12 +31,6 @@ impl WasiFile for Stdin {
fn as_any(&self) -> &dyn Any {
self
}
async fn datasync(&self) -> Result<(), Error> {
Ok(())
}
async fn sync(&self) -> Result<(), Error> {
Ok(())
}
async fn get_filetype(&self) -> Result<FileType, Error> {
if self.isatty() {
Ok(FileType::CharacterDevice)
@@ -44,34 +38,6 @@ impl WasiFile for Stdin {
Ok(FileType::Unknown)
}
}
async fn get_fdflags(&self) -> Result<FdFlags, Error> {
Ok(FdFlags::empty())
}
async fn set_fdflags(&mut self, _fdflags: FdFlags) -> Result<(), Error> {
Err(Error::badf())
}
async fn get_filestat(&self) -> Result<Filestat, Error> {
let meta = self.0.as_filelike_view::<File>().metadata()?;
Ok(Filestat {
device_id: 0,
inode: 0,
filetype: self.get_filetype().await?,
nlink: 0,
size: meta.len(),
atim: meta.accessed().ok(),
mtim: meta.modified().ok(),
ctim: meta.created().ok(),
})
}
async fn set_filestat_size(&self, _size: u64) -> Result<(), Error> {
Err(Error::badf())
}
async fn advise(&self, _offset: u64, _len: u64, _advice: Advice) -> Result<(), Error> {
Err(Error::badf())
}
async fn allocate(&self, _offset: u64, _len: u64) -> Result<(), Error> {
Err(Error::badf())
}
async fn read_vectored<'a>(&self, bufs: &mut [io::IoSliceMut<'a>]) -> Result<u64, Error> {
let n = self.0.as_filelike_view::<File>().read_vectored(bufs)?;
Ok(n.try_into().map_err(|_| Error::range())?)
@@ -83,16 +49,6 @@ impl WasiFile for Stdin {
) -> Result<u64, Error> {
Err(Error::seek_pipe())
}
async fn write_vectored<'a>(&self, _bufs: &[io::IoSlice<'a>]) -> Result<u64, Error> {
Err(Error::badf())
}
async fn write_vectored_at<'a>(
&self,
_bufs: &[io::IoSlice<'a>],
_offset: u64,
) -> Result<u64, Error> {
Err(Error::badf())
}
async fn seek(&self, _pos: std::io::SeekFrom) -> Result<u64, Error> {
Err(Error::seek_pipe())
}
@@ -114,16 +70,6 @@ impl WasiFile for Stdin {
fn isatty(&self) -> bool {
self.0.is_terminal()
}
async fn readable(&self) -> Result<(), Error> {
Err(Error::badf())
}
async fn writable(&self) -> Result<(), Error> {
Err(Error::badf())
}
async fn sock_accept(&mut self, _fdflags: FdFlags) -> Result<Box<dyn WasiFile>, Error> {
Err(Error::badf())
}
}
#[cfg(windows)]
impl AsHandle for Stdin {
@@ -152,12 +98,6 @@ macro_rules! wasi_file_write_impl {
fn as_any(&self) -> &dyn Any {
self
}
async fn datasync(&self) -> Result<(), Error> {
Ok(())
}
async fn sync(&self) -> Result<(), Error> {
Ok(())
}
async fn get_filetype(&self) -> Result<FileType, Error> {
if self.isatty() {
Ok(FileType::CharacterDevice)
@@ -168,9 +108,6 @@ macro_rules! wasi_file_write_impl {
async fn get_fdflags(&self) -> Result<FdFlags, Error> {
Ok(FdFlags::APPEND)
}
async fn set_fdflags(&mut self, _fdflags: FdFlags) -> Result<(), Error> {
Err(Error::badf())
}
async fn get_filestat(&self) -> Result<Filestat, Error> {
let meta = self.0.as_filelike_view::<File>().metadata()?;
Ok(Filestat {
@@ -184,28 +121,6 @@ macro_rules! wasi_file_write_impl {
ctim: meta.created().ok(),
})
}
async fn set_filestat_size(&self, _size: u64) -> Result<(), Error> {
Err(Error::badf())
}
async fn advise(&self, _offset: u64, _len: u64, _advice: Advice) -> Result<(), Error> {
Err(Error::badf())
}
async fn allocate(&self, _offset: u64, _len: u64) -> Result<(), Error> {
Err(Error::badf())
}
async fn read_vectored<'a>(
&self,
_bufs: &mut [io::IoSliceMut<'a>],
) -> Result<u64, Error> {
Err(Error::badf())
}
async fn read_vectored_at<'a>(
&self,
_bufs: &mut [io::IoSliceMut<'a>],
_offset: u64,
) -> Result<u64, Error> {
Err(Error::badf())
}
async fn write_vectored<'a>(&self, bufs: &[io::IoSlice<'a>]) -> Result<u64, Error> {
let n = self.0.as_filelike_view::<File>().write_vectored(bufs)?;
Ok(n.try_into().map_err(|c| Error::range().context(c))?)
@@ -220,9 +135,6 @@ macro_rules! wasi_file_write_impl {
async fn seek(&self, _pos: std::io::SeekFrom) -> Result<u64, Error> {
Err(Error::seek_pipe())
}
async fn peek(&self, _buf: &mut [u8]) -> Result<u64, Error> {
Err(Error::badf())
}
async fn set_times(
&self,
atime: Option<wasi_common::SystemTimeSpec>,
@@ -232,21 +144,9 @@ macro_rules! wasi_file_write_impl {
.set_times(convert_systimespec(atime), convert_systimespec(mtime))?;
Ok(())
}
async fn num_ready_bytes(&self) -> Result<u64, Error> {
Ok(0)
}
fn isatty(&self) -> bool {
self.0.is_terminal()
}
async fn readable(&self) -> Result<(), Error> {
Err(Error::badf())
}
async fn writable(&self) -> Result<(), Error> {
Err(Error::badf())
}
async fn sock_accept(&mut self, _fdflags: FdFlags) -> Result<Box<dyn WasiFile>, Error> {
Err(Error::badf())
}
}
#[cfg(windows)]
impl AsHandle for $ty {