[wasi-common] Clean up fd_filestat_get implementation (#757)

* Clean up fd_filestat_get implementation

This commit does 4 things:
* Adds `yanix::file::fstat`, a wrapper around `libc::fstat`.
* It essentially reverts 89fbde2 for Unix hosts -- in other words,
  it brings back the use of `fstat` to obtain `libc::stat` from a
  file descriptor, rather than relying on `std::fs::Metadata`. This
  way, we reuse `host_impl::filestat_from_nix` in
  `hostcalls_impl::fd_filestat_get` implementation rather than
  unnecessarily duplicate code for converting filestats into
  `__wasi_filestat_t`.
* Moves `crate::helpers::systemtime_to_timestamp` to Windows `host_impl`
  module. It does the same thing with helpers which assist in converting
  `std::fs::Metadata` into `__wasi_filestat_t`. This should retain symmetry
  between *nix and Windows impls.
* Makes timestamp conversions in `host_impl::filestat_from_nix` fallible.

* Backport changes to snapshot0

* Signal no overflow with `from` rather than `as` cast
This commit is contained in:
Jakub Konka
2020-01-08 16:34:38 +01:00
committed by GitHub
parent b46f26361f
commit 06be4b1495
15 changed files with 183 additions and 216 deletions

View File

@@ -190,6 +190,13 @@ pub unsafe fn fstatat<P: AsRef<OsStr>>(dirfd: RawFd, path: P, flags: AtFlag) ->
Ok(filestat.assume_init())
}
pub unsafe fn fstat(fd: RawFd) -> Result<libc::stat> {
use std::mem::MaybeUninit;
let mut filestat = MaybeUninit::<libc::stat>::uninit();
Errno::from_result(libc::fstat(fd, filestat.as_mut_ptr()))?;
Ok(filestat.assume_init())
}
/// `fionread()` function, equivalent to `ioctl(fd, FIONREAD, *bytes)`.
pub unsafe fn fionread(fd: RawFd) -> Result<usize> {
let mut nread: libc::c_int = 0;