* Refactor use of Refs and RefMuts in wasi-common This commit refactors the use of `Ref`s and `RefMut`s in `wasi-common`. Now, `Entry` is stored behind an `Rc` inside the `EntryTable`. The `Entry` itself on the other hand now stores rights behind a `RefCell` and the descriptor as `Rc<RefCell<..>>` combo to enable easy reference tracking and interior mutability which is required down the line in a couple of syscalls. In essence, this implies that we no longer have need for mutable accessor to `Entry` from `WasiCtx`, and so all related methods go away (`get_entry_mut`, etc.). While here, I've also simplified handling and aggregating of rights on the `Entry` object. Instead of storing base and inheriting rights as separate fields, they are now aggregated into one struct `EntryRights` which features convenient constructors for each possible combination; i.e., when only base rights are set, or both base and inheriting are set, or both are left as empty. Since we do need to be able to mutate those rights down the line in `fdstat_set_rights` syscall, this object is kept behind a `RefCell` (note no `Rc` since we don't need to pass it around anywhere). The descriptor field in `Entry` is now kept behind `Rc<RefCell<..>>` combo since we not only need to mutate it down the line, but we also need to be able to pass it around (as part of the machinery making `poll_oneoff` work). I've also removed `as_file` and `try_clone` methods on `Descriptor` struct since they were adding more noise than necessary, and making them work with `Rc` was unnecessarily complicated. Finally, I've converted the `get_dir_from_os_handle` function into a method attached to the `OsHandle` itself, called `dir_stream`. IMHO, it makes more sense to have it there directly as a method than as a separate function. * Use Cell for types that are Copy
134 lines
4.4 KiB
Rust
134 lines
4.4 KiB
Rust
use crate::entry::{Descriptor, EntryRights, OsHandleRef};
|
|
use crate::wasi::{types, RightsExt};
|
|
use std::fs::File;
|
|
use std::io;
|
|
use std::mem::ManuallyDrop;
|
|
use std::ops::Deref;
|
|
use std::os::windows::prelude::{AsRawHandle, FromRawHandle, RawHandle};
|
|
|
|
#[derive(Debug)]
|
|
pub(crate) struct OsHandle(File);
|
|
|
|
impl From<File> for OsHandle {
|
|
fn from(file: File) -> Self {
|
|
Self(file)
|
|
}
|
|
}
|
|
|
|
impl AsRawHandle for OsHandle {
|
|
fn as_raw_handle(&self) -> RawHandle {
|
|
self.0.as_raw_handle()
|
|
}
|
|
}
|
|
|
|
impl Deref for OsHandle {
|
|
type Target = File;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
&self.0
|
|
}
|
|
}
|
|
|
|
impl AsRawHandle for Descriptor {
|
|
fn as_raw_handle(&self) -> RawHandle {
|
|
match self {
|
|
Self::OsHandle(file) => file.as_raw_handle(),
|
|
Self::VirtualFile(_file) => {
|
|
unimplemented!("virtual as_raw_handle");
|
|
}
|
|
Self::Stdin => io::stdin().as_raw_handle(),
|
|
Self::Stdout => io::stdout().as_raw_handle(),
|
|
Self::Stderr => io::stderr().as_raw_handle(),
|
|
}
|
|
}
|
|
}
|
|
|
|
pub(crate) fn descriptor_as_oshandle<'lifetime>(
|
|
desc: &'lifetime Descriptor,
|
|
) -> OsHandleRef<'lifetime> {
|
|
OsHandleRef::new(ManuallyDrop::new(OsHandle::from(unsafe {
|
|
File::from_raw_handle(desc.as_raw_handle())
|
|
})))
|
|
}
|
|
|
|
/// Returns the set of all possible rights that are both relevant for the file
|
|
/// type and consistent with the open mode.
|
|
///
|
|
/// This function is unsafe because it operates on a raw file descriptor.
|
|
pub(crate) unsafe fn determine_type_and_access_rights<Handle: AsRawHandle>(
|
|
handle: &Handle,
|
|
) -> io::Result<(types::Filetype, EntryRights)> {
|
|
use winx::file::{query_access_information, AccessMode};
|
|
|
|
let (file_type, mut rights) = determine_type_rights(handle)?;
|
|
|
|
match file_type {
|
|
types::Filetype::Directory | types::Filetype::RegularFile => {
|
|
let mode = query_access_information(handle.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((file_type, rights))
|
|
}
|
|
|
|
/// Returns the set of all possible rights that are relevant for file type.
|
|
///
|
|
/// This function is unsafe because it operates on a raw file descriptor.
|
|
pub(crate) unsafe fn determine_type_rights<Handle: AsRawHandle>(
|
|
handle: &Handle,
|
|
) -> io::Result<(types::Filetype, EntryRights)> {
|
|
let (file_type, rights) = {
|
|
let file_type = winx::file::get_file_type(handle.as_raw_handle())?;
|
|
let (file_type, base, inheriting) = if file_type.is_char() {
|
|
// character file: LPT device or console
|
|
// TODO: rule out LPT device
|
|
(
|
|
types::Filetype::CharacterDevice,
|
|
types::Rights::tty_base(),
|
|
types::Rights::tty_base(),
|
|
)
|
|
} else if file_type.is_disk() {
|
|
// disk file: file, dir or disk device
|
|
let file = std::mem::ManuallyDrop::new(File::from_raw_handle(handle.as_raw_handle()));
|
|
let meta = file.metadata()?;
|
|
if meta.is_dir() {
|
|
(
|
|
types::Filetype::Directory,
|
|
types::Rights::directory_base(),
|
|
types::Rights::directory_inheriting(),
|
|
)
|
|
} else if meta.is_file() {
|
|
(
|
|
types::Filetype::RegularFile,
|
|
types::Rights::regular_file_base(),
|
|
types::Rights::regular_file_inheriting(),
|
|
)
|
|
} 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,
|
|
types::Rights::socket_base(),
|
|
types::Rights::socket_inheriting(),
|
|
)
|
|
} else {
|
|
return Err(io::Error::from_raw_os_error(libc::EINVAL));
|
|
};
|
|
(file_type, EntryRights::new(base, inheriting))
|
|
};
|
|
Ok((file_type, rights))
|
|
}
|