diff --git a/src/hostcalls/fs.rs b/src/hostcalls/fs.rs index f113123d21..6a8bc1ca95 100644 --- a/src/hostcalls/fs.rs +++ b/src/hostcalls/fs.rs @@ -308,7 +308,12 @@ pub fn fd_seek( Ok(fe) => fe, 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, Err(e) => return return_enc_errno(e), }; @@ -338,7 +343,12 @@ pub fn fd_tell( Ok(fe) => fe, 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, Err(e) => return return_enc_errno(e), }; @@ -367,24 +377,26 @@ pub fn fd_fdstat_get( Err(e) => return return_enc_errno(e), }; - let ret = if let Some(fe) = wasi_ctx.fds.get(&host_fd) { - 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 = 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 fe = match wasi_ctx.fds.get(&host_fd) { + Some(fe) => fe, + None => return return_enc_errno(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); - if let Err(e) = enc_fdstat_byref(memory, fdstat_ptr, host_fdstat) { - return return_enc_errno(e); - } + let ret = enc_fdstat_byref(memory, fdstat_ptr, host_fdstat) + .map(|_| host::__WASI_ESUCCESS) + .unwrap_or_else(identity); return_enc_errno(ret) } @@ -534,16 +546,21 @@ pub fn fd_advise( ); 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 fe = match wasi_ctx.get_fd_entry(host_fd, rights, 0) { Ok(fe) => fe, Err(e) => return return_enc_errno(e), }; - let advice = dec_advice(advice); - let offset = dec_filesize(offset); - let len = dec_filesize(len); + let file = match &*fe.fd_object.descriptor { + Descriptor::File(f) => f, + _ => 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, Err(e) => e, }; diff --git a/src/sys/unix/hostcalls_impl/fs.rs b/src/sys/unix/hostcalls_impl/fs.rs index 3f8749fa74..ba8768ee04 100644 --- a/src/sys/unix/hostcalls_impl/fs.rs +++ b/src/sys/unix/hostcalls_impl/fs.rs @@ -28,7 +28,7 @@ pub(crate) fn fd_pwrite(file: &File, buf: &[u8], offset: host::__wasi_filesize_t } pub(crate) fn fd_seek( - fd_entry: &FdEntry, + file: &File, offset: host::__wasi_filedelta_t, whence: host::__wasi_whence_t, ) -> Result { @@ -40,19 +40,15 @@ pub(crate) fn fd_seek( _ => return Err(host::__WASI_EINVAL), }; - let rawfd = fd_entry.fd_object.descriptor.as_raw_fd(); - - match lseek(rawfd, offset, nwhence) { + match lseek(file.as_raw_fd(), offset, nwhence) { Ok(offset) => Ok(offset as u64), Err(e) => Err(host_impl::errno_from_nix(e.as_errno().unwrap())), } } -pub(crate) fn fd_tell(fd_entry: &FdEntry) -> Result { +pub(crate) fn fd_tell(file: &File) -> Result { use nix::unistd::{lseek, Whence}; - - let rawfd = fd_entry.fd_object.descriptor.as_raw_fd(); - match lseek(rawfd, 0, Whence::SeekCur) { + match lseek(file.as_raw_fd(), 0, Whence::SeekCur) { Ok(newoffset) => Ok(newoffset as u64), 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 { pub(crate) fn fd_fdstat_get(fd_entry: &FdEntry) -> Result { use nix::fcntl::{fcntl, OFlag, F_GETFL}; - let rawfd = fd_entry.fd_object.descriptor.as_raw_fd(); match fcntl(rawfd, F_GETFL).map(OFlag::from_bits_truncate) { 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( - fd_entry: &FdEntry, + file: &File, advice: host::__wasi_advice_t, offset: 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, _ => return Err(host::__WASI_EINVAL), }; - let rawfd = fd_entry.fd_object.descriptor.as_raw_fd(); - let res = unsafe { libc::posix_fadvise(rawfd, offset as off_t, len as off_t, host_advice) }; + let res = unsafe { + libc::posix_fadvise(file.as_raw_fd(), offset as off_t, len as off_t, host_advice) + }; if res != 0 { 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"))] { - let _ = (fd_entry, offset, len); + let _ = (file, offset, len); match advice { host::__WASI_ADVICE_DONTNEED | host::__WASI_ADVICE_SEQUENTIAL diff --git a/src/sys/windows/hostcalls_impl/fs.rs b/src/sys/windows/hostcalls_impl/fs.rs index 408cddaa3e..5bd41f4080 100644 --- a/src/sys/windows/hostcalls_impl/fs.rs +++ b/src/sys/windows/hostcalls_impl/fs.rs @@ -47,20 +47,19 @@ pub(crate) fn fd_pwrite(file: &File, buf: &[u8], offset: host::__wasi_filesize_t } pub(crate) fn fd_seek( - fd_entry: &FdEntry, + file: &File, offset: host::__wasi_filedelta_t, whence: host::__wasi_whence_t, ) -> Result { unimplemented!("fd_seek") } -pub(crate) fn fd_tell(fd_entry: &FdEntry) -> Result { +pub(crate) fn fd_tell(file: &File) -> Result { unimplemented!("fd_tell") } pub(crate) fn fd_fdstat_get(fd_entry: &FdEntry) -> Result { use winx::file::AccessRight; - 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) { 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( - fd_entry: &FdEntry, + file: &File, advice: host::__wasi_advice_t, offset: host::__wasi_filesize_t, len: host::__wasi_filesize_t,