Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov committed Aug 21, 2019
1 parent 88fd945 commit a47e3c0
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 74 deletions.
6 changes: 3 additions & 3 deletions src/libstd/sys/wasi/args.rs
@@ -1,10 +1,10 @@
use crate::ffi::CStr;
use crate::io;
use crate::ffi::OsString;
use crate::marker::PhantomData;
use crate::os::wasi::ffi::OsStringExt;
use crate::vec;

use ::wasi::wasi_unstable as wasi;

pub unsafe fn init(_argc: isize, _argv: *const *const u8) {
}

Expand All @@ -20,7 +20,7 @@ pub struct Args {
pub fn args() -> Args {
let buf = wasi::args_sizes_get().and_then(|args_sizes| {
let mut buf = Vec::with_capacity(args_sizes.get_count());
wasi::get_args(args_sizes, |arg| {
wasi::args_get(args_sizes, |arg| {
let arg = OsString::from_vec(arg.to_vec());
buf.push(arg);
})?;
Expand Down
2 changes: 2 additions & 0 deletions src/libstd/sys/wasi/ext/fs.rs
Expand Up @@ -8,6 +8,8 @@ use crate::os::wasi::ffi::OsStrExt;
use crate::path::{Path, PathBuf};
use crate::sys_common::{AsInner, AsInnerMut, FromInner};

use ::wasi::wasi_unstable as wasi;

/// WASI-specific extensions to [`File`].
///
/// [`File`]: ../../../../std/fs/struct.File.html
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/sys/wasi/ext/io.rs
Expand Up @@ -8,7 +8,7 @@ use crate::sys;
use crate::net;
use crate::sys_common::{AsInner, FromInner, IntoInner};

use wasi::wasi_unstable as wasi;
use ::wasi::wasi_unstable as wasi;

/// Raw file descriptors.
pub type RawFd = u32;
Expand Down
76 changes: 38 additions & 38 deletions src/libstd/sys/wasi/fd.rs
Expand Up @@ -3,14 +3,15 @@
use crate::io::{self, IoSlice, IoSliceMut, SeekFrom};
use crate::mem;
use crate::net::Shutdown;
use wasi::wasi_unstable as wasi;
use super::err2io;
use ::wasi::wasi_unstable as wasi;

#[derive(Debug)]
pub struct WasiFd {
fd: wasi::Fd,
}

fn iovec(a: &mut [IoSliceMut<'_>]) -> &[wasi::IoVec] {
fn iovec<'a>(a: &'a mut [IoSliceMut<'_>]) -> &'a [wasi::IoVec] {
assert_eq!(
mem::size_of::<IoSliceMut<'_>>(),
mem::size_of::<wasi::IoVec>()
Expand All @@ -23,7 +24,7 @@ fn iovec(a: &mut [IoSliceMut<'_>]) -> &[wasi::IoVec] {
unsafe { mem::transmute(a) }
}

fn ciovec(a: &[IoSlice<'_>]) -> &[wasi::CIoVec] {
fn ciovec<'a>(a: &'a [IoSlice<'_>]) -> &'a [wasi::CIoVec] {
assert_eq!(
mem::size_of::<IoSlice<'_>>(),
mem::size_of::<wasi::CIoVec>()
Expand Down Expand Up @@ -52,23 +53,23 @@ impl WasiFd {
}

pub fn datasync(&self) -> io::Result<()> {
wasi::fd_datasync(self.fd).map_err(From::from)
wasi::fd_datasync(self.fd).map_err(err2io)
}

pub fn pread(&self, bufs: &mut [IoSliceMut<'_>], offset: u64) -> io::Result<usize> {
wasi::fd_pread(self.fd, iovec(bufs), offset).map_err(From::from)
wasi::fd_pread(self.fd, iovec(bufs), offset).map_err(err2io)
}

pub fn pwrite(&self, bufs: &[IoSlice<'_>], offset: u64) -> io::Result<usize> {
wasi::fd_pwrite(self.fd, ciovec(bufs), offset).map_err(From::from)
wasi::fd_pwrite(self.fd, ciovec(bufs), offset).map_err(err2io)
}

pub fn read(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
wasi::fd_read(self.fd, iovec(bufs)).map_err(From::from)
wasi::fd_read(self.fd, iovec(bufs)).map_err(err2io)
}

pub fn write(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
wasi::fd_write(self.fd, ciovec(bufs)).map_err(From::from)
wasi::fd_write(self.fd, ciovec(bufs)).map_err(err2io)
}

pub fn seek(&self, pos: SeekFrom) -> io::Result<u64> {
Expand All @@ -77,37 +78,37 @@ impl WasiFd {
SeekFrom::End(pos) => (wasi::WHENCE_END, pos),
SeekFrom::Current(pos) => (wasi::WHENCE_CUR, pos),
};
wasi::fd_seek(self.fd, offset, whence).map_err(From::from)
wasi::fd_seek(self.fd, offset, whence).map_err(err2io)
}

pub fn tell(&self) -> io::Result<u64> {
wasi::fd_tell(self.fd).map_err(From::from)
wasi::fd_tell(self.fd).map_err(err2io)
}

// FIXME: __wasi_fd_fdstat_get

pub fn set_flags(&self, flags: wasi::FdFlags) -> io::Result<()> {
wasi::fd_fdstat_set_flags(self.fd, flags).map_err(From::from)
wasi::fd_fdstat_set_flags(self.fd, flags).map_err(err2io)
}

pub fn set_rights(&self, base: wasi::Rights, inheriting: wasi::Rights) -> io::Result<()> {
wasi::fd_fdstat_set_rights(self.fd, base, inheriting).map_err(From::from)
wasi::fd_fdstat_set_rights(self.fd, base, inheriting).map_err(err2io)
}

pub fn sync(&self) -> io::Result<()> {
wasi::fd_sync(self.fd).map_err(From::from)
wasi::fd_sync(self.fd).map_err(err2io)
}

pub fn advise(&self, offset: u64, len: u64, advice: wasi::Advice) -> io::Result<()> {
wasi::fd_advise(self.fd, offset, len, advice).map_err(From::from)
wasi::fd_advise(self.fd, offset, len, advice).map_err(err2io)
}

pub fn allocate(&self, offset: u64, len: u64) -> io::Result<()> {
wasi::fd_allocate(self.fd, offset, len).map_err(From::from)
wasi::fd_allocate(self.fd, offset, len).map_err(err2io)
}

pub fn create_directory(&self, path: &[u8]) -> io::Result<()> {
wasi::path_create_directory(self.fd, path).map_err(From::from)
wasi::path_create_directory(self.fd, path).map_err(err2io)
}

pub fn link(
Expand All @@ -118,7 +119,7 @@ impl WasiFd {
new_path: &[u8],
) -> io::Result<()> {
wasi::path_link(self.fd, old_flags, old_path, new_fd.fd, new_path)
.map_err(From::from)
.map_err(err2io)
}

pub fn open(
Expand All @@ -130,33 +131,32 @@ impl WasiFd {
fs_rights_inheriting: wasi::Rights,
fs_flags: wasi::FdFlags,
) -> io::Result<WasiFd> {
let fd = wasi_path_open(
wasi::path_open(
self.fd,
dirflags,
path,
oflags,
fs_rights_base,
fs_rights_inheriting,
fs_flags,
)?;
Ok(WasiFd::from_raw(fd))
).map(|fd| unsafe { WasiFd::from_raw(fd) }).map_err(err2io)
}

pub fn readdir(&self, buf: &mut [u8], cookie: wasi::DirCookie) -> io::Result<usize> {
wasi::fd_readdir(self.fd, buf, cookie).map_err(From::from)
wasi::fd_readdir(self.fd, buf, cookie).map_err(err2io)
}

pub fn readlink(&self, path: &[u8], buf: &mut [u8]) -> io::Result<usize> {
wasi::path_readlink(self.fd, path, buf).map_err(From::from)
wasi::path_readlink(self.fd, path, buf).map_err(err2io)
}

pub fn rename(&self, old_path: &[u8], new_fd: &WasiFd, new_path: &[u8]) -> io::Result<()> {
wasi::path_rename(self.fd, old_path, new_fd.fd, new_path)
.map_err(From::from)
.map_err(err2io)
}

pub fn filestat_get(&self) -> io::Result<wasi::Filestat> {
wasi::fd_filestat_get(self.fd, buf).map_err(From::from)
pub fn filestat_get(&self) -> io::Result<wasi::FileStat> {
wasi::fd_filestat_get(self.fd).map_err(err2io)
}

pub fn filestat_set_times(
Expand All @@ -166,19 +166,19 @@ impl WasiFd {
fstflags: wasi::FstFlags,
) -> io::Result<()> {
wasi::fd_filestat_set_times(self.fd, atim, mtim, fstflags)
.map_err(From::from)
.map_err(err2io)
}

pub fn filestat_set_size(&self, size: u64) -> io::Result<()> {
wasi::fd_filestat_set_size(self.fd, size).map_err(From::from)
wasi::fd_filestat_set_size(self.fd, size).map_err(err2io)
}

pub fn path_filestat_get(
&self,
flags: wasi::LookupFlags,
path: &[u8],
) -> io::Result<wasi::FileStat> {
wasi::path_filestat_get(self.fd, flags, path).map_err(From::from)
wasi::path_filestat_get(self.fd, flags, path).map_err(err2io)
}

pub fn path_filestat_set_times(
Expand All @@ -196,40 +196,40 @@ impl WasiFd {
atim,
mtim,
fstflags,
).map_err(From::from)
).map_err(err2io)
}

pub fn symlink(&self, old_path: &[u8], new_path: &[u8]) -> io::Result<()> {
wasi::path_symlink(old_path, self.fd, new_path).map_err(From::from)
wasi::path_symlink(old_path, self.fd, new_path).map_err(err2io)
}

pub fn unlink_file(&self, path: &[u8]) -> io::Result<()> {
wasi::path_unlink_file(self.fd, path).map_err(From::from)
wasi::path_unlink_file(self.fd, path).map_err(err2io)
}

pub fn remove_directory(&self, path: &[u8]) -> io::Result<()> {
wasi::path_remove_directory(self.fd, path).map_err(From::from)
wasi::path_remove_directory(self.fd, path).map_err(err2io)
}

pub fn sock_recv(
&self,
ri_data: &mut [IoSliceMut<'_>],
ri_flags: wasi::RiFlags,
) -> io::Result<(usize, wasi::RoFlags)> {
wasi::sock_recv(self.fd, iovec(ri_data), ri_flags).map_err(From::from)
wasi::sock_recv(self.fd, iovec(ri_data), ri_flags).map_err(err2io)
}

pub fn sock_send(&self, si_data: &[IoSlice<'_>], si_flags: wasi::SiFlags) -> io::Result<usize> {
wasi::sock_send(self.fd, ciovec(si_data), si_flags).map_err(From::from)
wasi::sock_send(self.fd, ciovec(si_data), si_flags).map_err(err2io)
}

pub fn sock_shutdown(&self, how: Shutdown) -> io::Result<()> {
let how = match how {
Shutdown::Read => WASI::SHUT_RD,
Shutdown::Write => WASI::SHUT_WR,
Shutdown::Both => WASI::SHUT_WR | WASI::SHUT_RD,
Shutdown::Read => wasi::SHUT_RD,
Shutdown::Write => wasi::SHUT_WR,
Shutdown::Both => wasi::SHUT_WR | wasi::SHUT_RD,
};
wasi::sock_shutdown(self.fd, how).map_err(From::from)
wasi::sock_shutdown(self.fd, how).map_err(err2io)
}
}

Expand Down
16 changes: 5 additions & 11 deletions src/libstd/sys/wasi/fs.rs
Expand Up @@ -7,15 +7,15 @@ use crate::os::wasi::ffi::{OsStrExt, OsStringExt};
use crate::path::{Path, PathBuf};
use crate::ptr;
use crate::sync::Arc;
use crate::sys::fd::{DirCookie, WasiFd};
use crate::sys::fd::WasiFd;
use crate::sys::time::SystemTime;
use crate::sys::unsupported;
use crate::sys_common::FromInner;

pub use crate::sys_common::fs::copy;
pub use crate::sys_common::fs::remove_dir_all;

use wasi::wasi_unstable as wasi;
use ::wasi::wasi_unstable as wasi;

pub struct File {
fd: WasiFd,
Expand All @@ -28,7 +28,7 @@ pub struct FileAttr {

pub struct ReadDir {
inner: Arc<ReadDirInner>,
cookie: Option<DirCookie>,
cookie: Option<wasi::DirCookie>,
buf: Vec<u8>,
offset: usize,
cap: usize,
Expand Down Expand Up @@ -70,12 +70,6 @@ pub struct FileType {
pub struct DirBuilder {}

impl FileAttr {
fn zero() -> FileAttr {
FileAttr {
meta: unsafe { mem::zeroed() },
}
}

pub fn size(&self) -> u64 {
self.meta.st_size
}
Expand Down Expand Up @@ -390,7 +384,7 @@ impl File {
}

pub fn file_attr(&self) -> io::Result<FileAttr> {
self.fd.filestat_get().map_ok(|meta| FileAttr { meta })
self.fd.filestat_get().map(|meta| FileAttr { meta })
}

pub fn metadata_at(
Expand Down Expand Up @@ -601,7 +595,7 @@ fn metadata_at(
path: &Path,
) -> io::Result<FileAttr> {
fd.path_filestat_get(flags, path.as_os_str().as_bytes())
.map_ok(|meta| FileAttr { meta })
.map(|meta| FileAttr { meta })
}

pub fn canonicalize(_p: &Path) -> io::Result<PathBuf> {
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/sys/wasi/io.rs
@@ -1,7 +1,7 @@
use crate::marker::PhantomData;
use crate::slice;

use wasi::wasi_unstable as wasi;
use ::wasi::wasi_unstable as wasi;
use core::ffi::c_void;

#[repr(transparent)]
Expand Down
27 changes: 14 additions & 13 deletions src/libstd/sys/wasi/mod.rs
Expand Up @@ -14,10 +14,10 @@
//! compiling for wasm. That way it's a compile time error for something that's
//! guaranteed to be a runtime error!

use crate::io;
use crate::io as std_io;
use crate::mem;
use crate::os::raw::c_char;
use wasi::wasi_unstable as wasi;
use ::wasi::wasi_unstable as wasi;

pub mod alloc;
pub mod args;
Expand Down Expand Up @@ -56,16 +56,19 @@ pub mod ext;
pub fn init() {
}

pub fn unsupported<T>() -> crate::io::Result<T> {
pub fn unsupported<T>() -> std_io::Result<T> {
Err(unsupported_err())
}

pub fn unsupported_err() -> io::Error {
io::Error::new(io::ErrorKind::Other, "operation not supported on wasm yet")
pub fn unsupported_err() -> std_io::Error {
std_io::Error::new(
std_io::ErrorKind::Other,
"operation not supported on wasm yet",
)
}

pub fn decode_error_kind(_code: i32) -> io::ErrorKind {
io::ErrorKind::Other
pub fn decode_error_kind(_code: i32) -> std_io::ErrorKind {
std_io::ErrorKind::Other
}

// This enum is used as the storage for a bunch of types which can't actually
Expand Down Expand Up @@ -114,16 +117,14 @@ macro_rules! impl_is_minus_one {

impl_is_minus_one! { i8 i16 i32 i64 isize }

pub fn cvt<T: IsMinusOne>(t: T) -> crate::io::Result<T> {
pub fn cvt<T: IsMinusOne>(t: T) -> std_io::Result<T> {
if t.is_minus_one() {
Err(io::Error::last_os_error())
Err(std_io::Error::last_os_error())
} else {
Ok(t)
}
}

impl From<wasi::Error> for io::Error {
fn from(err: wasi::Error) -> Self {
Self::from_raw_os_error(err as i32)
}
fn err2io(err: wasi::Error) -> std_io::Error {
std_io::Error::from_raw_os_error(err.get() as i32)
}

0 comments on commit a47e3c0

Please sign in to comment.