Skip to content

Commit

Permalink
review: back out no-longer-needed changes from bytecodealliance#5326
Browse files Browse the repository at this point in the history
Co-authored-by: Alex Crichton <[email protected]>
  • Loading branch information
abrown and alexcrichton committed Feb 7, 2023
1 parent 3923526 commit 2959abc
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 110 deletions.
125 changes: 53 additions & 72 deletions crates/wasi-common/cap-std-sync/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use is_terminal::IsTerminal;
use std::any::Any;
use std::convert::TryInto;
use std::io;
use std::sync::{Arc, RwLock, RwLockReadGuard};
use system_interface::{
fs::{FileIoExt, GetSetFdFlags},
io::{IoExt, ReadReady},
Expand All @@ -15,48 +14,11 @@ use wasi_common::{
Error, ErrorExt,
};

#[cfg(unix)]
use io_lifetimes::{AsFd, BorrowedFd};

#[cfg(windows)]
use io_lifetimes::{AsHandle, BorrowedHandle};

#[cfg(windows)]
use io_extras::os::windows::{AsRawHandleOrSocket, RawHandleOrSocket};

pub struct BorrowedFile<'a>(RwLockReadGuard<'a, cap_std::fs::File>);

#[cfg(unix)]
impl AsFd for BorrowedFile<'_> {
fn as_fd(&self) -> BorrowedFd<'_> {
self.0.as_fd()
}
}

#[cfg(windows)]
impl AsHandle for BorrowedFile<'_> {
fn as_handle(&self) -> BorrowedHandle<'_> {
self.0.as_handle()
}
}

#[cfg(windows)]
impl AsRawHandleOrSocket for BorrowedFile<'_> {
#[inline]
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
self.0.as_raw_handle_or_socket()
}
}

pub struct File(RwLock<cap_std::fs::File>);
pub struct File(cap_std::fs::File);

impl File {
pub fn from_cap_std(file: cap_std::fs::File) -> Self {
File(RwLock::new(file))
}

pub fn borrow(&self) -> BorrowedFile {
BorrowedFile(self.0.read().unwrap())
File(file)
}
}

Expand All @@ -65,32 +27,28 @@ impl WasiFile for File {
fn as_any(&self) -> &dyn Any {
self
}

#[cfg(unix)]
fn pollable(&self) -> Option<Arc<dyn AsFd + '_>> {
Some(Arc::new(self.borrow()))
fn pollable(&self) -> Option<rustix::fd::BorrowedFd> {
Some(self.0.as_fd())
}

#[cfg(windows)]
fn pollable(&self) -> Option<Arc<dyn AsRawHandleOrSocket + '_>> {
Some(Arc::new(BorrowedFile(self.0.read().unwrap())))
fn pollable(&self) -> Option<io_extras::os::windows::RawHandleOrSocket> {
Some(self.0.as_raw_handle_or_socket())
}

async fn datasync(&self) -> Result<(), Error> {
self.0.read().unwrap().sync_data()?;
self.0.sync_data()?;
Ok(())
}
async fn sync(&self) -> Result<(), Error> {
self.0.read().unwrap().sync_all()?;
self.0.sync_all()?;
Ok(())
}
async fn get_filetype(&self) -> Result<FileType, Error> {
let meta = self.0.read().unwrap().metadata()?;
let meta = self.0.metadata()?;
Ok(filetype_from(&meta.file_type()))
}
async fn get_fdflags(&self) -> Result<FdFlags, Error> {
let file = self.0.read().unwrap();
let fdflags = get_fd_flags(&*file)?;
let fdflags = get_fd_flags(&self.0)?;
Ok(fdflags)
}
async fn set_fdflags(&mut self, fdflags: FdFlags) -> Result<(), Error> {
Expand All @@ -101,13 +59,12 @@ impl WasiFile for File {
) {
return Err(Error::invalid_argument().context("cannot set DSYNC, SYNC, or RSYNC flag"));
}
let file = self.0.get_mut().unwrap();
let set_fd_flags = (*file).new_set_fd_flags(to_sysif_fdflags(fdflags))?;
(*file).set_fd_flags(set_fd_flags)?;
let set_fd_flags = self.0.new_set_fd_flags(to_sysif_fdflags(fdflags))?;
self.0.set_fd_flags(set_fd_flags)?;
Ok(())
}
async fn get_filestat(&self) -> Result<Filestat, Error> {
let meta = self.0.read().unwrap().metadata()?;
let meta = self.0.metadata()?;
Ok(Filestat {
device_id: meta.dev(),
inode: meta.ino(),
Expand All @@ -120,18 +77,15 @@ impl WasiFile for File {
})
}
async fn set_filestat_size(&self, size: u64) -> Result<(), Error> {
self.0.read().unwrap().set_len(size)?;
self.0.set_len(size)?;
Ok(())
}
async fn advise(&self, offset: u64, len: u64, advice: Advice) -> Result<(), Error> {
self.0
.read()
.unwrap()
.advise(offset, len, convert_advice(advice))?;
self.0.advise(offset, len, convert_advice(advice))?;
Ok(())
}
async fn allocate(&self, offset: u64, len: u64) -> Result<(), Error> {
self.0.read().unwrap().allocate(offset, len)?;
self.0.allocate(offset, len)?;
Ok(())
}
async fn set_times(
Expand All @@ -140,47 +94,45 @@ impl WasiFile for File {
mtime: Option<wasi_common::SystemTimeSpec>,
) -> Result<(), Error> {
self.0
.read()
.unwrap()
.set_times(convert_systimespec(atime), convert_systimespec(mtime))?;
Ok(())
}
async fn read_vectored<'a>(&self, bufs: &mut [io::IoSliceMut<'a>]) -> Result<u64, Error> {
let n = self.0.read().unwrap().read_vectored(bufs)?;
let n = self.0.read_vectored(bufs)?;
Ok(n.try_into()?)
}
async fn read_vectored_at<'a>(
&self,
bufs: &mut [io::IoSliceMut<'a>],
offset: u64,
) -> Result<u64, Error> {
let n = self.0.read().unwrap().read_vectored_at(bufs, offset)?;
let n = self.0.read_vectored_at(bufs, offset)?;
Ok(n.try_into()?)
}
async fn write_vectored<'a>(&self, bufs: &[io::IoSlice<'a>]) -> Result<u64, Error> {
let n = self.0.read().unwrap().write_vectored(bufs)?;
let n = self.0.write_vectored(bufs)?;
Ok(n.try_into()?)
}
async fn write_vectored_at<'a>(
&self,
bufs: &[io::IoSlice<'a>],
offset: u64,
) -> Result<u64, Error> {
let n = self.0.read().unwrap().write_vectored_at(bufs, offset)?;
let n = self.0.write_vectored_at(bufs, offset)?;
Ok(n.try_into()?)
}
async fn seek(&self, pos: std::io::SeekFrom) -> Result<u64, Error> {
Ok(self.0.read().unwrap().seek(pos)?)
Ok(self.0.seek(pos)?)
}
async fn peek(&self, buf: &mut [u8]) -> Result<u64, Error> {
let n = self.0.read().unwrap().peek(buf)?;
let n = self.0.peek(buf)?;
Ok(n.try_into()?)
}
fn num_ready_bytes(&self) -> Result<u64, Error> {
Ok(self.0.read().unwrap().num_ready_bytes()?)
Ok(self.0.num_ready_bytes()?)
}
fn isatty(&self) -> bool {
self.0.read().unwrap().is_terminal()
self.0.is_terminal()
}
}

Expand All @@ -207,6 +159,35 @@ pub fn filetype_from(ft: &cap_std::fs::FileType) -> FileType {
}
}

#[cfg(windows)]
use io_lifetimes::{AsHandle, BorrowedHandle};
#[cfg(windows)]
impl AsHandle for File {
fn as_handle(&self) -> BorrowedHandle<'_> {
self.0.as_handle()
}
}

#[cfg(windows)]
use io_extras::os::windows::{AsRawHandleOrSocket, RawHandleOrSocket};
#[cfg(windows)]
impl AsRawHandleOrSocket for File {
#[inline]
fn as_raw_handle_or_socket(&self) -> RawHandleOrSocket {
self.0.as_raw_handle_or_socket()
}
}

#[cfg(unix)]
use io_lifetimes::{AsFd, BorrowedFd};

#[cfg(unix)]
impl AsFd for File {
fn as_fd(&self) -> BorrowedFd<'_> {
self.0.as_fd()
}
}

pub(crate) fn convert_systimespec(
t: Option<wasi_common::SystemTimeSpec>,
) -> Option<SystemTimeSpec> {
Expand Down
22 changes: 8 additions & 14 deletions crates/wasi-common/cap-std-sync/src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use io_lifetimes::{AsSocket, BorrowedSocket};
use std::any::Any;
use std::convert::TryInto;
use std::io;
use std::sync::Arc;
#[cfg(unix)]
use system_interface::fs::GetSetFdFlags;
use system_interface::io::IoExt;
Expand All @@ -19,11 +18,6 @@ use wasi_common::{
Error, ErrorExt,
};

#[cfg(unix)]
use wasi_common::file::BorrowedAsFd;
#[cfg(windows)]
use wasi_common::file::BorrowedAsRawHandleOrSocket;

pub enum Socket {
TcpListener(cap_std::net::TcpListener),
TcpStream(cap_std::net::TcpStream),
Expand Down Expand Up @@ -89,12 +83,12 @@ macro_rules! wasi_listen_write_impl {
self
}
#[cfg(unix)]
fn pollable(&self) -> Option<Arc<dyn AsFd + '_>> {
Some(Arc::new(BorrowedAsFd::new(&self.0)))
fn pollable(&self) -> Option<rustix::fd::BorrowedFd> {
Some(self.0.as_fd())
}
#[cfg(windows)]
fn pollable(&self) -> Option<Arc<dyn AsRawHandleOrSocket + '_>> {
Some(Arc::new(BorrowedAsRawHandleOrSocket::new(&self.0)))
fn pollable(&self) -> Option<io_extras::os::windows::RawHandleOrSocket> {
Some(self.0.as_raw_handle_or_socket())
}
async fn sock_accept(&self, fdflags: FdFlags) -> Result<Box<dyn WasiFile>, Error> {
let (stream, _) = self.0.accept()?;
Expand Down Expand Up @@ -182,12 +176,12 @@ macro_rules! wasi_stream_write_impl {
self
}
#[cfg(unix)]
fn pollable(&self) -> Option<Arc<dyn AsFd + '_>> {
Some(Arc::new(BorrowedAsFd::new(&self.0)))
fn pollable(&self) -> Option<rustix::fd::BorrowedFd> {
Some(self.0.as_fd())
}
#[cfg(windows)]
fn pollable(&self) -> Option<Arc<dyn AsRawHandleOrSocket + '_>> {
Some(Arc::new(BorrowedAsRawHandleOrSocket::new(&self.0)))
fn pollable(&self) -> Option<io_extras::os::windows::RawHandleOrSocket> {
Some(self.0.as_raw_handle_or_socket())
}
async fn get_filetype(&self) -> Result<FileType, Error> {
Ok(FileType::SocketStream)
Expand Down
11 changes: 3 additions & 8 deletions crates/wasi-common/cap-std-sync/src/sched/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,28 @@ pub async fn poll_oneoff<'a>(poll: &mut Poll<'a>) -> Result<(), Error> {
if poll.is_empty() {
return Ok(());
}
let mut poll_as_fds = Vec::new();
let mut pollfds = Vec::new();
for s in poll.rw_subscriptions() {
match s {
Subscription::Read(f) => {
let fd = f
.file
.pollable()
.ok_or(Error::invalid_argument().context("file is not pollable"))?;
poll_as_fds.push((fd, PollFlags::IN));
pollfds.push(PollFd::from_borrowed_fd(fd, PollFlags::IN));
}

Subscription::Write(f) => {
let fd = f
.file
.pollable()
.ok_or(Error::invalid_argument().context("file is not pollable"))?;
poll_as_fds.push((fd, PollFlags::OUT));
pollfds.push(PollFd::from_borrowed_fd(fd, PollFlags::OUT));
}
Subscription::MonotonicClock { .. } => unreachable!(),
}
}

let mut pollfds = poll_as_fds
.iter()
.map(|(fd, events)| PollFd::from_borrowed_fd(fd.as_fd(), events.clone()))
.collect::<Vec<_>>();

let ready = loop {
let poll_timeout = if let Some(t) = poll.earliest_clock_deadline() {
let duration = t.duration_until().unwrap_or(Duration::from_secs(0));
Expand Down
22 changes: 8 additions & 14 deletions crates/wasi-common/cap-std-sync/src/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,8 @@ use std::convert::TryInto;
use std::fs::File;
use std::io;
use std::io::{Read, Write};
use std::sync::Arc;
use system_interface::io::ReadReady;

#[cfg(unix)]
use wasi_common::file::BorrowedAsFd;
#[cfg(windows)]
use wasi_common::file::BorrowedAsRawHandleOrSocket;

#[cfg(windows)]
use io_extras::os::windows::{AsRawHandleOrSocket, RawHandleOrSocket};
#[cfg(unix)]
Expand All @@ -39,13 +33,13 @@ impl WasiFile for Stdin {
}

#[cfg(unix)]
fn pollable(&self) -> Option<Arc<dyn AsFd + '_>> {
Some(Arc::new(BorrowedAsFd::new(&self.0)))
fn pollable(&self) -> Option<rustix::fd::BorrowedFd> {
Some(self.0.as_fd())
}

#[cfg(windows)]
fn pollable(&self) -> Option<Arc<dyn AsRawHandleOrSocket + '_>> {
Some(Arc::new(BorrowedAsRawHandleOrSocket::new(&self.0)))
fn pollable(&self) -> Option<io_extras::os::windows::RawHandleOrSocket> {
Some(self.0.as_raw_handle_or_socket())
}

async fn get_filetype(&self) -> Result<FileType, Error> {
Expand Down Expand Up @@ -116,12 +110,12 @@ macro_rules! wasi_file_write_impl {
self
}
#[cfg(unix)]
fn pollable(&self) -> Option<Arc<dyn AsFd + '_>> {
Some(Arc::new(BorrowedAsFd::new(&self.0)))
fn pollable(&self) -> Option<rustix::fd::BorrowedFd> {
Some(self.0.as_fd())
}
#[cfg(windows)]
fn pollable(&self) -> Option<Arc<dyn AsRawHandleOrSocket + '_>> {
Some(Arc::new(BorrowedAsRawHandleOrSocket::new(&self.0)))
fn pollable(&self) -> Option<io_extras::os::windows::RawHandleOrSocket> {
Some(self.0.as_raw_handle_or_socket())
}
async fn get_filetype(&self) -> Result<FileType, Error> {
if self.isatty() {
Expand Down
4 changes: 2 additions & 2 deletions crates/wasi-common/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ pub trait WasiFile: Send + Sync {
async fn get_filetype(&self) -> Result<FileType, Error>;

#[cfg(unix)]
fn pollable(&self) -> Option<Arc<dyn AsFd + '_>> {
fn pollable(&self) -> Option<rustix::fd::BorrowedFd> {
None
}

#[cfg(windows)]
fn pollable(&self) -> Option<Arc<dyn AsRawHandleOrSocket + '_>> {
fn pollable(&self) -> Option<io_extras::os::windows::RawHandleOrSocket> {
None
}

Expand Down

0 comments on commit 2959abc

Please sign in to comment.