Module fs/file

fs/file

Async file I/O operations with Exception-based error handling.

Wraps a file descriptor with typed async I/O operations. Uses the Exception effect for error handling.

Example

{ File, read_string, write_file, OpenMode } :: import "std/fs/file";

main :: (fn(io : Io, exn : Exception) -> unit)({
  // Write a file
  io.await(write_file(Path.new(`test.txt`), `hello world`, io), { io, exn });

  // Read it back
  content := io.await(read_string(Path.new(`test.txt`), io), { io, exn });
  println(content);

  // Open with OpenMode
  f := io.await(File.open(Path.new(`test.txt`), .Read, io), { io, exn });
});

Types

File object
File

An open file handle with async read/write operations.

Provides methods for reading, writing, seeking, and querying file metadata. Files are automatically closed when disposed.

Fields

NameTypeDescription
_fdi32
_pathPath
_is_closedbool

Trait Implementations

impl(File, ...)
open_with : (File) fn(path : Path, mode : OpenMode, perm : FilePermission, io : Io) -> Impl(Future(File, IoExn))

Open a file at path with the given mode and custom permissions.

Parameters

NameTypeNotesDescription
pathPath

Get the path this file was opened with.

modeOpenMode
permFilePermission
ioIo

Returns: Impl(Future(File, IoExn))

open_with_str : (File) fn(path : str, mode : OpenMode, perm : FilePermission, io : Io) -> Impl(Future(File, IoExn))

Open a file from a str path with the given mode and custom permissions.

Parameters

NameTypeNotesDescription
pathstr

Get the path this file was opened with.

modeOpenMode
permFilePermission
ioIo

Returns: Impl(Future(File, IoExn))

open_with_cstr : (File) fn(path : *(u8), mode : OpenMode, perm : FilePermission, io : Io) -> Impl(Future(File, IoExn))

Open a file from a C string path with the given mode and custom permissions.

Parameters

NameTypeNotesDescription
path*(u8)

Get the path this file was opened with.

modeOpenMode
permFilePermission
ioIo

Returns: Impl(Future(File, IoExn))

open : (File) fn(path : Path, mode : OpenMode, io : Io) -> Impl(Future(File, IoExn))

Open a file at path with the given mode and default permissions (0o644).

Parameters

NameTypeNotesDescription
pathPath

Get the path this file was opened with.

modeOpenMode
ioIo

Returns: Impl(Future(File, IoExn))

open_str : (File) fn(path : str, mode : OpenMode, io : Io) -> Impl(Future(File, IoExn))

Open a file from a str path with the given mode and default permissions.

Parameters

NameTypeNotesDescription
pathstr

Get the path this file was opened with.

modeOpenMode
ioIo

Returns: Impl(Future(File, IoExn))

open_cstr : (File) fn(path : *(u8), mode : OpenMode, io : Io) -> Impl(Future(File, IoExn))

Open a file from a C string path with the given mode and default permissions.

Parameters

NameTypeNotesDescription
path*(u8)

Get the path this file was opened with.

modeOpenMode
ioIo

Returns: Impl(Future(File, IoExn))

read : (File) fn(self : File, buf : *(u8), size : u32, io : Io) -> Impl(Future(i32, IoExn))

Read up to size bytes into the buffer buf. Returns the number of bytes actually read.

Parameters

NameTypeNotesDescription
selfFile
buf*(u8)
sizeu32

Get the file size in bytes.

ioIo

Returns: Impl(Future(i32, IoExn))

write_string : (File) fn(self : File, data : String, io : Io) -> Impl(Future(i32, IoExn))

Write a String to the file. Returns the number of bytes written.

Parameters

NameTypeNotes
selfFile
dataString
ioIo

Returns: Impl(Future(i32, IoExn))

write_bytes : (File) fn(self : File, data : ArrayList(u8), io : Io) -> Impl(Future(i32, IoExn))

Write bytes to a file, creating or truncating it.

Parameters

NameTypeNotes
selfFile
dataArrayList(u8)
ioIo

Returns: Impl(Future(i32, IoExn))

read_bytes : (File) fn(self : File, io : Io) -> Impl(Future(ArrayList(u8), IoExn))

Read the entire file contents into an ArrayList(u8).

Parameters

NameTypeNotes
selfFile
ioIo

Returns: Impl(Future(ArrayList(u8), IoExn))

read_string : (File) fn(self : File, io : Io) -> Impl(Future(String, IoExn))

Read an entire file into a String.

Parameters

NameTypeNotes
selfFile
ioIo

Returns: Impl(Future(String, IoExn))

flush : (File) fn(self : File, io : Io) -> Impl(Future(unit, IoExn))

Flush (fsync) file data to disk.

Parameters

NameTypeNotes
selfFile
ioIo

Returns: Impl(Future(unit, IoExn))

seek : (File) fn(self : File, offset : i64, from : SeekFrom, exn : Exception) -> i64

Seek to a position relative to from. Returns the new absolute byte offset.

Parameters

NameTypeNotes
selfFile
offseti64
fromSeekFrom
exnException

Returns: i64

position : (File) fn(self : File) -> i64

Get the current file position (byte offset from the beginning).

Parameters

NameTypeNotes
selfFile

Returns: i64

size : (File) fn(self : File) -> i64

Get the file size in bytes.

Parameters

NameTypeNotes
selfFile

Returns: i64

close : (File) fn(self : File, io : Io) -> Impl(Future(unit, IoExn))

Close the file descriptor. Safe to call multiple times.

Parameters

NameTypeNotes
selfFile
ioIo

Returns: Impl(Future(unit, IoExn))

fd : (File) fn(self : File) -> i32

Get the underlying file descriptor.

Parameters

NameTypeNotes
selfFile

Returns: i32

path : (File) fn(self : File) -> Path

Get the path this file was opened with.

Parameters

NameTypeNotes
selfFile

Returns: Path

metadata : (File) fn(self : File, io : Io) -> Impl(Future(Metadata, IoExn))

Get file metadata (size, permissions, timestamps).

Parameters

NameTypeNotes
selfFile
ioIo

Returns: Impl(Future(Metadata, IoExn))

impl(File, Dispose(...))
dispose : (fn(self : Self) -> unit)

Returns: unit

OpenMode enum
OpenMode

Variants

VariantFieldsDescription
Read
Write
Append
ReadWrite
CreateNew
FilePermission newtype
FilePermission

Fields

NameTypeDescription
modeu32
Methods
default : (FilePermission) fn() -> FilePermission

Returns: FilePermission

executable : (FilePermission) fn() -> FilePermission

Returns: FilePermission

readonly : (FilePermission) fn() -> FilePermission

Returns: FilePermission

private : (FilePermission) fn() -> FilePermission

Returns: FilePermission

SeekFrom enum
SeekFrom

Variants

VariantFieldsDescription
Start
Current
End

Functions

read_string function
fn(path : Path, io : Io) -> Impl(Future(String, IoExn))

Read an entire file into a String.

Parameters

NameTypeNotesDescription
pathPath

Get the path this file was opened with.

ioIo

Returns: Impl(Future(String, IoExn))

read_string_str function
fn(path : str, io : Io) -> Impl(Future(String, IoExn))

Read an entire file into a String (str path variant).

Parameters

NameTypeNotesDescription
pathstr

Get the path this file was opened with.

ioIo

Returns: Impl(Future(String, IoExn))

read_string_cstr function
fn(path : *(u8), io : Io) -> Impl(Future(String, IoExn))

Read an entire file into a String (C string path variant).

Parameters

NameTypeNotesDescription
path*(u8)

Get the path this file was opened with.

ioIo

Returns: Impl(Future(String, IoExn))

read_file function
fn(path : Path, io : Io) -> Impl(Future(ArrayList(u8), IoExn))

Read an entire file into a byte list (ArrayList(u8)).

Parameters

NameTypeNotesDescription
pathPath

Get the path this file was opened with.

ioIo

Returns: Impl(Future(ArrayList(u8), IoExn))

read_file_str function
fn(path : str, io : Io) -> Impl(Future(ArrayList(u8), IoExn))

Read an entire file into bytes (str path variant).

Parameters

NameTypeNotesDescription
pathstr

Get the path this file was opened with.

ioIo

Returns: Impl(Future(ArrayList(u8), IoExn))

read_file_cstr function
fn(path : *(u8), io : Io) -> Impl(Future(ArrayList(u8), IoExn))

Read an entire file into bytes (C string path variant).

Parameters

NameTypeNotesDescription
path*(u8)

Get the path this file was opened with.

ioIo

Returns: Impl(Future(ArrayList(u8), IoExn))

write_file function
fn(path : Path, data : String, io : Io) -> Impl(Future(unit, IoExn))

Write a String to a file, creating or truncating it.

Parameters

NameTypeNotesDescription
pathPath

Get the path this file was opened with.

dataString
ioIo

Returns: Impl(Future(unit, IoExn))

write_file_str function
fn(path : str, data : str, io : Io) -> Impl(Future(unit, IoExn))

Write a str to a file, creating or truncating it (str path variant).

Parameters

NameTypeNotesDescription
pathstr

Get the path this file was opened with.

datastr
ioIo

Returns: Impl(Future(unit, IoExn))

write_file_cstr function
fn(path : *(u8), data : str, io : Io) -> Impl(Future(unit, IoExn))

Write a str to a file, creating or truncating it (C string path variant).

Parameters

NameTypeNotesDescription
path*(u8)

Get the path this file was opened with.

datastr
ioIo

Returns: Impl(Future(unit, IoExn))

write_bytes function
fn(path : Path, data : ArrayList(u8), io : Io) -> Impl(Future(unit, IoExn))

Write bytes to a file, creating or truncating it.

Parameters

NameTypeNotesDescription
pathPath

Get the path this file was opened with.

dataArrayList(u8)
ioIo

Returns: Impl(Future(unit, IoExn))

append_file function
fn(path : Path, data : String, io : Io) -> Impl(Future(unit, IoExn))

Append a String to a file, creating it if it does not exist.

Parameters

NameTypeNotesDescription
pathPath

Get the path this file was opened with.

dataString
ioIo

Returns: Impl(Future(unit, IoExn))

append_file_str function
fn(path : str, data : str, io : Io) -> Impl(Future(unit, IoExn))

Append a str to a file (str path variant).

Parameters

NameTypeNotesDescription
pathstr

Get the path this file was opened with.

datastr
ioIo

Returns: Impl(Future(unit, IoExn))

exists function
fn(path : Path, io : Io) -> Impl(Future(bool, Io))

Check if a path exists. Returns false for any error including file not found. Does not use the Exception effect.

Parameters

NameTypeNotesDescription
pathPath

Get the path this file was opened with.

ioIo

Returns: Impl(Future(bool, Io))

exists_str function
fn(path : str, io : Io) -> Impl(Future(bool, Io))

Check if a path exists (str path variant).

Parameters

NameTypeNotesDescription
pathstr

Get the path this file was opened with.

ioIo

Returns: Impl(Future(bool, Io))

exists_cstr function
fn(path : *(u8), io : Io) -> Impl(Future(bool, Io))

Check if a path exists (C string path variant).

Parameters

NameTypeNotesDescription
path*(u8)

Get the path this file was opened with.

ioIo

Returns: Impl(Future(bool, Io))

is_file function
fn(path : Path, io : Io) -> Impl(Future(bool, Io))

Check if a path is a regular file. Returns false for any error.

Parameters

NameTypeNotesDescription
pathPath

Get the path this file was opened with.

ioIo

Returns: Impl(Future(bool, Io))

is_file_str function
fn(path : str, io : Io) -> Impl(Future(bool, Io))

Check if a path is a regular file (str path variant).

Parameters

NameTypeNotesDescription
pathstr

Get the path this file was opened with.

ioIo

Returns: Impl(Future(bool, Io))

is_file_cstr function
fn(path : *(u8), io : Io) -> Impl(Future(bool, Io))

Check if a path is a regular file (C string path variant).

Parameters

NameTypeNotesDescription
path*(u8)

Get the path this file was opened with.

ioIo

Returns: Impl(Future(bool, Io))

is_dir function
fn(path : Path, io : Io) -> Impl(Future(bool, Io))

Check if a path is a directory. Returns false for any error.

Parameters

NameTypeNotesDescription
pathPath

Get the path this file was opened with.

ioIo

Returns: Impl(Future(bool, Io))

is_dir_str function
fn(path : str, io : Io) -> Impl(Future(bool, Io))

Check if a path is a directory (str path variant).

Parameters

NameTypeNotesDescription
pathstr

Get the path this file was opened with.

ioIo

Returns: Impl(Future(bool, Io))

is_dir_cstr function
fn(path : *(u8), io : Io) -> Impl(Future(bool, Io))

Check if a path is a directory (C string path variant).

Parameters

NameTypeNotesDescription
path*(u8)

Get the path this file was opened with.

ioIo

Returns: Impl(Future(bool, Io))

canonical function
fn(path : Path, io : Io) -> Impl(Future(Path, IoExn))

Resolve a path to its canonical absolute form (resolves symlinks, . and ..). Throws if the path does not exist.

Parameters

NameTypeNotesDescription
pathPath

Get the path this file was opened with.

ioIo

Returns: Impl(Future(Path, IoExn))

canonical_str function
fn(path : str, io : Io) -> Impl(Future(Path, IoExn))

Resolve a path to its canonical absolute form (str path variant).

Parameters

NameTypeNotesDescription
pathstr

Get the path this file was opened with.

ioIo

Returns: Impl(Future(Path, IoExn))

canonical_cstr function
fn(path : *(u8), io : Io) -> Impl(Future(Path, IoExn))

Resolve a path to its canonical absolute form (C string path variant).

Parameters

NameTypeNotesDescription
path*(u8)

Get the path this file was opened with.

ioIo

Returns: Impl(Future(Path, IoExn))