Skip to content

Commit

Permalink
golf
Browse files Browse the repository at this point in the history
  • Loading branch information
pchickey committed Sep 9, 2020
1 parent 6cc6600 commit 12094d9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 33 deletions.
31 changes: 30 additions & 1 deletion crates/wasi-common/src/entry/fd.rs
Expand Up @@ -6,6 +6,7 @@ use crate::handle::{
use crate::sched::Timestamp;
use crate::{Error, Result};
use std::convert::TryInto;
use std::ops::{Deref, DerefMut};

impl Entry {
pub fn fd_advise(&self, offset: Filesize, len: Filesize, advice: Advice) -> Result<()> {
Expand Down Expand Up @@ -91,7 +92,6 @@ impl Entry {
mut iovs: Vec<wiggle::GuestSlice<u8>>,
offset: Filesize,
) -> Result<Size> {
use std::ops::DerefMut;
let required_rights = HandleRights::from_base(Rights::FD_READ);
let mut io_slices = iovs
.iter_mut()
Expand Down Expand Up @@ -137,4 +137,33 @@ impl Entry {

Ok(())
}

pub fn fd_pwrite(&self, iovs: Vec<wiggle::GuestSlice<u8>>, offset: Filesize) -> Result<Size> {
if offset > i64::max_value() as u64 {
return Err(Error::Io);
}
let required_rights = HandleRights::from_base(Rights::FD_WRITE | Rights::FD_SEEK);
let io_slices = iovs
.iter()
.map(|s| std::io::IoSlice::new(s.deref()))
.collect::<Vec<std::io::IoSlice>>();
let host_nread = self
.as_handle(&required_rights)?
.pwritev(&io_slices, offset)?
.try_into()?;
Ok(host_nread)
}

pub fn fd_read(&self, mut iovs: Vec<wiggle::GuestSlice<u8>>) -> Result<Size> {
let required_rights = HandleRights::from_base(Rights::FD_READ);
let mut io_slices = iovs
.iter_mut()
.map(|s| std::io::IoSliceMut::new(s.deref_mut()))
.collect::<Vec<std::io::IoSliceMut>>();
let host_nread = self
.as_handle(&required_rights)?
.read_vectored(&mut io_slices)?
.try_into()?;
Ok(host_nread)
}
}
36 changes: 4 additions & 32 deletions crates/wasi-common/src/snapshots/wasi_snapshot_preview1.rs
Expand Up @@ -123,6 +123,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
iovs: &types::IovecArray<'_>,
offset: types::Filesize,
) -> Result<types::Size> {
let entry = self.get_entry(fd)?;
// Rather than expose the details of our IovecArray to the Entry, it accepts a
// Vec<GuestSlice<u8>>
let mut guest_slices: Vec<GuestSlice<'_, u8>> = Vec::new();
Expand All @@ -132,7 +133,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
guest_slices.push(iov.buf.as_array(iov.buf_len).as_slice()?);
}

self.get_entry(fd)?.fd_pread(guest_slices, offset)
entry.fd_pread(guest_slices, offset)
}

fn fd_prestat_get(&self, fd: types::Fd) -> Result<types::Prestat> {
Expand Down Expand Up @@ -163,23 +164,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
guest_slices.push(ciov.buf.as_array(ciov.buf_len).as_slice()?);
}

let required_rights =
HandleRights::from_base(types::Rights::FD_WRITE | types::Rights::FD_SEEK);
let entry = self.get_entry(fd)?;

if offset > i64::max_value() as u64 {
return Err(Error::Io);
}

let host_nwritten = {
let buf: Vec<io::IoSlice> =
guest_slices.iter().map(|s| io::IoSlice::new(&*s)).collect();
entry
.as_handle(&required_rights)?
.pwritev(&buf, offset)?
.try_into()?
};
Ok(host_nwritten)
self.get_entry(fd)?.fd_pwrite(guest_slices, offset)
}

fn fd_read(&self, fd: types::Fd, iovs: &types::IovecArray<'_>) -> Result<types::Size> {
Expand All @@ -190,20 +175,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
guest_slices.push(iov.buf.as_array(iov.buf_len).as_slice()?);
}

let required_rights = HandleRights::from_base(types::Rights::FD_READ);
let entry = self.get_entry(fd)?;
let host_nread = {
let mut slices: Vec<io::IoSliceMut> = guest_slices
.iter_mut()
.map(|s| io::IoSliceMut::new(&mut *s))
.collect();
entry
.as_handle(&required_rights)?
.read_vectored(&mut slices)?
.try_into()?
};

Ok(host_nread)
self.get_entry(fd)?.fd_read(guest_slices)
}

fn fd_readdir(
Expand Down

0 comments on commit 12094d9

Please sign in to comment.