files
include "files";
Read and write files, and list, create and remove directories.
Types
file
Native object
read
fn read(buffer buffer, int offset, int count) -> int
Reads up to count bytes at file offset into the start of buffer; 0 at end of file.
write
fn write(buffer buffer, int offset, int count) -> int
Writes up to count bytes from the start of buffer at file offset and returns how many were written.
close infallible
infallible fn close()
size
fn size() -> int
directory
Native object
next
fn next(buffer name, DirectoryEntryInfo info) -> int
close infallible
infallible fn close()
DirectoryEntryInfo
Object
What directory.next reports about an entry besides its name.
| Field | Type | Writable | Description |
|---|---|---|---|
isDirectory | bool | yes | |
size | int | yes |
FileEntry
Object
One entry of directory_list. Directories report size zero.
| Field | Type | Writable | Description |
|---|---|---|---|
name | string | yes | |
isDirectory | bool | yes | |
size | int | yes |
Functions
directory_open
fn directory_open(string path) -> directory
Opens a directory to read its entries one at a time.
directory_make
fn directory_make(string path)
Creates one directory level.
directory_temporary_into
fn directory_temporary_into(string path, buffer output) -> int
Creates a new private directory and writes its path into output; returns the byte count.
directory_remove
fn directory_remove(string path)
Removes an empty directory.
directory_current infallible
infallible fn directory_current() -> string
The runtime's file root as an absolute path (the working directory when no root is set). Relative paths resolve against it.
directory_home infallible
infallible fn directory_home() -> string
The user's home directory, falling back to directory_current().
directory_list
fn directory_list(string path) -> array<FileEntry>
A snapshot of a directory's immediate children: directories first, then files, each sorted case-insensitively. The directory is closed on success and failure.
file_open_read
fn file_open_read(string path) -> file
Opens an existing file for reading.
file_open_write
fn file_open_write(string path) -> file
Creates or truncates a file and opens it for writing.
file_remove
fn file_remove(string path)
Deletes a file.
file_read_bytes
fn file_read_bytes(string path) -> buffer
Reads a whole file into a buffer. The file is closed on success and failure.
file_read_text
fn file_read_text(string path) -> string
Reads a whole file as a string.
file_write_text
fn file_write_text(string path, string text)
Writes text as the whole contents of a file. The file is closed on success and failure.
file_write_text_atomic
fn file_write_text_atomic(string path, string text)
Writes path + ".tmp", then atomically replaces path with it, so path is never seen half-written.