diff --git a/src/hostcalls/fs.rs b/src/hostcalls/fs.rs index 1ad510b3ff..b75617b183 100644 --- a/src/hostcalls/fs.rs +++ b/src/hostcalls/fs.rs @@ -7,7 +7,7 @@ use crate::sys::{errno_from_host, host_impl, hostcalls_impl}; use crate::{host, wasm32}; use log::trace; use std::convert::identity; -use std::io::{self, Read, Write}; +use std::io::{self, Read, Seek, SeekFrom, Write}; use wasi_common_cbindgen::wasi_common_cbindgen; @@ -299,7 +299,7 @@ pub fn fd_seek( } else { host::__WASI_RIGHT_FD_SEEK | host::__WASI_RIGHT_FD_TELL }; - let fd = match wasi_ctx + let mut fd = match wasi_ctx .get_fd_entry(fd, rights, 0) .and_then(|fe| fe.fd_object.descriptor.as_file()) { @@ -307,8 +307,17 @@ pub fn fd_seek( Err(e) => return return_enc_errno(e), }; - let host_newoffset = match hostcalls_impl::fd_seek(fd, offset, whence) { - Ok(host_newoffset) => host_newoffset, + let pos = match whence { + host::__WASI_WHENCE_CUR => SeekFrom::Current(offset), + host::__WASI_WHENCE_END => SeekFrom::End(offset), + host::__WASI_WHENCE_SET => SeekFrom::Start(offset as u64), + _ => return return_enc_errno(host::__WASI_EINVAL), + }; + let host_newoffset = match fd + .seek(pos) + .map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host)) + { + Ok(offset) => offset, Err(e) => return return_enc_errno(e), }; diff --git a/src/sys/unix/hostcalls_impl/fs.rs b/src/sys/unix/hostcalls_impl/fs.rs index 4265173373..9d3d9bc253 100644 --- a/src/sys/unix/hostcalls_impl/fs.rs +++ b/src/sys/unix/hostcalls_impl/fs.rs @@ -27,25 +27,6 @@ pub(crate) fn fd_pwrite(file: &File, buf: &[u8], offset: host::__wasi_filesize_t .map_err(|e| e.raw_os_error().map_or(host::__WASI_EIO, errno_from_host)) } -pub(crate) fn fd_seek( - file: &File, - offset: host::__wasi_filedelta_t, - whence: host::__wasi_whence_t, -) -> Result { - use nix::unistd::{lseek, Whence}; - let nwhence = match whence { - host::__WASI_WHENCE_CUR => Whence::SeekCur, - host::__WASI_WHENCE_END => Whence::SeekEnd, - host::__WASI_WHENCE_SET => Whence::SeekSet, - _ => return Err(host::__WASI_EINVAL), - }; - - 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(file: &File) -> Result { use nix::unistd::{lseek, Whence}; match lseek(file.as_raw_fd(), 0, Whence::SeekCur) { diff --git a/src/sys/windows/hostcalls_impl/fs.rs b/src/sys/windows/hostcalls_impl/fs.rs index 7a7d882f88..b696694ce0 100644 --- a/src/sys/windows/hostcalls_impl/fs.rs +++ b/src/sys/windows/hostcalls_impl/fs.rs @@ -46,14 +46,6 @@ pub(crate) fn fd_pwrite(file: &File, buf: &[u8], offset: host::__wasi_filesize_t .map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host)) } -pub(crate) fn fd_seek( - file: &File, - offset: host::__wasi_filedelta_t, - whence: host::__wasi_whence_t, -) -> Result { - unimplemented!("fd_seek") -} - pub(crate) fn fd_tell(file: &File) -> Result { unimplemented!("fd_tell") }