Files
wasmtime/crates/wasi-common/src/sys/unix/bsd/fd.rs
Jakub Konka 1f6890e070 [wasi-common] Clean up the use of mutable Entry (#1395)
* Clean up the use of mutable Entry

Until now, several syscalls including `fd_pwrite` etc. were relying on
mutating `&mut Entry` by mutating its inner file handle. This is
unnecessary in almost all cases since all methods mutating `std::fs::File`
in Rust's libstd are also implemented for `&std::fs::File`.

While here, I've also modified `OsHandle` in BSD to include `RefCell<Option<Dir>>`
rather than `Option<Mutex<Dir>>` as was until now. While `RefCell`
could easily be replaced with `RefCell`, since going multithreading
will require a lot of (probably even) conceptual changes to `wasi-common`,
I thought it'd be best not to mix single- with multithreading contexts
and swap all places at once when it comes to it.

I've also had to make some modifications to virtual FS which mainly
swapped mutability for interior mutability in places.

* Use one-liners wherever convenient
2020-03-25 14:00:52 +01:00

22 lines
896 B
Rust

use crate::sys::entry::OsHandle;
use crate::wasi::Result;
use std::cell::RefMut;
use yanix::dir::Dir;
pub(crate) fn get_dir_from_os_handle(os_handle: &OsHandle) -> Result<RefMut<Dir>> {
if os_handle.dir.borrow().is_none() {
// We need to duplicate the fd, because `opendir(3)`:
// Upon successful return from fdopendir(), the file descriptor is under
// control of the system, and if any attempt is made to close the file
// 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_handle).try_clone()?;
let d = Dir::from(fd)?;
*os_handle.dir.borrow_mut() = Some(d);
}
Ok(RefMut::map(os_handle.dir.borrow_mut(), |dir| {
dir.as_mut().unwrap()
}))
}