Capy-Bearer Documentation

file_open

host call

Capy

function file_open(path : string, mode : string) u64

Description

file_open opens a file for incremental access. Use the handle with file_read, file_write, file_seek, and file_close.

The handle holds a lock on the file. Mode "r" takes a shared lock, and "w", "a", and "r+" take an exclusive lock. "w" empties the file after it gets the lock. When the lock is not free within the lock timeout (BEARER_FILE_LOCK_TIMEOUT_MS, 2000 milliseconds by default), file_open returns u64(0).

The lock lasts until file_close, or until the request ends, also after a trap. Close a write handle before you call file_get_contents on the same file in the same request, or that call waits for the lock and fails.

Parameters

path : path of the file to open

mode : access mode, such as "r", "w", "a", or "r+"

Return Values

A file handle, or u64(0) when Capy cannot open the file.

Example

Capy

var handle := file_open("/tmp/doc-open.txt", "w")
file_write(handle, "streamed")
file_close(handle)
print(file_get_contents("/tmp/doc-open.txt").value ?? "", "\n")

Output

streamed