Support fd_fdstat_get and fd_renumber on stdin/stdout/stderr (#631)

* Support fd_fdstat_get on stdin/stdout/stderr.

Add a routine for obtaining an `OsFile` containing a file descriptor for
stdin/stdout/stderr so that we can do fd_fdstat_get on them.

* Add a testcase for fd_fdstat_get etc. on stdin etc.

* Don't dup file descriptors in fd_renumber.

* Fix compilation on macOS

* Rename OsFile to OsHandle

This commits renames `OsFile` to `OsHandle` which seems to make
more sense semantically as it is permitted to hold a valid OS handle
to OS entities other than simply file/dir (e.g., socket, stream, etc.).
As such, this commit also renames methods on `Descriptor` struct
from `as_actual_file` to `as_file` as this in reality does pertain
ops on FS entities such as files/dirs, and `as_file` to `as_os_handle`
as in this case it can be anything, from file, through a socket, to
a stream.

* Fix compilation on Linux

* Introduce `OsHandleRef` for borrowing OS resources.

To prevent a `ManuallyDrop<OsHandleRef>` from outliving the resource it
holds on to, create an `OsHandleRef` class parameterized on the lifetime
of the `Descriptor`.

* Fix scoping to pub-priv and backport to snapshot_0
This commit is contained in:
Dan Gohman
2019-11-28 05:36:18 -08:00
committed by Jakub Konka
parent b69758f672
commit 1f9d764d5d
22 changed files with 278 additions and 191 deletions

View File

@@ -1,5 +1,5 @@
use super::super::dir::{Dir, Entry, SeekLoc};
use super::osfile::OsFile;
use super::oshandle::OsHandle;
use crate::hostcalls_impl::{Dirent, PathGet};
use crate::sys::host_impl;
use crate::sys::unix::str_to_cstring;
@@ -205,12 +205,12 @@ pub(crate) fn fd_advise(
}
pub(crate) fn fd_readdir<'a>(
os_file: &'a mut OsFile,
os_handle: &'a mut OsHandle,
cookie: wasi::__wasi_dircookie_t,
) -> Result<impl Iterator<Item = Result<Dirent>> + 'a> {
use std::sync::Mutex;
let dir = match os_file.dir {
let dir = match os_handle.dir {
Some(ref mut dir) => dir,
None => {
// We need to duplicate the fd, because `opendir(3)`:
@@ -219,9 +219,9 @@ pub(crate) fn fd_readdir<'a>(
// descriptor, or to modify the state of the associated description other
// than by means of closedir(), readdir(), readdir_r(), or rewinddir(),
// the behaviour is undefined.
let fd = (*os_file).try_clone()?;
let fd = (*os_handle).try_clone()?;
let dir = Dir::from(fd)?;
os_file.dir.get_or_insert(Mutex::new(dir))
os_handle.dir.get_or_insert(Mutex::new(dir))
}
};
let mut dir = dir.lock().unwrap();