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::sys::fdentry_impl::{determine_type_and_access_rights, OsFile};
|
||||||
use crate::{host, Error, Result};
|
use crate::{host, Error, Result};
|
||||||
use std::mem::ManuallyDrop;
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::{fs, io};
|
use std::{fs, io};
|
||||||
|
|
||||||
@@ -62,8 +61,7 @@ impl Descriptor {
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(crate) struct FdObject {
|
pub(crate) struct FdObject {
|
||||||
pub(crate) file_type: host::__wasi_filetype_t,
|
pub(crate) file_type: host::__wasi_filetype_t,
|
||||||
pub(crate) descriptor: ManuallyDrop<Descriptor>,
|
pub(crate) descriptor: Descriptor,
|
||||||
pub(crate) needs_close: bool,
|
|
||||||
// TODO: directories
|
// TODO: directories
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,22 +73,13 @@ pub(crate) struct FdEntry {
|
|||||||
pub(crate) preopen_path: Option<PathBuf>,
|
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 {
|
impl FdEntry {
|
||||||
pub(crate) fn from(file: fs::File) -> Result<Self> {
|
pub(crate) fn from(file: fs::File) -> Result<Self> {
|
||||||
unsafe { determine_type_and_access_rights(&file) }.map(
|
unsafe { determine_type_and_access_rights(&file) }.map(
|
||||||
|(file_type, rights_base, rights_inheriting)| Self {
|
|(file_type, rights_base, rights_inheriting)| Self {
|
||||||
fd_object: FdObject {
|
fd_object: FdObject {
|
||||||
file_type,
|
file_type,
|
||||||
descriptor: ManuallyDrop::new(Descriptor::OsFile(OsFile::from(file))),
|
descriptor: Descriptor::OsFile(OsFile::from(file)),
|
||||||
needs_close: true,
|
|
||||||
},
|
},
|
||||||
rights_base,
|
rights_base,
|
||||||
rights_inheriting,
|
rights_inheriting,
|
||||||
@@ -108,8 +97,7 @@ impl FdEntry {
|
|||||||
|(file_type, rights_base, rights_inheriting)| Self {
|
|(file_type, rights_base, rights_inheriting)| Self {
|
||||||
fd_object: FdObject {
|
fd_object: FdObject {
|
||||||
file_type,
|
file_type,
|
||||||
descriptor: ManuallyDrop::new(Descriptor::Stdin),
|
descriptor: Descriptor::Stdin,
|
||||||
needs_close: true,
|
|
||||||
},
|
},
|
||||||
rights_base,
|
rights_base,
|
||||||
rights_inheriting,
|
rights_inheriting,
|
||||||
@@ -123,8 +111,7 @@ impl FdEntry {
|
|||||||
|(file_type, rights_base, rights_inheriting)| Self {
|
|(file_type, rights_base, rights_inheriting)| Self {
|
||||||
fd_object: FdObject {
|
fd_object: FdObject {
|
||||||
file_type,
|
file_type,
|
||||||
descriptor: ManuallyDrop::new(Descriptor::Stdout),
|
descriptor: Descriptor::Stdout,
|
||||||
needs_close: true,
|
|
||||||
},
|
},
|
||||||
rights_base,
|
rights_base,
|
||||||
rights_inheriting,
|
rights_inheriting,
|
||||||
@@ -138,8 +125,7 @@ impl FdEntry {
|
|||||||
|(file_type, rights_base, rights_inheriting)| Self {
|
|(file_type, rights_base, rights_inheriting)| Self {
|
||||||
fd_object: FdObject {
|
fd_object: FdObject {
|
||||||
file_type,
|
file_type,
|
||||||
descriptor: ManuallyDrop::new(Descriptor::Stderr),
|
descriptor: Descriptor::Stderr,
|
||||||
needs_close: true,
|
|
||||||
},
|
},
|
||||||
rights_base,
|
rights_base,
|
||||||
rights_inheriting,
|
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)?;
|
wasi_ctx.fds.remove(&fd).ok_or(Error::EBADF)?;
|
||||||
fe.fd_object.needs_close = true;
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -158,7 +156,7 @@ pub(crate) unsafe fn fd_read(
|
|||||||
.map(|vec| host::iovec_to_host_mut(vec))
|
.map(|vec| host::iovec_to_host_mut(vec))
|
||||||
.collect();
|
.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::OsFile(file) => file.read_vectored(&mut iovs),
|
||||||
Descriptor::Stdin => io::stdin().lock().read_vectored(&mut iovs),
|
Descriptor::Stdin => io::stdin().lock().read_vectored(&mut iovs),
|
||||||
_ => return Err(Error::EBADF),
|
_ => 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();
|
let iovs: Vec<io::IoSlice> = iovs.iter().map(|vec| host::iovec_to_host(vec)).collect();
|
||||||
|
|
||||||
// perform unbuffered writes
|
// 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::OsFile(file) => file.write_vectored(&iovs)?,
|
||||||
Descriptor::Stdin => return Err(Error::EBADF),
|
Descriptor::Stdin => return Err(Error::EBADF),
|
||||||
Descriptor::Stdout => {
|
Descriptor::Stdout => {
|
||||||
|
|||||||
Reference in New Issue
Block a user