Documentation
WASI
fd_tell

fd_tell()

Get the offset of the file descriptor.

Description

The fd_tell() function retrieves the current offset of the specified file descriptor relative to the start of the file.

Syntax

  ;;; Note: This is similar to `lseek(fd, 0, SEEK_CUR)` in POSIX.
  ;;; Return the current offset of a file descriptor.
  (@interface func (export "fd_tell")
    (param $fd $fd)
    ;;; The current offset of the file descriptor, relative to the start of the file.
    (result $error (expected $filesize (error $errno)))
  )

Parameters

  • ctx: A mutable reference to the function environment.
  • fd: The file descriptor to access.
  • offset: A wasm pointer to a Filesize where the offset will be written.

Return Value

The function returns an Errno value indicating the success or failure of the operation. If the operation is successful, it returns Errno::Success. Otherwise, it returns an appropriate Errno value.

Logging

This function has been instrumented with debug-level logging. It will log the following information:

  • %fd: The file descriptor being accessed.
  • offset: The offset of the file descriptor.

Note

The fd_tell() function retrieves the current offset of the specified file descriptor (fd) relative to the start of the file.

The function first retrieves the memory view and file system state from the WASI environment using the function context.

Next, it retrieves the file descriptor entry for the specified file descriptor from the file system state using the get_fd() method.

If the file descriptor does not have the FD_TELL right, the function returns Errno::Access to indicate an access error.

The function then retrieves the current offset of the file descriptor by loading it from the file descriptor entry using the load() method.

It records the offset value in the current span for logging purposes.

Finally, it writes the offset value to the memory location specified by the offset wasm pointer using the write() method.

The function returns Errno::Success to indicate a successful operation.