integrate GetSetFdFlags!

change reopen_with_fdflags(&self, fdflags) -> Result<Box<dyn WasiFile>>
to set_fdflags(&mut self, fdflags) -> Result<()>.

this makes way more sense than my prior hare-brained schemes.
This commit is contained in:
Pat Hickey
2021-01-26 12:23:52 -08:00
parent c98d6f6201
commit 1eb8a8a7fe
6 changed files with 83 additions and 63 deletions

View File

@@ -3,8 +3,10 @@ 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;
use system_interface::{
fs::{Advice, FileIoExt, GetSetFdFlags},
io::ReadReady,
};
use wasi_c2::{
file::{FdFlags, FileType, Filestat, WasiFile},
Error,
@@ -35,11 +37,11 @@ impl WasiFile for File {
Ok(filetype_from(&meta.file_type()))
}
fn get_fdflags(&self) -> Result<FdFlags, Error> {
// XXX get_fdflags is not implemented but lets lie rather than panic:
Ok(FdFlags::empty())
let fdflags = self.0.get_fd_flags()?;
Ok(from_sysif_fdflags(fdflags))
}
unsafe fn reopen_with_fdflags(&self, _fdflags: FdFlags) -> Result<Box<dyn WasiFile>, Error> {
todo!("reopen_with_fdflags is not implemented")
fn set_fdflags(&mut self, fdflags: FdFlags) -> Result<(), Error> {
Ok(self.0.set_fd_flags(to_sysif_fdflags(fdflags))?)
}
fn get_filestat(&self) -> Result<Filestat, Error> {
let meta = self.0.metadata()?;
@@ -150,3 +152,42 @@ pub fn convert_systimespec(t: Option<wasi_c2::SystemTimeSpec>) -> Option<SystemT
None => None,
}
}
pub fn to_sysif_fdflags(f: wasi_c2::file::FdFlags) -> system_interface::fs::FdFlags {
let mut out = system_interface::fs::FdFlags::empty();
if f.contains(wasi_c2::file::FdFlags::APPEND) {
out |= system_interface::fs::FdFlags::APPEND;
}
if f.contains(wasi_c2::file::FdFlags::DSYNC) {
out |= system_interface::fs::FdFlags::DSYNC;
}
if f.contains(wasi_c2::file::FdFlags::NONBLOCK) {
out |= system_interface::fs::FdFlags::NONBLOCK;
}
if f.contains(wasi_c2::file::FdFlags::RSYNC) {
out |= system_interface::fs::FdFlags::RSYNC;
}
if f.contains(wasi_c2::file::FdFlags::SYNC) {
out |= system_interface::fs::FdFlags::SYNC;
}
out
}
pub fn from_sysif_fdflags(f: system_interface::fs::FdFlags) -> wasi_c2::file::FdFlags {
let mut out = wasi_c2::file::FdFlags::empty();
if f.contains(system_interface::fs::FdFlags::APPEND) {
out |= wasi_c2::file::FdFlags::APPEND;
}
if f.contains(system_interface::fs::FdFlags::DSYNC) {
out |= wasi_c2::file::FdFlags::DSYNC;
}
if f.contains(system_interface::fs::FdFlags::NONBLOCK) {
out |= wasi_c2::file::FdFlags::NONBLOCK;
}
if f.contains(system_interface::fs::FdFlags::RSYNC) {
out |= wasi_c2::file::FdFlags::RSYNC;
}
if f.contains(system_interface::fs::FdFlags::SYNC) {
out |= wasi_c2::file::FdFlags::SYNC;
}
out
}

View File

@@ -36,10 +36,9 @@ impl WasiFile for Stdin {
Ok(FileType::Unknown)
}
fn get_fdflags(&self) -> Result<FdFlags, Error> {
// XXX get_fdflags is not implemented but lets lie rather than panic:
Ok(FdFlags::empty())
}
unsafe fn reopen_with_fdflags(&self, _fdflags: FdFlags) -> Result<Box<dyn WasiFile>, Error> {
fn set_fdflags(&mut self, _fdflags: FdFlags) -> Result<(), Error> {
Err(Error::badf())
}
fn get_filestat(&self) -> Result<Filestat, Error> {
@@ -125,13 +124,9 @@ macro_rules! wasi_file_write_impl {
Ok(FileType::Unknown)
}
fn get_fdflags(&self) -> Result<FdFlags, Error> {
// XXX get_fdflags is not implemented but lets lie rather than panic:
Ok(FdFlags::empty())
Ok(FdFlags::APPEND)
}
unsafe fn reopen_with_fdflags(
&self,
_fdflags: FdFlags,
) -> Result<Box<dyn WasiFile>, Error> {
fn set_fdflags(&mut self, _fdflags: FdFlags) -> Result<(), Error> {
Err(Error::badf())
}
fn get_filestat(&self) -> Result<Filestat, Error> {

View File

@@ -1,8 +1,8 @@
use crate::{Error, ErrorExt, SystemTimeSpec};
use bitflags::bitflags;
use std::any::Any;
use std::cell::Ref;
use std::ops::Deref;
use std::cell::{Ref, RefMut};
use std::ops::{Deref, DerefMut};
pub trait WasiFile {
fn as_any(&self) -> &dyn Any;
@@ -10,9 +10,7 @@ pub trait WasiFile {
fn sync(&self) -> Result<(), Error>; // file op
fn get_filetype(&self) -> Result<FileType, Error>; // file op
fn get_fdflags(&self) -> Result<FdFlags, Error>; // file op
/// This method takes a `&self` so that it can be called on a `&dyn WasiFile`. However,
/// the caller makes the additional guarantee to drop `self` after the call is successful.
unsafe fn reopen_with_fdflags(&self, flags: FdFlags) -> Result<Box<dyn WasiFile>, Error>; // file op
fn set_fdflags(&mut self, flags: FdFlags) -> Result<(), Error>; // file op
fn get_filestat(&self) -> Result<Filestat, Error>; // split out get_length as a read & write op, rest is a file op
fn set_filestat_size(&self, _size: u64) -> Result<(), Error>; // write op
fn advise(
@@ -83,22 +81,14 @@ pub struct Filestat {
pub(crate) trait TableFileExt {
fn get_file(&self, fd: u32) -> Result<Ref<FileEntry>, Error>;
fn update_file_in_place<F>(&mut self, fd: u32, f: F) -> Result<(), Error>
where
F: FnOnce(&dyn WasiFile) -> Result<Box<dyn WasiFile>, Error>;
fn get_file_mut(&self, fd: u32) -> Result<RefMut<FileEntry>, Error>;
}
impl TableFileExt for crate::table::Table {
fn get_file(&self, fd: u32) -> Result<Ref<FileEntry>, Error> {
self.get(fd)
}
fn update_file_in_place<F>(&mut self, fd: u32, f: F) -> Result<(), Error>
where
F: FnOnce(&dyn WasiFile) -> Result<Box<dyn WasiFile>, Error>,
{
self.update_in_place(fd, |FileEntry { caps, file }| {
let file = f(file.deref())?;
Ok(FileEntry { caps, file })
})
fn get_file_mut(&self, fd: u32) -> Result<RefMut<FileEntry>, Error> {
self.get_mut(fd)
}
}
@@ -145,6 +135,16 @@ impl<'a> FileEntryExt<'a> for Ref<'a, FileEntry> {
Ok(Ref::map(self, |r| r.file.deref()))
}
}
pub trait FileEntryMutExt<'a> {
fn get_cap(self, caps: FileCaps) -> Result<RefMut<'a, dyn WasiFile>, Error>;
}
impl<'a> FileEntryMutExt<'a> for RefMut<'a, FileEntry> {
fn get_cap(self, caps: FileCaps) -> Result<RefMut<'a, dyn WasiFile>, Error> {
self.capable_of(caps)?;
Ok(RefMut::map(self, |r| r.file.deref_mut()))
}
}
bitflags! {
pub struct FileCaps : u32 {

View File

@@ -114,7 +114,7 @@ impl<R: Read + Any> WasiFile for ReadPipe<R> {
fn get_fdflags(&self) -> Result<FdFlags, Error> {
Ok(FdFlags::empty())
}
unsafe fn reopen_with_fdflags(&self, _fdflags: FdFlags) -> Result<Box<dyn WasiFile>, Error> {
fn set_fdflags(&mut self, _fdflags: FdFlags) -> Result<(), Error> {
Err(Error::badf())
}
fn get_filestat(&self) -> Result<Filestat, Error> {
@@ -250,7 +250,7 @@ impl<W: Write + Any> WasiFile for WritePipe<W> {
fn get_fdflags(&self) -> Result<FdFlags, Error> {
Ok(FdFlags::APPEND)
}
unsafe fn reopen_with_fdflags(&self, _fdflags: FdFlags) -> Result<Box<dyn WasiFile>, Error> {
fn set_fdflags(&mut self, _fdflags: FdFlags) -> Result<(), Error> {
Err(Error::badf())
}
fn get_filestat(&self) -> Result<Filestat, Error> {

View File

@@ -2,8 +2,8 @@
use crate::{
dir::{DirCaps, DirEntry, DirEntryExt, DirFdStat, ReaddirCursor, ReaddirEntity, TableDirExt},
file::{
FdFlags, FdStat, FileCaps, FileEntry, FileEntryExt, FileType, Filestat, OFlags,
TableFileExt,
FdFlags, FdStat, FileCaps, FileEntry, FileEntryExt, FileEntryMutExt, FileType, Filestat,
OFlags, TableFileExt,
},
sched::{
subscription::{RwEventFlags, SubscriptionResult},
@@ -332,14 +332,10 @@ impl<'a> wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
}
fn fd_fdstat_set_flags(&self, fd: types::Fd, flags: types::Fdflags) -> Result<(), Error> {
let mut table = self.table();
let fd = u32::from(fd);
let table_check = table.get_file(fd)?.get_cap(FileCaps::FDSTAT_SET_FLAGS)?;
drop(table_check);
table.update_file_in_place(fd, |f| unsafe {
// Safety: update_file_in_place will drop `f` after this call.
f.reopen_with_fdflags(FdFlags::from(&flags))
})
self.table()
.get_file_mut(u32::from(fd))?
.get_cap(FileCaps::FDSTAT_SET_FLAGS)?
.set_fdflags(FdFlags::from(&flags))
}
fn fd_fdstat_set_rights(