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,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))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user