Skip to content

Commit

Permalink
Improve code reuse in polling, reduce cfg usage too
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark McCaskey committed Aug 29, 2019
1 parent fba6dbf commit 85bf0c3
Showing 1 changed file with 36 additions and 84 deletions.
120 changes: 36 additions & 84 deletions lib/wasi/src/state/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,26 +452,11 @@ impl WasiFile for HostFile {
std::fs::rename(&self.host_path, new_name).map_err(Into::into)
}

#[cfg(unix)]
fn bytes_available(&self) -> Result<usize, WasiFsError> {
use std::os::unix::io::AsRawFd;
let host_fd = self.inner.as_raw_fd();

let mut bytes_found = 0 as libc::c_int;
let result = unsafe { libc::ioctl(host_fd, libc::FIONREAD, &mut bytes_found) };

match result {
// success
0 => Ok(bytes_found.try_into().unwrap_or(0)),
libc::EBADF => Err(WasiFsError::InvalidFd),
libc::EFAULT => Err(WasiFsError::InvalidData),
libc::EINVAL => Err(WasiFsError::InvalidInput),
_ => Err(WasiFsError::IOError),
}
}
#[cfg(not(unix))]
fn bytes_available(&self) -> Result<usize, WasiFsError> {
unimplemented!("HostFile::bytes_available in WasiFile is not implemented for non-Unix-like targets yet");
// unwrap is safe because of get_raw_fd implementation
let host_fd = self.get_raw_fd().unwrap();

host_file_bytes_available(host_fd)
}

#[cfg(unix)]
Expand Down Expand Up @@ -514,6 +499,26 @@ impl From<io::Error> for WasiFsError {
}
}

#[cfg(unix)]
fn host_file_bytes_available(host_fd: i32) -> Result<usize, WasiFsError> {
let mut bytes_found = 0 as libc::c_int;
let result = unsafe { libc::ioctl(host_fd, libc::FIONREAD, &mut bytes_found) };

match result {
// success
0 => Ok(bytes_found.try_into().unwrap_or(0)),
libc::EBADF => Err(WasiFsError::InvalidFd),
libc::EFAULT => Err(WasiFsError::InvalidData),
libc::EINVAL => Err(WasiFsError::InvalidInput),
_ => Err(WasiFsError::IOError),
}
}

#[cfg(not(unix))]
fn host_file_bytes_available(raw_fd: i32) -> Result<usize, WasiFsError> {
unimplemented!("host_file_bytes_available not yet implemented for non-Unix-like targets. This probably means the program tried to use wasi::poll_oneoff")
}

#[derive(Debug)]
pub struct Stdout(pub std::io::Stdout);
impl Read for Stdout {
Expand Down Expand Up @@ -579,29 +584,11 @@ impl WasiFile for Stdout {
0
}

#[cfg(unix)]
fn bytes_available(&self) -> Result<usize, WasiFsError> {
use std::os::unix::io::AsRawFd;
let host_fd = self.0.as_raw_fd();

let mut bytes_found = 0 as libc::c_int;
// TODO: check that this makes sense
let result = unsafe { libc::ioctl(host_fd, libc::FIONREAD, &mut bytes_found) };

match result {
// success
0 => Ok(bytes_found.try_into().unwrap_or(0)),
libc::EBADF => Err(WasiFsError::InvalidFd),
libc::EFAULT => Err(WasiFsError::InvalidData),
libc::EINVAL => Err(WasiFsError::InvalidInput),
_ => Err(WasiFsError::IOError),
}
}
#[cfg(not(unix))]
fn bytes_available(&self) -> Result<usize, WasiFsError> {
unimplemented!(
"Stdout::bytes_available in WasiFile is not implemented for non-Unix-like targets yet"
);
// unwrap is safe because of get_raw_fd implementation
let host_fd = self.get_raw_fd().unwrap();

host_file_bytes_available(host_fd)
}

#[cfg(unix)]
Expand Down Expand Up @@ -683,29 +670,11 @@ impl WasiFile for Stderr {
0
}

#[cfg(unix)]
fn bytes_available(&self) -> Result<usize, WasiFsError> {
use std::os::unix::io::AsRawFd;
let host_fd = self.0.as_raw_fd();

let mut bytes_found = 0 as libc::c_int;
// TODO: check that this makes sense
let result = unsafe { libc::ioctl(host_fd, libc::FIONREAD, &mut bytes_found) };

match result {
// success
0 => Ok(bytes_found.try_into().unwrap_or(0)),
libc::EBADF => Err(WasiFsError::InvalidFd),
libc::EFAULT => Err(WasiFsError::InvalidData),
libc::EINVAL => Err(WasiFsError::InvalidInput),
_ => Err(WasiFsError::IOError),
}
}
#[cfg(not(unix))]
fn bytes_available(&self) -> Result<usize, WasiFsError> {
unimplemented!(
"Stderr::bytes_available in WasiFile is not implemented for non-Unix-like targets yet"
);
// unwrap is safe because of get_raw_fd implementation
let host_fd = self.get_raw_fd().unwrap();

host_file_bytes_available(host_fd)
}

#[cfg(unix)]
Expand Down Expand Up @@ -787,28 +756,11 @@ impl WasiFile for Stdin {
0
}

#[cfg(unix)]
fn bytes_available(&self) -> Result<usize, WasiFsError> {
use std::os::unix::io::AsRawFd;
let host_fd = self.0.as_raw_fd();

let mut bytes_found = 0 as libc::c_int;
let result = unsafe { libc::ioctl(host_fd, libc::FIONREAD, &mut bytes_found) };

match result {
// success
0 => Ok(bytes_found.try_into().unwrap_or(0)),
libc::EBADF => Err(WasiFsError::InvalidFd),
libc::EFAULT => Err(WasiFsError::InvalidData),
libc::EINVAL => Err(WasiFsError::InvalidInput),
_ => Err(WasiFsError::IOError),
}
}
#[cfg(not(unix))]
fn bytes_available(&self) -> Result<usize, WasiFsError> {
unimplemented!(
"Stdin::bytes_available in WasiFile is not implemented for non-Unix-like targets yet"
);
// unwrap is safe because of get_raw_fd implementation
let host_fd = self.get_raw_fd().unwrap();

host_file_bytes_available(host_fd)
}

#[cfg(unix)]
Expand Down

0 comments on commit 85bf0c3

Please sign in to comment.