Documentation
WASI
fd_filestat_set_size

fd_filestat_set_size()

Change the size of an open file, zeroing out any new bytes.

Description

The fd_filestat_set_size() function is used to modify the size of an open file identified by a file descriptor. It allows adjusting the size of the file and zeroing out any newly allocated bytes.

In POSIX systems, files have a size attribute that represents the amount of data stored in the file. The fd_filestat_set_size() function enables applications to change the size of an open file. When increasing the file size, any newly allocated bytes are automatically filled with zeros.

Syntax

  ;;; Note: This is similar to `ftruncate` in POSIX.
  ;;; Adjust the size of an open file. If this increases the file's size, the extra bytes are filled with zeros.
  (@interface func (export "fd_filestat_set_size")
    (param $fd $fd)
    ;;; The desired file size.
    (param $size $filesize)
    (result $error (expected (error $errno)))
  )

Parameters

  • ctx: A mutable reference to the function environment.
  • fd: The file descriptor of the open file to adjust.
  • st_size: The new size to set for the file.

Return Value

The function returns an Errno value indicating the success or failure of the operation.

Logging

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

  • %fd: The file descriptor being modified.
  • %st_size: The new size that the file is set to.

Note

The fd_filestat_set_size() function allows changing the size of an open file identified by the provided file descriptor. If the file size is increased, any newly allocated bytes are zeroed out. This function requires appropriate access rights on the file descriptor, and failure to meet the access requirements will result in an Errno::Access error.