Remove needs_close from FdObject
This commit removes `needs_close` field from `FdObject`, and stores the underlying `Descriptor` without the `ManuallyDrop` wrapper.
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
use crate::sys::fdentry_impl::{determine_type_and_access_rights, OsFile};
|
||||
use crate::{host, Error, Result};
|
||||
use std::mem::ManuallyDrop;
|
||||
use std::path::PathBuf;
|
||||
use std::{fs, io};
|
||||
|
||||
@@ -62,8 +61,7 @@ impl Descriptor {
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct FdObject {
|
||||
pub(crate) file_type: host::__wasi_filetype_t,
|
||||
pub(crate) descriptor: ManuallyDrop<Descriptor>,
|
||||
pub(crate) needs_close: bool,
|
||||
pub(crate) descriptor: Descriptor,
|
||||
// TODO: directories
|
||||
}
|
||||
|
||||
@@ -75,22 +73,13 @@ pub(crate) struct FdEntry {
|
||||
pub(crate) preopen_path: Option<PathBuf>,
|
||||
}
|
||||
|
||||
impl Drop for FdObject {
|
||||
fn drop(&mut self) {
|
||||
if self.needs_close {
|
||||
unsafe { ManuallyDrop::drop(&mut self.descriptor) };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FdEntry {
|
||||
pub(crate) fn from(file: fs::File) -> Result<Self> {
|
||||
unsafe { determine_type_and_access_rights(&file) }.map(
|
||||
|(file_type, rights_base, rights_inheriting)| Self {
|
||||
fd_object: FdObject {
|
||||
file_type,
|
||||
descriptor: ManuallyDrop::new(Descriptor::OsFile(OsFile::from(file))),
|
||||
needs_close: true,
|
||||
descriptor: Descriptor::OsFile(OsFile::from(file)),
|
||||
},
|
||||
rights_base,
|
||||
rights_inheriting,
|
||||
@@ -108,8 +97,7 @@ impl FdEntry {
|
||||
|(file_type, rights_base, rights_inheriting)| Self {
|
||||
fd_object: FdObject {
|
||||
file_type,
|
||||
descriptor: ManuallyDrop::new(Descriptor::Stdin),
|
||||
needs_close: true,
|
||||
descriptor: Descriptor::Stdin,
|
||||
},
|
||||
rights_base,
|
||||
rights_inheriting,
|
||||
@@ -123,8 +111,7 @@ impl FdEntry {
|
||||
|(file_type, rights_base, rights_inheriting)| Self {
|
||||
fd_object: FdObject {
|
||||
file_type,
|
||||
descriptor: ManuallyDrop::new(Descriptor::Stdout),
|
||||
needs_close: true,
|
||||
descriptor: Descriptor::Stdout,
|
||||
},
|
||||
rights_base,
|
||||
rights_inheriting,
|
||||
@@ -138,8 +125,7 @@ impl FdEntry {
|
||||
|(file_type, rights_base, rights_inheriting)| Self {
|
||||
fd_object: FdObject {
|
||||
file_type,
|
||||
descriptor: ManuallyDrop::new(Descriptor::Stderr),
|
||||
needs_close: true,
|
||||
descriptor: Descriptor::Stderr,
|
||||
},
|
||||
rights_base,
|
||||
rights_inheriting,
|
||||
|
||||
@@ -24,9 +24,7 @@ pub(crate) unsafe fn fd_close(wasi_ctx: &mut WasiCtx, fd: wasm32::__wasi_fd_t) -
|
||||
}
|
||||
}
|
||||
|
||||
let mut fe = wasi_ctx.fds.remove(&fd).ok_or(Error::EBADF)?;
|
||||
fe.fd_object.needs_close = true;
|
||||
|
||||
wasi_ctx.fds.remove(&fd).ok_or(Error::EBADF)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -158,7 +156,7 @@ pub(crate) unsafe fn fd_read(
|
||||
.map(|vec| host::iovec_to_host_mut(vec))
|
||||
.collect();
|
||||
|
||||
let maybe_host_nread = match &mut *fe.fd_object.descriptor {
|
||||
let maybe_host_nread = match &mut fe.fd_object.descriptor {
|
||||
Descriptor::OsFile(file) => file.read_vectored(&mut iovs),
|
||||
Descriptor::Stdin => io::stdin().lock().read_vectored(&mut iovs),
|
||||
_ => return Err(Error::EBADF),
|
||||
@@ -374,7 +372,7 @@ pub(crate) unsafe fn fd_write(
|
||||
let iovs: Vec<io::IoSlice> = iovs.iter().map(|vec| host::iovec_to_host(vec)).collect();
|
||||
|
||||
// perform unbuffered writes
|
||||
let host_nwritten = match &mut *fe.fd_object.descriptor {
|
||||
let host_nwritten = match &mut fe.fd_object.descriptor {
|
||||
Descriptor::OsFile(file) => file.write_vectored(&iovs)?,
|
||||
Descriptor::Stdin => return Err(Error::EBADF),
|
||||
Descriptor::Stdout => {
|
||||
|
||||
Reference in New Issue
Block a user