Skip to content

Commit

Permalink
Rename OsHandle to RawOsHandle
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kubkon committed May 6, 2020
1 parent e503624 commit 6f0d437
Show file tree
Hide file tree
Showing 15 changed files with 58 additions and 74 deletions.
4 changes: 2 additions & 2 deletions crates/wasi-common/src/sys/osdir.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::sys_impl::oshandle::OsHandle;
use super::sys_impl::oshandle::RawOsHandle;
use super::{fd, path, AsFile};
use crate::handle::{Handle, HandleRights};
use crate::wasi::{types, Errno, Result};
Expand All @@ -13,7 +13,7 @@ use std::ops::Deref;
pub(crate) use super::sys_impl::osdir::OsDir;

impl Deref for OsDir {
type Target = OsHandle;
type Target = RawOsHandle;

fn deref(&self) -> &Self::Target {
&self.handle
Expand Down
8 changes: 4 additions & 4 deletions crates/wasi-common/src/sys/osfile.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::sys_impl::oshandle::OsHandle;
use super::sys_impl::oshandle::RawOsHandle;
use super::{fd, AsFile};
use crate::handle::{Handle, HandleRights};
use crate::wasi::{types, Errno, Result};
Expand All @@ -11,18 +11,18 @@ use std::ops::Deref;
#[derive(Debug)]
pub(crate) struct OsFile {
rights: Cell<HandleRights>,
handle: OsHandle,
handle: RawOsHandle,
}

impl OsFile {
pub(super) fn new(rights: HandleRights, handle: OsHandle) -> Self {
pub(super) fn new(rights: HandleRights, handle: RawOsHandle) -> Self {
let rights = Cell::new(rights);
Self { rights, handle }
}
}

impl Deref for OsFile {
type Target = OsHandle;
type Target = RawOsHandle;

fn deref(&self) -> &Self::Target {
&self.handle
Expand Down
8 changes: 4 additions & 4 deletions crates/wasi-common/src/sys/osother.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::sys_impl::oshandle::OsHandle;
use super::sys_impl::oshandle::RawOsHandle;
use super::{fd, AsFile};
use crate::handle::{Handle, HandleRights};
use crate::sandboxed_tty_writer::SandboxedTTYWriter;
Expand All @@ -19,11 +19,11 @@ pub(crate) trait OsOtherExt {
pub(crate) struct OsOther {
file_type: Filetype,
rights: Cell<HandleRights>,
handle: OsHandle,
handle: RawOsHandle,
}

impl OsOther {
pub(super) fn new(file_type: Filetype, rights: HandleRights, handle: OsHandle) -> Self {
pub(super) fn new(file_type: Filetype, rights: HandleRights, handle: RawOsHandle) -> Self {
let rights = Cell::new(rights);
Self {
file_type,
Expand All @@ -34,7 +34,7 @@ impl OsOther {
}

impl Deref for OsOther {
type Target = OsHandle;
type Target = RawOsHandle;

fn deref(&self) -> &Self::Target {
&self.handle
Expand Down
6 changes: 3 additions & 3 deletions crates/wasi-common/src/sys/unix/bsd/osdir.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::handle::HandleRights;
use crate::sys::sys_impl::oshandle::OsHandle;
use crate::sys::sys_impl::oshandle::RawOsHandle;
use crate::wasi::Result;
use std::cell::{Cell, RefCell, RefMut};
use std::io;
Expand All @@ -8,7 +8,7 @@ use yanix::dir::Dir;
#[derive(Debug)]
pub(crate) struct OsDir {
pub(crate) rights: Cell<HandleRights>,
pub(crate) handle: OsHandle,
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
Expand All @@ -23,7 +23,7 @@ pub(crate) struct OsDir {
}

impl OsDir {
pub(crate) fn new(rights: HandleRights, handle: OsHandle) -> io::Result<Self> {
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
Expand Down
4 changes: 2 additions & 2 deletions crates/wasi-common/src/sys/unix/fd.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::oshandle::OsHandle;
use super::oshandle::RawOsHandle;
use crate::sys::osdir::OsDir;
use crate::sys::osfile::OsFile;
use crate::wasi::{self, types, Result};
Expand All @@ -11,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<OsHandle>> {
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.
Expand Down
6 changes: 3 additions & 3 deletions crates/wasi-common/src/sys/unix/linux/osdir.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::handle::HandleRights;
use crate::sys::sys_impl::oshandle::OsHandle;
use crate::sys::sys_impl::oshandle::RawOsHandle;
use crate::wasi::Result;
use std::cell::Cell;
use std::io;
Expand All @@ -8,11 +8,11 @@ use yanix::dir::Dir;
#[derive(Debug)]
pub(crate) struct OsDir {
pub(crate) rights: Cell<HandleRights>,
pub(crate) handle: OsHandle,
pub(crate) handle: RawOsHandle,
}

impl OsDir {
pub(crate) fn new(rights: HandleRights, handle: OsHandle) -> io::Result<Self> {
pub(crate) fn new(rights: HandleRights, handle: RawOsHandle) -> io::Result<Self> {
let rights = Cell::new(rights);
Ok(Self { rights, handle })
}
Expand Down
4 changes: 2 additions & 2 deletions crates/wasi-common/src/sys/unix/osdir.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::oshandle::OsHandle;
use super::oshandle::RawOsHandle;
use crate::handle::HandleRights;
use crate::wasi::{types, RightsExt};
use std::convert::TryFrom;
Expand All @@ -17,7 +17,7 @@ impl TryFrom<File> for OsDir {
return Err(io::Error::from_raw_os_error(libc::EINVAL));
}
let rights = get_rights(&file)?;
let handle = unsafe { OsHandle::from_raw_fd(file.into_raw_fd()) };
let handle = unsafe { RawOsHandle::from_raw_fd(file.into_raw_fd()) };
Self::new(rights, handle)
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/wasi-common/src/sys/unix/osfile.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::oshandle::OsHandle;
use super::oshandle::RawOsHandle;
use crate::handle::HandleRights;
use crate::sys::osfile::OsFile;
use crate::wasi::{types, RightsExt};
Expand All @@ -16,7 +16,7 @@ impl TryFrom<File> for OsFile {
return Err(io::Error::from_raw_os_error(libc::EINVAL));
}
let rights = get_rights(&file)?;
let handle = unsafe { OsHandle::from_raw_fd(file.into_raw_fd()) };
let handle = unsafe { RawOsHandle::from_raw_fd(file.into_raw_fd()) };
Ok(Self::new(rights, handle))
}
}
Expand Down
47 changes: 14 additions & 33 deletions crates/wasi-common/src/sys/unix/oshandle.rs
Original file line number Diff line number Diff line change
@@ -1,59 +1,40 @@
use crate::sys::AsFile;
use std::cell::Cell;
use std::fs::File;
use std::io;
use std::mem::ManuallyDrop;
use std::os::unix::prelude::{AsRawFd, FromRawFd, IntoRawFd, RawFd};

#[derive(Debug)]
pub(crate) struct OsHandle(Cell<RawFd>);
pub(crate) struct RawOsHandle(File);

impl OsHandle {
impl RawOsHandle {
/// Tries clone `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())))
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) {
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);
}
pub(crate) fn update_from(&self, _other: Self) {
panic!("RawOsHandle::update_from should never be issued on Unix!")
}
}

impl Drop for OsHandle {
fn drop(&mut self) {
unsafe {
File::from_raw_fd(self.as_raw_fd());
}
}
}

impl AsRawFd for OsHandle {
impl AsRawFd for RawOsHandle {
fn as_raw_fd(&self) -> RawFd {
self.0.get()
self.0.as_raw_fd()
}
}

impl FromRawFd for OsHandle {
unsafe fn from_raw_fd(fd: RawFd) -> Self {
Self(Cell::new(fd))
impl IntoRawFd for RawOsHandle {
fn into_raw_fd(self) -> RawFd {
self.0.into_raw_fd()
}
}

impl IntoRawFd for OsHandle {
fn into_raw_fd(self) -> RawFd {
// We need to prevent dropping of self
let wrapped = ManuallyDrop::new(self);
wrapped.0.get()
impl FromRawFd for RawOsHandle {
unsafe fn from_raw_fd(raw: RawFd) -> Self {
Self(File::from_raw_fd(raw))
}
}
4 changes: 2 additions & 2 deletions crates/wasi-common/src/sys/unix/osother.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::get_file_type;
use super::oshandle::OsHandle;
use super::oshandle::RawOsHandle;
use crate::handle::{Handle, HandleRights};
use crate::sys::osother::{OsOther, OsOtherExt};
use crate::wasi::{types, RightsExt};
Expand All @@ -17,7 +17,7 @@ impl TryFrom<File> for OsOther {
return Err(io::Error::from_raw_os_error(libc::EINVAL));
}
let rights = get_rights(&file, &file_type)?;
let handle = unsafe { OsHandle::from_raw_fd(file.into_raw_fd()) };
let handle = unsafe { RawOsHandle::from_raw_fd(file.into_raw_fd()) };
Ok(Self::new(file_type, rights, handle))
}
}
Expand Down
9 changes: 6 additions & 3 deletions crates/wasi-common/src/sys/windows/fd.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::file_serial_no;
use super::oshandle::OsHandle;
use super::oshandle::RawOsHandle;
use crate::path;
use crate::sys::osdir::OsDir;
use crate::sys::osfile::OsFile;
Expand Down Expand Up @@ -41,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<OsHandle>> {
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(
Expand All @@ -51,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(OsHandle::from_raw_handle(winx::file::reopen_file(
Ok(Some(RawOsHandle::from_raw_handle(winx::file::reopen_file(
handle,
new_access_mode,
fdflags.into(),
Expand Down
8 changes: 4 additions & 4 deletions crates/wasi-common/src/sys/windows/osdir.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::oshandle::OsHandle;
use super::oshandle::RawOsHandle;
use crate::handle::HandleRights;
use crate::wasi::{types, RightsExt};
use std::cell::Cell;
Expand All @@ -10,11 +10,11 @@ use std::os::windows::prelude::{AsRawHandle, FromRawHandle, IntoRawHandle};
#[derive(Debug)]
pub(crate) struct OsDir {
pub(crate) rights: Cell<HandleRights>,
pub(crate) handle: OsHandle,
pub(crate) handle: RawOsHandle,
}

impl OsDir {
pub(crate) fn new(rights: HandleRights, handle: OsHandle) -> io::Result<Self> {
pub(crate) fn new(rights: HandleRights, handle: RawOsHandle) -> io::Result<Self> {
let rights = Cell::new(rights);
Ok(Self { rights, handle })
}
Expand All @@ -29,7 +29,7 @@ impl TryFrom<File> for OsDir {
return Err(io::Error::from_raw_os_error(libc::EINVAL));
}
let rights = get_rights(&file)?;
let handle = unsafe { OsHandle::from_raw_handle(file.into_raw_handle()) };
let handle = unsafe { RawOsHandle::from_raw_handle(file.into_raw_handle()) };
Self::new(rights, handle)
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/wasi-common/src/sys/windows/osfile.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::oshandle::OsHandle;
use super::oshandle::RawOsHandle;
use crate::handle::HandleRights;
use crate::sys::osfile::OsFile;
use crate::wasi::{types, RightsExt};
Expand All @@ -16,7 +16,7 @@ impl TryFrom<File> for OsFile {
return Err(io::Error::from_raw_os_error(libc::EINVAL));
}
let rights = get_rights(&file)?;
let handle = unsafe { OsHandle::from_raw_handle(file.into_raw_handle()) };
let handle = unsafe { RawOsHandle::from_raw_handle(file.into_raw_handle()) };
Ok(Self::new(rights, handle))
}
}
Expand Down
12 changes: 6 additions & 6 deletions crates/wasi-common/src/sys/windows/oshandle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use std::mem::ManuallyDrop;
use std::os::windows::prelude::{AsRawHandle, FromRawHandle, IntoRawHandle, RawHandle};

#[derive(Debug)]
pub(crate) struct OsHandle(Cell<RawHandle>);
pub(crate) struct RawOsHandle(Cell<RawHandle>);

impl OsHandle {
impl RawOsHandle {
/// Tries cloning `self`.
pub(crate) fn try_clone(&self) -> io::Result<Self> {
let handle = self.as_file()?.try_clone()?;
Expand All @@ -27,27 +27,27 @@ impl OsHandle {
}
}

impl Drop for OsHandle {
impl Drop for RawOsHandle {
fn drop(&mut self) {
unsafe {
File::from_raw_handle(self.as_raw_handle());
}
}
}

impl AsRawHandle for OsHandle {
impl AsRawHandle for RawOsHandle {
fn as_raw_handle(&self) -> RawHandle {
self.0.get()
}
}

impl FromRawHandle for OsHandle {
impl FromRawHandle for RawOsHandle {
unsafe fn from_raw_handle(handle: RawHandle) -> Self {
Self(Cell::new(handle))
}
}

impl IntoRawHandle for OsHandle {
impl IntoRawHandle for RawOsHandle {
fn into_raw_handle(self) -> RawHandle {
// We need to prevent dropping of the OsFile
let wrapped = ManuallyDrop::new(self);
Expand Down
4 changes: 2 additions & 2 deletions crates/wasi-common/src/sys/windows/osother.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::get_file_type;
use super::oshandle::OsHandle;
use super::oshandle::RawOsHandle;
use crate::handle::{Handle, HandleRights};
use crate::sys::osother::{OsOther, OsOtherExt};
use crate::wasi::{types, RightsExt};
Expand All @@ -17,7 +17,7 @@ impl TryFrom<File> for OsOther {
return Err(io::Error::from_raw_os_error(libc::EINVAL));
}
let rights = get_rights(&file_type)?;
let handle = unsafe { OsHandle::from_raw_handle(file.into_raw_handle()) };
let handle = unsafe { RawOsHandle::from_raw_handle(file.into_raw_handle()) };
Ok(Self::new(file_type, rights, handle))
}
}
Expand Down

0 comments on commit 6f0d437

Please sign in to comment.