Clean up more hostcalls
This commit is contained in:
@@ -308,7 +308,12 @@ pub fn fd_seek(
|
|||||||
Ok(fe) => fe,
|
Ok(fe) => fe,
|
||||||
Err(e) => return return_enc_errno(e),
|
Err(e) => return return_enc_errno(e),
|
||||||
};
|
};
|
||||||
let host_newoffset = match hostcalls_impl::fd_seek(fe, offset, whence) {
|
let file = match &*fe.fd_object.descriptor {
|
||||||
|
Descriptor::File(f) => f,
|
||||||
|
_ => return return_enc_errno(host::__WASI_EBADF),
|
||||||
|
};
|
||||||
|
|
||||||
|
let host_newoffset = match hostcalls_impl::fd_seek(file, offset, whence) {
|
||||||
Ok(host_newoffset) => host_newoffset,
|
Ok(host_newoffset) => host_newoffset,
|
||||||
Err(e) => return return_enc_errno(e),
|
Err(e) => return return_enc_errno(e),
|
||||||
};
|
};
|
||||||
@@ -338,7 +343,12 @@ pub fn fd_tell(
|
|||||||
Ok(fe) => fe,
|
Ok(fe) => fe,
|
||||||
Err(e) => return return_enc_errno(e),
|
Err(e) => return return_enc_errno(e),
|
||||||
};
|
};
|
||||||
let host_offset = match hostcalls_impl::fd_tell(fe) {
|
let file = match &*fe.fd_object.descriptor {
|
||||||
|
Descriptor::File(f) => f,
|
||||||
|
_ => return return_enc_errno(host::__WASI_EBADF),
|
||||||
|
};
|
||||||
|
|
||||||
|
let host_offset = match hostcalls_impl::fd_tell(file) {
|
||||||
Ok(host_offset) => host_offset,
|
Ok(host_offset) => host_offset,
|
||||||
Err(e) => return return_enc_errno(e),
|
Err(e) => return return_enc_errno(e),
|
||||||
};
|
};
|
||||||
@@ -367,24 +377,26 @@ pub fn fd_fdstat_get(
|
|||||||
Err(e) => return return_enc_errno(e),
|
Err(e) => return return_enc_errno(e),
|
||||||
};
|
};
|
||||||
|
|
||||||
let ret = if let Some(fe) = wasi_ctx.fds.get(&host_fd) {
|
let fe = match wasi_ctx.fds.get(&host_fd) {
|
||||||
host_fdstat.fs_filetype = fe.fd_object.file_type;
|
Some(fe) => fe,
|
||||||
host_fdstat.fs_rights_base = fe.rights_base;
|
None => return return_enc_errno(host::__WASI_EBADF),
|
||||||
host_fdstat.fs_rights_inheriting = fe.rights_inheriting;
|
|
||||||
host_fdstat.fs_flags = match hostcalls_impl::fd_fdstat_get(fe) {
|
|
||||||
Ok(flags) => flags,
|
|
||||||
Err(e) => return return_enc_errno(e),
|
|
||||||
};
|
|
||||||
host::__WASI_ESUCCESS
|
|
||||||
} else {
|
|
||||||
host::__WASI_EBADF
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let fs_flags = match hostcalls_impl::fd_fdstat_get(fe) {
|
||||||
|
Ok(flags) => flags,
|
||||||
|
Err(e) => return return_enc_errno(e),
|
||||||
|
};
|
||||||
|
|
||||||
|
host_fdstat.fs_filetype = fe.fd_object.file_type;
|
||||||
|
host_fdstat.fs_rights_base = fe.rights_base;
|
||||||
|
host_fdstat.fs_rights_inheriting = fe.rights_inheriting;
|
||||||
|
host_fdstat.fs_flags = fs_flags;
|
||||||
|
|
||||||
trace!(" | *buf={:?}", host_fdstat);
|
trace!(" | *buf={:?}", host_fdstat);
|
||||||
|
|
||||||
if let Err(e) = enc_fdstat_byref(memory, fdstat_ptr, host_fdstat) {
|
let ret = enc_fdstat_byref(memory, fdstat_ptr, host_fdstat)
|
||||||
return return_enc_errno(e);
|
.map(|_| host::__WASI_ESUCCESS)
|
||||||
}
|
.unwrap_or_else(identity);
|
||||||
|
|
||||||
return_enc_errno(ret)
|
return_enc_errno(ret)
|
||||||
}
|
}
|
||||||
@@ -534,16 +546,21 @@ pub fn fd_advise(
|
|||||||
);
|
);
|
||||||
|
|
||||||
let host_fd = dec_fd(fd);
|
let host_fd = dec_fd(fd);
|
||||||
|
let advice = dec_advice(advice);
|
||||||
|
let offset = dec_filesize(offset);
|
||||||
|
let len = dec_filesize(len);
|
||||||
let rights = host::__WASI_RIGHT_FD_ADVISE;
|
let rights = host::__WASI_RIGHT_FD_ADVISE;
|
||||||
|
|
||||||
let fe = match wasi_ctx.get_fd_entry(host_fd, rights, 0) {
|
let fe = match wasi_ctx.get_fd_entry(host_fd, rights, 0) {
|
||||||
Ok(fe) => fe,
|
Ok(fe) => fe,
|
||||||
Err(e) => return return_enc_errno(e),
|
Err(e) => return return_enc_errno(e),
|
||||||
};
|
};
|
||||||
let advice = dec_advice(advice);
|
let file = match &*fe.fd_object.descriptor {
|
||||||
let offset = dec_filesize(offset);
|
Descriptor::File(f) => f,
|
||||||
let len = dec_filesize(len);
|
_ => return return_enc_errno(host::__WASI_EBADF),
|
||||||
|
};
|
||||||
|
|
||||||
let ret = match hostcalls_impl::fd_advise(fe, advice, offset, len) {
|
let ret = match hostcalls_impl::fd_advise(file, advice, offset, len) {
|
||||||
Ok(()) => host::__WASI_ESUCCESS,
|
Ok(()) => host::__WASI_ESUCCESS,
|
||||||
Err(e) => e,
|
Err(e) => e,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ pub(crate) fn fd_pwrite(file: &File, buf: &[u8], offset: host::__wasi_filesize_t
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn fd_seek(
|
pub(crate) fn fd_seek(
|
||||||
fd_entry: &FdEntry,
|
file: &File,
|
||||||
offset: host::__wasi_filedelta_t,
|
offset: host::__wasi_filedelta_t,
|
||||||
whence: host::__wasi_whence_t,
|
whence: host::__wasi_whence_t,
|
||||||
) -> Result<u64> {
|
) -> Result<u64> {
|
||||||
@@ -40,19 +40,15 @@ pub(crate) fn fd_seek(
|
|||||||
_ => return Err(host::__WASI_EINVAL),
|
_ => return Err(host::__WASI_EINVAL),
|
||||||
};
|
};
|
||||||
|
|
||||||
let rawfd = fd_entry.fd_object.descriptor.as_raw_fd();
|
match lseek(file.as_raw_fd(), offset, nwhence) {
|
||||||
|
|
||||||
match lseek(rawfd, offset, nwhence) {
|
|
||||||
Ok(offset) => Ok(offset as u64),
|
Ok(offset) => Ok(offset as u64),
|
||||||
Err(e) => Err(host_impl::errno_from_nix(e.as_errno().unwrap())),
|
Err(e) => Err(host_impl::errno_from_nix(e.as_errno().unwrap())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn fd_tell(fd_entry: &FdEntry) -> Result<u64> {
|
pub(crate) fn fd_tell(file: &File) -> Result<u64> {
|
||||||
use nix::unistd::{lseek, Whence};
|
use nix::unistd::{lseek, Whence};
|
||||||
|
match lseek(file.as_raw_fd(), 0, Whence::SeekCur) {
|
||||||
let rawfd = fd_entry.fd_object.descriptor.as_raw_fd();
|
|
||||||
match lseek(rawfd, 0, Whence::SeekCur) {
|
|
||||||
Ok(newoffset) => Ok(newoffset as u64),
|
Ok(newoffset) => Ok(newoffset as u64),
|
||||||
Err(e) => Err(host_impl::errno_from_nix(e.as_errno().unwrap())),
|
Err(e) => Err(host_impl::errno_from_nix(e.as_errno().unwrap())),
|
||||||
}
|
}
|
||||||
@@ -60,7 +56,6 @@ pub(crate) fn fd_tell(fd_entry: &FdEntry) -> Result<u64> {
|
|||||||
|
|
||||||
pub(crate) fn fd_fdstat_get(fd_entry: &FdEntry) -> Result<host::__wasi_fdflags_t> {
|
pub(crate) fn fd_fdstat_get(fd_entry: &FdEntry) -> Result<host::__wasi_fdflags_t> {
|
||||||
use nix::fcntl::{fcntl, OFlag, F_GETFL};
|
use nix::fcntl::{fcntl, OFlag, F_GETFL};
|
||||||
|
|
||||||
let rawfd = fd_entry.fd_object.descriptor.as_raw_fd();
|
let rawfd = fd_entry.fd_object.descriptor.as_raw_fd();
|
||||||
match fcntl(rawfd, F_GETFL).map(OFlag::from_bits_truncate) {
|
match fcntl(rawfd, F_GETFL).map(OFlag::from_bits_truncate) {
|
||||||
Ok(flags) => Ok(host_impl::fdflags_from_nix(flags)),
|
Ok(flags) => Ok(host_impl::fdflags_from_nix(flags)),
|
||||||
@@ -81,7 +76,7 @@ pub(crate) fn fd_fdstat_set_flags(
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn fd_advise(
|
pub(crate) fn fd_advise(
|
||||||
fd_entry: &FdEntry,
|
file: &File,
|
||||||
advice: host::__wasi_advice_t,
|
advice: host::__wasi_advice_t,
|
||||||
offset: host::__wasi_filesize_t,
|
offset: host::__wasi_filesize_t,
|
||||||
len: host::__wasi_filesize_t,
|
len: host::__wasi_filesize_t,
|
||||||
@@ -97,8 +92,9 @@ pub(crate) fn fd_advise(
|
|||||||
host::__WASI_ADVICE_NORMAL => libc::POSIX_FADV_NORMAL,
|
host::__WASI_ADVICE_NORMAL => libc::POSIX_FADV_NORMAL,
|
||||||
_ => return Err(host::__WASI_EINVAL),
|
_ => return Err(host::__WASI_EINVAL),
|
||||||
};
|
};
|
||||||
let rawfd = fd_entry.fd_object.descriptor.as_raw_fd();
|
let res = unsafe {
|
||||||
let res = unsafe { libc::posix_fadvise(rawfd, offset as off_t, len as off_t, host_advice) };
|
libc::posix_fadvise(file.as_raw_fd(), offset as off_t, len as off_t, host_advice)
|
||||||
|
};
|
||||||
if res != 0 {
|
if res != 0 {
|
||||||
return Err(host_impl::errno_from_nix(nix::errno::Errno::last()));
|
return Err(host_impl::errno_from_nix(nix::errno::Errno::last()));
|
||||||
}
|
}
|
||||||
@@ -106,7 +102,7 @@ pub(crate) fn fd_advise(
|
|||||||
|
|
||||||
#[cfg(not(target_os = "linux"))]
|
#[cfg(not(target_os = "linux"))]
|
||||||
{
|
{
|
||||||
let _ = (fd_entry, offset, len);
|
let _ = (file, offset, len);
|
||||||
match advice {
|
match advice {
|
||||||
host::__WASI_ADVICE_DONTNEED
|
host::__WASI_ADVICE_DONTNEED
|
||||||
| host::__WASI_ADVICE_SEQUENTIAL
|
| host::__WASI_ADVICE_SEQUENTIAL
|
||||||
|
|||||||
@@ -47,20 +47,19 @@ pub(crate) fn fd_pwrite(file: &File, buf: &[u8], offset: host::__wasi_filesize_t
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn fd_seek(
|
pub(crate) fn fd_seek(
|
||||||
fd_entry: &FdEntry,
|
file: &File,
|
||||||
offset: host::__wasi_filedelta_t,
|
offset: host::__wasi_filedelta_t,
|
||||||
whence: host::__wasi_whence_t,
|
whence: host::__wasi_whence_t,
|
||||||
) -> Result<u64> {
|
) -> Result<u64> {
|
||||||
unimplemented!("fd_seek")
|
unimplemented!("fd_seek")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn fd_tell(fd_entry: &FdEntry) -> Result<u64> {
|
pub(crate) fn fd_tell(file: &File) -> Result<u64> {
|
||||||
unimplemented!("fd_tell")
|
unimplemented!("fd_tell")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn fd_fdstat_get(fd_entry: &FdEntry) -> Result<host::__wasi_fdflags_t> {
|
pub(crate) fn fd_fdstat_get(fd_entry: &FdEntry) -> Result<host::__wasi_fdflags_t> {
|
||||||
use winx::file::AccessRight;
|
use winx::file::AccessRight;
|
||||||
|
|
||||||
let raw_handle = fd_entry.fd_object.descriptor.as_raw_handle();
|
let raw_handle = fd_entry.fd_object.descriptor.as_raw_handle();
|
||||||
match winx::file::get_file_access_rights(raw_handle).map(AccessRight::from_bits_truncate) {
|
match winx::file::get_file_access_rights(raw_handle).map(AccessRight::from_bits_truncate) {
|
||||||
Ok(rights) => Ok(host_impl::fdflags_from_win(rights)),
|
Ok(rights) => Ok(host_impl::fdflags_from_win(rights)),
|
||||||
@@ -76,7 +75,7 @@ pub(crate) fn fd_fdstat_set_flags(
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn fd_advise(
|
pub(crate) fn fd_advise(
|
||||||
fd_entry: &FdEntry,
|
file: &File,
|
||||||
advice: host::__wasi_advice_t,
|
advice: host::__wasi_advice_t,
|
||||||
offset: host::__wasi_filesize_t,
|
offset: host::__wasi_filesize_t,
|
||||||
len: host::__wasi_filesize_t,
|
len: host::__wasi_filesize_t,
|
||||||
|
|||||||
Reference in New Issue
Block a user