Introduce strongly-typed system primitives (#1561)
* Introduce strongly-typed system primitives This commit does a lot of reshuffling and even some more. It introduces strongly-typed system primitives which are: `OsFile`, `OsDir`, `Stdio`, and `OsOther`. Those primitives are separate structs now, each implementing a subset of `Handle` methods, rather than all being an enumeration of some supertype such as `OsHandle`. To summarise the structs: * `OsFile` represents a regular file, and implements fd-ops of `Handle` trait * `OsDir` represents a directory, and primarily implements path-ops, plus `readdir` and some common fd-ops such as `fdstat`, etc. * `Stdio` represents a stdio handle, and implements a subset of fd-ops such as `fdstat` _and_ `read_` and `write_vectored` calls * `OsOther` currently represents anything else and implements a set similar to that implemented by `Stdio` This commit is effectively an experiment and an excercise into better understanding what's going on for each OS resource/type under-the-hood. It's meant to give us some intuition in order to move on with the idea of having strongly-typed handles in WASI both in the syscall impl as well as at the libc level. Some more minor changes include making `OsHandle` represent an OS-specific wrapper for a raw OS handle (Unix fd or Windows handle). Also, since `OsDir` is tricky across OSes, we also have a supertype of `OsHandle` called `OsDirHandle` which may store a `DIR*` stream pointer (mainly BSD). Last but not least, the `Filetype` and `Rights` are now computed when the resource is created, rather than every time we call `Handle::get_file_type` and `Handle::get_rights`. Finally, in order to facilitate the latter, I've converted `EntryRights` into `HandleRights` and pushed them into each `Handle` implementor. * Do not adjust rights on Stdio * Clean up testing for TTY and escaping writes * Implement AsFile for dyn Handle This cleans up a lot of repeating boilerplate code todo with dynamic dispatch. * Delegate definition of OsDir to OS-specific modules Delegates defining `OsDir` struct to OS-specific modules (BSD, Linux, Emscripten, Windows). This way, `OsDir` can safely re-use `OsHandle` for raw OS handle storage, and can store some aux data such as an initialized stream ptr in case of BSD. As a result, we can safely get rid of `OsDirHandle` which IMHO was causing unnecessary noise and overcomplicating the design. On the other hand, delegating definition of `OsDir` to OS-specific modules isn't super clean in and of itself either. Perhaps there's a better way of handling this? * Check if filetype of OS handle matches WASI filetype when creating It seems prudent to check if the passed in `File` instance is of type matching that of the requested WASI filetype. In other words, we'd like to avoid situations where `OsFile` is created from a pipe. * Make AsFile fallible Return `EBADF` in `AsFile` in case a `Handle` cannot be made into a `std::fs::File`. * Remove unnecessary as_file conversion * Remove unnecessary check for TTY for Stdio handle type * Fix incorrect stdio ctors on Unix * Split Stdio into three separate types: Stdin, Stdout, Stderr * Rename PendingEntry::File to PendingEntry::OsHandle to avoid confusion * Rename OsHandle to RawOsHandle Also, since `RawOsHandle` on *nix doesn't need interior mutability wrt the inner raw file descriptor, we can safely swap the `RawFd` for `File` instance. * Add docs explaining what OsOther is * Allow for stdio to be non-character-device (e.g., piped) * Return error on bad preopen rather than panic
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
pub(crate) mod clock;
|
||||
pub(crate) mod fd;
|
||||
pub(crate) mod oshandle;
|
||||
pub(crate) mod osdir;
|
||||
pub(crate) mod osfile;
|
||||
pub(crate) mod osother;
|
||||
pub(crate) mod stdio;
|
||||
|
||||
use cfg_if::cfg_if;
|
||||
|
||||
@@ -20,3 +23,65 @@ cfg_if! {
|
||||
|
||||
pub(crate) use sys_impl::path;
|
||||
pub(crate) use sys_impl::poll;
|
||||
|
||||
use super::handle::Handle;
|
||||
use crate::wasi::types;
|
||||
use osdir::OsDir;
|
||||
use osfile::OsFile;
|
||||
use osother::OsOther;
|
||||
use std::convert::TryFrom;
|
||||
use std::fs::File;
|
||||
use std::io;
|
||||
use std::mem::ManuallyDrop;
|
||||
use stdio::{Stderr, Stdin, Stdout};
|
||||
use sys_impl::get_file_type;
|
||||
|
||||
pub(crate) trait AsFile {
|
||||
fn as_file(&self) -> io::Result<ManuallyDrop<File>>;
|
||||
}
|
||||
|
||||
impl AsFile for dyn Handle + 'static {
|
||||
fn as_file(&self) -> io::Result<ManuallyDrop<File>> {
|
||||
if let Some(file) = self.as_any().downcast_ref::<OsFile>() {
|
||||
file.as_file()
|
||||
} else if let Some(dir) = self.as_any().downcast_ref::<OsDir>() {
|
||||
dir.as_file()
|
||||
} else if let Some(stdin) = self.as_any().downcast_ref::<Stdin>() {
|
||||
stdin.as_file()
|
||||
} else if let Some(stdout) = self.as_any().downcast_ref::<Stdout>() {
|
||||
stdout.as_file()
|
||||
} else if let Some(stderr) = self.as_any().downcast_ref::<Stderr>() {
|
||||
stderr.as_file()
|
||||
} else if let Some(other) = self.as_any().downcast_ref::<OsOther>() {
|
||||
other.as_file()
|
||||
} else {
|
||||
log::error!("tried to make std::fs::File from non-OS handle");
|
||||
Err(io::Error::from_raw_os_error(libc::EBADF))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<File> for Box<dyn Handle> {
|
||||
type Error = io::Error;
|
||||
|
||||
fn try_from(file: File) -> io::Result<Self> {
|
||||
let file_type = get_file_type(&file)?;
|
||||
match file_type {
|
||||
types::Filetype::RegularFile => {
|
||||
let handle = OsFile::try_from(file)?;
|
||||
log::debug!("Created new instance of OsFile: {:?}", handle);
|
||||
Ok(Box::new(handle))
|
||||
}
|
||||
types::Filetype::Directory => {
|
||||
let handle = OsDir::try_from(file)?;
|
||||
log::debug!("Created new instance of OsDir: {:?}", handle);
|
||||
Ok(Box::new(handle))
|
||||
}
|
||||
_ => {
|
||||
let handle = OsOther::try_from(file)?;
|
||||
log::debug!("Created new instance of OsOther: {:?}", handle);
|
||||
Ok(Box::new(handle))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
129
crates/wasi-common/src/sys/osdir.rs
Normal file
129
crates/wasi-common/src/sys/osdir.rs
Normal file
@@ -0,0 +1,129 @@
|
||||
use super::sys_impl::oshandle::RawOsHandle;
|
||||
use super::{fd, path, AsFile};
|
||||
use crate::handle::{Handle, HandleRights};
|
||||
use crate::wasi::{types, Errno, Result};
|
||||
use log::{debug, error};
|
||||
use std::any::Any;
|
||||
use std::io;
|
||||
use std::ops::Deref;
|
||||
|
||||
// TODO could this be cleaned up?
|
||||
// The actual `OsDir` struct is OS-dependent, therefore we delegate
|
||||
// its definition to OS-specific modules.
|
||||
pub(crate) use super::sys_impl::osdir::OsDir;
|
||||
|
||||
impl Deref for OsDir {
|
||||
type Target = RawOsHandle;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.handle
|
||||
}
|
||||
}
|
||||
|
||||
impl Handle for OsDir {
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
fn try_clone(&self) -> io::Result<Box<dyn Handle>> {
|
||||
let handle = self.handle.try_clone()?;
|
||||
let new = Self::new(self.rights.get(), handle)?;
|
||||
Ok(Box::new(new))
|
||||
}
|
||||
fn get_file_type(&self) -> types::Filetype {
|
||||
types::Filetype::Directory
|
||||
}
|
||||
fn get_rights(&self) -> HandleRights {
|
||||
self.rights.get()
|
||||
}
|
||||
fn set_rights(&self, rights: HandleRights) {
|
||||
self.rights.set(rights)
|
||||
}
|
||||
// FdOps
|
||||
fn fdstat_get(&self) -> Result<types::Fdflags> {
|
||||
fd::fdstat_get(&*self.as_file()?)
|
||||
}
|
||||
fn fdstat_set_flags(&self, fdflags: types::Fdflags) -> Result<()> {
|
||||
if let Some(new_file) = fd::fdstat_set_flags(&*self.as_file()?, fdflags)? {
|
||||
self.handle.update_from(new_file);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn filestat_get(&self) -> Result<types::Filestat> {
|
||||
fd::filestat_get(&*self.as_file()?)
|
||||
}
|
||||
fn filestat_set_times(
|
||||
&self,
|
||||
atim: types::Timestamp,
|
||||
mtim: types::Timestamp,
|
||||
fst_flags: types::Fstflags,
|
||||
) -> Result<()> {
|
||||
fd::filestat_set_times(&*self.as_file()?, atim, mtim, fst_flags)
|
||||
}
|
||||
fn readdir<'a>(
|
||||
&'a self,
|
||||
cookie: types::Dircookie,
|
||||
) -> Result<Box<dyn Iterator<Item = Result<(types::Dirent, String)>> + 'a>> {
|
||||
fd::readdir(self, cookie)
|
||||
}
|
||||
// PathOps
|
||||
fn create_directory(&self, path: &str) -> Result<()> {
|
||||
path::create_directory(self, path)
|
||||
}
|
||||
fn openat(
|
||||
&self,
|
||||
path: &str,
|
||||
read: bool,
|
||||
write: bool,
|
||||
oflags: types::Oflags,
|
||||
fd_flags: types::Fdflags,
|
||||
) -> Result<Box<dyn Handle>> {
|
||||
path::open(self, path, read, write, oflags, fd_flags)
|
||||
}
|
||||
fn link(
|
||||
&self,
|
||||
old_path: &str,
|
||||
new_handle: Box<dyn Handle>,
|
||||
new_path: &str,
|
||||
follow: bool,
|
||||
) -> Result<()> {
|
||||
let new_handle = match new_handle.as_any().downcast_ref::<Self>() {
|
||||
None => {
|
||||
error!("Tried to link with handle that's not an OsDir");
|
||||
return Err(Errno::Badf);
|
||||
}
|
||||
Some(handle) => handle,
|
||||
};
|
||||
path::link(self, old_path, new_handle, new_path, follow)
|
||||
}
|
||||
fn symlink(&self, old_path: &str, new_path: &str) -> Result<()> {
|
||||
path::symlink(old_path, self, new_path)
|
||||
}
|
||||
fn readlink(&self, path: &str, buf: &mut [u8]) -> Result<usize> {
|
||||
path::readlink(self, path, buf)
|
||||
}
|
||||
fn readlinkat(&self, path: &str) -> Result<String> {
|
||||
path::readlinkat(self, path)
|
||||
}
|
||||
fn rename(&self, old_path: &str, new_handle: Box<dyn Handle>, new_path: &str) -> Result<()> {
|
||||
let new_handle = match new_handle.as_any().downcast_ref::<Self>() {
|
||||
None => {
|
||||
error!("Tried to rename with handle that's not an OsDir");
|
||||
return Err(Errno::Badf);
|
||||
}
|
||||
Some(handle) => handle,
|
||||
};
|
||||
debug!("rename (old_dirfd, old_path)=({:?}, {:?})", self, old_path);
|
||||
debug!(
|
||||
"rename (new_dirfd, new_path)=({:?}, {:?})",
|
||||
new_handle, new_path
|
||||
);
|
||||
path::rename(self, old_path, new_handle, new_path)
|
||||
}
|
||||
fn remove_directory(&self, path: &str) -> Result<()> {
|
||||
debug!("remove_directory (dirfd, path)=({:?}, {:?})", self, path);
|
||||
path::remove_directory(self, path)
|
||||
}
|
||||
fn unlink_file(&self, path: &str) -> Result<()> {
|
||||
path::unlink_file(self, path)
|
||||
}
|
||||
}
|
||||
133
crates/wasi-common/src/sys/osfile.rs
Normal file
133
crates/wasi-common/src/sys/osfile.rs
Normal file
@@ -0,0 +1,133 @@
|
||||
use super::sys_impl::oshandle::RawOsHandle;
|
||||
use super::{fd, AsFile};
|
||||
use crate::handle::{Handle, HandleRights};
|
||||
use crate::wasi::{types, Errno, Result};
|
||||
use std::any::Any;
|
||||
use std::cell::Cell;
|
||||
use std::fs::File;
|
||||
use std::io::{self, Read, Seek, SeekFrom, Write};
|
||||
use std::ops::Deref;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct OsFile {
|
||||
rights: Cell<HandleRights>,
|
||||
handle: RawOsHandle,
|
||||
}
|
||||
|
||||
impl OsFile {
|
||||
pub(super) fn new(rights: HandleRights, handle: RawOsHandle) -> Self {
|
||||
let rights = Cell::new(rights);
|
||||
Self { rights, handle }
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for OsFile {
|
||||
type Target = RawOsHandle;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.handle
|
||||
}
|
||||
}
|
||||
|
||||
impl Handle for OsFile {
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
fn try_clone(&self) -> io::Result<Box<dyn Handle>> {
|
||||
let handle = self.handle.try_clone()?;
|
||||
let rights = self.rights.clone();
|
||||
Ok(Box::new(Self { rights, handle }))
|
||||
}
|
||||
fn get_file_type(&self) -> types::Filetype {
|
||||
types::Filetype::RegularFile
|
||||
}
|
||||
fn get_rights(&self) -> HandleRights {
|
||||
self.rights.get()
|
||||
}
|
||||
fn set_rights(&self, rights: HandleRights) {
|
||||
self.rights.set(rights)
|
||||
}
|
||||
// FdOps
|
||||
fn advise(
|
||||
&self,
|
||||
advice: types::Advice,
|
||||
offset: types::Filesize,
|
||||
len: types::Filesize,
|
||||
) -> Result<()> {
|
||||
fd::advise(self, advice, offset, len)
|
||||
}
|
||||
fn allocate(&self, offset: types::Filesize, len: types::Filesize) -> Result<()> {
|
||||
let fd = self.as_file()?;
|
||||
let metadata = fd.metadata()?;
|
||||
let current_size = metadata.len();
|
||||
let wanted_size = offset.checked_add(len).ok_or(Errno::TooBig)?;
|
||||
// This check will be unnecessary when rust-lang/rust#63326 is fixed
|
||||
if wanted_size > i64::max_value() as u64 {
|
||||
return Err(Errno::TooBig);
|
||||
}
|
||||
if wanted_size > current_size {
|
||||
fd.set_len(wanted_size)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn datasync(&self) -> Result<()> {
|
||||
self.as_file()?.sync_data()?;
|
||||
Ok(())
|
||||
}
|
||||
fn fdstat_get(&self) -> Result<types::Fdflags> {
|
||||
fd::fdstat_get(&*self.as_file()?)
|
||||
}
|
||||
fn fdstat_set_flags(&self, fdflags: types::Fdflags) -> Result<()> {
|
||||
if let Some(new_handle) = fd::fdstat_set_flags(&*self.as_file()?, fdflags)? {
|
||||
self.handle.update_from(new_handle);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn filestat_get(&self) -> Result<types::Filestat> {
|
||||
fd::filestat_get(&*self.as_file()?)
|
||||
}
|
||||
fn filestat_set_size(&self, size: types::Filesize) -> Result<()> {
|
||||
self.as_file()?.set_len(size)?;
|
||||
Ok(())
|
||||
}
|
||||
fn filestat_set_times(
|
||||
&self,
|
||||
atim: types::Timestamp,
|
||||
mtim: types::Timestamp,
|
||||
fst_flags: types::Fstflags,
|
||||
) -> Result<()> {
|
||||
fd::filestat_set_times(&*self.as_file()?, atim, mtim, fst_flags)
|
||||
}
|
||||
fn preadv(&self, buf: &mut [io::IoSliceMut], offset: u64) -> Result<usize> {
|
||||
let mut fd: &File = &*self.as_file()?;
|
||||
let cur_pos = fd.seek(SeekFrom::Current(0))?;
|
||||
fd.seek(SeekFrom::Start(offset))?;
|
||||
let nread = self.read_vectored(buf)?;
|
||||
fd.seek(SeekFrom::Start(cur_pos))?;
|
||||
Ok(nread)
|
||||
}
|
||||
fn pwritev(&self, buf: &[io::IoSlice], offset: u64) -> Result<usize> {
|
||||
let mut fd: &File = &*self.as_file()?;
|
||||
let cur_pos = fd.seek(SeekFrom::Current(0))?;
|
||||
fd.seek(SeekFrom::Start(offset))?;
|
||||
let nwritten = self.write_vectored(&buf)?;
|
||||
fd.seek(SeekFrom::Start(cur_pos))?;
|
||||
Ok(nwritten)
|
||||
}
|
||||
fn read_vectored(&self, iovs: &mut [io::IoSliceMut]) -> Result<usize> {
|
||||
let nread = self.as_file()?.read_vectored(iovs)?;
|
||||
Ok(nread)
|
||||
}
|
||||
fn seek(&self, offset: SeekFrom) -> Result<u64> {
|
||||
let pos = self.as_file()?.seek(offset)?;
|
||||
Ok(pos)
|
||||
}
|
||||
fn sync(&self) -> Result<()> {
|
||||
self.as_file()?.sync_all()?;
|
||||
Ok(())
|
||||
}
|
||||
fn write_vectored(&self, iovs: &[io::IoSlice]) -> Result<usize> {
|
||||
let nwritten = self.as_file()?.write_vectored(&iovs)?;
|
||||
Ok(nwritten)
|
||||
}
|
||||
}
|
||||
@@ -1,277 +0,0 @@
|
||||
use super::{fd, path};
|
||||
use crate::entry::EntryRights;
|
||||
use crate::handle::Handle;
|
||||
use crate::sandboxed_tty_writer::SandboxedTTYWriter;
|
||||
use crate::wasi::{types, Errno, Result};
|
||||
use log::{debug, error};
|
||||
use std::any::Any;
|
||||
use std::fs::File;
|
||||
use std::io::{self, Read, Seek, SeekFrom, Write};
|
||||
use std::mem::ManuallyDrop;
|
||||
|
||||
pub(crate) use super::sys_impl::oshandle::*;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) enum OsHandle {
|
||||
OsFile(OsFile),
|
||||
Stdin,
|
||||
Stdout,
|
||||
Stderr,
|
||||
}
|
||||
|
||||
impl OsHandle {
|
||||
pub(crate) fn as_os_file(&self) -> Result<&OsFile> {
|
||||
match self {
|
||||
Self::OsFile(fd) => Ok(fd),
|
||||
_ => Err(Errno::Badf),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn stdin() -> Self {
|
||||
Self::Stdin
|
||||
}
|
||||
|
||||
pub(crate) fn stdout() -> Self {
|
||||
Self::Stdout
|
||||
}
|
||||
|
||||
pub(crate) fn stderr() -> Self {
|
||||
Self::Stderr
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) trait AsFile {
|
||||
fn as_file(&self) -> ManuallyDrop<File>;
|
||||
}
|
||||
|
||||
pub(crate) trait OsHandleExt: Sized {
|
||||
/// Returns the file type.
|
||||
fn get_file_type(&self) -> io::Result<types::Filetype>;
|
||||
/// Returns the set of all possible rights that are both relevant for the file
|
||||
/// type and consistent with the open mode.
|
||||
fn get_rights(&self, filetype: types::Filetype) -> io::Result<EntryRights>;
|
||||
fn from_null() -> io::Result<Self>;
|
||||
}
|
||||
|
||||
impl From<OsFile> for OsHandle {
|
||||
fn from(file: OsFile) -> Self {
|
||||
Self::OsFile(file)
|
||||
}
|
||||
}
|
||||
|
||||
impl Handle for OsHandle {
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
fn try_clone(&self) -> io::Result<Box<dyn Handle>> {
|
||||
let new_handle = match self {
|
||||
Self::OsFile(file) => Self::OsFile(file.try_clone()?),
|
||||
Self::Stdin => Self::Stdin,
|
||||
Self::Stdout => Self::Stdout,
|
||||
Self::Stderr => Self::Stderr,
|
||||
};
|
||||
Ok(Box::new(new_handle))
|
||||
}
|
||||
fn get_file_type(&self) -> io::Result<types::Filetype> {
|
||||
<Self as OsHandleExt>::get_file_type(self)
|
||||
}
|
||||
|
||||
fn get_rights(&self) -> io::Result<EntryRights> {
|
||||
<Self as OsHandleExt>::get_rights(self, <Self as Handle>::get_file_type(self)?)
|
||||
}
|
||||
// FdOps
|
||||
fn advise(
|
||||
&self,
|
||||
advice: types::Advice,
|
||||
offset: types::Filesize,
|
||||
len: types::Filesize,
|
||||
) -> Result<()> {
|
||||
fd::advise(self.as_os_file()?, advice, offset, len)
|
||||
}
|
||||
fn allocate(&self, offset: types::Filesize, len: types::Filesize) -> Result<()> {
|
||||
let fd = self.as_file();
|
||||
let metadata = fd.metadata()?;
|
||||
let current_size = metadata.len();
|
||||
let wanted_size = offset.checked_add(len).ok_or(Errno::TooBig)?;
|
||||
// This check will be unnecessary when rust-lang/rust#63326 is fixed
|
||||
if wanted_size > i64::max_value() as u64 {
|
||||
return Err(Errno::TooBig);
|
||||
}
|
||||
if wanted_size > current_size {
|
||||
fd.set_len(wanted_size)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn datasync(&self) -> Result<()> {
|
||||
self.as_file().sync_data()?;
|
||||
Ok(())
|
||||
}
|
||||
fn fdstat_get(&self) -> Result<types::Fdflags> {
|
||||
fd::fdstat_get(&self.as_file())
|
||||
}
|
||||
fn fdstat_set_flags(&self, fdflags: types::Fdflags) -> Result<()> {
|
||||
if let Some(new_file) = fd::fdstat_set_flags(&self.as_file(), fdflags)? {
|
||||
// If we don't deal with OsFile, then something went wrong, and we
|
||||
// should fail. On the other hand, is that even possible?
|
||||
self.as_os_file()?.update_from(new_file);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn filestat_get(&self) -> Result<types::Filestat> {
|
||||
fd::filestat_get(&self.as_file())
|
||||
}
|
||||
fn filestat_set_size(&self, size: types::Filesize) -> Result<()> {
|
||||
self.as_os_file()?.as_file().set_len(size)?;
|
||||
Ok(())
|
||||
}
|
||||
fn filestat_set_times(
|
||||
&self,
|
||||
atim: types::Timestamp,
|
||||
mtim: types::Timestamp,
|
||||
fst_flags: types::Fstflags,
|
||||
) -> Result<()> {
|
||||
fd::filestat_set_times(&self.as_file(), atim, mtim, fst_flags)
|
||||
}
|
||||
fn preadv(&self, buf: &mut [io::IoSliceMut], offset: u64) -> Result<usize> {
|
||||
let mut fd: &File = &self.as_os_file()?.as_file();
|
||||
let cur_pos = fd.seek(SeekFrom::Current(0))?;
|
||||
fd.seek(SeekFrom::Start(offset))?;
|
||||
let nread = self.read_vectored(buf)?;
|
||||
fd.seek(SeekFrom::Start(cur_pos))?;
|
||||
Ok(nread)
|
||||
}
|
||||
fn pwritev(&self, buf: &[io::IoSlice], offset: u64) -> Result<usize> {
|
||||
let mut fd: &File = &self.as_os_file()?.as_file();
|
||||
let cur_pos = fd.seek(SeekFrom::Current(0))?;
|
||||
fd.seek(SeekFrom::Start(offset))?;
|
||||
let nwritten = self.write_vectored(&buf, false)?;
|
||||
fd.seek(SeekFrom::Start(cur_pos))?;
|
||||
Ok(nwritten)
|
||||
}
|
||||
fn read_vectored(&self, iovs: &mut [io::IoSliceMut]) -> Result<usize> {
|
||||
let nread = match self {
|
||||
Self::OsFile(file) => file.as_file().read_vectored(iovs)?,
|
||||
Self::Stdin => io::stdin().read_vectored(iovs)?,
|
||||
_ => return Err(Errno::Badf),
|
||||
};
|
||||
Ok(nread)
|
||||
}
|
||||
fn readdir<'a>(
|
||||
&'a self,
|
||||
cookie: types::Dircookie,
|
||||
) -> Result<Box<dyn Iterator<Item = Result<(types::Dirent, String)>> + 'a>> {
|
||||
fd::readdir(self.as_os_file()?, cookie)
|
||||
}
|
||||
fn seek(&self, offset: SeekFrom) -> Result<u64> {
|
||||
let pos = self.as_os_file()?.as_file().seek(offset)?;
|
||||
Ok(pos)
|
||||
}
|
||||
fn sync(&self) -> Result<()> {
|
||||
self.as_os_file()?.as_file().sync_all()?;
|
||||
Ok(())
|
||||
}
|
||||
fn write_vectored(&self, iovs: &[io::IoSlice], isatty: bool) -> Result<usize> {
|
||||
let nwritten = match self {
|
||||
Self::OsFile(file) => {
|
||||
let mut file: &File = &file.as_file();
|
||||
if isatty {
|
||||
SandboxedTTYWriter::new(&mut file).write_vectored(&iovs)?
|
||||
} else {
|
||||
file.write_vectored(&iovs)?
|
||||
}
|
||||
}
|
||||
Self::Stdin => return Err(Errno::Badf),
|
||||
Self::Stdout => {
|
||||
// lock for the duration of the scope
|
||||
let stdout = io::stdout();
|
||||
let mut stdout = stdout.lock();
|
||||
let nwritten = if isatty {
|
||||
SandboxedTTYWriter::new(&mut stdout).write_vectored(&iovs)?
|
||||
} else {
|
||||
stdout.write_vectored(&iovs)?
|
||||
};
|
||||
stdout.flush()?;
|
||||
nwritten
|
||||
}
|
||||
// Always sanitize stderr, even if it's not directly connected to a tty,
|
||||
// because stderr is meant for diagnostics rather than binary output,
|
||||
// and may be redirected to a file which could end up being displayed
|
||||
// on a tty later.
|
||||
Self::Stderr => SandboxedTTYWriter::new(&mut io::stderr()).write_vectored(&iovs)?,
|
||||
};
|
||||
Ok(nwritten)
|
||||
}
|
||||
// PathOps
|
||||
fn create_directory(&self, path: &str) -> Result<()> {
|
||||
path::create_directory(self.as_os_file()?, path)
|
||||
}
|
||||
fn openat(
|
||||
&self,
|
||||
path: &str,
|
||||
read: bool,
|
||||
write: bool,
|
||||
oflags: types::Oflags,
|
||||
fd_flags: types::Fdflags,
|
||||
) -> Result<Box<dyn Handle>> {
|
||||
let handle = path::open(self.as_os_file()?, path, read, write, oflags, fd_flags)?;
|
||||
Ok(Box::new(handle))
|
||||
}
|
||||
fn link(
|
||||
&self,
|
||||
old_path: &str,
|
||||
new_handle: Box<dyn Handle>,
|
||||
new_path: &str,
|
||||
follow: bool,
|
||||
) -> Result<()> {
|
||||
let new_handle = match new_handle.as_any().downcast_ref::<Self>() {
|
||||
None => {
|
||||
error!("Tried to link OS resource with Virtual");
|
||||
return Err(Errno::Badf);
|
||||
}
|
||||
Some(handle) => handle,
|
||||
};
|
||||
path::link(
|
||||
self.as_os_file()?,
|
||||
old_path,
|
||||
new_handle.as_os_file()?,
|
||||
new_path,
|
||||
follow,
|
||||
)
|
||||
}
|
||||
fn symlink(&self, old_path: &str, new_path: &str) -> Result<()> {
|
||||
path::symlink(old_path, self.as_os_file()?, new_path)
|
||||
}
|
||||
fn readlink(&self, path: &str, buf: &mut [u8]) -> Result<usize> {
|
||||
path::readlink(self.as_os_file()?, path, buf)
|
||||
}
|
||||
fn readlinkat(&self, path: &str) -> Result<String> {
|
||||
path::readlinkat(self.as_os_file()?, path)
|
||||
}
|
||||
fn rename(&self, old_path: &str, new_handle: Box<dyn Handle>, new_path: &str) -> Result<()> {
|
||||
let new_handle = match new_handle.as_any().downcast_ref::<Self>() {
|
||||
None => {
|
||||
error!("Tried to link OS resource with Virtual");
|
||||
return Err(Errno::Badf);
|
||||
}
|
||||
Some(handle) => handle,
|
||||
};
|
||||
debug!("rename (old_dirfd, old_path)=({:?}, {:?})", self, old_path);
|
||||
debug!(
|
||||
"rename (new_dirfd, new_path)=({:?}, {:?})",
|
||||
new_handle, new_path
|
||||
);
|
||||
path::rename(
|
||||
self.as_os_file()?,
|
||||
old_path,
|
||||
new_handle.as_os_file()?,
|
||||
new_path,
|
||||
)
|
||||
}
|
||||
fn remove_directory(&self, path: &str) -> Result<()> {
|
||||
debug!("remove_directory (dirfd, path)=({:?}, {:?})", self, path);
|
||||
path::remove_directory(self.as_os_file()?, path)
|
||||
}
|
||||
fn unlink_file(&self, path: &str) -> Result<()> {
|
||||
path::unlink_file(self.as_os_file()?, path)
|
||||
}
|
||||
}
|
||||
95
crates/wasi-common/src/sys/osother.rs
Normal file
95
crates/wasi-common/src/sys/osother.rs
Normal file
@@ -0,0 +1,95 @@
|
||||
use super::sys_impl::oshandle::RawOsHandle;
|
||||
use super::{fd, AsFile};
|
||||
use crate::handle::{Handle, HandleRights};
|
||||
use crate::sandboxed_tty_writer::SandboxedTTYWriter;
|
||||
use crate::wasi::types::{self, Filetype};
|
||||
use crate::wasi::Result;
|
||||
use std::any::Any;
|
||||
use std::cell::Cell;
|
||||
use std::fs::File;
|
||||
use std::io::{self, Read, Write};
|
||||
use std::ops::Deref;
|
||||
|
||||
pub(crate) trait OsOtherExt {
|
||||
/// Create `OsOther` as `dyn Handle` from null device.
|
||||
fn from_null() -> io::Result<Box<dyn Handle>>;
|
||||
}
|
||||
|
||||
/// `OsOther` is something of a catch-all for everything not covered with the specific handle
|
||||
/// types (`OsFile`, `OsDir`, `Stdio`). It currently encapsulates handles such as OS pipes,
|
||||
/// sockets, streams, etc. As such, when redirecting stdio within `WasiCtxBuilder`, the redirected
|
||||
/// pipe should be encapsulated within this instance _and not_ `OsFile` which represents a regular
|
||||
/// OS file.
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct OsOther {
|
||||
file_type: Filetype,
|
||||
rights: Cell<HandleRights>,
|
||||
handle: RawOsHandle,
|
||||
}
|
||||
|
||||
impl OsOther {
|
||||
pub(super) fn new(file_type: Filetype, rights: HandleRights, handle: RawOsHandle) -> Self {
|
||||
let rights = Cell::new(rights);
|
||||
Self {
|
||||
file_type,
|
||||
rights,
|
||||
handle,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for OsOther {
|
||||
type Target = RawOsHandle;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.handle
|
||||
}
|
||||
}
|
||||
|
||||
impl Handle for OsOther {
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
fn try_clone(&self) -> io::Result<Box<dyn Handle>> {
|
||||
let file_type = self.file_type;
|
||||
let handle = self.handle.try_clone()?;
|
||||
let rights = self.rights.clone();
|
||||
Ok(Box::new(Self {
|
||||
file_type,
|
||||
rights,
|
||||
handle,
|
||||
}))
|
||||
}
|
||||
fn get_file_type(&self) -> Filetype {
|
||||
self.file_type
|
||||
}
|
||||
fn get_rights(&self) -> HandleRights {
|
||||
self.rights.get()
|
||||
}
|
||||
fn set_rights(&self, new_rights: HandleRights) {
|
||||
self.rights.set(new_rights)
|
||||
}
|
||||
// FdOps
|
||||
fn fdstat_get(&self) -> Result<types::Fdflags> {
|
||||
fd::fdstat_get(&*self.as_file()?)
|
||||
}
|
||||
fn fdstat_set_flags(&self, fdflags: types::Fdflags) -> Result<()> {
|
||||
if let Some(handle) = fd::fdstat_set_flags(&*self.as_file()?, fdflags)? {
|
||||
self.handle.update_from(handle);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn read_vectored(&self, iovs: &mut [io::IoSliceMut]) -> Result<usize> {
|
||||
let nread = self.as_file()?.read_vectored(iovs)?;
|
||||
Ok(nread)
|
||||
}
|
||||
fn write_vectored(&self, iovs: &[io::IoSlice]) -> Result<usize> {
|
||||
let mut fd: &File = &*self.as_file()?;
|
||||
let nwritten = if self.is_tty() {
|
||||
SandboxedTTYWriter::new(&mut fd).write_vectored(&iovs)?
|
||||
} else {
|
||||
fd.write_vectored(iovs)?
|
||||
};
|
||||
Ok(nwritten)
|
||||
}
|
||||
}
|
||||
176
crates/wasi-common/src/sys/stdio.rs
Normal file
176
crates/wasi-common/src/sys/stdio.rs
Normal file
@@ -0,0 +1,176 @@
|
||||
// The reason we have a separate Stdio wrappers is to correctly facilitate redirects on Windows.
|
||||
// To elaborate further, in POSIX, we can get a stdio handle by opening a specific fd {0,1,2}.
|
||||
// On Windows however, we need to issue a syscall that's separate from standard Windows "open"
|
||||
// to get a console handle, and this is GetStdHandle. This is exactly what Rust does and what
|
||||
// is wrapped inside their Stdio object in the libstd. We wrap it here as well because of this
|
||||
// nuance on Windows:
|
||||
//
|
||||
// The standard handles of a process may be redirected by a call to SetStdHandle, in which
|
||||
// case GetStdHandle returns the redirected handle.
|
||||
//
|
||||
// The MSDN also says this however:
|
||||
//
|
||||
// If the standard handles have been redirected, you can specify the CONIN$ value in a call
|
||||
// to the CreateFile function to get a handle to a console's input buffer. Similarly, you
|
||||
// can specify the CONOUT$ value to get a handle to a console's active screen buffer.
|
||||
//
|
||||
// TODO it might worth re-investigating the suitability of this type on Windows.
|
||||
|
||||
use super::{fd, AsFile};
|
||||
use crate::handle::{Handle, HandleRights};
|
||||
use crate::sandboxed_tty_writer::SandboxedTTYWriter;
|
||||
use crate::wasi::types::{self, Filetype};
|
||||
use crate::wasi::Result;
|
||||
use std::any::Any;
|
||||
use std::cell::Cell;
|
||||
use std::io::{self, Read, Write};
|
||||
|
||||
pub(crate) trait StdinExt: Sized {
|
||||
/// Create `Stdin` from `io::stdin`.
|
||||
fn stdin() -> io::Result<Box<dyn Handle>>;
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub(crate) struct Stdin {
|
||||
pub(crate) file_type: Filetype,
|
||||
pub(crate) rights: Cell<HandleRights>,
|
||||
}
|
||||
|
||||
impl Handle for Stdin {
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
fn try_clone(&self) -> io::Result<Box<dyn Handle>> {
|
||||
Ok(Box::new(self.clone()))
|
||||
}
|
||||
fn get_file_type(&self) -> Filetype {
|
||||
self.file_type
|
||||
}
|
||||
fn get_rights(&self) -> HandleRights {
|
||||
self.rights.get()
|
||||
}
|
||||
fn set_rights(&self, new_rights: HandleRights) {
|
||||
self.rights.set(new_rights)
|
||||
}
|
||||
// FdOps
|
||||
fn fdstat_get(&self) -> Result<types::Fdflags> {
|
||||
fd::fdstat_get(&*self.as_file()?)
|
||||
}
|
||||
fn fdstat_set_flags(&self, fdflags: types::Fdflags) -> Result<()> {
|
||||
if let Some(_) = fd::fdstat_set_flags(&*self.as_file()?, fdflags)? {
|
||||
// OK, this means we should somehow update the underlying os handle,
|
||||
// and we can't do that with `std::io::std{in, out, err}`, so we'll
|
||||
// panic for now.
|
||||
panic!("Tried updating Fdflags on Stdio handle by re-opening as file!");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn read_vectored(&self, iovs: &mut [io::IoSliceMut]) -> Result<usize> {
|
||||
let nread = io::stdin().read_vectored(iovs)?;
|
||||
Ok(nread)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) trait StdoutExt: Sized {
|
||||
/// Create `Stdout` from `io::stdout`.
|
||||
fn stdout() -> io::Result<Box<dyn Handle>>;
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub(crate) struct Stdout {
|
||||
pub(crate) file_type: Filetype,
|
||||
pub(crate) rights: Cell<HandleRights>,
|
||||
}
|
||||
|
||||
impl Handle for Stdout {
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
fn try_clone(&self) -> io::Result<Box<dyn Handle>> {
|
||||
Ok(Box::new(self.clone()))
|
||||
}
|
||||
fn get_file_type(&self) -> Filetype {
|
||||
self.file_type
|
||||
}
|
||||
fn get_rights(&self) -> HandleRights {
|
||||
self.rights.get()
|
||||
}
|
||||
fn set_rights(&self, new_rights: HandleRights) {
|
||||
self.rights.set(new_rights)
|
||||
}
|
||||
// FdOps
|
||||
fn fdstat_get(&self) -> Result<types::Fdflags> {
|
||||
fd::fdstat_get(&*self.as_file()?)
|
||||
}
|
||||
fn fdstat_set_flags(&self, fdflags: types::Fdflags) -> Result<()> {
|
||||
if let Some(_) = fd::fdstat_set_flags(&*self.as_file()?, fdflags)? {
|
||||
// OK, this means we should somehow update the underlying os handle,
|
||||
// and we can't do that with `std::io::std{in, out, err}`, so we'll
|
||||
// panic for now.
|
||||
panic!("Tried updating Fdflags on Stdio handle by re-opening as file!");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn write_vectored(&self, iovs: &[io::IoSlice]) -> Result<usize> {
|
||||
// lock for the duration of the scope
|
||||
let stdout = io::stdout();
|
||||
let mut stdout = stdout.lock();
|
||||
let nwritten = if self.is_tty() {
|
||||
SandboxedTTYWriter::new(&mut stdout).write_vectored(&iovs)?
|
||||
} else {
|
||||
stdout.write_vectored(iovs)?
|
||||
};
|
||||
stdout.flush()?;
|
||||
Ok(nwritten)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) trait StderrExt: Sized {
|
||||
/// Create `Stderr` from `io::stderr`.
|
||||
fn stderr() -> io::Result<Box<dyn Handle>>;
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub(crate) struct Stderr {
|
||||
pub(crate) file_type: Filetype,
|
||||
pub(crate) rights: Cell<HandleRights>,
|
||||
}
|
||||
|
||||
impl Handle for Stderr {
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
fn try_clone(&self) -> io::Result<Box<dyn Handle>> {
|
||||
Ok(Box::new(self.clone()))
|
||||
}
|
||||
fn get_file_type(&self) -> Filetype {
|
||||
self.file_type
|
||||
}
|
||||
fn get_rights(&self) -> HandleRights {
|
||||
self.rights.get()
|
||||
}
|
||||
fn set_rights(&self, new_rights: HandleRights) {
|
||||
self.rights.set(new_rights)
|
||||
}
|
||||
// FdOps
|
||||
fn fdstat_get(&self) -> Result<types::Fdflags> {
|
||||
fd::fdstat_get(&*self.as_file()?)
|
||||
}
|
||||
fn fdstat_set_flags(&self, fdflags: types::Fdflags) -> Result<()> {
|
||||
if let Some(_) = fd::fdstat_set_flags(&*self.as_file()?, fdflags)? {
|
||||
// OK, this means we should somehow update the underlying os handle,
|
||||
// and we can't do that with `std::io::std{in, out, err}`, so we'll
|
||||
// panic for now.
|
||||
panic!("Tried updating Fdflags on Stdio handle by re-opening as file!");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn write_vectored(&self, iovs: &[io::IoSlice]) -> Result<usize> {
|
||||
// Always sanitize stderr, even if it's not directly connected to a tty,
|
||||
// because stderr is meant for diagnostics rather than binary output,
|
||||
// and may be redirected to a file which could end up being displayed
|
||||
// on a tty later.
|
||||
let nwritten = SandboxedTTYWriter::new(&mut io::stderr()).write_vectored(&iovs)?;
|
||||
Ok(nwritten)
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
pub(crate) mod osfile;
|
||||
pub(crate) mod osdir;
|
||||
pub(crate) mod path;
|
||||
|
||||
pub(crate) const O_RSYNC: yanix::file::OFlag = yanix::file::OFlag::SYNC;
|
||||
|
||||
46
crates/wasi-common/src/sys/unix/bsd/osdir.rs
Normal file
46
crates/wasi-common/src/sys/unix/bsd/osdir.rs
Normal file
@@ -0,0 +1,46 @@
|
||||
use crate::handle::HandleRights;
|
||||
use crate::sys::sys_impl::oshandle::RawOsHandle;
|
||||
use crate::wasi::Result;
|
||||
use std::cell::{Cell, RefCell, RefMut};
|
||||
use std::io;
|
||||
use yanix::dir::Dir;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct OsDir {
|
||||
pub(crate) rights: Cell<HandleRights>,
|
||||
pub(crate) handle: RawOsHandle,
|
||||
// When the client makes a `fd_readdir` syscall on this descriptor,
|
||||
// we will need to cache the `libc::DIR` pointer manually in order
|
||||
// to be able to seek on it later. While on Linux, this is handled
|
||||
// by the OS, BSD Unixes require the client to do this caching.
|
||||
//
|
||||
// This comes directly from the BSD man pages on `readdir`:
|
||||
// > Values returned by telldir() are good only for the lifetime
|
||||
// > of the DIR pointer, dirp, from which they are derived.
|
||||
// > If the directory is closed and then reopened, prior values
|
||||
// > returned by telldir() will no longer be valid.
|
||||
stream_ptr: RefCell<Dir>,
|
||||
}
|
||||
|
||||
impl OsDir {
|
||||
pub(crate) fn new(rights: HandleRights, handle: RawOsHandle) -> io::Result<Self> {
|
||||
let rights = Cell::new(rights);
|
||||
// We need to duplicate the handle, because `opendir(3)`:
|
||||
// Upon successful return from fdopendir(), the file descriptor is under
|
||||
// control of the system, and if any attempt is made to close the file
|
||||
// descriptor, or to modify the state of the associated description other
|
||||
// than by means of closedir(), readdir(), readdir_r(), or rewinddir(),
|
||||
// the behaviour is undefined.
|
||||
let stream_ptr = Dir::from(handle.try_clone()?)?;
|
||||
let stream_ptr = RefCell::new(stream_ptr);
|
||||
Ok(Self {
|
||||
rights,
|
||||
handle,
|
||||
stream_ptr,
|
||||
})
|
||||
}
|
||||
/// Returns the `Dir` stream pointer associated with this `OsDir`.
|
||||
pub(crate) fn stream_ptr(&self) -> Result<RefMut<Dir>> {
|
||||
Ok(self.stream_ptr.borrow_mut())
|
||||
}
|
||||
}
|
||||
@@ -1,109 +0,0 @@
|
||||
use crate::sys::oshandle::AsFile;
|
||||
use crate::wasi::Result;
|
||||
use std::cell::{Cell, RefCell, RefMut};
|
||||
use std::fs::File;
|
||||
use std::io;
|
||||
use std::mem::ManuallyDrop;
|
||||
use std::os::unix::prelude::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
|
||||
use yanix::dir::Dir;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct OsFile {
|
||||
fd: Cell<RawFd>,
|
||||
// In case that this `OsHandle` actually refers to a directory,
|
||||
// when the client makes a `fd_readdir` syscall on this descriptor,
|
||||
// we will need to cache the `libc::DIR` pointer manually in order
|
||||
// to be able to seek on it later. While on Linux, this is handled
|
||||
// by the OS, BSD Unixes require the client to do this caching.
|
||||
//
|
||||
// This comes directly from the BSD man pages on `readdir`:
|
||||
// > Values returned by telldir() are good only for the lifetime
|
||||
// > of the DIR pointer, dirp, from which they are derived.
|
||||
// > If the directory is closed and then reopened, prior values
|
||||
// > returned by telldir() will no longer be valid.
|
||||
dir: RefCell<Option<Dir>>,
|
||||
}
|
||||
|
||||
impl OsFile {
|
||||
/// Consumes `other` taking the ownership of the underlying
|
||||
/// `RawFd` file descriptor.
|
||||
///
|
||||
/// Note that the state of `Dir` stream pointer *will* not be carried
|
||||
/// across from `other` to `self`.
|
||||
pub(crate) fn update_from(&self, other: Self) {
|
||||
let new_fd = other.into_raw_fd();
|
||||
let old_fd = self.fd.get();
|
||||
self.fd.set(new_fd);
|
||||
// We need to remember to close the old_fd.
|
||||
unsafe {
|
||||
File::from_raw_fd(old_fd);
|
||||
}
|
||||
}
|
||||
/// Clones `self` uninitializing the `Dir` stream pointer
|
||||
/// (if any).
|
||||
pub(crate) fn try_clone(&self) -> io::Result<Self> {
|
||||
let fd = self.as_file().try_clone()?;
|
||||
Ok(Self {
|
||||
fd: Cell::new(fd.into_raw_fd()),
|
||||
dir: RefCell::new(None),
|
||||
})
|
||||
}
|
||||
/// Returns the `Dir` stream pointer associated with
|
||||
/// this instance.
|
||||
///
|
||||
/// Initializes the `Dir` stream pointer if `None`.
|
||||
pub(crate) fn dir_stream(&self) -> Result<RefMut<Dir>> {
|
||||
if self.dir.borrow().is_none() {
|
||||
// We need to duplicate the fd, because `opendir(3)`:
|
||||
// Upon successful return from fdopendir(), the file descriptor is under
|
||||
// control of the system, and if any attempt is made to close the file
|
||||
// descriptor, or to modify the state of the associated description other
|
||||
// than by means of closedir(), readdir(), readdir_r(), or rewinddir(),
|
||||
// the behaviour is undefined.
|
||||
let file = self.try_clone()?;
|
||||
let d = Dir::from(file)?;
|
||||
*self.dir.borrow_mut() = Some(d);
|
||||
}
|
||||
Ok(RefMut::map(self.dir.borrow_mut(), |dir| {
|
||||
dir.as_mut().unwrap()
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for OsFile {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
File::from_raw_fd(self.as_raw_fd());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRawFd for OsFile {
|
||||
fn as_raw_fd(&self) -> RawFd {
|
||||
self.fd.get()
|
||||
}
|
||||
}
|
||||
|
||||
impl FromRawFd for OsFile {
|
||||
unsafe fn from_raw_fd(fd: RawFd) -> Self {
|
||||
Self {
|
||||
fd: Cell::new(fd),
|
||||
dir: RefCell::new(None),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoRawFd for OsFile {
|
||||
fn into_raw_fd(self) -> RawFd {
|
||||
// We need to prevent dropping of the OsFile
|
||||
let wrapped = ManuallyDrop::new(self);
|
||||
wrapped.fd.get()
|
||||
}
|
||||
}
|
||||
|
||||
impl AsFile for OsFile {
|
||||
fn as_file(&self) -> ManuallyDrop<File> {
|
||||
let file = unsafe { File::from_raw_fd(self.fd.get()) };
|
||||
ManuallyDrop::new(file)
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
use super::osfile::OsFile;
|
||||
use crate::sys::osdir::OsDir;
|
||||
use crate::wasi::{Errno, Result};
|
||||
use std::os::unix::prelude::AsRawFd;
|
||||
|
||||
pub(crate) fn unlink_file(dirfd: &OsFile, path: &str) -> Result<()> {
|
||||
pub(crate) fn unlink_file(dirfd: &OsDir, path: &str) -> Result<()> {
|
||||
use yanix::file::{unlinkat, AtFlag};
|
||||
match unsafe { unlinkat(dirfd.as_raw_fd(), path, AtFlag::empty()) } {
|
||||
Err(err) => {
|
||||
@@ -35,7 +35,7 @@ pub(crate) fn unlink_file(dirfd: &OsFile, path: &str) -> Result<()> {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn symlink(old_path: &str, new_dirfd: &OsFile, new_path: &str) -> Result<()> {
|
||||
pub(crate) fn symlink(old_path: &str, new_dirfd: &OsDir, new_path: &str) -> Result<()> {
|
||||
use yanix::file::{fstatat, symlinkat, AtFlag};
|
||||
|
||||
log::debug!("path_symlink old_path = {:?}", old_path);
|
||||
@@ -69,9 +69,9 @@ pub(crate) fn symlink(old_path: &str, new_dirfd: &OsFile, new_path: &str) -> Res
|
||||
}
|
||||
|
||||
pub(crate) fn rename(
|
||||
old_dirfd: &OsFile,
|
||||
old_dirfd: &OsDir,
|
||||
old_path: &str,
|
||||
new_dirfd: &OsFile,
|
||||
new_dirfd: &OsDir,
|
||||
new_path: &str,
|
||||
) -> Result<()> {
|
||||
use yanix::file::{fstatat, renameat, AtFlag};
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#[path = "../linux/osfile.rs"]
|
||||
pub(crate) mod osfile;
|
||||
#[path = "../linux/osdir.rs"]
|
||||
pub(crate) mod osdir;
|
||||
#[path = "../linux/path.rs"]
|
||||
pub(crate) mod path;
|
||||
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
use super::oshandle::OsFile;
|
||||
use super::oshandle::RawOsHandle;
|
||||
use crate::sys::osdir::OsDir;
|
||||
use crate::sys::osfile::OsFile;
|
||||
use crate::wasi::{self, types, Result};
|
||||
use std::convert::TryInto;
|
||||
use std::fs::File;
|
||||
@@ -9,7 +11,7 @@ pub(crate) fn fdstat_get(fd: &File) -> Result<types::Fdflags> {
|
||||
Ok(fdflags.into())
|
||||
}
|
||||
|
||||
pub(crate) fn fdstat_set_flags(fd: &File, fdflags: types::Fdflags) -> Result<Option<OsFile>> {
|
||||
pub(crate) fn fdstat_set_flags(fd: &File, fdflags: types::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.
|
||||
@@ -45,14 +47,14 @@ pub(crate) fn filestat_get(file: &File) -> Result<types::Filestat> {
|
||||
}
|
||||
|
||||
pub(crate) fn readdir<'a>(
|
||||
file: &'a OsFile,
|
||||
dirfd: &'a OsDir,
|
||||
cookie: types::Dircookie,
|
||||
) -> Result<Box<dyn Iterator<Item = Result<(types::Dirent, String)>> + 'a>> {
|
||||
use yanix::dir::{DirIter, Entry, EntryExt, SeekLoc};
|
||||
|
||||
// Get an instance of `Dir`; this is host-specific due to intricasies
|
||||
// of managing a dir stream between Linux and BSD *nixes
|
||||
let mut dir = file.dir_stream()?;
|
||||
let mut dir = dirfd.stream_ptr()?;
|
||||
|
||||
// Seek if needed. Unless cookie is wasi::__WASI_DIRCOOKIE_START,
|
||||
// new items may not be returned to the caller.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
pub(crate) mod osfile;
|
||||
pub(crate) mod osdir;
|
||||
pub(crate) mod path;
|
||||
|
||||
pub(crate) const O_RSYNC: yanix::file::OFlag = yanix::file::OFlag::RSYNC;
|
||||
|
||||
35
crates/wasi-common/src/sys/unix/linux/osdir.rs
Normal file
35
crates/wasi-common/src/sys/unix/linux/osdir.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
use crate::handle::HandleRights;
|
||||
use crate::sys::sys_impl::oshandle::RawOsHandle;
|
||||
use crate::wasi::Result;
|
||||
use std::cell::Cell;
|
||||
use std::io;
|
||||
use yanix::dir::Dir;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct OsDir {
|
||||
pub(crate) rights: Cell<HandleRights>,
|
||||
pub(crate) handle: RawOsHandle,
|
||||
}
|
||||
|
||||
impl OsDir {
|
||||
pub(crate) fn new(rights: HandleRights, handle: RawOsHandle) -> io::Result<Self> {
|
||||
let rights = Cell::new(rights);
|
||||
Ok(Self { rights, handle })
|
||||
}
|
||||
/// Returns the `Dir` stream pointer associated with this `OsDir`.
|
||||
pub(crate) fn stream_ptr(&self) -> Result<Box<Dir>> {
|
||||
// We need to duplicate the handle, because `opendir(3)`:
|
||||
// After a successful call to fdopendir(), fd is used internally by the implementation,
|
||||
// and should not otherwise be used by the application.
|
||||
// `opendir(3p)` also says that it's undefined behavior to
|
||||
// modify the state of the fd in a different way than by accessing DIR*.
|
||||
//
|
||||
// Still, rewinddir will be needed because the two file descriptors
|
||||
// share progress. But we can safely execute closedir now.
|
||||
let file = self.handle.try_clone()?;
|
||||
// TODO This doesn't look very clean. Can we do something about it?
|
||||
// Boxing is needed here in order to satisfy `yanix`'s trait requirement for the `DirIter`
|
||||
// where `T: Deref<Target = Dir>`.
|
||||
Ok(Box::new(Dir::from(file)?))
|
||||
}
|
||||
}
|
||||
@@ -1,82 +0,0 @@
|
||||
use crate::sys::oshandle::AsFile;
|
||||
use crate::wasi::Result;
|
||||
use std::cell::Cell;
|
||||
use std::fs::File;
|
||||
use std::io;
|
||||
use std::mem::ManuallyDrop;
|
||||
use std::os::unix::prelude::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
|
||||
use yanix::dir::Dir;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct OsFile(Cell<RawFd>);
|
||||
|
||||
impl OsFile {
|
||||
/// Consumes `other` taking the ownership of the underlying
|
||||
/// `RawFd` file descriptor.
|
||||
pub(crate) fn update_from(&self, other: Self) {
|
||||
let new_fd = other.into_raw_fd();
|
||||
let old_fd = self.0.get();
|
||||
self.0.set(new_fd);
|
||||
// We need to remember to close the old_fd.
|
||||
unsafe {
|
||||
File::from_raw_fd(old_fd);
|
||||
}
|
||||
}
|
||||
/// Clones `self`.
|
||||
pub(crate) fn try_clone(&self) -> io::Result<Self> {
|
||||
let fd = self.as_file().try_clone()?;
|
||||
Ok(Self(Cell::new(fd.into_raw_fd())))
|
||||
}
|
||||
/// Returns the `Dir` stream pointer associated with
|
||||
/// this instance.
|
||||
pub(crate) fn dir_stream(&self) -> Result<Box<Dir>> {
|
||||
// We need to duplicate the fd, because `opendir(3)`:
|
||||
// After a successful call to fdopendir(), fd is used internally by the implementation,
|
||||
// and should not otherwise be used by the application.
|
||||
// `opendir(3p)` also says that it's undefined behavior to
|
||||
// modify the state of the fd in a different way than by accessing DIR*.
|
||||
//
|
||||
// Still, rewinddir will be needed because the two file descriptors
|
||||
// share progress. But we can safely execute closedir now.
|
||||
let file = self.try_clone()?;
|
||||
// TODO This doesn't look very clean. Can we do something about it?
|
||||
// Boxing is needed here in order to satisfy `yanix`'s trait requirement for the `DirIter`
|
||||
// where `T: Deref<Target = Dir>`.
|
||||
Ok(Box::new(Dir::from(file)?))
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for OsFile {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
File::from_raw_fd(self.as_raw_fd());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRawFd for OsFile {
|
||||
fn as_raw_fd(&self) -> RawFd {
|
||||
self.0.get()
|
||||
}
|
||||
}
|
||||
|
||||
impl FromRawFd for OsFile {
|
||||
unsafe fn from_raw_fd(fd: RawFd) -> Self {
|
||||
Self(Cell::new(fd))
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoRawFd for OsFile {
|
||||
fn into_raw_fd(self) -> RawFd {
|
||||
// We need to prevent dropping of the OsFile
|
||||
let wrapped = ManuallyDrop::new(self);
|
||||
wrapped.0.get()
|
||||
}
|
||||
}
|
||||
|
||||
impl AsFile for OsFile {
|
||||
fn as_file(&self) -> ManuallyDrop<File> {
|
||||
let file = unsafe { File::from_raw_fd(self.0.get()) };
|
||||
ManuallyDrop::new(file)
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,14 @@
|
||||
use super::osfile::OsFile;
|
||||
use crate::sys::osdir::OsDir;
|
||||
use crate::wasi::Result;
|
||||
use std::os::unix::prelude::AsRawFd;
|
||||
|
||||
pub(crate) fn unlink_file(dirfd: &OsFile, path: &str) -> Result<()> {
|
||||
pub(crate) fn unlink_file(dirfd: &OsDir, path: &str) -> Result<()> {
|
||||
use yanix::file::{unlinkat, AtFlag};
|
||||
unsafe { unlinkat(dirfd.as_raw_fd(), path, AtFlag::empty())? };
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn symlink(old_path: &str, new_dirfd: &OsFile, new_path: &str) -> Result<()> {
|
||||
pub(crate) fn symlink(old_path: &str, new_dirfd: &OsDir, new_path: &str) -> Result<()> {
|
||||
use yanix::file::symlinkat;
|
||||
|
||||
log::debug!("path_symlink old_path = {:?}", old_path);
|
||||
@@ -23,9 +23,9 @@ pub(crate) fn symlink(old_path: &str, new_dirfd: &OsFile, new_path: &str) -> Res
|
||||
}
|
||||
|
||||
pub(crate) fn rename(
|
||||
old_dirfd: &OsFile,
|
||||
old_dirfd: &OsDir,
|
||||
old_path: &str,
|
||||
new_dirfd: &OsFile,
|
||||
new_dirfd: &OsDir,
|
||||
new_path: &str,
|
||||
) -> Result<()> {
|
||||
use yanix::file::renameat;
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
pub(crate) mod clock;
|
||||
pub(crate) mod fd;
|
||||
pub(crate) mod osdir;
|
||||
pub(crate) mod osfile;
|
||||
pub(crate) mod oshandle;
|
||||
pub(crate) mod osother;
|
||||
pub(crate) mod path;
|
||||
pub(crate) mod poll;
|
||||
pub(crate) mod stdio;
|
||||
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(target_os = "linux")] {
|
||||
@@ -22,16 +26,105 @@ cfg_if::cfg_if! {
|
||||
}
|
||||
}
|
||||
|
||||
use crate::wasi::{types, Errno, Result};
|
||||
use crate::handle::HandleRights;
|
||||
use crate::sys::AsFile;
|
||||
use crate::wasi::{types, Errno, Result, RightsExt};
|
||||
use std::convert::{TryFrom, TryInto};
|
||||
use std::fs::File;
|
||||
use std::io;
|
||||
use std::mem::ManuallyDrop;
|
||||
use std::os::unix::prelude::{AsRawFd, FileTypeExt, FromRawFd};
|
||||
use std::path::Path;
|
||||
use yanix::clock::ClockId;
|
||||
use yanix::file::{AtFlag, OFlag};
|
||||
|
||||
pub(crate) use sys_impl::*;
|
||||
|
||||
impl<T: AsRawFd> AsFile for T {
|
||||
fn as_file(&self) -> io::Result<ManuallyDrop<File>> {
|
||||
let file = unsafe { File::from_raw_fd(self.as_raw_fd()) };
|
||||
Ok(ManuallyDrop::new(file))
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn get_file_type(file: &File) -> io::Result<types::Filetype> {
|
||||
let ft = file.metadata()?.file_type();
|
||||
let file_type = if ft.is_block_device() {
|
||||
log::debug!("Host fd {:?} is a block device", file.as_raw_fd());
|
||||
types::Filetype::BlockDevice
|
||||
} else if ft.is_char_device() {
|
||||
log::debug!("Host fd {:?} is a char device", file.as_raw_fd());
|
||||
types::Filetype::CharacterDevice
|
||||
} else if ft.is_dir() {
|
||||
log::debug!("Host fd {:?} is a directory", file.as_raw_fd());
|
||||
types::Filetype::Directory
|
||||
} else if ft.is_file() {
|
||||
log::debug!("Host fd {:?} is a file", file.as_raw_fd());
|
||||
types::Filetype::RegularFile
|
||||
} else if ft.is_socket() {
|
||||
log::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,
|
||||
_ => return Err(io::Error::from_raw_os_error(libc::EINVAL)),
|
||||
}
|
||||
} else if ft.is_fifo() {
|
||||
log::debug!("Host fd {:?} is a fifo", file.as_raw_fd());
|
||||
types::Filetype::Unknown
|
||||
} else {
|
||||
log::debug!("Host fd {:?} is unknown", file.as_raw_fd());
|
||||
return Err(io::Error::from_raw_os_error(libc::EINVAL));
|
||||
};
|
||||
Ok(file_type)
|
||||
}
|
||||
|
||||
pub(super) fn get_rights(file: &File, file_type: &types::Filetype) -> io::Result<HandleRights> {
|
||||
use yanix::{fcntl, file::OFlag};
|
||||
let (base, inheriting) = match file_type {
|
||||
types::Filetype::BlockDevice => (
|
||||
types::Rights::block_device_base(),
|
||||
types::Rights::block_device_inheriting(),
|
||||
),
|
||||
types::Filetype::CharacterDevice => {
|
||||
use yanix::file::isatty;
|
||||
if unsafe { isatty(file.as_raw_fd())? } {
|
||||
(types::Rights::tty_base(), types::Rights::tty_base())
|
||||
} else {
|
||||
(
|
||||
types::Rights::character_device_base(),
|
||||
types::Rights::character_device_inheriting(),
|
||||
)
|
||||
}
|
||||
}
|
||||
types::Filetype::SocketDgram | types::Filetype::SocketStream => (
|
||||
types::Rights::socket_base(),
|
||||
types::Rights::socket_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(),
|
||||
),
|
||||
};
|
||||
let mut rights = HandleRights::new(base, inheriting);
|
||||
let flags = unsafe { fcntl::get_status_flags(file.as_raw_fd())? };
|
||||
let accmode = flags & OFlag::ACCMODE;
|
||||
if accmode == OFlag::RDONLY {
|
||||
rights.base &= !types::Rights::FD_WRITE;
|
||||
} else if accmode == OFlag::WRONLY {
|
||||
rights.base &= !types::Rights::FD_READ;
|
||||
}
|
||||
Ok(rights)
|
||||
}
|
||||
|
||||
pub fn preopen_dir<P: AsRef<Path>>(path: P) -> io::Result<File> {
|
||||
File::open(path)
|
||||
}
|
||||
|
||||
39
crates/wasi-common/src/sys/unix/osdir.rs
Normal file
39
crates/wasi-common/src/sys/unix/osdir.rs
Normal file
@@ -0,0 +1,39 @@
|
||||
use super::oshandle::RawOsHandle;
|
||||
use crate::handle::HandleRights;
|
||||
use crate::wasi::{types, RightsExt};
|
||||
use std::convert::TryFrom;
|
||||
use std::fs::File;
|
||||
use std::io;
|
||||
use std::os::unix::prelude::{AsRawFd, FromRawFd, IntoRawFd};
|
||||
|
||||
pub(crate) use super::sys_impl::osdir::OsDir;
|
||||
|
||||
impl TryFrom<File> for OsDir {
|
||||
type Error = io::Error;
|
||||
|
||||
fn try_from(file: File) -> io::Result<Self> {
|
||||
let ft = file.metadata()?.file_type();
|
||||
if !ft.is_dir() {
|
||||
return Err(io::Error::from_raw_os_error(libc::EINVAL));
|
||||
}
|
||||
let rights = get_rights(&file)?;
|
||||
let handle = unsafe { RawOsHandle::from_raw_fd(file.into_raw_fd()) };
|
||||
Self::new(rights, handle)
|
||||
}
|
||||
}
|
||||
|
||||
fn get_rights(file: &File) -> io::Result<HandleRights> {
|
||||
use yanix::{fcntl, file::OFlag};
|
||||
let mut rights = HandleRights::new(
|
||||
types::Rights::directory_base(),
|
||||
types::Rights::directory_inheriting(),
|
||||
);
|
||||
let flags = unsafe { fcntl::get_status_flags(file.as_raw_fd())? };
|
||||
let accmode = flags & OFlag::ACCMODE;
|
||||
if accmode == OFlag::RDONLY {
|
||||
rights.base &= !types::Rights::FD_WRITE;
|
||||
} else if accmode == OFlag::WRONLY {
|
||||
rights.base &= !types::Rights::FD_READ;
|
||||
}
|
||||
Ok(rights)
|
||||
}
|
||||
38
crates/wasi-common/src/sys/unix/osfile.rs
Normal file
38
crates/wasi-common/src/sys/unix/osfile.rs
Normal file
@@ -0,0 +1,38 @@
|
||||
use super::oshandle::RawOsHandle;
|
||||
use crate::handle::HandleRights;
|
||||
use crate::sys::osfile::OsFile;
|
||||
use crate::wasi::{types, RightsExt};
|
||||
use std::convert::TryFrom;
|
||||
use std::fs::File;
|
||||
use std::io;
|
||||
use std::os::unix::prelude::{AsRawFd, FromRawFd, IntoRawFd};
|
||||
|
||||
impl TryFrom<File> for OsFile {
|
||||
type Error = io::Error;
|
||||
|
||||
fn try_from(file: File) -> io::Result<Self> {
|
||||
let ft = file.metadata()?.file_type();
|
||||
if !ft.is_file() {
|
||||
return Err(io::Error::from_raw_os_error(libc::EINVAL));
|
||||
}
|
||||
let rights = get_rights(&file)?;
|
||||
let handle = unsafe { RawOsHandle::from_raw_fd(file.into_raw_fd()) };
|
||||
Ok(Self::new(rights, handle))
|
||||
}
|
||||
}
|
||||
|
||||
fn get_rights(file: &File) -> io::Result<HandleRights> {
|
||||
use yanix::{fcntl, file::OFlag};
|
||||
let mut rights = HandleRights::new(
|
||||
types::Rights::regular_file_base(),
|
||||
types::Rights::regular_file_inheriting(),
|
||||
);
|
||||
let flags = unsafe { fcntl::get_status_flags(file.as_raw_fd())? };
|
||||
let accmode = flags & OFlag::ACCMODE;
|
||||
if accmode == OFlag::RDONLY {
|
||||
rights.base &= !types::Rights::FD_WRITE;
|
||||
} else if accmode == OFlag::WRONLY {
|
||||
rights.base &= !types::Rights::FD_READ;
|
||||
}
|
||||
Ok(rights)
|
||||
}
|
||||
@@ -1,123 +1,40 @@
|
||||
use crate::entry::EntryRights;
|
||||
use crate::sys::oshandle::{AsFile, OsHandle, OsHandleExt};
|
||||
use crate::wasi::{types, RightsExt};
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::fs::File;
|
||||
use std::io;
|
||||
use std::mem::ManuallyDrop;
|
||||
use std::os::unix::prelude::{AsRawFd, FileTypeExt, FromRawFd, IntoRawFd, RawFd};
|
||||
use std::os::unix::prelude::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
|
||||
|
||||
pub(crate) use super::sys_impl::osfile::*;
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct RawOsHandle(File);
|
||||
|
||||
impl AsRawFd for OsHandle {
|
||||
impl RawOsHandle {
|
||||
/// Tries clone `self`.
|
||||
pub(crate) fn try_clone(&self) -> io::Result<Self> {
|
||||
let fd = self.0.try_clone()?;
|
||||
Ok(unsafe { Self::from_raw_fd(fd.into_raw_fd()) })
|
||||
}
|
||||
/// Consumes `other` taking the ownership of the underlying
|
||||
/// `RawFd` file descriptor.
|
||||
///
|
||||
/// Note that the state of `Dir` stream pointer *will* not be carried
|
||||
/// across from `other` to `self`.
|
||||
pub(crate) fn update_from(&self, _other: Self) {
|
||||
panic!("RawOsHandle::update_from should never be issued on Unix!")
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRawFd for RawOsHandle {
|
||||
fn as_raw_fd(&self) -> RawFd {
|
||||
match self {
|
||||
Self::OsFile(file) => file.as_raw_fd(),
|
||||
Self::Stdin => io::stdin().as_raw_fd(),
|
||||
Self::Stdout => io::stdout().as_raw_fd(),
|
||||
Self::Stderr => io::stderr().as_raw_fd(),
|
||||
}
|
||||
self.0.as_raw_fd()
|
||||
}
|
||||
}
|
||||
|
||||
impl AsFile for OsHandle {
|
||||
fn as_file(&self) -> ManuallyDrop<File> {
|
||||
let file = unsafe { File::from_raw_fd(self.as_raw_fd()) };
|
||||
ManuallyDrop::new(file)
|
||||
impl IntoRawFd for RawOsHandle {
|
||||
fn into_raw_fd(self) -> RawFd {
|
||||
self.0.into_raw_fd()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<File> for OsHandle {
|
||||
fn from(file: File) -> Self {
|
||||
Self::from(unsafe { OsFile::from_raw_fd(file.into_raw_fd()) })
|
||||
}
|
||||
}
|
||||
|
||||
impl OsHandleExt for OsHandle {
|
||||
fn get_file_type(&self) -> io::Result<types::Filetype> {
|
||||
let file = self.as_file();
|
||||
let ft = file.metadata()?.file_type();
|
||||
let file_type = if ft.is_block_device() {
|
||||
log::debug!("Host fd {:?} is a block device", self.as_raw_fd());
|
||||
types::Filetype::BlockDevice
|
||||
} else if ft.is_char_device() {
|
||||
log::debug!("Host fd {:?} is a char device", self.as_raw_fd());
|
||||
types::Filetype::CharacterDevice
|
||||
} else if ft.is_dir() {
|
||||
log::debug!("Host fd {:?} is a directory", self.as_raw_fd());
|
||||
types::Filetype::Directory
|
||||
} else if ft.is_file() {
|
||||
log::debug!("Host fd {:?} is a file", self.as_raw_fd());
|
||||
types::Filetype::RegularFile
|
||||
} else if ft.is_socket() {
|
||||
log::debug!("Host fd {:?} is a socket", self.as_raw_fd());
|
||||
use yanix::socket::{get_socket_type, SockType};
|
||||
match unsafe { get_socket_type(self.as_raw_fd())? } {
|
||||
SockType::Datagram => types::Filetype::SocketDgram,
|
||||
SockType::Stream => types::Filetype::SocketStream,
|
||||
_ => return Err(io::Error::from_raw_os_error(libc::EINVAL)),
|
||||
}
|
||||
} else if ft.is_fifo() {
|
||||
log::debug!("Host fd {:?} is a fifo", self.as_raw_fd());
|
||||
types::Filetype::Unknown
|
||||
} else {
|
||||
log::debug!("Host fd {:?} is unknown", self.as_raw_fd());
|
||||
return Err(io::Error::from_raw_os_error(libc::EINVAL));
|
||||
};
|
||||
|
||||
Ok(file_type)
|
||||
}
|
||||
|
||||
fn get_rights(&self, file_type: types::Filetype) -> io::Result<EntryRights> {
|
||||
use yanix::{fcntl, file::OFlag};
|
||||
let (base, inheriting) = match file_type {
|
||||
types::Filetype::BlockDevice => (
|
||||
types::Rights::block_device_base(),
|
||||
types::Rights::block_device_inheriting(),
|
||||
),
|
||||
types::Filetype::CharacterDevice => {
|
||||
use yanix::file::isatty;
|
||||
if unsafe { isatty(self.as_raw_fd())? } {
|
||||
(types::Rights::tty_base(), types::Rights::tty_base())
|
||||
} else {
|
||||
(
|
||||
types::Rights::character_device_base(),
|
||||
types::Rights::character_device_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(),
|
||||
),
|
||||
types::Filetype::SocketDgram | types::Filetype::SocketStream => (
|
||||
types::Rights::socket_base(),
|
||||
types::Rights::socket_inheriting(),
|
||||
),
|
||||
types::Filetype::SymbolicLink | types::Filetype::Unknown => (
|
||||
types::Rights::regular_file_base(),
|
||||
types::Rights::regular_file_inheriting(),
|
||||
),
|
||||
};
|
||||
let mut rights = EntryRights::new(base, inheriting);
|
||||
let flags = unsafe { fcntl::get_status_flags(self.as_raw_fd())? };
|
||||
let accmode = flags & OFlag::ACCMODE;
|
||||
if accmode == OFlag::RDONLY {
|
||||
rights.base &= !types::Rights::FD_WRITE;
|
||||
} else if accmode == OFlag::WRONLY {
|
||||
rights.base &= !types::Rights::FD_READ;
|
||||
}
|
||||
Ok(rights)
|
||||
}
|
||||
|
||||
fn from_null() -> io::Result<Self> {
|
||||
let file = OpenOptions::new()
|
||||
.read(true)
|
||||
.write(true)
|
||||
.open("/dev/null")?;
|
||||
Ok(Self::from(file))
|
||||
impl FromRawFd for RawOsHandle {
|
||||
unsafe fn from_raw_fd(raw: RawFd) -> Self {
|
||||
Self(File::from_raw_fd(raw))
|
||||
}
|
||||
}
|
||||
|
||||
34
crates/wasi-common/src/sys/unix/osother.rs
Normal file
34
crates/wasi-common/src/sys/unix/osother.rs
Normal file
@@ -0,0 +1,34 @@
|
||||
use super::oshandle::RawOsHandle;
|
||||
use super::{get_file_type, get_rights};
|
||||
use crate::handle::Handle;
|
||||
use crate::sys::osother::{OsOther, OsOtherExt};
|
||||
use crate::wasi::types;
|
||||
use std::convert::TryFrom;
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::io;
|
||||
use std::os::unix::prelude::{FromRawFd, IntoRawFd};
|
||||
|
||||
impl TryFrom<File> for OsOther {
|
||||
type Error = io::Error;
|
||||
|
||||
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 {
|
||||
return Err(io::Error::from_raw_os_error(libc::EINVAL));
|
||||
}
|
||||
let rights = get_rights(&file, &file_type)?;
|
||||
let handle = unsafe { RawOsHandle::from_raw_fd(file.into_raw_fd()) };
|
||||
Ok(Self::new(file_type, rights, handle))
|
||||
}
|
||||
}
|
||||
|
||||
impl OsOtherExt for OsOther {
|
||||
fn from_null() -> io::Result<Box<dyn Handle>> {
|
||||
let file = OpenOptions::new()
|
||||
.read(true)
|
||||
.write(true)
|
||||
.open("/dev/null")?;
|
||||
let file = Self::try_from(file)?;
|
||||
Ok(Box::new(file))
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,9 @@
|
||||
use super::oshandle::OsFile;
|
||||
use crate::entry::EntryRights;
|
||||
use crate::sys::oshandle::OsHandle;
|
||||
use crate::handle::{Handle, HandleRights};
|
||||
use crate::sys::osdir::OsDir;
|
||||
use crate::wasi::{types, Errno, Result};
|
||||
use std::convert::TryFrom;
|
||||
use std::ffi::OsStr;
|
||||
use std::fs::File;
|
||||
use std::os::unix::prelude::{AsRawFd, FromRawFd, OsStrExt};
|
||||
use std::str;
|
||||
use yanix::file::OFlag;
|
||||
@@ -19,10 +20,10 @@ pub(crate) fn from_host<S: AsRef<OsStr>>(s: S) -> Result<String> {
|
||||
}
|
||||
|
||||
pub(crate) fn open_rights(
|
||||
input_rights: &EntryRights,
|
||||
input_rights: &HandleRights,
|
||||
oflags: types::Oflags,
|
||||
fs_flags: types::Fdflags,
|
||||
) -> EntryRights {
|
||||
) -> HandleRights {
|
||||
// which rights are needed on the dirfd?
|
||||
let mut needed_base = types::Rights::PATH_OPEN;
|
||||
let mut needed_inheriting = input_rights.base | input_rights.inheriting;
|
||||
@@ -45,10 +46,10 @@ pub(crate) fn open_rights(
|
||||
needed_inheriting |= types::Rights::FD_SYNC;
|
||||
}
|
||||
|
||||
EntryRights::new(needed_base, needed_inheriting)
|
||||
HandleRights::new(needed_base, needed_inheriting)
|
||||
}
|
||||
|
||||
pub(crate) fn readlinkat(dirfd: &OsFile, path: &str) -> Result<String> {
|
||||
pub(crate) fn readlinkat(dirfd: &OsDir, path: &str) -> Result<String> {
|
||||
use std::os::unix::prelude::AsRawFd;
|
||||
use yanix::file::readlinkat;
|
||||
|
||||
@@ -59,16 +60,16 @@ pub(crate) fn readlinkat(dirfd: &OsFile, path: &str) -> Result<String> {
|
||||
Ok(path)
|
||||
}
|
||||
|
||||
pub(crate) fn create_directory(base: &OsFile, path: &str) -> Result<()> {
|
||||
pub(crate) fn create_directory(base: &OsDir, path: &str) -> Result<()> {
|
||||
use yanix::file::{mkdirat, Mode};
|
||||
unsafe { mkdirat(base.as_raw_fd(), path, Mode::from_bits_truncate(0o777))? };
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn link(
|
||||
old_dirfd: &OsFile,
|
||||
old_dirfd: &OsDir,
|
||||
old_path: &str,
|
||||
new_dirfd: &OsFile,
|
||||
new_dirfd: &OsDir,
|
||||
new_path: &str,
|
||||
follow_symlinks: bool,
|
||||
) -> Result<()> {
|
||||
@@ -91,13 +92,13 @@ pub(crate) fn link(
|
||||
}
|
||||
|
||||
pub(crate) fn open(
|
||||
dirfd: &OsFile,
|
||||
dirfd: &OsDir,
|
||||
path: &str,
|
||||
read: bool,
|
||||
write: bool,
|
||||
oflags: types::Oflags,
|
||||
fs_flags: types::Fdflags,
|
||||
) -> Result<OsHandle> {
|
||||
) -> Result<Box<dyn Handle>> {
|
||||
use yanix::file::{fstatat, openat, AtFlag, FileType, Mode, OFlag};
|
||||
|
||||
let mut nix_all_oflags = if read && write {
|
||||
@@ -181,10 +182,12 @@ pub(crate) fn open(
|
||||
log::debug!("path_open (host) new_fd = {:?}", new_fd);
|
||||
|
||||
// Determine the type of the new file descriptor and which rights contradict with this type
|
||||
Ok(OsHandle::from(unsafe { OsFile::from_raw_fd(new_fd) }))
|
||||
let file = unsafe { File::from_raw_fd(new_fd) };
|
||||
let handle = <Box<dyn Handle>>::try_from(file)?;
|
||||
Ok(handle)
|
||||
}
|
||||
|
||||
pub(crate) fn readlink(dirfd: &OsFile, path: &str, buf: &mut [u8]) -> Result<usize> {
|
||||
pub(crate) fn readlink(dirfd: &OsDir, path: &str, buf: &mut [u8]) -> Result<usize> {
|
||||
use std::cmp::min;
|
||||
use yanix::file::readlinkat;
|
||||
let read_link = unsafe { readlinkat(dirfd.as_raw_fd(), path)? };
|
||||
@@ -196,7 +199,7 @@ pub(crate) fn readlink(dirfd: &OsFile, path: &str, buf: &mut [u8]) -> Result<usi
|
||||
Ok(copy_len)
|
||||
}
|
||||
|
||||
pub(crate) fn remove_directory(dirfd: &OsFile, path: &str) -> Result<()> {
|
||||
pub(crate) fn remove_directory(dirfd: &OsDir, path: &str) -> Result<()> {
|
||||
use yanix::file::{unlinkat, AtFlag};
|
||||
unsafe { unlinkat(dirfd.as_raw_fd(), path, AtFlag::REMOVEDIR)? };
|
||||
Ok(())
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use super::super::oshandle::OsHandle;
|
||||
use crate::entry::EntryHandle;
|
||||
use crate::poll::{ClockEventData, FdEventData};
|
||||
use crate::sys::oshandle::AsFile;
|
||||
use crate::sys::AsFile;
|
||||
use crate::wasi::{types, Errno, Result};
|
||||
use std::io;
|
||||
use std::{convert::TryInto, os::unix::prelude::AsRawFd};
|
||||
@@ -16,7 +16,7 @@ pub(crate) fn oneoff(
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let mut poll_fds: Vec<_> = fd_events
|
||||
let poll_fds: Result<Vec<_>> = fd_events
|
||||
.iter()
|
||||
.map(|event| {
|
||||
let mut flags = PollFlags::empty();
|
||||
@@ -28,14 +28,11 @@ pub(crate) fn oneoff(
|
||||
// events we filtered before. If we get something else here, the code has a serious bug.
|
||||
_ => unreachable!(),
|
||||
};
|
||||
let handle = event
|
||||
.handle
|
||||
.as_any()
|
||||
.downcast_ref::<OsHandle>()
|
||||
.expect("can poll FdEvent for OS resources only");
|
||||
unsafe { PollFd::new(handle.as_raw_fd(), flags) }
|
||||
let file = event.handle.as_file()?;
|
||||
unsafe { Ok(PollFd::new(file.as_raw_fd(), flags)) }
|
||||
})
|
||||
.collect();
|
||||
let mut poll_fds = poll_fds?;
|
||||
|
||||
let poll_timeout = timeout.map_or(-1, |timeout| {
|
||||
let delay = timeout.delay / 1_000_000; // poll syscall requires delay to expressed in milliseconds
|
||||
@@ -80,18 +77,17 @@ fn handle_fd_event(
|
||||
ready_events: impl Iterator<Item = (FdEventData, yanix::poll::PollFd)>,
|
||||
events: &mut Vec<types::Event>,
|
||||
) -> Result<()> {
|
||||
fn query_nbytes(handle: &OsHandle) -> Result<u64> {
|
||||
// fionread may overflow for large files, so use another way for regular files.
|
||||
if let OsHandle::OsFile(file) = handle {
|
||||
let meta = file.as_file().metadata()?;
|
||||
if meta.file_type().is_file() {
|
||||
use yanix::file::tell;
|
||||
let len = meta.len();
|
||||
let host_offset = unsafe { tell(file.as_raw_fd())? };
|
||||
return Ok(len - host_offset);
|
||||
}
|
||||
fn query_nbytes(handle: EntryHandle) -> Result<u64> {
|
||||
let file = handle.as_file()?;
|
||||
if handle.get_file_type() == types::Filetype::RegularFile {
|
||||
// fionread may overflow for large files, so use another way for regular files.
|
||||
use yanix::file::tell;
|
||||
let meta = file.metadata()?;
|
||||
let len = meta.len();
|
||||
let host_offset = unsafe { tell(file.as_raw_fd())? };
|
||||
return Ok(len - host_offset);
|
||||
}
|
||||
unsafe { Ok(fionread(handle.as_raw_fd())?.into()) }
|
||||
Ok(unsafe { fionread(file.as_raw_fd())?.into() })
|
||||
}
|
||||
|
||||
for (fd_event, poll_fd) in ready_events {
|
||||
@@ -106,12 +102,7 @@ fn handle_fd_event(
|
||||
log::debug!("poll_oneoff_handle_fd_event revents = {:?}", revents);
|
||||
|
||||
let nbytes = if fd_event.r#type == types::Eventtype::FdRead {
|
||||
let handle = fd_event
|
||||
.handle
|
||||
.as_any()
|
||||
.downcast_ref::<OsHandle>()
|
||||
.expect("can poll FdEvent for OS resources only");
|
||||
query_nbytes(handle)?
|
||||
query_nbytes(fd_event.handle)?
|
||||
} else {
|
||||
0
|
||||
};
|
||||
|
||||
59
crates/wasi-common/src/sys/unix/stdio.rs
Normal file
59
crates/wasi-common/src/sys/unix/stdio.rs
Normal file
@@ -0,0 +1,59 @@
|
||||
use super::{get_file_type, get_rights};
|
||||
use crate::handle::Handle;
|
||||
use crate::sys::stdio::{Stderr, StderrExt, Stdin, StdinExt, Stdout, StdoutExt};
|
||||
use std::cell::Cell;
|
||||
use std::fs::File;
|
||||
use std::io;
|
||||
use std::mem::ManuallyDrop;
|
||||
use std::os::unix::prelude::{AsRawFd, FromRawFd, RawFd};
|
||||
|
||||
impl AsRawFd for Stdin {
|
||||
fn as_raw_fd(&self) -> RawFd {
|
||||
io::stdin().as_raw_fd()
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRawFd for Stdout {
|
||||
fn as_raw_fd(&self) -> RawFd {
|
||||
io::stdout().as_raw_fd()
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRawFd for Stderr {
|
||||
fn as_raw_fd(&self) -> RawFd {
|
||||
io::stderr().as_raw_fd()
|
||||
}
|
||||
}
|
||||
|
||||
impl StdinExt for Stdin {
|
||||
fn stdin() -> io::Result<Box<dyn Handle>> {
|
||||
let file = unsafe { File::from_raw_fd(io::stdin().as_raw_fd()) };
|
||||
let file = ManuallyDrop::new(file);
|
||||
let file_type = get_file_type(&file)?;
|
||||
let rights = get_rights(&file, &file_type)?;
|
||||
let rights = Cell::new(rights);
|
||||
Ok(Box::new(Self { file_type, rights }))
|
||||
}
|
||||
}
|
||||
|
||||
impl StdoutExt for Stdout {
|
||||
fn stdout() -> io::Result<Box<dyn Handle>> {
|
||||
let file = unsafe { File::from_raw_fd(io::stdout().as_raw_fd()) };
|
||||
let file = ManuallyDrop::new(file);
|
||||
let file_type = get_file_type(&file)?;
|
||||
let rights = get_rights(&file, &file_type)?;
|
||||
let rights = Cell::new(rights);
|
||||
Ok(Box::new(Self { file_type, rights }))
|
||||
}
|
||||
}
|
||||
|
||||
impl StderrExt for Stderr {
|
||||
fn stderr() -> io::Result<Box<dyn Handle>> {
|
||||
let file = unsafe { File::from_raw_fd(io::stderr().as_raw_fd()) };
|
||||
let file = ManuallyDrop::new(file);
|
||||
let file_type = get_file_type(&file)?;
|
||||
let rights = get_rights(&file, &file_type)?;
|
||||
let rights = Cell::new(rights);
|
||||
Ok(Box::new(Self { file_type, rights }))
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
use super::file_serial_no;
|
||||
use super::oshandle::OsFile;
|
||||
use super::oshandle::RawOsHandle;
|
||||
use crate::path;
|
||||
use crate::sys::oshandle::AsFile;
|
||||
use crate::sys::osdir::OsDir;
|
||||
use crate::sys::osfile::OsFile;
|
||||
use crate::sys::AsFile;
|
||||
use crate::wasi::{types, Result};
|
||||
use log::trace;
|
||||
use std::convert::TryInto;
|
||||
@@ -39,7 +41,10 @@ pub(crate) fn fdstat_get(file: &File) -> Result<types::Fdflags> {
|
||||
// handle came from `CreateFile`, but the Rust's libstd will use `GetStdHandle`
|
||||
// rather than `CreateFile`. Relevant discussion can be found in:
|
||||
// https://github.com/rust-lang/rust/issues/40490
|
||||
pub(crate) fn fdstat_set_flags(file: &File, fdflags: types::Fdflags) -> Result<Option<OsFile>> {
|
||||
pub(crate) fn fdstat_set_flags(
|
||||
file: &File,
|
||||
fdflags: types::Fdflags,
|
||||
) -> Result<Option<RawOsHandle>> {
|
||||
let handle = file.as_raw_handle();
|
||||
let access_mode = winx::file::query_access_information(handle)?;
|
||||
let new_access_mode = file_access_mode_from_fdflags(
|
||||
@@ -49,7 +54,7 @@ pub(crate) fn fdstat_set_flags(file: &File, fdflags: types::Fdflags) -> Result<O
|
||||
| access_mode.contains(AccessMode::FILE_APPEND_DATA),
|
||||
);
|
||||
unsafe {
|
||||
Ok(Some(OsFile::from_raw_handle(winx::file::reopen_file(
|
||||
Ok(Some(RawOsHandle::from_raw_handle(winx::file::reopen_file(
|
||||
handle,
|
||||
new_access_mode,
|
||||
fdflags.into(),
|
||||
@@ -120,13 +125,13 @@ fn file_access_mode_from_fdflags(fdflags: types::Fdflags, read: bool, write: boo
|
||||
// .. gets cookie = 2
|
||||
// other entries, in order they were returned by FindNextFileW get subsequent integers as their cookies
|
||||
pub(crate) fn readdir(
|
||||
file: &OsFile,
|
||||
dirfd: &OsDir,
|
||||
cookie: types::Dircookie,
|
||||
) -> Result<Box<dyn Iterator<Item = Result<(types::Dirent, String)>>>> {
|
||||
use winx::file::get_file_path;
|
||||
|
||||
let cookie = cookie.try_into()?;
|
||||
let path = get_file_path(&file.as_file())?;
|
||||
let path = get_file_path(&*dirfd.as_file()?)?;
|
||||
// std::fs::ReadDir doesn't return . and .., so we need to emulate it
|
||||
let path = Path::new(&path);
|
||||
// The directory /.. is the same as / on Unix (at least on ext4), so emulate this behavior too
|
||||
|
||||
@@ -1,18 +1,87 @@
|
||||
pub(crate) mod clock;
|
||||
pub(crate) mod fd;
|
||||
pub(crate) mod osdir;
|
||||
pub(crate) mod osfile;
|
||||
pub(crate) mod oshandle;
|
||||
pub(crate) mod osother;
|
||||
pub(crate) mod path;
|
||||
pub(crate) mod poll;
|
||||
pub(crate) mod stdio;
|
||||
|
||||
use crate::wasi::{types, Errno, Result};
|
||||
use crate::handle::HandleRights;
|
||||
use crate::sys::AsFile;
|
||||
use crate::wasi::{types, Errno, Result, RightsExt};
|
||||
use std::convert::{TryFrom, TryInto};
|
||||
use std::fs::File;
|
||||
use std::mem::ManuallyDrop;
|
||||
use std::os::windows::prelude::{AsRawHandle, FromRawHandle};
|
||||
use std::path::Path;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
use std::{io, string};
|
||||
use winapi::shared::winerror;
|
||||
use winx::file::{CreationDisposition, Flags};
|
||||
|
||||
impl<T: AsRawHandle> AsFile for T {
|
||||
fn as_file(&self) -> io::Result<ManuallyDrop<File>> {
|
||||
let file = unsafe { File::from_raw_handle(self.as_raw_handle()) };
|
||||
Ok(ManuallyDrop::new(file))
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn get_file_type(file: &File) -> io::Result<types::Filetype> {
|
||||
let file_type = unsafe { winx::file::get_file_type(file.as_raw_handle())? };
|
||||
let file_type = if file_type.is_char() {
|
||||
// character file: LPT device or console
|
||||
// TODO: rule out LPT device
|
||||
types::Filetype::CharacterDevice
|
||||
} else if file_type.is_disk() {
|
||||
// disk file: file, dir or disk device
|
||||
let meta = file.metadata()?;
|
||||
if meta.is_dir() {
|
||||
types::Filetype::Directory
|
||||
} else if meta.is_file() {
|
||||
types::Filetype::RegularFile
|
||||
} else {
|
||||
return Err(io::Error::from_raw_os_error(libc::EINVAL));
|
||||
}
|
||||
} else if file_type.is_pipe() {
|
||||
// pipe object: socket, named pipe or anonymous pipe
|
||||
// TODO: what about pipes, etc?
|
||||
types::Filetype::SocketStream
|
||||
} else {
|
||||
return Err(io::Error::from_raw_os_error(libc::EINVAL));
|
||||
};
|
||||
Ok(file_type)
|
||||
}
|
||||
|
||||
pub(super) fn get_rights(file_type: &types::Filetype) -> io::Result<HandleRights> {
|
||||
let (base, inheriting) = match file_type {
|
||||
types::Filetype::BlockDevice => (
|
||||
types::Rights::block_device_base(),
|
||||
types::Rights::block_device_inheriting(),
|
||||
),
|
||||
types::Filetype::CharacterDevice => (types::Rights::tty_base(), types::Rights::tty_base()),
|
||||
types::Filetype::SocketDgram | types::Filetype::SocketStream => (
|
||||
types::Rights::socket_base(),
|
||||
types::Rights::socket_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(),
|
||||
),
|
||||
};
|
||||
let rights = HandleRights::new(base, inheriting);
|
||||
Ok(rights)
|
||||
}
|
||||
|
||||
pub fn preopen_dir<P: AsRef<Path>>(path: P) -> io::Result<File> {
|
||||
use std::fs::OpenOptions;
|
||||
use std::os::windows::fs::OpenOptionsExt;
|
||||
|
||||
51
crates/wasi-common/src/sys/windows/osdir.rs
Normal file
51
crates/wasi-common/src/sys/windows/osdir.rs
Normal file
@@ -0,0 +1,51 @@
|
||||
use super::oshandle::RawOsHandle;
|
||||
use crate::handle::HandleRights;
|
||||
use crate::wasi::{types, RightsExt};
|
||||
use std::cell::Cell;
|
||||
use std::convert::TryFrom;
|
||||
use std::fs::File;
|
||||
use std::io;
|
||||
use std::os::windows::prelude::{AsRawHandle, FromRawHandle, IntoRawHandle};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct OsDir {
|
||||
pub(crate) rights: Cell<HandleRights>,
|
||||
pub(crate) handle: RawOsHandle,
|
||||
}
|
||||
|
||||
impl OsDir {
|
||||
pub(crate) fn new(rights: HandleRights, handle: RawOsHandle) -> io::Result<Self> {
|
||||
let rights = Cell::new(rights);
|
||||
Ok(Self { rights, handle })
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<File> for OsDir {
|
||||
type Error = io::Error;
|
||||
|
||||
fn try_from(file: File) -> io::Result<Self> {
|
||||
let ft = file.metadata()?.file_type();
|
||||
if !ft.is_dir() {
|
||||
return Err(io::Error::from_raw_os_error(libc::EINVAL));
|
||||
}
|
||||
let rights = get_rights(&file)?;
|
||||
let handle = unsafe { RawOsHandle::from_raw_handle(file.into_raw_handle()) };
|
||||
Self::new(rights, handle)
|
||||
}
|
||||
}
|
||||
|
||||
fn get_rights(file: &File) -> io::Result<HandleRights> {
|
||||
use winx::file::{query_access_information, AccessMode};
|
||||
let mut rights = HandleRights::new(
|
||||
types::Rights::directory_base(),
|
||||
types::Rights::directory_inheriting(),
|
||||
);
|
||||
let mode = query_access_information(file.as_raw_handle())?;
|
||||
if mode.contains(AccessMode::FILE_GENERIC_READ) {
|
||||
rights.base |= types::Rights::FD_READ;
|
||||
}
|
||||
if mode.contains(AccessMode::FILE_GENERIC_WRITE) {
|
||||
rights.base |= types::Rights::FD_WRITE;
|
||||
}
|
||||
Ok(rights)
|
||||
}
|
||||
38
crates/wasi-common/src/sys/windows/osfile.rs
Normal file
38
crates/wasi-common/src/sys/windows/osfile.rs
Normal file
@@ -0,0 +1,38 @@
|
||||
use super::oshandle::RawOsHandle;
|
||||
use crate::handle::HandleRights;
|
||||
use crate::sys::osfile::OsFile;
|
||||
use crate::wasi::{types, RightsExt};
|
||||
use std::convert::TryFrom;
|
||||
use std::fs::File;
|
||||
use std::io;
|
||||
use std::os::windows::prelude::{AsRawHandle, FromRawHandle, IntoRawHandle};
|
||||
|
||||
impl TryFrom<File> for OsFile {
|
||||
type Error = io::Error;
|
||||
|
||||
fn try_from(file: File) -> io::Result<Self> {
|
||||
let ft = file.metadata()?.file_type();
|
||||
if !ft.is_file() {
|
||||
return Err(io::Error::from_raw_os_error(libc::EINVAL));
|
||||
}
|
||||
let rights = get_rights(&file)?;
|
||||
let handle = unsafe { RawOsHandle::from_raw_handle(file.into_raw_handle()) };
|
||||
Ok(Self::new(rights, handle))
|
||||
}
|
||||
}
|
||||
|
||||
fn get_rights(file: &File) -> io::Result<HandleRights> {
|
||||
use winx::file::{query_access_information, AccessMode};
|
||||
let mut rights = HandleRights::new(
|
||||
types::Rights::regular_file_base(),
|
||||
types::Rights::regular_file_inheriting(),
|
||||
);
|
||||
let mode = query_access_information(file.as_raw_handle())?;
|
||||
if mode.contains(AccessMode::FILE_GENERIC_READ) {
|
||||
rights.base |= types::Rights::FD_READ;
|
||||
}
|
||||
if mode.contains(AccessMode::FILE_GENERIC_WRITE) {
|
||||
rights.base |= types::Rights::FD_WRITE;
|
||||
}
|
||||
Ok(rights)
|
||||
}
|
||||
@@ -1,16 +1,19 @@
|
||||
use crate::entry::EntryRights;
|
||||
use crate::sys::oshandle::{AsFile, OsHandle, OsHandleExt};
|
||||
use crate::wasi::{types, RightsExt};
|
||||
use crate::sys::AsFile;
|
||||
use std::cell::Cell;
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::fs::File;
|
||||
use std::io;
|
||||
use std::mem::ManuallyDrop;
|
||||
use std::os::windows::prelude::{AsRawHandle, FromRawHandle, IntoRawHandle, RawHandle};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct OsFile(Cell<RawHandle>);
|
||||
pub(crate) struct RawOsHandle(Cell<RawHandle>);
|
||||
|
||||
impl OsFile {
|
||||
impl RawOsHandle {
|
||||
/// Tries cloning `self`.
|
||||
pub(crate) fn try_clone(&self) -> io::Result<Self> {
|
||||
let handle = self.as_file()?.try_clone()?;
|
||||
Ok(Self(Cell::new(handle.into_raw_handle())))
|
||||
}
|
||||
/// Consumes `other` taking the ownership of the underlying
|
||||
/// `RawHandle` file handle.
|
||||
pub(crate) fn update_from(&self, other: Self) {
|
||||
@@ -22,14 +25,9 @@ impl OsFile {
|
||||
File::from_raw_handle(old_handle);
|
||||
}
|
||||
}
|
||||
/// Clones `self`.
|
||||
pub(crate) fn try_clone(&self) -> io::Result<Self> {
|
||||
let handle = self.as_file().try_clone()?;
|
||||
Ok(Self(Cell::new(handle.into_raw_handle())))
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for OsFile {
|
||||
impl Drop for RawOsHandle {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
File::from_raw_handle(self.as_raw_handle());
|
||||
@@ -37,133 +35,22 @@ impl Drop for OsFile {
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRawHandle for OsFile {
|
||||
impl AsRawHandle for RawOsHandle {
|
||||
fn as_raw_handle(&self) -> RawHandle {
|
||||
self.0.get()
|
||||
}
|
||||
}
|
||||
|
||||
impl FromRawHandle for OsFile {
|
||||
impl FromRawHandle for RawOsHandle {
|
||||
unsafe fn from_raw_handle(handle: RawHandle) -> Self {
|
||||
Self(Cell::new(handle))
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoRawHandle for OsFile {
|
||||
impl IntoRawHandle for RawOsHandle {
|
||||
fn into_raw_handle(self) -> RawHandle {
|
||||
// We need to prevent dropping of the OsFile
|
||||
let wrapped = ManuallyDrop::new(self);
|
||||
wrapped.0.get()
|
||||
}
|
||||
}
|
||||
|
||||
impl AsFile for OsFile {
|
||||
fn as_file(&self) -> ManuallyDrop<File> {
|
||||
let file = unsafe { File::from_raw_handle(self.0.get()) };
|
||||
ManuallyDrop::new(file)
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRawHandle for OsHandle {
|
||||
fn as_raw_handle(&self) -> RawHandle {
|
||||
match self {
|
||||
Self::OsFile(file) => file.as_raw_handle(),
|
||||
Self::Stdin => io::stdin().as_raw_handle(),
|
||||
Self::Stdout => io::stdout().as_raw_handle(),
|
||||
Self::Stderr => io::stderr().as_raw_handle(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AsFile for OsHandle {
|
||||
fn as_file(&self) -> ManuallyDrop<File> {
|
||||
let file = unsafe { File::from_raw_handle(self.as_raw_handle()) };
|
||||
ManuallyDrop::new(file)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<File> for OsHandle {
|
||||
fn from(file: File) -> Self {
|
||||
Self::from(unsafe { OsFile::from_raw_handle(file.into_raw_handle()) })
|
||||
}
|
||||
}
|
||||
|
||||
impl OsHandleExt for OsHandle {
|
||||
fn get_file_type(&self) -> io::Result<types::Filetype> {
|
||||
let file_type = unsafe { winx::file::get_file_type(self.as_raw_handle())? };
|
||||
let file_type = if file_type.is_char() {
|
||||
// character file: LPT device or console
|
||||
// TODO: rule out LPT device
|
||||
types::Filetype::CharacterDevice
|
||||
} else if file_type.is_disk() {
|
||||
// disk file: file, dir or disk device
|
||||
let file = self.as_file();
|
||||
let meta = file.metadata()?;
|
||||
if meta.is_dir() {
|
||||
types::Filetype::Directory
|
||||
} else if meta.is_file() {
|
||||
types::Filetype::RegularFile
|
||||
} else {
|
||||
return Err(io::Error::from_raw_os_error(libc::EINVAL));
|
||||
}
|
||||
} else if file_type.is_pipe() {
|
||||
// pipe object: socket, named pipe or anonymous pipe
|
||||
// TODO: what about pipes, etc?
|
||||
types::Filetype::SocketStream
|
||||
} else {
|
||||
return Err(io::Error::from_raw_os_error(libc::EINVAL));
|
||||
};
|
||||
Ok(file_type)
|
||||
}
|
||||
|
||||
fn get_rights(&self, file_type: types::Filetype) -> io::Result<EntryRights> {
|
||||
use winx::file::{query_access_information, AccessMode};
|
||||
let (base, inheriting) = match file_type {
|
||||
types::Filetype::BlockDevice => (
|
||||
types::Rights::block_device_base(),
|
||||
types::Rights::block_device_inheriting(),
|
||||
),
|
||||
types::Filetype::CharacterDevice => {
|
||||
(types::Rights::tty_base(), types::Rights::tty_base())
|
||||
}
|
||||
types::Filetype::Directory => (
|
||||
types::Rights::directory_base(),
|
||||
types::Rights::directory_inheriting(),
|
||||
),
|
||||
types::Filetype::RegularFile => (
|
||||
types::Rights::regular_file_base(),
|
||||
types::Rights::regular_file_inheriting(),
|
||||
),
|
||||
types::Filetype::SocketDgram | types::Filetype::SocketStream => (
|
||||
types::Rights::socket_base(),
|
||||
types::Rights::socket_inheriting(),
|
||||
),
|
||||
types::Filetype::SymbolicLink | types::Filetype::Unknown => (
|
||||
types::Rights::regular_file_base(),
|
||||
types::Rights::regular_file_inheriting(),
|
||||
),
|
||||
};
|
||||
let mut rights = EntryRights::new(base, inheriting);
|
||||
match file_type {
|
||||
types::Filetype::Directory | types::Filetype::RegularFile => {
|
||||
let mode = query_access_information(self.as_raw_handle())?;
|
||||
if mode.contains(AccessMode::FILE_GENERIC_READ) {
|
||||
rights.base |= types::Rights::FD_READ;
|
||||
}
|
||||
if mode.contains(AccessMode::FILE_GENERIC_WRITE) {
|
||||
rights.base |= types::Rights::FD_WRITE;
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
// TODO: is there a way around this? On windows, it seems
|
||||
// we cannot check access rights for anything but dirs and regular files
|
||||
}
|
||||
}
|
||||
Ok(rights)
|
||||
}
|
||||
|
||||
fn from_null() -> io::Result<Self> {
|
||||
let file = OpenOptions::new().read(true).write(true).open("NUL")?;
|
||||
Ok(Self::from(file))
|
||||
}
|
||||
}
|
||||
|
||||
31
crates/wasi-common/src/sys/windows/osother.rs
Normal file
31
crates/wasi-common/src/sys/windows/osother.rs
Normal file
@@ -0,0 +1,31 @@
|
||||
use super::oshandle::RawOsHandle;
|
||||
use super::{get_file_type, get_rights};
|
||||
use crate::handle::Handle;
|
||||
use crate::sys::osother::{OsOther, OsOtherExt};
|
||||
use crate::wasi::types;
|
||||
use std::convert::TryFrom;
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::io;
|
||||
use std::os::windows::prelude::{FromRawHandle, IntoRawHandle};
|
||||
|
||||
impl TryFrom<File> for OsOther {
|
||||
type Error = io::Error;
|
||||
|
||||
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 {
|
||||
return Err(io::Error::from_raw_os_error(libc::EINVAL));
|
||||
}
|
||||
let rights = get_rights(&file_type)?;
|
||||
let handle = unsafe { RawOsHandle::from_raw_handle(file.into_raw_handle()) };
|
||||
Ok(Self::new(file_type, rights, handle))
|
||||
}
|
||||
}
|
||||
|
||||
impl OsOtherExt for OsOther {
|
||||
fn from_null() -> io::Result<Box<dyn Handle>> {
|
||||
let file = OpenOptions::new().read(true).write(true).open("NUL")?;
|
||||
let file = Self::try_from(file)?;
|
||||
Ok(Box::new(file))
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
use super::oshandle::OsFile;
|
||||
use crate::entry::EntryRights;
|
||||
use crate::sys::oshandle::{AsFile, OsHandle};
|
||||
use crate::handle::{Handle, HandleRights};
|
||||
use crate::sys::osdir::OsDir;
|
||||
use crate::sys::AsFile;
|
||||
use crate::wasi::{types, Errno, Result};
|
||||
use std::convert::TryFrom;
|
||||
use std::ffi::{OsStr, OsString};
|
||||
use std::fs::{self, Metadata, OpenOptions};
|
||||
use std::os::windows::ffi::{OsStrExt, OsStringExt};
|
||||
@@ -10,7 +11,7 @@ use std::path::{Path, PathBuf};
|
||||
use winapi::shared::winerror;
|
||||
use winx::file::AccessMode;
|
||||
|
||||
fn strip_trailing_slashes_and_concatenate(dirfd: &OsFile, path: &str) -> Result<Option<PathBuf>> {
|
||||
fn strip_trailing_slashes_and_concatenate(dirfd: &OsDir, path: &str) -> Result<Option<PathBuf>> {
|
||||
if path.ends_with('/') {
|
||||
let suffix = path.trim_end_matches('/');
|
||||
concatenate(dirfd, Path::new(suffix)).map(Some)
|
||||
@@ -28,7 +29,7 @@ fn strip_extended_prefix<P: AsRef<OsStr>>(path: P) -> OsString {
|
||||
}
|
||||
}
|
||||
|
||||
fn concatenate<P: AsRef<Path>>(file: &OsFile, path: P) -> Result<PathBuf> {
|
||||
fn concatenate<P: AsRef<Path>>(file: &OsDir, path: P) -> Result<PathBuf> {
|
||||
use winx::file::get_file_path;
|
||||
|
||||
// WASI is not able to deal with absolute paths
|
||||
@@ -37,7 +38,7 @@ fn concatenate<P: AsRef<Path>>(file: &OsFile, path: P) -> Result<PathBuf> {
|
||||
return Err(Errno::Notcapable);
|
||||
}
|
||||
|
||||
let dir_path = get_file_path(&file.as_file())?;
|
||||
let dir_path = get_file_path(&*file.as_file()?)?;
|
||||
// concatenate paths
|
||||
let mut out_path = PathBuf::from(dir_path);
|
||||
out_path.push(path.as_ref());
|
||||
@@ -89,10 +90,10 @@ pub(crate) fn from_host<S: AsRef<OsStr>>(s: S) -> Result<String> {
|
||||
}
|
||||
|
||||
pub(crate) fn open_rights(
|
||||
input_rights: &EntryRights,
|
||||
input_rights: &HandleRights,
|
||||
oflags: types::Oflags,
|
||||
fdflags: types::Fdflags,
|
||||
) -> EntryRights {
|
||||
) -> HandleRights {
|
||||
// which rights are needed on the dirfd?
|
||||
let mut needed_base = types::Rights::PATH_OPEN;
|
||||
let mut needed_inheriting = input_rights.base | input_rights.inheriting;
|
||||
@@ -113,10 +114,10 @@ pub(crate) fn open_rights(
|
||||
needed_inheriting |= types::Rights::FD_SYNC;
|
||||
}
|
||||
|
||||
EntryRights::new(needed_base, needed_inheriting)
|
||||
HandleRights::new(needed_base, needed_inheriting)
|
||||
}
|
||||
|
||||
pub(crate) fn readlinkat(dirfd: &OsFile, s_path: &str) -> Result<String> {
|
||||
pub(crate) fn readlinkat(dirfd: &OsDir, s_path: &str) -> Result<String> {
|
||||
use winx::file::get_file_path;
|
||||
|
||||
let path = concatenate(dirfd, Path::new(s_path))?;
|
||||
@@ -126,7 +127,7 @@ pub(crate) fn readlinkat(dirfd: &OsFile, s_path: &str) -> Result<String> {
|
||||
// we need to strip the prefix from the absolute path
|
||||
// as otherwise we will error out since WASI is not capable
|
||||
// of dealing with absolute paths
|
||||
let dir_path = get_file_path(&dirfd.as_file())?;
|
||||
let dir_path = get_file_path(&*dirfd.as_file()?)?;
|
||||
let dir_path = PathBuf::from(strip_extended_prefix(dir_path));
|
||||
let target_path = target_path
|
||||
.strip_prefix(dir_path)
|
||||
@@ -151,16 +152,16 @@ pub(crate) fn readlinkat(dirfd: &OsFile, s_path: &str) -> Result<String> {
|
||||
Err(err.into())
|
||||
}
|
||||
|
||||
pub(crate) fn create_directory(file: &OsFile, path: &str) -> Result<()> {
|
||||
pub(crate) fn create_directory(file: &OsDir, path: &str) -> Result<()> {
|
||||
let path = concatenate(file, path)?;
|
||||
std::fs::create_dir(&path)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn link(
|
||||
old_dirfd: &OsFile,
|
||||
old_dirfd: &OsDir,
|
||||
old_path: &str,
|
||||
new_dirfd: &OsFile,
|
||||
new_dirfd: &OsDir,
|
||||
new_path: &str,
|
||||
follow_symlinks: bool,
|
||||
) -> Result<()> {
|
||||
@@ -197,13 +198,13 @@ pub(crate) fn link(
|
||||
}
|
||||
|
||||
pub(crate) fn open(
|
||||
dirfd: &OsFile,
|
||||
dirfd: &OsDir,
|
||||
path: &str,
|
||||
read: bool,
|
||||
write: bool,
|
||||
oflags: types::Oflags,
|
||||
fdflags: types::Fdflags,
|
||||
) -> Result<OsHandle> {
|
||||
) -> Result<Box<dyn Handle>> {
|
||||
use winx::file::{AccessMode, CreationDisposition, Flags};
|
||||
|
||||
let is_trunc = oflags.contains(&types::Oflags::TRUNC);
|
||||
@@ -280,11 +281,11 @@ pub(crate) fn open(
|
||||
.access_mode(access_mode.bits())
|
||||
.custom_flags(flags.bits())
|
||||
.open(&path)?;
|
||||
let handle = OsHandle::from(file);
|
||||
let handle = <Box<dyn Handle>>::try_from(file)?;
|
||||
Ok(handle)
|
||||
}
|
||||
|
||||
pub(crate) fn readlink(dirfd: &OsFile, path: &str, buf: &mut [u8]) -> Result<usize> {
|
||||
pub(crate) fn readlink(dirfd: &OsDir, path: &str, buf: &mut [u8]) -> Result<usize> {
|
||||
use winx::file::get_file_path;
|
||||
|
||||
let path = concatenate(dirfd, path)?;
|
||||
@@ -294,7 +295,7 @@ pub(crate) fn readlink(dirfd: &OsFile, path: &str, buf: &mut [u8]) -> Result<usi
|
||||
// we need to strip the prefix from the absolute path
|
||||
// as otherwise we will error out since WASI is not capable
|
||||
// of dealing with absolute paths
|
||||
let dir_path = get_file_path(&dirfd.as_file())?;
|
||||
let dir_path = get_file_path(&*dirfd.as_file()?)?;
|
||||
let dir_path = PathBuf::from(strip_extended_prefix(dir_path));
|
||||
let target_path = target_path
|
||||
.strip_prefix(dir_path)
|
||||
@@ -322,9 +323,9 @@ pub(crate) fn readlink(dirfd: &OsFile, path: &str, buf: &mut [u8]) -> Result<usi
|
||||
}
|
||||
|
||||
pub(crate) fn rename(
|
||||
old_dirfd: &OsFile,
|
||||
old_dirfd: &OsDir,
|
||||
old_path_: &str,
|
||||
new_dirfd: &OsFile,
|
||||
new_dirfd: &OsDir,
|
||||
new_path_: &str,
|
||||
) -> Result<()> {
|
||||
use std::fs;
|
||||
@@ -390,7 +391,7 @@ pub(crate) fn rename(
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn symlink(old_path: &str, new_dirfd: &OsFile, new_path_: &str) -> Result<()> {
|
||||
pub(crate) fn symlink(old_path: &str, new_dirfd: &OsDir, new_path_: &str) -> Result<()> {
|
||||
use std::os::windows::fs::{symlink_dir, symlink_file};
|
||||
|
||||
let old_path = concatenate(new_dirfd, Path::new(old_path))?;
|
||||
@@ -447,7 +448,7 @@ pub(crate) fn symlink(old_path: &str, new_dirfd: &OsFile, new_path_: &str) -> Re
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn unlink_file(dirfd: &OsFile, path: &str) -> Result<()> {
|
||||
pub(crate) fn unlink_file(dirfd: &OsDir, path: &str) -> Result<()> {
|
||||
use std::fs;
|
||||
|
||||
let path = concatenate(dirfd, path)?;
|
||||
@@ -489,7 +490,7 @@ pub(crate) fn unlink_file(dirfd: &OsFile, path: &str) -> Result<()> {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn remove_directory(dirfd: &OsFile, path: &str) -> Result<()> {
|
||||
pub(crate) fn remove_directory(dirfd: &OsDir, path: &str) -> Result<()> {
|
||||
let path = concatenate(dirfd, path)?;
|
||||
std::fs::remove_dir(&path).map_err(Into::into)
|
||||
}
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
use super::super::oshandle::OsHandle;
|
||||
use crate::handle::Handle;
|
||||
use crate::poll::{ClockEventData, FdEventData};
|
||||
use crate::sys::oshandle::AsFile;
|
||||
use crate::sys::osdir::OsDir;
|
||||
use crate::sys::osfile::OsFile;
|
||||
use crate::sys::osother::OsOther;
|
||||
use crate::sys::stdio::{Stderr, Stdin, Stdout};
|
||||
use crate::sys::AsFile;
|
||||
use crate::wasi::{types, Errno, Result};
|
||||
use lazy_static::lazy_static;
|
||||
use log::{debug, error, trace, warn};
|
||||
use std::convert::TryInto;
|
||||
use std::os::windows::io::AsRawHandle;
|
||||
use std::sync::mpsc::{self, Receiver, RecvTimeoutError, Sender, TryRecvError};
|
||||
use std::sync::Mutex;
|
||||
use std::thread;
|
||||
@@ -141,32 +144,31 @@ fn handle_timeout_event(timeout_event: ClockEventData, events: &mut Vec<types::E
|
||||
}
|
||||
|
||||
fn handle_rw_event(event: FdEventData, out_events: &mut Vec<types::Event>) {
|
||||
let handle = event
|
||||
.handle
|
||||
.as_any()
|
||||
.downcast_ref::<OsHandle>()
|
||||
.expect("can poll FdEvent for OS resources only");
|
||||
let size = match handle {
|
||||
OsHandle::OsFile(file) => {
|
||||
if event.r#type == types::Eventtype::FdRead {
|
||||
file.as_file()
|
||||
.metadata()
|
||||
.map(|m| m.len())
|
||||
.map_err(Into::into)
|
||||
} else {
|
||||
// The spec is unclear what nbytes should actually be for __WASI_EVENTTYPE_FD_WRITE and
|
||||
// the implementation on Unix just returns 0 here, so it's probably fine
|
||||
// to do the same on Windows for now.
|
||||
// cf. https://github.com/WebAssembly/WASI/issues/148
|
||||
Ok(0)
|
||||
}
|
||||
}
|
||||
let handle = &event.handle;
|
||||
let size = if let Some(_) = handle.as_any().downcast_ref::<Stdin>() {
|
||||
// We return the only universally correct lower bound, see the comment later in the function.
|
||||
OsHandle::Stdin => Ok(1),
|
||||
Ok(1)
|
||||
} else if let Some(_) = handle.as_any().downcast_ref::<Stdout>() {
|
||||
// On Unix, ioctl(FIONREAD) will return 0 for stdout. Emulate the same behavior on Windows.
|
||||
Ok(0)
|
||||
} else if let Some(_) = handle.as_any().downcast_ref::<Stderr>() {
|
||||
// On Unix, ioctl(FIONREAD) will return 0 for stdout/stderr. Emulate the same behavior on Windows.
|
||||
OsHandle::Stdout | OsHandle::Stderr => Ok(0),
|
||||
Ok(0)
|
||||
} else {
|
||||
if event.r#type == types::Eventtype::FdRead {
|
||||
handle
|
||||
.as_file()
|
||||
.and_then(|f| f.metadata())
|
||||
.map(|m| m.len())
|
||||
.map_err(Into::into)
|
||||
} else {
|
||||
// The spec is unclear what nbytes should actually be for __WASI_EVENTTYPE_FD_WRITE and
|
||||
// the implementation on Unix just returns 0 here, so it's probably fine
|
||||
// to do the same on Windows for now.
|
||||
// cf. https://github.com/WebAssembly/WASI/issues/148
|
||||
Ok(0)
|
||||
}
|
||||
};
|
||||
|
||||
let new_event = make_rw_event(&event, size);
|
||||
out_events.push(new_event);
|
||||
}
|
||||
@@ -206,33 +208,42 @@ pub(crate) fn oneoff(
|
||||
let mut pipe_events = vec![];
|
||||
|
||||
for event in fd_events {
|
||||
let handle = event
|
||||
.handle
|
||||
.as_any()
|
||||
.downcast_ref::<OsHandle>()
|
||||
.expect("can poll FdEvent for OS resources only");
|
||||
match handle {
|
||||
OsHandle::Stdin if event.r#type == types::Eventtype::FdRead => stdin_events.push(event),
|
||||
// stdout/stderr are always considered ready to write because there seems to
|
||||
let handle = &event.handle;
|
||||
if let Some(_) = handle.as_any().downcast_ref::<OsFile>() {
|
||||
immediate_events.push(event);
|
||||
} else if let Some(_) = handle.as_any().downcast_ref::<OsDir>() {
|
||||
immediate_events.push(event);
|
||||
} else if let Some(_) = handle.as_any().downcast_ref::<Stdin>() {
|
||||
stdin_events.push(event);
|
||||
} else if let Some(_) = handle.as_any().downcast_ref::<Stdout>() {
|
||||
// stdout are always considered ready to write because there seems to
|
||||
// be no way of checking if a write to stdout would block.
|
||||
//
|
||||
// If stdin is polled for anything else then reading, then it is also
|
||||
// considered immediately ready, following the behavior on Linux.
|
||||
OsHandle::Stdin | OsHandle::Stderr | OsHandle::Stdout => immediate_events.push(event),
|
||||
OsHandle::OsFile(file) => {
|
||||
let ftype = unsafe { winx::file::get_file_type(file.as_raw_handle()) }?;
|
||||
if ftype.is_unknown() || ftype.is_char() {
|
||||
debug!("poll_oneoff: unsupported file type: {:?}", ftype);
|
||||
handle_error_event(event, Errno::Notsup, events);
|
||||
} else if ftype.is_disk() {
|
||||
immediate_events.push(event);
|
||||
} else if ftype.is_pipe() {
|
||||
pipe_events.push(event);
|
||||
} else {
|
||||
unreachable!();
|
||||
}
|
||||
immediate_events.push(event);
|
||||
} else if let Some(_) = handle.as_any().downcast_ref::<Stderr>() {
|
||||
// stderr are always considered ready to write because there seems to
|
||||
// be no way of checking if a write to stdout would block.
|
||||
//
|
||||
// If stdin is polled for anything else then reading, then it is also
|
||||
// considered immediately ready, following the behavior on Linux.
|
||||
immediate_events.push(event);
|
||||
} else if let Some(other) = handle.as_any().downcast_ref::<OsOther>() {
|
||||
if other.get_file_type() == types::Filetype::SocketStream {
|
||||
// We map pipe to SocketStream
|
||||
pipe_events.push(event);
|
||||
} else {
|
||||
debug!(
|
||||
"poll_oneoff: unsupported file type: {}",
|
||||
other.get_file_type()
|
||||
);
|
||||
handle_error_event(event, Errno::Notsup, events);
|
||||
}
|
||||
};
|
||||
} else {
|
||||
log::error!("can poll FdEvent for OS resources only");
|
||||
return Err(Errno::Badf);
|
||||
}
|
||||
}
|
||||
|
||||
let immediate = !immediate_events.is_empty();
|
||||
|
||||
59
crates/wasi-common/src/sys/windows/stdio.rs
Normal file
59
crates/wasi-common/src/sys/windows/stdio.rs
Normal file
@@ -0,0 +1,59 @@
|
||||
use super::{get_file_type, get_rights};
|
||||
use crate::handle::Handle;
|
||||
use crate::sys::stdio::{Stderr, StderrExt, Stdin, StdinExt, Stdout, StdoutExt};
|
||||
use std::cell::Cell;
|
||||
use std::fs::File;
|
||||
use std::io;
|
||||
use std::mem::ManuallyDrop;
|
||||
use std::os::windows::prelude::{AsRawHandle, FromRawHandle, RawHandle};
|
||||
|
||||
impl AsRawHandle for Stdin {
|
||||
fn as_raw_handle(&self) -> RawHandle {
|
||||
io::stdin().as_raw_handle()
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRawHandle for Stdout {
|
||||
fn as_raw_handle(&self) -> RawHandle {
|
||||
io::stdout().as_raw_handle()
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRawHandle for Stderr {
|
||||
fn as_raw_handle(&self) -> RawHandle {
|
||||
io::stderr().as_raw_handle()
|
||||
}
|
||||
}
|
||||
|
||||
impl StdinExt for Stdin {
|
||||
fn stdin() -> io::Result<Box<dyn Handle>> {
|
||||
let file = unsafe { File::from_raw_handle(io::stdin().as_raw_handle()) };
|
||||
let file = ManuallyDrop::new(file);
|
||||
let file_type = get_file_type(&file)?;
|
||||
let rights = get_rights(&file_type)?;
|
||||
let rights = Cell::new(rights);
|
||||
Ok(Box::new(Self { file_type, rights }))
|
||||
}
|
||||
}
|
||||
|
||||
impl StdoutExt for Stdout {
|
||||
fn stdout() -> io::Result<Box<dyn Handle>> {
|
||||
let file = unsafe { File::from_raw_handle(io::stdin().as_raw_handle()) };
|
||||
let file = ManuallyDrop::new(file);
|
||||
let file_type = get_file_type(&file)?;
|
||||
let rights = get_rights(&file_type)?;
|
||||
let rights = Cell::new(rights);
|
||||
Ok(Box::new(Self { file_type, rights }))
|
||||
}
|
||||
}
|
||||
|
||||
impl StderrExt for Stderr {
|
||||
fn stderr() -> io::Result<Box<dyn Handle>> {
|
||||
let file = unsafe { File::from_raw_handle(io::stdin().as_raw_handle()) };
|
||||
let file = ManuallyDrop::new(file);
|
||||
let file_type = get_file_type(&file)?;
|
||||
let rights = get_rights(&file_type)?;
|
||||
let rights = Cell::new(rights);
|
||||
Ok(Box::new(Self { file_type, rights }))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user