Reuse errno_from_ioerror to simplify error handling
This commit is contained in:
committed by
Jakub Konka
parent
e18175c556
commit
92c2b563fc
@@ -1,5 +1,5 @@
|
|||||||
use crate::fdentry::FdEntry;
|
use crate::fdentry::FdEntry;
|
||||||
use crate::sys::{dev_null, errno_from_host};
|
use crate::sys::{dev_null, errno_from_ioerror};
|
||||||
use crate::{host, Result};
|
use crate::{host, Result};
|
||||||
use std::borrow::Borrow;
|
use std::borrow::Borrow;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
@@ -92,11 +92,7 @@ impl WasiCtxBuilder {
|
|||||||
// startup code starts looking at fd 3 for preopens
|
// startup code starts looking at fd 3 for preopens
|
||||||
let mut preopen_fd = 3;
|
let mut preopen_fd = 3;
|
||||||
for (guest_path, dir) in self.preopens {
|
for (guest_path, dir) in self.preopens {
|
||||||
if !dir
|
if !dir.metadata().map_err(errno_from_ioerror)?.is_dir() {
|
||||||
.metadata()
|
|
||||||
.map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))?
|
|
||||||
.is_dir()
|
|
||||||
{
|
|
||||||
return Err(host::__WASI_EBADF);
|
return Err(host::__WASI_EBADF);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use crate::sys::{errno_from_host, fdentry_impl};
|
use crate::sys::{errno_from_ioerror, fdentry_impl};
|
||||||
use crate::{host, Result};
|
use crate::{host, Result};
|
||||||
|
|
||||||
use std::mem::ManuallyDrop;
|
use std::mem::ManuallyDrop;
|
||||||
@@ -92,7 +92,7 @@ impl FdEntry {
|
|||||||
|
|
||||||
pub fn duplicate(file: &fs::File) -> Result<Self> {
|
pub fn duplicate(file: &fs::File) -> Result<Self> {
|
||||||
file.try_clone()
|
file.try_clone()
|
||||||
.map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))
|
.map_err(errno_from_ioerror)
|
||||||
.and_then(Self::from)
|
.and_then(Self::from)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ use crate::fdentry::{Descriptor, FdEntry};
|
|||||||
use crate::memory::*;
|
use crate::memory::*;
|
||||||
use crate::sys::fdentry_impl::determine_type_rights;
|
use crate::sys::fdentry_impl::determine_type_rights;
|
||||||
use crate::sys::hostcalls_impl::fs_helpers::path_open_rights;
|
use crate::sys::hostcalls_impl::fs_helpers::path_open_rights;
|
||||||
use crate::sys::{errno_from_host, host_impl, hostcalls_impl};
|
use crate::sys::{errno_from_ioerror, host_impl, hostcalls_impl};
|
||||||
use crate::{host, wasm32, Result};
|
use crate::{host, wasm32, Result};
|
||||||
use log::trace;
|
use log::trace;
|
||||||
use std::io::{self, Read, Seek, SeekFrom, Write};
|
use std::io::{self, Read, Seek, SeekFrom, Write};
|
||||||
@@ -35,8 +35,7 @@ pub(crate) fn fd_datasync(wasi_ctx: &WasiCtx, fd: wasm32::__wasi_fd_t) -> Result
|
|||||||
.get_fd_entry(fd, host::__WASI_RIGHT_FD_DATASYNC, 0)
|
.get_fd_entry(fd, host::__WASI_RIGHT_FD_DATASYNC, 0)
|
||||||
.and_then(|fe| fe.fd_object.descriptor.as_file())?;
|
.and_then(|fe| fe.fd_object.descriptor.as_file())?;
|
||||||
|
|
||||||
fd.sync_data()
|
fd.sync_data().map_err(errno_from_ioerror)
|
||||||
.map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn fd_pread(
|
pub(crate) fn fd_pread(
|
||||||
@@ -161,8 +160,7 @@ pub(crate) fn fd_read(
|
|||||||
_ => return Err(host::__WASI_EBADF),
|
_ => return Err(host::__WASI_EBADF),
|
||||||
};
|
};
|
||||||
|
|
||||||
let host_nread = maybe_host_nread
|
let host_nread = maybe_host_nread.map_err(errno_from_ioerror)?;
|
||||||
.map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))?;
|
|
||||||
|
|
||||||
trace!(" | *nread={:?}", host_nread);
|
trace!(" | *nread={:?}", host_nread);
|
||||||
|
|
||||||
@@ -245,10 +243,7 @@ pub(crate) fn fd_seek(
|
|||||||
host::__WASI_WHENCE_SET => SeekFrom::Start(offset as u64),
|
host::__WASI_WHENCE_SET => SeekFrom::Start(offset as u64),
|
||||||
_ => return Err(host::__WASI_EINVAL),
|
_ => return Err(host::__WASI_EINVAL),
|
||||||
};
|
};
|
||||||
let host_newoffset = fd.seek(pos).map_err(|err| {
|
let host_newoffset = fd.seek(pos).map_err(errno_from_ioerror)?;
|
||||||
log::debug!("fd_seek error={:?}", err);
|
|
||||||
err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host)
|
|
||||||
})?;
|
|
||||||
|
|
||||||
trace!(" | *newoffset={:?}", host_newoffset);
|
trace!(" | *newoffset={:?}", host_newoffset);
|
||||||
|
|
||||||
@@ -268,9 +263,7 @@ pub(crate) fn fd_tell(
|
|||||||
.get_fd_entry(fd, host::__WASI_RIGHT_FD_TELL, 0)
|
.get_fd_entry(fd, host::__WASI_RIGHT_FD_TELL, 0)
|
||||||
.and_then(|fe| fe.fd_object.descriptor.as_file())?;
|
.and_then(|fe| fe.fd_object.descriptor.as_file())?;
|
||||||
|
|
||||||
let host_offset = fd
|
let host_offset = fd.seek(SeekFrom::Current(0)).map_err(errno_from_ioerror)?;
|
||||||
.seek(SeekFrom::Current(0))
|
|
||||||
.map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))?;
|
|
||||||
|
|
||||||
trace!(" | *newoffset={:?}", host_offset);
|
trace!(" | *newoffset={:?}", host_offset);
|
||||||
|
|
||||||
@@ -352,8 +345,7 @@ pub(crate) fn fd_sync(wasi_ctx: &WasiCtx, fd: wasm32::__wasi_fd_t) -> Result<()>
|
|||||||
let fd = wasi_ctx
|
let fd = wasi_ctx
|
||||||
.get_fd_entry(fd, host::__WASI_RIGHT_FD_SYNC, 0)
|
.get_fd_entry(fd, host::__WASI_RIGHT_FD_SYNC, 0)
|
||||||
.and_then(|fe| fe.fd_object.descriptor.as_file())?;
|
.and_then(|fe| fe.fd_object.descriptor.as_file())?;
|
||||||
fd.sync_all()
|
fd.sync_all().map_err(errno_from_ioerror)
|
||||||
.map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn fd_write(
|
pub(crate) fn fd_write(
|
||||||
@@ -382,26 +374,20 @@ pub(crate) fn fd_write(
|
|||||||
|
|
||||||
// perform unbuffered writes
|
// perform unbuffered writes
|
||||||
let host_nwritten = match &mut *fe.fd_object.descriptor {
|
let host_nwritten = match &mut *fe.fd_object.descriptor {
|
||||||
Descriptor::File(f) => f
|
Descriptor::File(f) => f.write_vectored(&iovs).map_err(errno_from_ioerror)?,
|
||||||
.write_vectored(&iovs)
|
|
||||||
.map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))?,
|
|
||||||
Descriptor::Stdin => return Err(host::__WASI_EBADF),
|
Descriptor::Stdin => return Err(host::__WASI_EBADF),
|
||||||
Descriptor::Stdout => {
|
Descriptor::Stdout => {
|
||||||
// lock for the duration of the scope
|
// lock for the duration of the scope
|
||||||
let stdout = io::stdout();
|
let stdout = io::stdout();
|
||||||
let mut stdout = stdout.lock();
|
let mut stdout = stdout.lock();
|
||||||
let nwritten = stdout
|
let nwritten = stdout.write_vectored(&iovs).map_err(errno_from_ioerror)?;
|
||||||
.write_vectored(&iovs)
|
stdout.flush().map_err(errno_from_ioerror)?;
|
||||||
.map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))?;
|
|
||||||
stdout
|
|
||||||
.flush()
|
|
||||||
.map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))?;
|
|
||||||
nwritten
|
nwritten
|
||||||
}
|
}
|
||||||
Descriptor::Stderr => io::stderr()
|
Descriptor::Stderr => io::stderr()
|
||||||
.lock()
|
.lock()
|
||||||
.write_vectored(&iovs)
|
.write_vectored(&iovs)
|
||||||
.map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))?,
|
.map_err(errno_from_ioerror)?,
|
||||||
};
|
};
|
||||||
|
|
||||||
trace!(" | *nwritten={:?}", host_nwritten);
|
trace!(" | *nwritten={:?}", host_nwritten);
|
||||||
@@ -450,9 +436,7 @@ pub(crate) fn fd_allocate(
|
|||||||
.get_fd_entry(fd, host::__WASI_RIGHT_FD_ALLOCATE, 0)
|
.get_fd_entry(fd, host::__WASI_RIGHT_FD_ALLOCATE, 0)
|
||||||
.and_then(|fe| fe.fd_object.descriptor.as_file())?;
|
.and_then(|fe| fe.fd_object.descriptor.as_file())?;
|
||||||
|
|
||||||
let metadata = fd
|
let metadata = fd.metadata().map_err(errno_from_ioerror)?;
|
||||||
.metadata()
|
|
||||||
.map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))?;
|
|
||||||
|
|
||||||
let current_size = metadata.len();
|
let current_size = metadata.len();
|
||||||
let wanted_size = offset.checked_add(len).ok_or(host::__WASI_E2BIG)?;
|
let wanted_size = offset.checked_add(len).ok_or(host::__WASI_E2BIG)?;
|
||||||
@@ -461,8 +445,7 @@ pub(crate) fn fd_allocate(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if wanted_size > current_size {
|
if wanted_size > current_size {
|
||||||
fd.set_len(wanted_size)
|
fd.set_len(wanted_size).map_err(errno_from_ioerror)
|
||||||
.map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))
|
|
||||||
} else {
|
} else {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use crate::fdentry::Descriptor;
|
use crate::fdentry::Descriptor;
|
||||||
use crate::sys::errno_from_host;
|
use crate::sys::{errno_from_host, errno_from_ioerror};
|
||||||
use crate::{host, Result};
|
use crate::{host, Result};
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::os::unix::prelude::{AsRawFd, FileTypeExt, FromRawFd, RawFd};
|
use std::os::unix::prelude::{AsRawFd, FileTypeExt, FromRawFd, RawFd};
|
||||||
@@ -53,7 +53,7 @@ pub(crate) fn determine_type_rights<Fd: AsRawFd>(
|
|||||||
std::mem::ManuallyDrop::new(unsafe { std::fs::File::from_raw_fd(fd.as_raw_fd()) });
|
std::mem::ManuallyDrop::new(unsafe { std::fs::File::from_raw_fd(fd.as_raw_fd()) });
|
||||||
let ft = file
|
let ft = file
|
||||||
.metadata()
|
.metadata()
|
||||||
.map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))?
|
.map_err(errno_from_ioerror)?
|
||||||
.file_type();
|
.file_type();
|
||||||
if ft.is_block_device() {
|
if ft.is_block_device() {
|
||||||
(
|
(
|
||||||
|
|||||||
@@ -3,8 +3,8 @@
|
|||||||
use super::fs_helpers::*;
|
use super::fs_helpers::*;
|
||||||
use crate::helpers::systemtime_to_timestamp;
|
use crate::helpers::systemtime_to_timestamp;
|
||||||
use crate::hostcalls_impl::PathGet;
|
use crate::hostcalls_impl::PathGet;
|
||||||
|
use crate::sys::errno_from_ioerror;
|
||||||
use crate::sys::host_impl;
|
use crate::sys::host_impl;
|
||||||
use crate::sys::{errno_from_host, errno_from_ioerror};
|
|
||||||
use crate::{host, wasm32, Result};
|
use crate::{host, wasm32, Result};
|
||||||
use nix::libc::{self, c_long, c_void, off_t};
|
use nix::libc::{self, c_long, c_void, off_t};
|
||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
@@ -18,13 +18,11 @@ pub(crate) fn fd_pread(
|
|||||||
buf: &mut [u8],
|
buf: &mut [u8],
|
||||||
offset: host::__wasi_filesize_t,
|
offset: host::__wasi_filesize_t,
|
||||||
) -> Result<usize> {
|
) -> Result<usize> {
|
||||||
file.read_at(buf, offset)
|
file.read_at(buf, offset).map_err(errno_from_ioerror)
|
||||||
.map_err(|e| e.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn fd_pwrite(file: &File, buf: &[u8], offset: host::__wasi_filesize_t) -> Result<usize> {
|
pub(crate) fn fd_pwrite(file: &File, buf: &[u8], offset: host::__wasi_filesize_t) -> Result<usize> {
|
||||||
file.write_at(buf, offset)
|
file.write_at(buf, offset).map_err(errno_from_ioerror)
|
||||||
.map_err(|e| e.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn fd_fdstat_get(fd: &File) -> Result<host::__wasi_fdflags_t> {
|
pub(crate) fn fd_fdstat_get(fd: &File) -> Result<host::__wasi_fdflags_t> {
|
||||||
|
|||||||
@@ -2,16 +2,15 @@ pub(crate) mod fdentry_impl;
|
|||||||
pub(crate) mod host_impl;
|
pub(crate) mod host_impl;
|
||||||
pub(crate) mod hostcalls_impl;
|
pub(crate) mod hostcalls_impl;
|
||||||
|
|
||||||
use crate::sys::errno_from_host;
|
use crate::sys::errno_from_ioerror;
|
||||||
use crate::{host, Result};
|
use crate::Result;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
pub(crate) fn dev_null() -> Result<File> {
|
pub(crate) fn dev_null() -> Result<File> {
|
||||||
File::open("/dev/null")
|
File::open("/dev/null").map_err(errno_from_ioerror)
|
||||||
.map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn preopen_dir<P: AsRef<Path>>(path: P) -> Result<File> {
|
pub fn preopen_dir<P: AsRef<Path>>(path: P) -> Result<File> {
|
||||||
File::open(path).map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))
|
File::open(path).map_err(errno_from_ioerror)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,12 +42,12 @@ pub(crate) fn fd_pread(
|
|||||||
offset: host::__wasi_filesize_t,
|
offset: host::__wasi_filesize_t,
|
||||||
) -> Result<usize> {
|
) -> Result<usize> {
|
||||||
read_at(file, buf, offset)
|
read_at(file, buf, offset)
|
||||||
.map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))
|
.map_err(errno_from_ioerror)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn fd_pwrite(file: &File, buf: &[u8], offset: host::__wasi_filesize_t) -> Result<usize> {
|
pub(crate) fn fd_pwrite(file: &File, buf: &[u8], offset: host::__wasi_filesize_t) -> Result<usize> {
|
||||||
write_at(file, buf, offset)
|
write_at(file, buf, offset)
|
||||||
.map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))
|
.map_err(errno_from_ioerror)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn fd_fdstat_get(fd: &File) -> Result<host::__wasi_fdflags_t> {
|
pub(crate) fn fd_fdstat_get(fd: &File) -> Result<host::__wasi_fdflags_t> {
|
||||||
|
|||||||
@@ -2,13 +2,13 @@ pub(crate) mod fdentry_impl;
|
|||||||
pub(crate) mod host_impl;
|
pub(crate) mod host_impl;
|
||||||
pub(crate) mod hostcalls_impl;
|
pub(crate) mod hostcalls_impl;
|
||||||
|
|
||||||
use crate::sys::errno_from_host;
|
use crate::sys::errno_from_ioerror;
|
||||||
use crate::{host, Result};
|
use crate::Result;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
pub(crate) fn dev_null() -> Result<File> {
|
pub(crate) fn dev_null() -> Result<File> {
|
||||||
File::open("NUL").map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))
|
File::open("NUL").map_err(errno_from_ioerror)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn preopen_dir<P: AsRef<Path>>(path: P) -> Result<File> {
|
pub fn preopen_dir<P: AsRef<Path>>(path: P) -> Result<File> {
|
||||||
@@ -25,5 +25,5 @@ pub fn preopen_dir<P: AsRef<Path>>(path: P) -> Result<File> {
|
|||||||
.read(true)
|
.read(true)
|
||||||
.attributes(FILE_FLAG_BACKUP_SEMANTICS)
|
.attributes(FILE_FLAG_BACKUP_SEMANTICS)
|
||||||
.open(path)
|
.open(path)
|
||||||
.map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))
|
.map_err(errno_from_ioerror)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user