feat(wasi)!: make most WasiFile methods take &mut self (#3901)

1. This makes it easier for implementors to deal with internal APIs.
2. This matches the signatures of the WASI Snapshot traits.

Although it is likely true that these methods would have to become
immutable in order to implement threading efficiently, threading will
impact a large number of existing traits. So this change is practical
for now with an already-unavoidable change required for threading.

Signed-off-by: Nathaniel McCallum <nathaniel@profian.com>
This commit is contained in:
Nathaniel McCallum
2022-03-09 18:22:42 -05:00
committed by GitHub
parent 44a435a43a
commit 8b48ce7fb7
10 changed files with 138 additions and 127 deletions

View File

@@ -26,19 +26,19 @@ impl WasiFile for File {
fn as_any(&self) -> &dyn Any { fn as_any(&self) -> &dyn Any {
self self
} }
async fn datasync(&self) -> Result<(), Error> { async fn datasync(&mut self) -> Result<(), Error> {
self.0.sync_data()?; self.0.sync_data()?;
Ok(()) Ok(())
} }
async fn sync(&self) -> Result<(), Error> { async fn sync(&mut self) -> Result<(), Error> {
self.0.sync_all()?; self.0.sync_all()?;
Ok(()) Ok(())
} }
async fn get_filetype(&self) -> Result<FileType, Error> { async fn get_filetype(&mut self) -> Result<FileType, Error> {
let meta = self.0.metadata()?; let meta = self.0.metadata()?;
Ok(filetype_from(&meta.file_type())) Ok(filetype_from(&meta.file_type()))
} }
async fn get_fdflags(&self) -> Result<FdFlags, Error> { async fn get_fdflags(&mut self) -> Result<FdFlags, Error> {
let fdflags = self.0.get_fd_flags()?; let fdflags = self.0.get_fd_flags()?;
Ok(from_sysif_fdflags(fdflags)) Ok(from_sysif_fdflags(fdflags))
} }
@@ -54,7 +54,7 @@ impl WasiFile for File {
self.0.set_fd_flags(set_fd_flags)?; self.0.set_fd_flags(set_fd_flags)?;
Ok(()) Ok(())
} }
async fn get_filestat(&self) -> Result<Filestat, Error> { async fn get_filestat(&mut self) -> Result<Filestat, Error> {
let meta = self.0.metadata()?; let meta = self.0.metadata()?;
Ok(Filestat { Ok(Filestat {
device_id: meta.dev(), device_id: meta.dev(),
@@ -67,20 +67,20 @@ impl WasiFile for File {
ctim: meta.created().map(|t| Some(t.into_std())).unwrap_or(None), ctim: meta.created().map(|t| Some(t.into_std())).unwrap_or(None),
}) })
} }
async fn set_filestat_size(&self, size: u64) -> Result<(), Error> { async fn set_filestat_size(&mut self, size: u64) -> Result<(), Error> {
self.0.set_len(size)?; self.0.set_len(size)?;
Ok(()) Ok(())
} }
async fn advise(&self, offset: u64, len: u64, advice: Advice) -> Result<(), Error> { async fn advise(&mut self, offset: u64, len: u64, advice: Advice) -> Result<(), Error> {
self.0.advise(offset, len, convert_advice(advice))?; self.0.advise(offset, len, convert_advice(advice))?;
Ok(()) Ok(())
} }
async fn allocate(&self, offset: u64, len: u64) -> Result<(), Error> { async fn allocate(&mut self, offset: u64, len: u64) -> Result<(), Error> {
self.0.allocate(offset, len)?; self.0.allocate(offset, len)?;
Ok(()) Ok(())
} }
async fn set_times( async fn set_times(
&self, &mut self,
atime: Option<wasi_common::SystemTimeSpec>, atime: Option<wasi_common::SystemTimeSpec>,
mtime: Option<wasi_common::SystemTimeSpec>, mtime: Option<wasi_common::SystemTimeSpec>,
) -> Result<(), Error> { ) -> Result<(), Error> {
@@ -88,41 +88,41 @@ impl WasiFile for File {
.set_times(convert_systimespec(atime), convert_systimespec(mtime))?; .set_times(convert_systimespec(atime), convert_systimespec(mtime))?;
Ok(()) Ok(())
} }
async fn read_vectored<'a>(&self, bufs: &mut [io::IoSliceMut<'a>]) -> Result<u64, Error> { async fn read_vectored<'a>(&mut self, bufs: &mut [io::IoSliceMut<'a>]) -> Result<u64, Error> {
let n = self.0.read_vectored(bufs)?; let n = self.0.read_vectored(bufs)?;
Ok(n.try_into()?) Ok(n.try_into()?)
} }
async fn read_vectored_at<'a>( async fn read_vectored_at<'a>(
&self, &mut self,
bufs: &mut [io::IoSliceMut<'a>], bufs: &mut [io::IoSliceMut<'a>],
offset: u64, offset: u64,
) -> Result<u64, Error> { ) -> Result<u64, Error> {
let n = self.0.read_vectored_at(bufs, offset)?; let n = self.0.read_vectored_at(bufs, offset)?;
Ok(n.try_into()?) Ok(n.try_into()?)
} }
async fn write_vectored<'a>(&self, bufs: &[io::IoSlice<'a>]) -> Result<u64, Error> { async fn write_vectored<'a>(&mut self, bufs: &[io::IoSlice<'a>]) -> Result<u64, Error> {
let n = self.0.write_vectored(bufs)?; let n = self.0.write_vectored(bufs)?;
Ok(n.try_into()?) Ok(n.try_into()?)
} }
async fn write_vectored_at<'a>( async fn write_vectored_at<'a>(
&self, &mut self,
bufs: &[io::IoSlice<'a>], bufs: &[io::IoSlice<'a>],
offset: u64, offset: u64,
) -> Result<u64, Error> { ) -> Result<u64, Error> {
let n = self.0.write_vectored_at(bufs, offset)?; let n = self.0.write_vectored_at(bufs, offset)?;
Ok(n.try_into()?) Ok(n.try_into()?)
} }
async fn seek(&self, pos: std::io::SeekFrom) -> Result<u64, Error> { async fn seek(&mut self, pos: std::io::SeekFrom) -> Result<u64, Error> {
Ok(self.0.seek(pos)?) Ok(self.0.seek(pos)?)
} }
async fn peek(&self, buf: &mut [u8]) -> Result<u64, Error> { async fn peek(&mut self, buf: &mut [u8]) -> Result<u64, Error> {
let n = self.0.peek(buf)?; let n = self.0.peek(buf)?;
Ok(n.try_into()?) Ok(n.try_into()?)
} }
async fn num_ready_bytes(&self) -> Result<u64, Error> { async fn num_ready_bytes(&self) -> Result<u64, Error> {
Ok(self.0.num_ready_bytes()?) Ok(self.0.num_ready_bytes()?)
} }
fn isatty(&self) -> bool { fn isatty(&mut self) -> bool {
self.0.is_terminal() self.0.is_terminal()
} }
} }

View File

@@ -91,11 +91,11 @@ macro_rules! wasi_listen_write_impl {
stream.set_fdflags(fdflags).await?; stream.set_fdflags(fdflags).await?;
Ok(Box::new(stream)) Ok(Box::new(stream))
} }
async fn get_filetype(&self) -> Result<FileType, Error> { async fn get_filetype(&mut self) -> Result<FileType, Error> {
Ok(FileType::SocketStream) Ok(FileType::SocketStream)
} }
#[cfg(unix)] #[cfg(unix)]
async fn get_fdflags(&self) -> Result<FdFlags, Error> { async fn get_fdflags(&mut self) -> Result<FdFlags, Error> {
let fdflags = self.0.as_filelike().get_fd_flags()?; let fdflags = self.0.as_filelike().get_fd_flags()?;
Ok(from_sysif_fdflags(fdflags)) Ok(from_sysif_fdflags(fdflags))
} }
@@ -170,11 +170,11 @@ macro_rules! wasi_stream_write_impl {
fn as_any(&self) -> &dyn Any { fn as_any(&self) -> &dyn Any {
self self
} }
async fn get_filetype(&self) -> Result<FileType, Error> { async fn get_filetype(&mut self) -> Result<FileType, Error> {
Ok(FileType::SocketStream) Ok(FileType::SocketStream)
} }
#[cfg(unix)] #[cfg(unix)]
async fn get_fdflags(&self) -> Result<FdFlags, Error> { async fn get_fdflags(&mut self) -> Result<FdFlags, Error> {
let fdflags = self.0.as_filelike().get_fd_flags()?; let fdflags = self.0.as_filelike().get_fd_flags()?;
Ok(from_sysif_fdflags(fdflags)) Ok(from_sysif_fdflags(fdflags))
} }
@@ -191,19 +191,19 @@ macro_rules! wasi_stream_write_impl {
Ok(()) Ok(())
} }
async fn read_vectored<'a>( async fn read_vectored<'a>(
&self, &mut self,
bufs: &mut [io::IoSliceMut<'a>], bufs: &mut [io::IoSliceMut<'a>],
) -> Result<u64, Error> { ) -> Result<u64, Error> {
use std::io::Read; use std::io::Read;
let n = Read::read_vectored(&mut *self.as_socketlike_view::<$std_ty>(), bufs)?; let n = Read::read_vectored(&mut *self.as_socketlike_view::<$std_ty>(), bufs)?;
Ok(n.try_into()?) Ok(n.try_into()?)
} }
async fn write_vectored<'a>(&self, bufs: &[io::IoSlice<'a>]) -> Result<u64, Error> { async fn write_vectored<'a>(&mut self, bufs: &[io::IoSlice<'a>]) -> Result<u64, Error> {
use std::io::Write; use std::io::Write;
let n = Write::write_vectored(&mut *self.as_socketlike_view::<$std_ty>(), bufs)?; let n = Write::write_vectored(&mut *self.as_socketlike_view::<$std_ty>(), bufs)?;
Ok(n.try_into()?) Ok(n.try_into()?)
} }
async fn peek(&self, buf: &mut [u8]) -> Result<u64, Error> { async fn peek(&mut self, buf: &mut [u8]) -> Result<u64, Error> {
let n = self.0.peek(buf)?; let n = self.0.peek(buf)?;
Ok(n.try_into()?) Ok(n.try_into()?)
} }

View File

@@ -31,32 +31,32 @@ impl WasiFile for Stdin {
fn as_any(&self) -> &dyn Any { fn as_any(&self) -> &dyn Any {
self self
} }
async fn get_filetype(&self) -> Result<FileType, Error> { async fn get_filetype(&mut self) -> Result<FileType, Error> {
if self.isatty() { if self.isatty() {
Ok(FileType::CharacterDevice) Ok(FileType::CharacterDevice)
} else { } else {
Ok(FileType::Unknown) Ok(FileType::Unknown)
} }
} }
async fn read_vectored<'a>(&self, bufs: &mut [io::IoSliceMut<'a>]) -> Result<u64, Error> { async fn read_vectored<'a>(&mut self, bufs: &mut [io::IoSliceMut<'a>]) -> Result<u64, Error> {
let n = self.0.as_filelike_view::<File>().read_vectored(bufs)?; let n = self.0.as_filelike_view::<File>().read_vectored(bufs)?;
Ok(n.try_into().map_err(|_| Error::range())?) Ok(n.try_into().map_err(|_| Error::range())?)
} }
async fn read_vectored_at<'a>( async fn read_vectored_at<'a>(
&self, &mut self,
_bufs: &mut [io::IoSliceMut<'a>], _bufs: &mut [io::IoSliceMut<'a>],
_offset: u64, _offset: u64,
) -> Result<u64, Error> { ) -> Result<u64, Error> {
Err(Error::seek_pipe()) Err(Error::seek_pipe())
} }
async fn seek(&self, _pos: std::io::SeekFrom) -> Result<u64, Error> { async fn seek(&mut self, _pos: std::io::SeekFrom) -> Result<u64, Error> {
Err(Error::seek_pipe()) Err(Error::seek_pipe())
} }
async fn peek(&self, _buf: &mut [u8]) -> Result<u64, Error> { async fn peek(&mut self, _buf: &mut [u8]) -> Result<u64, Error> {
Err(Error::seek_pipe()) Err(Error::seek_pipe())
} }
async fn set_times( async fn set_times(
&self, &mut self,
atime: Option<wasi_common::SystemTimeSpec>, atime: Option<wasi_common::SystemTimeSpec>,
mtime: Option<wasi_common::SystemTimeSpec>, mtime: Option<wasi_common::SystemTimeSpec>,
) -> Result<(), Error> { ) -> Result<(), Error> {
@@ -67,7 +67,7 @@ impl WasiFile for Stdin {
async fn num_ready_bytes(&self) -> Result<u64, Error> { async fn num_ready_bytes(&self) -> Result<u64, Error> {
Ok(self.0.num_ready_bytes()?) Ok(self.0.num_ready_bytes()?)
} }
fn isatty(&self) -> bool { fn isatty(&mut self) -> bool {
self.0.is_terminal() self.0.is_terminal()
} }
} }
@@ -98,17 +98,17 @@ macro_rules! wasi_file_write_impl {
fn as_any(&self) -> &dyn Any { fn as_any(&self) -> &dyn Any {
self self
} }
async fn get_filetype(&self) -> Result<FileType, Error> { async fn get_filetype(&mut self) -> Result<FileType, Error> {
if self.isatty() { if self.isatty() {
Ok(FileType::CharacterDevice) Ok(FileType::CharacterDevice)
} else { } else {
Ok(FileType::Unknown) Ok(FileType::Unknown)
} }
} }
async fn get_fdflags(&self) -> Result<FdFlags, Error> { async fn get_fdflags(&mut self) -> Result<FdFlags, Error> {
Ok(FdFlags::APPEND) Ok(FdFlags::APPEND)
} }
async fn get_filestat(&self) -> Result<Filestat, Error> { async fn get_filestat(&mut self) -> Result<Filestat, Error> {
let meta = self.0.as_filelike_view::<File>().metadata()?; let meta = self.0.as_filelike_view::<File>().metadata()?;
Ok(Filestat { Ok(Filestat {
device_id: 0, device_id: 0,
@@ -121,22 +121,22 @@ macro_rules! wasi_file_write_impl {
ctim: meta.created().ok(), ctim: meta.created().ok(),
}) })
} }
async fn write_vectored<'a>(&self, bufs: &[io::IoSlice<'a>]) -> Result<u64, Error> { async fn write_vectored<'a>(&mut self, bufs: &[io::IoSlice<'a>]) -> Result<u64, Error> {
let n = self.0.as_filelike_view::<File>().write_vectored(bufs)?; let n = self.0.as_filelike_view::<File>().write_vectored(bufs)?;
Ok(n.try_into().map_err(|c| Error::range().context(c))?) Ok(n.try_into().map_err(|c| Error::range().context(c))?)
} }
async fn write_vectored_at<'a>( async fn write_vectored_at<'a>(
&self, &mut self,
_bufs: &[io::IoSlice<'a>], _bufs: &[io::IoSlice<'a>],
_offset: u64, _offset: u64,
) -> Result<u64, Error> { ) -> Result<u64, Error> {
Err(Error::seek_pipe()) Err(Error::seek_pipe())
} }
async fn seek(&self, _pos: std::io::SeekFrom) -> Result<u64, Error> { async fn seek(&mut self, _pos: std::io::SeekFrom) -> Result<u64, Error> {
Err(Error::seek_pipe()) Err(Error::seek_pipe())
} }
async fn set_times( async fn set_times(
&self, &mut self,
atime: Option<wasi_common::SystemTimeSpec>, atime: Option<wasi_common::SystemTimeSpec>,
mtime: Option<wasi_common::SystemTimeSpec>, mtime: Option<wasi_common::SystemTimeSpec>,
) -> Result<(), Error> { ) -> Result<(), Error> {
@@ -144,7 +144,7 @@ macro_rules! wasi_file_write_impl {
.set_times(convert_systimespec(atime), convert_systimespec(mtime))?; .set_times(convert_systimespec(atime), convert_systimespec(mtime))?;
Ok(()) Ok(())
} }
fn isatty(&self) -> bool { fn isatty(&mut self) -> bool {
self.0.is_terminal() self.0.is_terminal()
} }
} }

View File

@@ -70,22 +70,22 @@ impl WasiCtx {
Ok(()) Ok(())
} }
pub fn set_stdin(&mut self, f: Box<dyn WasiFile>) { pub fn set_stdin(&mut self, mut f: Box<dyn WasiFile>) {
let rights = Self::stdio_rights(&*f); let rights = Self::stdio_rights(&mut *f);
self.insert_file(0, f, rights); self.insert_file(0, f, rights);
} }
pub fn set_stdout(&mut self, f: Box<dyn WasiFile>) { pub fn set_stdout(&mut self, mut f: Box<dyn WasiFile>) {
let rights = Self::stdio_rights(&*f); let rights = Self::stdio_rights(&mut *f);
self.insert_file(1, f, rights); self.insert_file(1, f, rights);
} }
pub fn set_stderr(&mut self, f: Box<dyn WasiFile>) { pub fn set_stderr(&mut self, mut f: Box<dyn WasiFile>) {
let rights = Self::stdio_rights(&*f); let rights = Self::stdio_rights(&mut *f);
self.insert_file(2, f, rights); self.insert_file(2, f, rights);
} }
fn stdio_rights(f: &dyn WasiFile) -> FileCaps { fn stdio_rights(f: &mut dyn WasiFile) -> FileCaps {
let mut rights = FileCaps::all(); let mut rights = FileCaps::all();
// If `f` is a tty, restrict the `tell` and `seek` capabilities, so // If `f` is a tty, restrict the `tell` and `seek` capabilities, so

View File

@@ -5,9 +5,9 @@ use std::any::Any;
#[wiggle::async_trait] #[wiggle::async_trait]
pub trait WasiFile: Send + Sync { pub trait WasiFile: Send + Sync {
fn as_any(&self) -> &dyn Any; fn as_any(&self) -> &dyn Any;
async fn get_filetype(&self) -> Result<FileType, Error>; async fn get_filetype(&mut self) -> Result<FileType, Error>;
fn isatty(&self) -> bool { fn isatty(&mut self) -> bool {
false false
} }
@@ -15,15 +15,15 @@ pub trait WasiFile: Send + Sync {
Err(Error::badf()) Err(Error::badf())
} }
async fn datasync(&self) -> Result<(), Error> { async fn datasync(&mut self) -> Result<(), Error> {
Ok(()) Ok(())
} }
async fn sync(&self) -> Result<(), Error> { async fn sync(&mut self) -> Result<(), Error> {
Ok(()) Ok(())
} }
async fn get_fdflags(&self) -> Result<FdFlags, Error> { async fn get_fdflags(&mut self) -> Result<FdFlags, Error> {
Ok(FdFlags::empty()) Ok(FdFlags::empty())
} }
@@ -31,7 +31,7 @@ pub trait WasiFile: Send + Sync {
Err(Error::badf()) Err(Error::badf())
} }
async fn get_filestat(&self) -> Result<Filestat, Error> { async fn get_filestat(&mut self) -> Result<Filestat, Error> {
Ok(Filestat { Ok(Filestat {
device_id: 0, device_id: 0,
inode: 0, inode: 0,
@@ -44,55 +44,58 @@ pub trait WasiFile: Send + Sync {
}) })
} }
async fn set_filestat_size(&self, _size: u64) -> Result<(), Error> { async fn set_filestat_size(&mut self, _size: u64) -> Result<(), Error> {
Err(Error::badf()) Err(Error::badf())
} }
async fn advise(&self, _offset: u64, _len: u64, _advice: Advice) -> Result<(), Error> { async fn advise(&mut self, _offset: u64, _len: u64, _advice: Advice) -> Result<(), Error> {
Err(Error::badf()) Err(Error::badf())
} }
async fn allocate(&self, _offset: u64, _len: u64) -> Result<(), Error> { async fn allocate(&mut self, _offset: u64, _len: u64) -> Result<(), Error> {
Err(Error::badf()) Err(Error::badf())
} }
async fn set_times( async fn set_times(
&self, &mut self,
_atime: Option<SystemTimeSpec>, _atime: Option<SystemTimeSpec>,
_mtime: Option<SystemTimeSpec>, _mtime: Option<SystemTimeSpec>,
) -> Result<(), Error> { ) -> Result<(), Error> {
Err(Error::badf()) Err(Error::badf())
} }
async fn read_vectored<'a>(&self, _bufs: &mut [std::io::IoSliceMut<'a>]) -> Result<u64, Error> { async fn read_vectored<'a>(
&mut self,
_bufs: &mut [std::io::IoSliceMut<'a>],
) -> Result<u64, Error> {
Err(Error::badf()) Err(Error::badf())
} }
async fn read_vectored_at<'a>( async fn read_vectored_at<'a>(
&self, &mut self,
_bufs: &mut [std::io::IoSliceMut<'a>], _bufs: &mut [std::io::IoSliceMut<'a>],
_offset: u64, _offset: u64,
) -> Result<u64, Error> { ) -> Result<u64, Error> {
Err(Error::badf()) Err(Error::badf())
} }
async fn write_vectored<'a>(&self, _bufs: &[std::io::IoSlice<'a>]) -> Result<u64, Error> { async fn write_vectored<'a>(&mut self, _bufs: &[std::io::IoSlice<'a>]) -> Result<u64, Error> {
Err(Error::badf()) Err(Error::badf())
} }
async fn write_vectored_at<'a>( async fn write_vectored_at<'a>(
&self, &mut self,
_bufs: &[std::io::IoSlice<'a>], _bufs: &[std::io::IoSlice<'a>],
_offset: u64, _offset: u64,
) -> Result<u64, Error> { ) -> Result<u64, Error> {
Err(Error::badf()) Err(Error::badf())
} }
async fn seek(&self, _pos: std::io::SeekFrom) -> Result<u64, Error> { async fn seek(&mut self, _pos: std::io::SeekFrom) -> Result<u64, Error> {
Err(Error::badf()) Err(Error::badf())
} }
async fn peek(&self, _buf: &mut [u8]) -> Result<u64, Error> { async fn peek(&mut self, _buf: &mut [u8]) -> Result<u64, Error> {
Err(Error::badf()) Err(Error::badf())
} }
@@ -190,7 +193,7 @@ impl FileEntry {
Ok(()) Ok(())
} }
pub async fn get_fdstat(&self) -> Result<FdStat, Error> { pub async fn get_fdstat(&mut self) -> Result<FdStat, Error> {
Ok(FdStat { Ok(FdStat {
filetype: self.file.get_filetype().await?, filetype: self.file.get_filetype().await?,
caps: self.caps, caps: self.caps,

View File

@@ -105,10 +105,10 @@ impl<R: Read + Any + Send + Sync> WasiFile for ReadPipe<R> {
fn as_any(&self) -> &dyn Any { fn as_any(&self) -> &dyn Any {
self self
} }
async fn get_filetype(&self) -> Result<FileType, Error> { async fn get_filetype(&mut self) -> Result<FileType, Error> {
Ok(FileType::Pipe) Ok(FileType::Pipe)
} }
async fn read_vectored<'a>(&self, bufs: &mut [io::IoSliceMut<'a>]) -> Result<u64, Error> { async fn read_vectored<'a>(&mut self, bufs: &mut [io::IoSliceMut<'a>]) -> Result<u64, Error> {
let n = self.borrow().read_vectored(bufs)?; let n = self.borrow().read_vectored(bufs)?;
Ok(n.try_into()?) Ok(n.try_into()?)
} }
@@ -189,13 +189,13 @@ impl<W: Write + Any + Send + Sync> WasiFile for WritePipe<W> {
fn as_any(&self) -> &dyn Any { fn as_any(&self) -> &dyn Any {
self self
} }
async fn get_filetype(&self) -> Result<FileType, Error> { async fn get_filetype(&mut self) -> Result<FileType, Error> {
Ok(FileType::Pipe) Ok(FileType::Pipe)
} }
async fn get_fdflags(&self) -> Result<FdFlags, Error> { async fn get_fdflags(&mut self) -> Result<FdFlags, Error> {
Ok(FdFlags::APPEND) Ok(FdFlags::APPEND)
} }
async fn write_vectored<'a>(&self, bufs: &[io::IoSlice<'a>]) -> Result<u64, Error> { async fn write_vectored<'a>(&mut self, bufs: &[io::IoSlice<'a>]) -> Result<u64, Error> {
let n = self.borrow().write_vectored(bufs)?; let n = self.borrow().write_vectored(bufs)?;
Ok(n.try_into()?) Ok(n.try_into()?)
} }

View File

@@ -462,8 +462,10 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
fd: types::Fd, fd: types::Fd,
iovs: &types::IovecArray<'a>, iovs: &types::IovecArray<'a>,
) -> Result<types::Size, Error> { ) -> Result<types::Size, Error> {
let table = self.table(); let f = self
let f = table.get_file(u32::from(fd))?.get_cap(FileCaps::READ)?; .table()
.get_file_mut(u32::from(fd))?
.get_cap_mut(FileCaps::READ)?;
let mut guest_slices: Vec<wiggle::GuestSliceMut<u8>> = iovs let mut guest_slices: Vec<wiggle::GuestSliceMut<u8>> = iovs
.iter() .iter()
@@ -489,10 +491,10 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
iovs: &types::IovecArray<'a>, iovs: &types::IovecArray<'a>,
offset: types::Filesize, offset: types::Filesize,
) -> Result<types::Size, Error> { ) -> Result<types::Size, Error> {
let table = self.table(); let f = self
let f = table .table()
.get_file(u32::from(fd))? .get_file_mut(u32::from(fd))?
.get_cap(FileCaps::READ | FileCaps::SEEK)?; .get_cap_mut(FileCaps::READ | FileCaps::SEEK)?;
let mut guest_slices: Vec<wiggle::GuestSliceMut<u8>> = iovs let mut guest_slices: Vec<wiggle::GuestSliceMut<u8>> = iovs
.iter() .iter()
@@ -517,8 +519,10 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
fd: types::Fd, fd: types::Fd,
ciovs: &types::CiovecArray<'a>, ciovs: &types::CiovecArray<'a>,
) -> Result<types::Size, Error> { ) -> Result<types::Size, Error> {
let table = self.table(); let f = self
let f = table.get_file(u32::from(fd))?.get_cap(FileCaps::WRITE)?; .table()
.get_file_mut(u32::from(fd))?
.get_cap_mut(FileCaps::WRITE)?;
let guest_slices: Vec<wiggle::GuestSlice<u8>> = ciovs let guest_slices: Vec<wiggle::GuestSlice<u8>> = ciovs
.iter() .iter()
@@ -544,10 +548,10 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
ciovs: &types::CiovecArray<'a>, ciovs: &types::CiovecArray<'a>,
offset: types::Filesize, offset: types::Filesize,
) -> Result<types::Size, Error> { ) -> Result<types::Size, Error> {
let table = self.table(); let f = self
let f = table .table()
.get_file(u32::from(fd))? .get_file_mut(u32::from(fd))?
.get_cap(FileCaps::WRITE | FileCaps::SEEK)?; .get_cap_mut(FileCaps::WRITE | FileCaps::SEEK)?;
let guest_slices: Vec<wiggle::GuestSlice<u8>> = ciovs let guest_slices: Vec<wiggle::GuestSlice<u8>> = ciovs
.iter() .iter()

View File

@@ -260,8 +260,8 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
advice: types::Advice, advice: types::Advice,
) -> Result<(), Error> { ) -> Result<(), Error> {
self.table() self.table()
.get_file(u32::from(fd))? .get_file_mut(u32::from(fd))?
.get_cap(FileCaps::ADVISE)? .get_cap_mut(FileCaps::ADVISE)?
.advise(offset, len, advice.into()) .advise(offset, len, advice.into())
.await?; .await?;
Ok(()) Ok(())
@@ -274,8 +274,8 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
len: types::Filesize, len: types::Filesize,
) -> Result<(), Error> { ) -> Result<(), Error> {
self.table() self.table()
.get_file(u32::from(fd))? .get_file_mut(u32::from(fd))?
.get_cap(FileCaps::ALLOCATE)? .get_cap_mut(FileCaps::ALLOCATE)?
.allocate(offset, len) .allocate(offset, len)
.await?; .await?;
Ok(()) Ok(())
@@ -309,8 +309,8 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
async fn fd_datasync(&mut self, fd: types::Fd) -> Result<(), Error> { async fn fd_datasync(&mut self, fd: types::Fd) -> Result<(), Error> {
self.table() self.table()
.get_file(u32::from(fd))? .get_file_mut(u32::from(fd))?
.get_cap(FileCaps::DATASYNC)? .get_cap_mut(FileCaps::DATASYNC)?
.datasync() .datasync()
.await?; .await?;
Ok(()) Ok(())
@@ -320,7 +320,7 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
let table = self.table(); let table = self.table();
let fd = u32::from(fd); let fd = u32::from(fd);
if table.is::<FileEntry>(fd) { if table.is::<FileEntry>(fd) {
let file_entry: &FileEntry = table.get(fd)?; let file_entry: &mut FileEntry = table.get_mut(fd)?;
let fdstat = file_entry.get_fdstat().await?; let fdstat = file_entry.get_fdstat().await?;
Ok(types::Fdstat::from(&fdstat)) Ok(types::Fdstat::from(&fdstat))
} else if table.is::<DirEntry>(fd) { } else if table.is::<DirEntry>(fd) {
@@ -371,8 +371,8 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
let fd = u32::from(fd); let fd = u32::from(fd);
if table.is::<FileEntry>(fd) { if table.is::<FileEntry>(fd) {
let filestat = table let filestat = table
.get_file(fd)? .get_file_mut(fd)?
.get_cap(FileCaps::FILESTAT_GET)? .get_cap_mut(FileCaps::FILESTAT_GET)?
.get_filestat() .get_filestat()
.await?; .await?;
Ok(filestat.into()) Ok(filestat.into())
@@ -394,8 +394,8 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
size: types::Filesize, size: types::Filesize,
) -> Result<(), Error> { ) -> Result<(), Error> {
self.table() self.table()
.get_file(u32::from(fd))? .get_file_mut(u32::from(fd))?
.get_cap(FileCaps::FILESTAT_SET_SIZE)? .get_cap_mut(FileCaps::FILESTAT_SET_SIZE)?
.set_filestat_size(size) .set_filestat_size(size)
.await?; .await?;
Ok(()) Ok(())
@@ -421,9 +421,9 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
if table.is::<FileEntry>(fd) { if table.is::<FileEntry>(fd) {
table table
.get_file(fd) .get_file_mut(fd)
.expect("checked that entry is file") .expect("checked that entry is file")
.get_cap(FileCaps::FILESTAT_SET_TIMES)? .get_cap_mut(FileCaps::FILESTAT_SET_TIMES)?
.set_times(atim, mtim) .set_times(atim, mtim)
.await .await
} else if table.is::<DirEntry>(fd) { } else if table.is::<DirEntry>(fd) {
@@ -443,8 +443,10 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
fd: types::Fd, fd: types::Fd,
iovs: &types::IovecArray<'a>, iovs: &types::IovecArray<'a>,
) -> Result<types::Size, Error> { ) -> Result<types::Size, Error> {
let table = self.table(); let f = self
let f = table.get_file(u32::from(fd))?.get_cap(FileCaps::READ)?; .table()
.get_file_mut(u32::from(fd))?
.get_cap_mut(FileCaps::READ)?;
let mut guest_slices: Vec<wiggle::GuestSliceMut<u8>> = iovs let mut guest_slices: Vec<wiggle::GuestSliceMut<u8>> = iovs
.iter() .iter()
@@ -470,10 +472,10 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
iovs: &types::IovecArray<'a>, iovs: &types::IovecArray<'a>,
offset: types::Filesize, offset: types::Filesize,
) -> Result<types::Size, Error> { ) -> Result<types::Size, Error> {
let table = self.table(); let f = self
let f = table .table()
.get_file(u32::from(fd))? .get_file_mut(u32::from(fd))?
.get_cap(FileCaps::READ | FileCaps::SEEK)?; .get_cap_mut(FileCaps::READ | FileCaps::SEEK)?;
let mut guest_slices: Vec<wiggle::GuestSliceMut<u8>> = iovs let mut guest_slices: Vec<wiggle::GuestSliceMut<u8>> = iovs
.iter() .iter()
@@ -498,8 +500,10 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
fd: types::Fd, fd: types::Fd,
ciovs: &types::CiovecArray<'a>, ciovs: &types::CiovecArray<'a>,
) -> Result<types::Size, Error> { ) -> Result<types::Size, Error> {
let table = self.table(); let f = self
let f = table.get_file(u32::from(fd))?.get_cap(FileCaps::WRITE)?; .table()
.get_file_mut(u32::from(fd))?
.get_cap_mut(FileCaps::WRITE)?;
let guest_slices: Vec<wiggle::GuestSlice<u8>> = ciovs let guest_slices: Vec<wiggle::GuestSlice<u8>> = ciovs
.iter() .iter()
@@ -525,10 +529,10 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
ciovs: &types::CiovecArray<'a>, ciovs: &types::CiovecArray<'a>,
offset: types::Filesize, offset: types::Filesize,
) -> Result<types::Size, Error> { ) -> Result<types::Size, Error> {
let table = self.table(); let f = self
let f = table .table()
.get_file(u32::from(fd))? .get_file_mut(u32::from(fd))?
.get_cap(FileCaps::WRITE | FileCaps::SEEK)?; .get_cap_mut(FileCaps::WRITE | FileCaps::SEEK)?;
let guest_slices: Vec<wiggle::GuestSlice<u8>> = ciovs let guest_slices: Vec<wiggle::GuestSlice<u8>> = ciovs
.iter() .iter()
@@ -622,8 +626,8 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
}; };
let newoffset = self let newoffset = self
.table() .table()
.get_file(u32::from(fd))? .get_file_mut(u32::from(fd))?
.get_cap(required_caps)? .get_cap_mut(required_caps)?
.seek(whence) .seek(whence)
.await?; .await?;
Ok(newoffset) Ok(newoffset)
@@ -631,8 +635,8 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
async fn fd_sync(&mut self, fd: types::Fd) -> Result<(), Error> { async fn fd_sync(&mut self, fd: types::Fd) -> Result<(), Error> {
self.table() self.table()
.get_file(u32::from(fd))? .get_file_mut(u32::from(fd))?
.get_cap(FileCaps::SYNC)? .get_cap_mut(FileCaps::SYNC)?
.sync() .sync()
.await?; .await?;
Ok(()) Ok(())
@@ -642,8 +646,8 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
// XXX should this be stream_position? // XXX should this be stream_position?
let offset = self let offset = self
.table() .table()
.get_file(u32::from(fd))? .get_file_mut(u32::from(fd))?
.get_cap(FileCaps::TELL)? .get_cap_mut(FileCaps::TELL)?
.seek(std::io::SeekFrom::Current(0)) .seek(std::io::SeekFrom::Current(0))
.await?; .await?;
Ok(offset) Ok(offset)

View File

@@ -94,64 +94,64 @@ macro_rules! wasi_file_impl {
fn as_any(&self) -> &dyn Any { fn as_any(&self) -> &dyn Any {
self self
} }
async fn datasync(&self) -> Result<(), Error> { async fn datasync(&mut self) -> Result<(), Error> {
block_on_dummy_executor(|| self.0.datasync()) block_on_dummy_executor(|| self.0.datasync())
} }
async fn sync(&self) -> Result<(), Error> { async fn sync(&mut self) -> Result<(), Error> {
block_on_dummy_executor(|| self.0.sync()) block_on_dummy_executor(|| self.0.sync())
} }
async fn get_filetype(&self) -> Result<FileType, Error> { async fn get_filetype(&mut self) -> Result<FileType, Error> {
block_on_dummy_executor(|| self.0.get_filetype()) block_on_dummy_executor(|| self.0.get_filetype())
} }
async fn get_fdflags(&self) -> Result<FdFlags, Error> { async fn get_fdflags(&mut self) -> Result<FdFlags, Error> {
block_on_dummy_executor(|| self.0.get_fdflags()) block_on_dummy_executor(|| self.0.get_fdflags())
} }
async fn set_fdflags(&mut self, fdflags: FdFlags) -> Result<(), Error> { async fn set_fdflags(&mut self, fdflags: FdFlags) -> Result<(), Error> {
block_on_dummy_executor(|| self.0.set_fdflags(fdflags)) block_on_dummy_executor(|| self.0.set_fdflags(fdflags))
} }
async fn get_filestat(&self) -> Result<Filestat, Error> { async fn get_filestat(&mut self) -> Result<Filestat, Error> {
block_on_dummy_executor(|| self.0.get_filestat()) block_on_dummy_executor(|| self.0.get_filestat())
} }
async fn set_filestat_size(&self, size: u64) -> Result<(), Error> { async fn set_filestat_size(&mut self, size: u64) -> Result<(), Error> {
block_on_dummy_executor(move || self.0.set_filestat_size(size)) block_on_dummy_executor(move || self.0.set_filestat_size(size))
} }
async fn advise(&self, offset: u64, len: u64, advice: Advice) -> Result<(), Error> { async fn advise(&mut self, offset: u64, len: u64, advice: Advice) -> Result<(), Error> {
block_on_dummy_executor(move || self.0.advise(offset, len, advice)) block_on_dummy_executor(move || self.0.advise(offset, len, advice))
} }
async fn allocate(&self, offset: u64, len: u64) -> Result<(), Error> { async fn allocate(&mut self, offset: u64, len: u64) -> Result<(), Error> {
block_on_dummy_executor(move || self.0.allocate(offset, len)) block_on_dummy_executor(move || self.0.allocate(offset, len))
} }
async fn read_vectored<'a>( async fn read_vectored<'a>(
&self, &mut self,
bufs: &mut [io::IoSliceMut<'a>], bufs: &mut [io::IoSliceMut<'a>],
) -> Result<u64, Error> { ) -> Result<u64, Error> {
block_on_dummy_executor(move || self.0.read_vectored(bufs)) block_on_dummy_executor(move || self.0.read_vectored(bufs))
} }
async fn read_vectored_at<'a>( async fn read_vectored_at<'a>(
&self, &mut self,
bufs: &mut [io::IoSliceMut<'a>], bufs: &mut [io::IoSliceMut<'a>],
offset: u64, offset: u64,
) -> Result<u64, Error> { ) -> Result<u64, Error> {
block_on_dummy_executor(move || self.0.read_vectored_at(bufs, offset)) block_on_dummy_executor(move || self.0.read_vectored_at(bufs, offset))
} }
async fn write_vectored<'a>(&self, bufs: &[io::IoSlice<'a>]) -> Result<u64, Error> { async fn write_vectored<'a>(&mut self, bufs: &[io::IoSlice<'a>]) -> Result<u64, Error> {
block_on_dummy_executor(move || self.0.write_vectored(bufs)) block_on_dummy_executor(move || self.0.write_vectored(bufs))
} }
async fn write_vectored_at<'a>( async fn write_vectored_at<'a>(
&self, &mut self,
bufs: &[io::IoSlice<'a>], bufs: &[io::IoSlice<'a>],
offset: u64, offset: u64,
) -> Result<u64, Error> { ) -> Result<u64, Error> {
block_on_dummy_executor(move || self.0.write_vectored_at(bufs, offset)) block_on_dummy_executor(move || self.0.write_vectored_at(bufs, offset))
} }
async fn seek(&self, pos: std::io::SeekFrom) -> Result<u64, Error> { async fn seek(&mut self, pos: std::io::SeekFrom) -> Result<u64, Error> {
block_on_dummy_executor(move || self.0.seek(pos)) block_on_dummy_executor(move || self.0.seek(pos))
} }
async fn peek(&self, buf: &mut [u8]) -> Result<u64, Error> { async fn peek(&mut self, buf: &mut [u8]) -> Result<u64, Error> {
block_on_dummy_executor(move || self.0.peek(buf)) block_on_dummy_executor(move || self.0.peek(buf))
} }
async fn set_times( async fn set_times(
&self, &mut self,
atime: Option<wasi_common::SystemTimeSpec>, atime: Option<wasi_common::SystemTimeSpec>,
mtime: Option<wasi_common::SystemTimeSpec>, mtime: Option<wasi_common::SystemTimeSpec>,
) -> Result<(), Error> { ) -> Result<(), Error> {
@@ -160,7 +160,7 @@ macro_rules! wasi_file_impl {
async fn num_ready_bytes(&self) -> Result<u64, Error> { async fn num_ready_bytes(&self) -> Result<u64, Error> {
block_on_dummy_executor(|| self.0.num_ready_bytes()) block_on_dummy_executor(|| self.0.num_ready_bytes())
} }
fn isatty(&self) -> bool { fn isatty(&mut self) -> bool {
self.0.isatty() self.0.isatty()
} }

View File

@@ -20,7 +20,7 @@ async fn empty_file_readable() -> Result<(), Error> {
let d = workspace.open_dir("d").context("open dir")?; let d = workspace.open_dir("d").context("open dir")?;
let d = Dir::from_cap_std(d); let d = Dir::from_cap_std(d);
let f = d let mut f = d
.open_file(false, "f", OFlags::CREATE, false, true, FdFlags::empty()) .open_file(false, "f", OFlags::CREATE, false, true, FdFlags::empty())
.await .await
.context("create writable file f")?; .context("create writable file f")?;