sys: import types from handle or sched, not wasi. drop types:: prefix.

This commit is contained in:
Pat Hickey
2020-09-14 16:32:35 -07:00
parent e5129b39cb
commit 4763678be2
23 changed files with 328 additions and 362 deletions

View File

@@ -1,17 +1,17 @@
use crate::wasi::types;
use crate::sched::{Clockid, Timestamp};
use crate::{Error, Result};
use yanix::clock::{clock_getres, clock_gettime, ClockId};
pub(crate) fn res_get(clock_id: types::Clockid) -> Result<types::Timestamp> {
pub(crate) fn res_get(clock_id: Clockid) -> Result<Timestamp> {
let clock_id: ClockId = clock_id.into();
let timespec = clock_getres(clock_id)?;
// convert to nanoseconds, returning EOVERFLOW in case of overflow;
// this is freelancing a bit from the spec but seems like it'll
// be an unusual situation to hit
(timespec.tv_sec as types::Timestamp)
(timespec.tv_sec as Timestamp)
.checked_mul(1_000_000_000)
.and_then(|sec_ns| sec_ns.checked_add(timespec.tv_nsec as types::Timestamp))
.and_then(|sec_ns| sec_ns.checked_add(timespec.tv_nsec as Timestamp))
.map_or(Err(Error::Overflow), |resolution| {
// a supported clock can never return zero; this case will probably never get hit, but
// make sure we follow the spec
@@ -23,14 +23,14 @@ pub(crate) fn res_get(clock_id: types::Clockid) -> Result<types::Timestamp> {
})
}
pub(crate) fn time_get(clock_id: types::Clockid) -> Result<types::Timestamp> {
pub(crate) fn time_get(clock_id: Clockid) -> Result<Timestamp> {
let clock_id: ClockId = clock_id.into();
let timespec = clock_gettime(clock_id)?;
// convert to nanoseconds, returning EOVERFLOW in case of overflow; this is freelancing a bit
// from the spec but seems like it'll be an unusual situation to hit
(timespec.tv_sec as types::Timestamp)
(timespec.tv_sec as Timestamp)
.checked_mul(1_000_000_000)
.and_then(|sec_ns| sec_ns.checked_add(timespec.tv_nsec as types::Timestamp))
.and_then(|sec_ns| sec_ns.checked_add(timespec.tv_nsec as Timestamp))
.map_or(Err(Error::Overflow), Ok)
}

View File

@@ -1,18 +1,18 @@
use super::oshandle::RawOsHandle;
use crate::handle::{Advice, Dircookie, Dirent, Fdflags, Filesize, Filestat, DIRCOOKIE_START};
use crate::sys::osdir::OsDir;
use crate::sys::osfile::OsFile;
use crate::wasi::{self, types};
use crate::Result;
use std::convert::TryInto;
use std::fs::File;
use std::os::unix::prelude::AsRawFd;
pub(crate) fn fdstat_get(fd: &File) -> Result<types::Fdflags> {
pub(crate) fn fdstat_get(fd: &File) -> Result<Fdflags> {
let fdflags = unsafe { yanix::fcntl::get_status_flags(fd.as_raw_fd())? };
Ok(fdflags.into())
}
pub(crate) fn fdstat_set_flags(fd: &File, fdflags: types::Fdflags) -> Result<Option<RawOsHandle>> {
pub(crate) fn fdstat_set_flags(fd: &File, fdflags: Fdflags) -> Result<Option<RawOsHandle>> {
unsafe { yanix::fcntl::set_status_flags(fd.as_raw_fd(), fdflags.into())? };
// We return None here to signal that the operation succeeded on the original
// file descriptor and mutating the original WASI Descriptor is thus unnecessary.
@@ -20,28 +20,23 @@ pub(crate) fn fdstat_set_flags(fd: &File, fdflags: types::Fdflags) -> Result<Opt
Ok(None)
}
pub(crate) fn advise(
file: &OsFile,
advice: types::Advice,
offset: types::Filesize,
len: types::Filesize,
) -> Result<()> {
pub(crate) fn advise(file: &OsFile, advice: Advice, offset: Filesize, len: Filesize) -> Result<()> {
use yanix::fadvise::{posix_fadvise, PosixFadviseAdvice};
let offset = offset.try_into()?;
let len = len.try_into()?;
let host_advice = match advice {
types::Advice::Dontneed => PosixFadviseAdvice::DontNeed,
types::Advice::Sequential => PosixFadviseAdvice::Sequential,
types::Advice::Willneed => PosixFadviseAdvice::WillNeed,
types::Advice::Noreuse => PosixFadviseAdvice::NoReuse,
types::Advice::Random => PosixFadviseAdvice::Random,
types::Advice::Normal => PosixFadviseAdvice::Normal,
Advice::Dontneed => PosixFadviseAdvice::DontNeed,
Advice::Sequential => PosixFadviseAdvice::Sequential,
Advice::Willneed => PosixFadviseAdvice::WillNeed,
Advice::Noreuse => PosixFadviseAdvice::NoReuse,
Advice::Random => PosixFadviseAdvice::Random,
Advice::Normal => PosixFadviseAdvice::Normal,
};
unsafe { posix_fadvise(file.as_raw_fd(), offset, len, host_advice)? };
Ok(())
}
pub(crate) fn filestat_get(file: &File) -> Result<types::Filestat> {
pub(crate) fn filestat_get(file: &File) -> Result<Filestat> {
use yanix::file::fstat;
let stat = unsafe { fstat(file.as_raw_fd())? };
Ok(stat.try_into()?)
@@ -49,8 +44,8 @@ pub(crate) fn filestat_get(file: &File) -> Result<types::Filestat> {
pub(crate) fn readdir<'a>(
dirfd: &'a OsDir,
cookie: types::Dircookie,
) -> Result<Box<dyn Iterator<Item = Result<(types::Dirent, String)>> + 'a>> {
cookie: Dircookie,
) -> Result<Box<dyn Iterator<Item = Result<(Dirent, String)>> + 'a>> {
use yanix::dir::{DirIter, Entry, EntryExt, SeekLoc};
// Get an instance of `Dir`; this is host-specific due to intricasies
@@ -59,7 +54,7 @@ pub(crate) fn readdir<'a>(
// Seek if needed. Unless cookie is wasi::__WASI_DIRCOOKIE_START,
// new items may not be returned to the caller.
if cookie == wasi::DIRCOOKIE_START {
if cookie == DIRCOOKIE_START {
tracing::trace!("fd_readdir: doing rewinddir");
dir.rewind();
} else {
@@ -71,7 +66,7 @@ pub(crate) fn readdir<'a>(
Ok(Box::new(DirIter::new(dir).map(|entry| {
let entry: Entry = entry?;
let name = entry.file_name().to_str()?.to_owned();
let dirent = types::Dirent {
let dirent = Dirent {
d_next: entry.seek_loc()?.to_raw().try_into()?,
d_ino: entry.ino(),
d_namlen: name.len().try_into()?,

View File

@@ -26,9 +26,11 @@ cfg_if::cfg_if! {
}
}
use crate::handle::HandleRights;
use crate::handle::{
Fdflags, Filesize, Filestat, Filetype, HandleRights, Lookupflags, Oflags, Rights, RightsExt,
};
use crate::sched::{Clockid, Timestamp};
use crate::sys::AsFile;
use crate::wasi::{types, RightsExt};
use crate::{Error, Result};
use std::convert::{TryFrom, TryInto};
use std::fs::File;
@@ -48,34 +50,34 @@ impl<T: AsRawFd> AsFile for T {
}
}
pub(super) fn get_file_type(file: &File) -> io::Result<types::Filetype> {
pub(super) fn get_file_type(file: &File) -> io::Result<Filetype> {
let ft = file.metadata()?.file_type();
let file_type = if ft.is_block_device() {
tracing::debug!(
host_fd = tracing::field::display(file.as_raw_fd()),
"Host fd is a block device"
);
types::Filetype::BlockDevice
Filetype::BlockDevice
} else if ft.is_char_device() {
tracing::debug!("Host fd {:?} is a char device", file.as_raw_fd());
types::Filetype::CharacterDevice
Filetype::CharacterDevice
} else if ft.is_dir() {
tracing::debug!("Host fd {:?} is a directory", file.as_raw_fd());
types::Filetype::Directory
Filetype::Directory
} else if ft.is_file() {
tracing::debug!("Host fd {:?} is a file", file.as_raw_fd());
types::Filetype::RegularFile
Filetype::RegularFile
} else if ft.is_socket() {
tracing::debug!("Host fd {:?} is a socket", file.as_raw_fd());
use yanix::socket::{get_socket_type, SockType};
match unsafe { get_socket_type(file.as_raw_fd())? } {
SockType::Datagram => types::Filetype::SocketDgram,
SockType::Stream => types::Filetype::SocketStream,
SockType::Datagram => Filetype::SocketDgram,
SockType::Stream => Filetype::SocketStream,
_ => return Err(io::Error::from_raw_os_error(libc::EINVAL)),
}
} else if ft.is_fifo() {
tracing::debug!("Host fd {:?} is a fifo", file.as_raw_fd());
types::Filetype::Unknown
Filetype::Unknown
} else {
tracing::debug!("Host fd {:?} is unknown", file.as_raw_fd());
return Err(io::Error::from_raw_os_error(libc::EINVAL));
@@ -83,48 +85,44 @@ pub(super) fn get_file_type(file: &File) -> io::Result<types::Filetype> {
Ok(file_type)
}
pub(super) fn get_rights(file: &File, file_type: &types::Filetype) -> io::Result<HandleRights> {
pub(super) fn get_rights(file: &File, file_type: &Filetype) -> io::Result<HandleRights> {
use yanix::{fcntl, file::OFlags};
let (base, inheriting) = match file_type {
types::Filetype::BlockDevice => (
types::Rights::block_device_base(),
types::Rights::block_device_inheriting(),
Filetype::BlockDevice => (
Rights::block_device_base(),
Rights::block_device_inheriting(),
),
types::Filetype::CharacterDevice => {
Filetype::CharacterDevice => {
use yanix::file::isatty;
if unsafe { isatty(file.as_raw_fd())? } {
(types::Rights::tty_base(), types::Rights::tty_base())
(Rights::tty_base(), Rights::tty_base())
} else {
(
types::Rights::character_device_base(),
types::Rights::character_device_inheriting(),
Rights::character_device_base(),
Rights::character_device_inheriting(),
)
}
}
types::Filetype::SocketDgram | types::Filetype::SocketStream => (
types::Rights::socket_base(),
types::Rights::socket_inheriting(),
Filetype::SocketDgram | Filetype::SocketStream => {
(Rights::socket_base(), Rights::socket_inheriting())
}
Filetype::SymbolicLink | Filetype::Unknown => (
Rights::regular_file_base(),
Rights::regular_file_inheriting(),
),
types::Filetype::SymbolicLink | types::Filetype::Unknown => (
types::Rights::regular_file_base(),
types::Rights::regular_file_inheriting(),
),
types::Filetype::Directory => (
types::Rights::directory_base(),
types::Rights::directory_inheriting(),
),
types::Filetype::RegularFile => (
types::Rights::regular_file_base(),
types::Rights::regular_file_inheriting(),
Filetype::Directory => (Rights::directory_base(), Rights::directory_inheriting()),
Filetype::RegularFile => (
Rights::regular_file_base(),
Rights::regular_file_inheriting(),
),
};
let mut rights = HandleRights::new(base, inheriting);
let flags = unsafe { fcntl::get_status_flags(file.as_raw_fd())? };
let accmode = flags & OFlags::ACCMODE;
if accmode == OFlags::RDONLY {
rights.base &= !types::Rights::FD_WRITE;
rights.base &= !Rights::FD_WRITE;
} else if accmode == OFlags::WRONLY {
rights.base &= !types::Rights::FD_READ;
rights.base &= !Rights::FD_READ;
}
Ok(rights)
}
@@ -133,9 +131,9 @@ pub fn preopen_dir<P: AsRef<Path>>(path: P) -> io::Result<File> {
File::open(path)
}
impl From<types::Clockid> for ClockId {
fn from(clock_id: types::Clockid) -> Self {
use types::Clockid::*;
impl From<Clockid> for ClockId {
fn from(clock_id: Clockid) -> Self {
use Clockid::*;
match clock_id {
Realtime => Self::Realtime,
Monotonic => Self::Monotonic,
@@ -145,29 +143,29 @@ impl From<types::Clockid> for ClockId {
}
}
impl From<types::Fdflags> for OFlags {
fn from(fdflags: types::Fdflags) -> Self {
impl From<Fdflags> for OFlags {
fn from(fdflags: Fdflags) -> Self {
let mut nix_flags = Self::empty();
if fdflags.contains(&types::Fdflags::APPEND) {
if fdflags.contains(&Fdflags::APPEND) {
nix_flags.insert(Self::APPEND);
}
if fdflags.contains(&types::Fdflags::DSYNC) {
if fdflags.contains(&Fdflags::DSYNC) {
nix_flags.insert(Self::DSYNC);
}
if fdflags.contains(&types::Fdflags::NONBLOCK) {
if fdflags.contains(&Fdflags::NONBLOCK) {
nix_flags.insert(Self::NONBLOCK);
}
if fdflags.contains(&types::Fdflags::RSYNC) {
if fdflags.contains(&Fdflags::RSYNC) {
nix_flags.insert(O_RSYNC);
}
if fdflags.contains(&types::Fdflags::SYNC) {
if fdflags.contains(&Fdflags::SYNC) {
nix_flags.insert(Self::SYNC);
}
nix_flags
}
}
impl From<OFlags> for types::Fdflags {
impl From<OFlags> for Fdflags {
fn from(oflags: OFlags) -> Self {
let mut fdflags = Self::empty();
if oflags.contains(OFlags::APPEND) {
@@ -189,30 +187,30 @@ impl From<OFlags> for types::Fdflags {
}
}
impl From<types::Oflags> for OFlags {
fn from(oflags: types::Oflags) -> Self {
impl From<Oflags> for OFlags {
fn from(oflags: Oflags) -> Self {
let mut nix_flags = Self::empty();
if oflags.contains(&types::Oflags::CREAT) {
if oflags.contains(&Oflags::CREAT) {
nix_flags.insert(Self::CREAT);
}
if oflags.contains(&types::Oflags::DIRECTORY) {
if oflags.contains(&Oflags::DIRECTORY) {
nix_flags.insert(Self::DIRECTORY);
}
if oflags.contains(&types::Oflags::EXCL) {
if oflags.contains(&Oflags::EXCL) {
nix_flags.insert(Self::EXCL);
}
if oflags.contains(&types::Oflags::TRUNC) {
if oflags.contains(&Oflags::TRUNC) {
nix_flags.insert(Self::TRUNC);
}
nix_flags
}
}
impl TryFrom<libc::stat> for types::Filestat {
impl TryFrom<libc::stat> for Filestat {
type Error = Error;
fn try_from(filestat: libc::stat) -> Result<Self> {
fn filestat_to_timestamp(secs: u64, nsecs: u64) -> Result<types::Timestamp> {
fn filestat_to_timestamp(secs: u64, nsecs: u64) -> Result<Timestamp> {
secs.checked_mul(1_000_000_000)
.and_then(|sec_nsec| sec_nsec.checked_add(nsecs))
.ok_or(Error::Overflow)
@@ -238,7 +236,7 @@ impl TryFrom<libc::stat> for types::Filestat {
dev,
ino,
nlink: filestat.st_nlink.into(),
size: filestat.st_size as types::Filesize,
size: filestat.st_size as Filesize,
atim,
ctim,
mtim,
@@ -247,7 +245,7 @@ impl TryFrom<libc::stat> for types::Filestat {
}
}
impl From<yanix::file::FileType> for types::Filetype {
impl From<yanix::file::FileType> for Filetype {
fn from(ft: yanix::file::FileType) -> Self {
use yanix::file::FileType::*;
match ft {
@@ -267,10 +265,10 @@ impl From<yanix::file::FileType> for types::Filetype {
}
}
impl From<types::Lookupflags> for AtFlags {
fn from(flags: types::Lookupflags) -> Self {
impl From<Lookupflags> for AtFlags {
fn from(flags: Lookupflags) -> Self {
match flags {
types::Lookupflags::SYMLINK_FOLLOW => Self::empty(),
Lookupflags::SYMLINK_FOLLOW => Self::empty(),
_ => Self::SYMLINK_NOFOLLOW,
}
}

View File

@@ -1,6 +1,5 @@
use super::oshandle::RawOsHandle;
use crate::handle::HandleRights;
use crate::wasi::{types, RightsExt};
use crate::handle::{HandleRights, Rights, RightsExt};
use std::convert::TryFrom;
use std::fs::File;
use std::io;
@@ -24,16 +23,13 @@ impl TryFrom<File> for OsDir {
fn get_rights(file: &File) -> io::Result<HandleRights> {
use yanix::{fcntl, file::OFlags};
let mut rights = HandleRights::new(
types::Rights::directory_base(),
types::Rights::directory_inheriting(),
);
let mut rights = HandleRights::new(Rights::directory_base(), Rights::directory_inheriting());
let flags = unsafe { fcntl::get_status_flags(file.as_raw_fd())? };
let accmode = flags & OFlags::ACCMODE;
if accmode == OFlags::RDONLY {
rights.base &= !types::Rights::FD_WRITE;
rights.base &= !Rights::FD_WRITE;
} else if accmode == OFlags::WRONLY {
rights.base &= !types::Rights::FD_READ;
rights.base &= !Rights::FD_READ;
}
Ok(rights)
}

View File

@@ -1,7 +1,6 @@
use super::oshandle::RawOsHandle;
use crate::handle::HandleRights;
use crate::handle::{HandleRights, Rights, RightsExt};
use crate::sys::osfile::OsFile;
use crate::wasi::{types, RightsExt};
use std::convert::TryFrom;
use std::fs::File;
use std::io;
@@ -24,15 +23,15 @@ impl TryFrom<File> for OsFile {
fn get_rights(file: &File) -> io::Result<HandleRights> {
use yanix::{fcntl, file::OFlags};
let mut rights = HandleRights::new(
types::Rights::regular_file_base(),
types::Rights::regular_file_inheriting(),
Rights::regular_file_base(),
Rights::regular_file_inheriting(),
);
let flags = unsafe { fcntl::get_status_flags(file.as_raw_fd())? };
let accmode = flags & OFlags::ACCMODE;
if accmode == OFlags::RDONLY {
rights.base &= !types::Rights::FD_WRITE;
rights.base &= !Rights::FD_WRITE;
} else if accmode == OFlags::WRONLY {
rights.base &= !types::Rights::FD_READ;
rights.base &= !Rights::FD_READ;
}
Ok(rights)
}

View File

@@ -1,7 +1,7 @@
use super::oshandle::RawOsHandle;
use super::{get_file_type, get_rights};
use crate::handle::Filetype;
use crate::sys::osother::OsOther;
use crate::wasi::types;
use std::convert::TryFrom;
use std::fs::File;
use std::io;
@@ -12,7 +12,7 @@ impl TryFrom<File> for OsOther {
fn try_from(file: File) -> io::Result<Self> {
let file_type = get_file_type(&file)?;
if file_type == types::Filetype::RegularFile || file_type == types::Filetype::Directory {
if file_type == Filetype::RegularFile || file_type == Filetype::Directory {
return Err(io::Error::from_raw_os_error(libc::EINVAL));
}
let rights = get_rights(&file, &file_type)?;

View File

@@ -1,7 +1,7 @@
use crate::handle::{Handle, HandleRights};
use crate::handle::{Fdflags, Filestat, Fstflags, Handle, HandleRights, Oflags, Rights};
use crate::sched::Timestamp;
use crate::sys::osdir::OsDir;
use crate::sys::AsFile;
use crate::wasi::types;
use crate::{Error, Result};
use std::convert::{TryFrom, TryInto};
use std::ffi::OsStr;
@@ -23,29 +23,29 @@ pub(crate) fn from_host<S: AsRef<OsStr>>(s: S) -> Result<String> {
pub(crate) fn open_rights(
input_rights: &HandleRights,
oflags: types::Oflags,
fs_flags: types::Fdflags,
oflags: Oflags,
fs_flags: Fdflags,
) -> HandleRights {
// which rights are needed on the dirfd?
let mut needed_base = types::Rights::PATH_OPEN;
let mut needed_base = Rights::PATH_OPEN;
let mut needed_inheriting = input_rights.base | input_rights.inheriting;
// convert open flags
let oflags: OFlags = oflags.into();
if oflags.contains(OFlags::CREAT) {
needed_base |= types::Rights::PATH_CREATE_FILE;
needed_base |= Rights::PATH_CREATE_FILE;
}
if oflags.contains(OFlags::TRUNC) {
needed_base |= types::Rights::PATH_FILESTAT_SET_SIZE;
needed_base |= Rights::PATH_FILESTAT_SET_SIZE;
}
// convert file descriptor flags
let fdflags: OFlags = fs_flags.into();
if fdflags.contains(OFlags::DSYNC) {
needed_inheriting |= types::Rights::FD_DATASYNC;
needed_inheriting |= Rights::FD_DATASYNC;
}
if fdflags.intersects(super::O_RSYNC | OFlags::SYNC) {
needed_inheriting |= types::Rights::FD_SYNC;
needed_inheriting |= Rights::FD_SYNC;
}
HandleRights::new(needed_base, needed_inheriting)
@@ -98,8 +98,8 @@ pub(crate) fn open(
path: &str,
read: bool,
write: bool,
oflags: types::Oflags,
fs_flags: types::Fdflags,
oflags: Oflags,
fs_flags: Fdflags,
) -> Result<Box<dyn Handle>> {
use yanix::file::{fstatat, openat, AtFlags, FileType, Mode, OFlags};
@@ -216,7 +216,7 @@ pub(crate) fn remove_directory(dirfd: &OsDir, path: &str) -> Result<()> {
Ok(())
}
pub(crate) fn filestat_get_at(dirfd: &OsDir, path: &str, follow: bool) -> Result<types::Filestat> {
pub(crate) fn filestat_get_at(dirfd: &OsDir, path: &str, follow: bool) -> Result<Filestat> {
use yanix::file::{fstatat, AtFlags};
let flags = if follow {
AtFlags::empty()
@@ -231,18 +231,18 @@ pub(crate) fn filestat_get_at(dirfd: &OsDir, path: &str, follow: bool) -> Result
pub(crate) fn filestat_set_times_at(
dirfd: &OsDir,
path: &str,
atim: types::Timestamp,
mtim: types::Timestamp,
fst_flags: types::Fstflags,
atim: Timestamp,
mtim: Timestamp,
fst_flags: Fstflags,
follow: bool,
) -> Result<()> {
use std::time::{Duration, UNIX_EPOCH};
use yanix::filetime::*;
let set_atim = fst_flags.contains(&types::Fstflags::ATIM);
let set_atim_now = fst_flags.contains(&types::Fstflags::ATIM_NOW);
let set_mtim = fst_flags.contains(&types::Fstflags::MTIM);
let set_mtim_now = fst_flags.contains(&types::Fstflags::MTIM_NOW);
let set_atim = fst_flags.contains(&Fstflags::ATIM);
let set_atim_now = fst_flags.contains(&Fstflags::ATIM_NOW);
let set_mtim = fst_flags.contains(&Fstflags::MTIM);
let set_mtim_now = fst_flags.contains(&Fstflags::MTIM_NOW);
if (set_atim && set_atim_now) || (set_mtim && set_mtim_now) {
return Err(Error::Inval);

View File

@@ -1,7 +1,9 @@
use crate::entry::EntryHandle;
use crate::poll::{ClockEventData, FdEventData};
use crate::handle::Filetype;
use crate::sched::{
ClockEventData, Errno, Event, EventFdReadwrite, Eventrwflags, Eventtype, FdEventData,
};
use crate::sys::AsFile;
use crate::wasi::types;
use crate::{Error, Result};
use std::io;
use std::{convert::TryInto, os::unix::prelude::AsRawFd};
@@ -11,7 +13,7 @@ use yanix::poll::{poll, PollFd, PollFlags};
pub(crate) fn oneoff(
timeout: Option<ClockEventData>,
fd_events: Vec<FdEventData>,
events: &mut Vec<types::Event>,
events: &mut Vec<Event>,
) -> Result<()> {
if fd_events.is_empty() && timeout.is_none() {
return Ok(());
@@ -22,8 +24,8 @@ pub(crate) fn oneoff(
.map(|event| {
let mut flags = PollFlags::empty();
match event.r#type {
types::Eventtype::FdRead => flags.insert(PollFlags::POLLIN),
types::Eventtype::FdWrite => flags.insert(PollFlags::POLLOUT),
Eventtype::FdRead => flags.insert(PollFlags::POLLIN),
Eventtype::FdWrite => flags.insert(PollFlags::POLLOUT),
// An event on a file descriptor can currently only be of type FD_READ or FD_WRITE
// Nothing else has been defined in the specification, and these are also the only two
// events we filtered before. If we get something else here, the code has a serious bug.
@@ -65,13 +67,13 @@ pub(crate) fn oneoff(
})
}
fn handle_timeout_event(timeout: ClockEventData, events: &mut Vec<types::Event>) {
events.push(types::Event {
fn handle_timeout_event(timeout: ClockEventData, events: &mut Vec<Event>) {
events.push(Event {
userdata: timeout.userdata,
error: types::Errno::Success,
type_: types::Eventtype::Clock,
fd_readwrite: types::EventFdReadwrite {
flags: types::Eventrwflags::empty(),
error: Errno::Success,
type_: Eventtype::Clock,
fd_readwrite: EventFdReadwrite {
flags: Eventrwflags::empty(),
nbytes: 0,
},
});
@@ -79,11 +81,11 @@ fn handle_timeout_event(timeout: ClockEventData, events: &mut Vec<types::Event>)
fn handle_fd_event(
ready_events: impl Iterator<Item = (FdEventData, yanix::poll::PollFd)>,
events: &mut Vec<types::Event>,
events: &mut Vec<Event>,
) -> Result<()> {
fn query_nbytes(handle: EntryHandle) -> Result<u64> {
let file = handle.as_file()?;
if handle.get_file_type() == types::Filetype::RegularFile {
if handle.get_file_type() == Filetype::RegularFile {
// fionread may overflow for large files, so use another way for regular files.
use yanix::file::tell;
let meta = file.metadata()?;
@@ -106,50 +108,50 @@ fn handle_fd_event(
None => continue,
};
let nbytes = if fd_event.r#type == types::Eventtype::FdRead {
let nbytes = if fd_event.r#type == Eventtype::FdRead {
query_nbytes(fd_event.handle)?
} else {
0
};
let output_event = if revents.contains(PollFlags::POLLNVAL) {
types::Event {
Event {
userdata: fd_event.userdata,
error: Error::Badf.into(),
type_: fd_event.r#type,
fd_readwrite: types::EventFdReadwrite {
fd_readwrite: EventFdReadwrite {
nbytes: 0,
flags: types::Eventrwflags::FD_READWRITE_HANGUP,
flags: Eventrwflags::FD_READWRITE_HANGUP,
},
}
} else if revents.contains(PollFlags::POLLERR) {
types::Event {
Event {
userdata: fd_event.userdata,
error: Error::Io.into(),
type_: fd_event.r#type,
fd_readwrite: types::EventFdReadwrite {
fd_readwrite: EventFdReadwrite {
nbytes: 0,
flags: types::Eventrwflags::FD_READWRITE_HANGUP,
flags: Eventrwflags::FD_READWRITE_HANGUP,
},
}
} else if revents.contains(PollFlags::POLLHUP) {
types::Event {
Event {
userdata: fd_event.userdata,
error: types::Errno::Success,
error: Errno::Success,
type_: fd_event.r#type,
fd_readwrite: types::EventFdReadwrite {
fd_readwrite: EventFdReadwrite {
nbytes: 0,
flags: types::Eventrwflags::FD_READWRITE_HANGUP,
flags: Eventrwflags::FD_READWRITE_HANGUP,
},
}
} else if revents.contains(PollFlags::POLLIN) | revents.contains(PollFlags::POLLOUT) {
types::Event {
Event {
userdata: fd_event.userdata,
error: types::Errno::Success,
error: Errno::Success,
type_: fd_event.r#type,
fd_readwrite: types::EventFdReadwrite {
fd_readwrite: EventFdReadwrite {
nbytes: nbytes.try_into()?,
flags: types::Eventrwflags::empty(),
flags: Eventrwflags::empty(),
},
}
} else {