wasmer_wasix/syscalls/wasi/
fd_tell.rs

1use super::*;
2use crate::syscalls::*;
3
4/// ### `fd_tell()`
5/// Get the offset of the file descriptor
6/// Inputs:
7/// - `Fd fd`
8///     The file descriptor to access
9/// Output:
10/// - `Filesize *offset`
11///     The offset of `fd` relative to the start of the file
12#[instrument(level = "trace", skip_all, fields(%fd, offset = field::Empty), ret)]
13pub fn fd_tell<M: MemorySize>(
14    mut ctx: FunctionEnvMut<'_, WasiEnv>,
15    fd: WasiFd,
16    offset: WasmPtr<Filesize, M>,
17) -> Result<Errno, WasiError> {
18    WasiEnv::do_pending_operations(&mut ctx)?;
19
20    let env = ctx.data();
21    let (memory, mut state) = unsafe { env.get_memory_and_wasi_state(&ctx, 0) };
22    let offset_ref = offset.deref(&memory);
23
24    let fd_entry = wasi_try_ok!(state.fs.get_fd(fd));
25
26    if !fd_entry.inner.rights.contains(Rights::FD_TELL) {
27        return Ok(Errno::Access);
28    }
29
30    let offset = fd_entry.inner.offset.load(Ordering::Acquire);
31    Span::current().record("offset", offset);
32    wasi_try_mem_ok!(offset_ref.write(offset));
33
34    Ok(Errno::Success)
35}