Check for socket type in filetype.
The signature for Windows has also been changed, so that the function can be reused in cross-platform modules.
This commit is contained in:
committed by
Jakub Konka
parent
8db57bd6c6
commit
d617fe67ad
@@ -339,29 +339,35 @@ pub(crate) fn fd_filestat_get_impl(file: &std::fs::File) -> Result<host::__wasi_
|
||||
.modified()
|
||||
.map_err(errno_from_ioerror)
|
||||
.and_then(systemtime_to_timestamp)?,
|
||||
st_filetype: filetype(&metadata),
|
||||
st_filetype: filetype(file, &metadata)?,
|
||||
})
|
||||
}
|
||||
|
||||
fn filetype(metadata: &Metadata) -> host::__wasi_filetype_t {
|
||||
fn filetype(file: &File, metadata: &Metadata) -> Result<host::__wasi_filetype_t> {
|
||||
use std::os::unix::fs::FileTypeExt;
|
||||
use nix::sys::socket::{self, SockType};
|
||||
let ftype = metadata.file_type();
|
||||
if ftype.is_file() {
|
||||
host::__WASI_FILETYPE_REGULAR_FILE
|
||||
Ok(host::__WASI_FILETYPE_REGULAR_FILE)
|
||||
} else if ftype.is_dir() {
|
||||
host::__WASI_FILETYPE_DIRECTORY
|
||||
Ok(host::__WASI_FILETYPE_DIRECTORY)
|
||||
} else if ftype.is_symlink() {
|
||||
host::__WASI_FILETYPE_SYMBOLIC_LINK
|
||||
Ok(host::__WASI_FILETYPE_SYMBOLIC_LINK)
|
||||
} else if ftype.is_char_device() {
|
||||
host::__WASI_FILETYPE_CHARACTER_DEVICE
|
||||
Ok(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
|
||||
Ok(host::__WASI_FILETYPE_BLOCK_DEVICE)
|
||||
} else if ftype.is_fifo() {
|
||||
Ok(host::__WASI_FILETYPE_SOCKET_STREAM)
|
||||
} else if ftype.is_socket() {
|
||||
match socket::getsockopt(file.as_raw_fd(), socket::sockopt::SockType).map_err(|err|
|
||||
err.as_errno().unwrap()).map_err(host_impl::errno_from_nix)? {
|
||||
SockType::Datagram => Ok(host::__WASI_FILETYPE_SOCKET_DGRAM),
|
||||
SockType::Stream => Ok(host::__WASI_FILETYPE_SOCKET_STREAM),
|
||||
_ => Ok(host::__WASI_FILETYPE_UNKNOWN),
|
||||
}
|
||||
} else {
|
||||
host::__WASI_FILETYPE_UNKNOWN
|
||||
Ok(host::__WASI_FILETYPE_UNKNOWN)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -288,11 +288,11 @@ pub(crate) fn fd_filestat_get_impl(file: &std::fs::File) -> Result<host::__wasi_
|
||||
.modified()
|
||||
.map_err(errno_from_ioerror)
|
||||
.and_then(systemtime_to_timestamp)?,
|
||||
st_filetype: filetype(&metadata).map_err(errno_from_ioerror)?,
|
||||
st_filetype: filetype(&metadata)?,
|
||||
})
|
||||
}
|
||||
|
||||
fn filetype(metadata: &Metadata) -> io::Result<host::__wasi_filetype_t> {
|
||||
fn filetype(_file: &File, metadata: &Metadata) -> Result<host::__wasi_filetype_t> {
|
||||
let ftype = metadata.file_type();
|
||||
let ret = if ftype.is_file() {
|
||||
host::__WASI_FILETYPE_REGULAR_FILE
|
||||
|
||||
Reference in New Issue
Block a user