Implement fd_filestat_get for all platforms (#42)
* Implement fd_filestat_get for all platforms * Remove an old comment * Remove panics from the syscall wrappers * Return WASI error type * Reuse Metadata if possible to save syscalls. * Refactor the change for two separate fd_filestat_get_impl * Refactor error handling
This commit is contained in:
committed by
Jakub Konka
parent
e759e3c2a4
commit
89fbde2c3f
@@ -6,17 +6,27 @@ cfg_if! {
|
||||
mod unix;
|
||||
pub use self::unix::*;
|
||||
|
||||
pub fn errno_from_host(err: i32) -> host::__wasi_errno_t {
|
||||
pub(crate) fn errno_from_host(err: i32) -> host::__wasi_errno_t {
|
||||
host_impl::errno_from_nix(nix::errno::from_i32(err))
|
||||
}
|
||||
} else if #[cfg(windows)] {
|
||||
mod windows;
|
||||
pub use self::windows::*;
|
||||
|
||||
pub fn errno_from_host(err: i32) -> host::__wasi_errno_t {
|
||||
pub(crate) fn errno_from_host(err: i32) -> host::__wasi_errno_t {
|
||||
host_impl::errno_from_win(winx::winerror::WinError::from_u32(err as u32))
|
||||
}
|
||||
} else {
|
||||
compile_error!("wasi-common doesn't compile for this platform yet");
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn errno_from_ioerror(e: std::io::Error) -> host::__wasi_errno_t {
|
||||
match e.raw_os_error() {
|
||||
Some(code) => errno_from_host(code),
|
||||
None => {
|
||||
log::debug!("Inconvertible OS error: {}", e);
|
||||
host::__WASI_EIO
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,13 +3,15 @@
|
||||
use super::fs_helpers::*;
|
||||
use crate::ctx::WasiCtx;
|
||||
use crate::fdentry::FdEntry;
|
||||
use crate::sys::errno_from_host;
|
||||
use crate::helpers::systemtime_to_timestamp;
|
||||
use crate::sys::fdentry_impl::determine_type_rights;
|
||||
use crate::sys::host_impl;
|
||||
use crate::sys::{errno_from_host, errno_from_ioerror};
|
||||
use crate::{host, wasm32, Result};
|
||||
use nix::libc::{self, c_long, c_void, off_t};
|
||||
use std::convert::TryInto;
|
||||
use std::ffi::CString;
|
||||
use std::fs::File;
|
||||
use std::fs::{File, Metadata};
|
||||
use std::os::unix::fs::FileExt;
|
||||
use std::os::unix::prelude::{AsRawFd, FromRawFd};
|
||||
|
||||
@@ -356,11 +358,54 @@ pub(crate) fn path_rename(
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn fd_filestat_get(fd: &File) -> Result<host::__wasi_filestat_t> {
|
||||
use nix::sys::stat::fstat;
|
||||
let filestat =
|
||||
fstat(fd.as_raw_fd()).map_err(|err| host_impl::errno_from_nix(err.as_errno().unwrap()))?;
|
||||
host_impl::filestat_from_nix(filestat)
|
||||
pub(crate) fn fd_filestat_get_impl(file: &std::fs::File) -> Result<host::__wasi_filestat_t> {
|
||||
use std::os::unix::fs::MetadataExt;
|
||||
|
||||
let metadata = file.metadata().map_err(errno_from_ioerror)?;
|
||||
Ok(host::__wasi_filestat_t {
|
||||
st_dev: metadata.dev(),
|
||||
st_ino: metadata.ino(),
|
||||
st_nlink: metadata
|
||||
.nlink()
|
||||
.try_into()
|
||||
.map_err(|_| host::__WASI_EOVERFLOW)?, // u64 doesn't fit into u32
|
||||
st_size: metadata.len(),
|
||||
st_atim: metadata
|
||||
.accessed()
|
||||
.map_err(errno_from_ioerror)
|
||||
.and_then(systemtime_to_timestamp)?,
|
||||
st_ctim: metadata
|
||||
.ctime()
|
||||
.try_into()
|
||||
.map_err(|_| host::__WASI_EOVERFLOW)?, // i64 doesn't fit into u64
|
||||
st_mtim: metadata
|
||||
.modified()
|
||||
.map_err(errno_from_ioerror)
|
||||
.and_then(systemtime_to_timestamp)?,
|
||||
st_filetype: filetype(&metadata),
|
||||
})
|
||||
}
|
||||
|
||||
fn filetype(metadata: &Metadata) -> host::__wasi_filetype_t {
|
||||
use std::os::unix::fs::FileTypeExt;
|
||||
let ftype = metadata.file_type();
|
||||
if ftype.is_file() {
|
||||
host::__WASI_FILETYPE_REGULAR_FILE
|
||||
} else if ftype.is_dir() {
|
||||
host::__WASI_FILETYPE_DIRECTORY
|
||||
} else if ftype.is_symlink() {
|
||||
host::__WASI_FILETYPE_SYMBOLIC_LINK
|
||||
} else if ftype.is_char_device() {
|
||||
host::__WASI_FILETYPE_CHARACTER_DEVICE
|
||||
} else if ftype.is_block_device() {
|
||||
host::__WASI_FILETYPE_BLOCK_DEVICE
|
||||
} else if ftype.is_socket() || ftype.is_fifo() {
|
||||
// TODO we should use getsockopt to find out if it's
|
||||
// SOCKET_STREAM or SOCKET_DGRAM
|
||||
host::__WASI_FILETYPE_SOCKET_STREAM
|
||||
} else {
|
||||
host::__WASI_FILETYPE_UNKNOWN
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn fd_filestat_set_times(
|
||||
|
||||
@@ -3,11 +3,13 @@
|
||||
use super::fs_helpers::*;
|
||||
use crate::ctx::WasiCtx;
|
||||
use crate::fdentry::FdEntry;
|
||||
use crate::sys::errno_from_host;
|
||||
use crate::helpers::systemtime_to_timestamp;
|
||||
use crate::sys::{errno_from_ioerror, errno_from_host};
|
||||
use crate::sys::fdentry_impl::determine_type_rights;
|
||||
use crate::sys::host_impl;
|
||||
use crate::{host, Result};
|
||||
use std::fs::File;
|
||||
use std::convert::TryInto;
|
||||
use std::fs::{File, Metadata};
|
||||
use std::io::{self, Seek, SeekFrom};
|
||||
use std::os::windows::fs::FileExt;
|
||||
use std::os::windows::prelude::{AsRawHandle, FromRawHandle};
|
||||
@@ -173,8 +175,65 @@ pub(crate) fn path_rename(
|
||||
unimplemented!("path_rename")
|
||||
}
|
||||
|
||||
pub(crate) fn fd_filestat_get(fd: &File) -> Result<host::__wasi_filestat_t> {
|
||||
unimplemented!("fd_filestat_get")
|
||||
pub(crate) fn num_hardlinks(file: &File, _metadata: &Metadata) -> io::Result<u64> {
|
||||
Ok(winx::file::get_fileinfo(file)?.nNumberOfLinks.into())
|
||||
}
|
||||
|
||||
pub(crate) fn device_id(file: &File, _metadata: &Metadata) -> io::Result<u64> {
|
||||
Ok(winx::file::get_fileinfo(file)?.dwVolumeSerialNumber.into())
|
||||
}
|
||||
|
||||
pub(crate) fn file_serial_no(file: &File, _metadata: &Metadata) -> io::Result<u64> {
|
||||
let info = winx::file::get_fileinfo(file)?;
|
||||
let high = info.nFileIndexHigh;
|
||||
let low = info.nFileIndexLow;
|
||||
let no = ((high as u64) << 32) | (low as u64);
|
||||
Ok(no)
|
||||
}
|
||||
|
||||
pub(crate) fn change_time(file: &File, _metadata: &Metadata) -> io::Result<i64> {
|
||||
winx::file::change_time(file)
|
||||
}
|
||||
|
||||
pub(crate) fn fd_filestat_get_impl(file: &std::fs::File) -> Result<host::__wasi_filestat_t> {
|
||||
let metadata = file.metadata().map_err(errno_from_ioerror)?;
|
||||
Ok(host::__wasi_filestat_t {
|
||||
st_dev: device_id(file, &metadata).map_err(errno_from_ioerror)?,
|
||||
st_ino: file_serial_no(file, &metadata).map_err(errno_from_ioerror)?,
|
||||
st_nlink: num_hardlinks(file, &metadata)
|
||||
.map_err(errno_from_ioerror)?
|
||||
.try_into()
|
||||
.map_err(|_| host::__WASI_EOVERFLOW)?, // u64 doesn't fit into u32
|
||||
st_size: metadata.len(),
|
||||
st_atim: metadata
|
||||
.accessed()
|
||||
.map_err(errno_from_ioerror)
|
||||
.and_then(systemtime_to_timestamp)?,
|
||||
st_ctim: change_time(file, &metadata)
|
||||
.map_err(errno_from_ioerror)?
|
||||
.try_into()
|
||||
.map_err(|_| host::__WASI_EOVERFLOW)?, // i64 doesn't fit into u64
|
||||
st_mtim: metadata
|
||||
.modified()
|
||||
.map_err(errno_from_ioerror)
|
||||
.and_then(systemtime_to_timestamp)?,
|
||||
st_filetype: filetype(&metadata).map_err(errno_from_ioerror)?,
|
||||
})
|
||||
}
|
||||
|
||||
fn filetype(metadata: &Metadata) -> io::Result<host::__wasi_filetype_t> {
|
||||
let ftype = metadata.file_type();
|
||||
let ret = if ftype.is_file() {
|
||||
host::__WASI_FILETYPE_REGULAR_FILE
|
||||
} else if ftype.is_dir() {
|
||||
host::__WASI_FILETYPE_DIRECTORY
|
||||
} else if ftype.is_symlink() {
|
||||
host::__WASI_FILETYPE_SYMBOLIC_LINK
|
||||
} else {
|
||||
host::__WASI_FILETYPE_UNKNOWN
|
||||
};
|
||||
|
||||
Ok(ret)
|
||||
}
|
||||
|
||||
pub(crate) fn fd_filestat_set_times(
|
||||
|
||||
Reference in New Issue
Block a user