File API Reference

File I/O with memory-mapped files and advisory locks

Import

U std/file

Constants

Seek Origin

ConstantValueDescription
SEEK_SET0Beginning of file
SEEK_CUR1Current position
SEEK_END2End of file

Structs

File

S File { handle: i64, mode: i64 }

MappedFile

S MappedFile { addr: i64, len: i64 }

FileLock

S FileLock { fd: i64, locked: i64 }

File Methods

MethodSignatureDescription
open_readF open_read(path: i64) -> FileOpen for reading
open_writeF open_write(path: i64) -> FileOpen for writing (creates/truncates)
open_appendF open_append(path: i64) -> FileOpen for appending
is_openF is_open(&self) -> i64Check if file is open
get_modeF get_mode(&self) -> i64Get file mode (0=closed, 1=read, 2=write, 3=append)
read_byteF read_byte(&self) -> i64Read single byte (-1 on EOF)
read_byte_optF read_byte_opt(&self) -> OptionRead byte as Option
readF read(&self, buffer: i64, count: i64) -> i64Read bytes into buffer
read_lineF read_line(&self, buffer: i64, max_len: i64) -> i64Read a line
read_line_resultF read_line_result(&self, buffer: i64, max_len: i64) -> ResultRead line with Result
write_byteF write_byte(&self, byte: i64) -> i64Write single byte
writeF write(&self, buffer: i64, count: i64) -> i64Write bytes
write_strF write_str(&self, str: i64) -> i64Write null-terminated string
flushF flush(&self) -> i64Flush buffer
syncF sync(&self) -> i64Fsync data + metadata
datasyncF datasync(&self) -> i64Sync data only (no metadata)
seekF seek(&self, offset: i64, origin: i64) -> i64Seek (0=start, 1=current, 2=end)
tellF tell(&self) -> i64Get current position
eofF eof(&self) -> i64Check end-of-file
closeF close(&self) -> i64Close the file
dropF drop(&self) -> i64Alias for close (RAII)

Free Functions

FunctionSignatureDescription
file_read_allF file_read_all(path: i64, size_out: i64) -> i64Read entire file
file_write_allF file_write_all(path: i64, buffer: i64, size: i64) -> i64Write buffer to file
file_appendF file_append(path: i64, buffer: i64, size: i64) -> i64Append to file
file_existsF file_exists(path: i64) -> i64Check if file exists
file_read_all_resultF file_read_all_result(path: i64, size_out: i64) -> ResultRead entire file with Result
file_syncF file_sync(path: i64) -> i64Sync file to disk by path
dir_syncF dir_sync(path: i64) -> i64Sync directory metadata

MappedFile Methods

MethodSignatureDescription
mapF map(fd: i64, len: i64, prot: i64, flags: i64, offset: i64) -> MappedFileMap file descriptor
map_readF map_read(fd: i64, len: i64) -> MappedFileMap for reading
map_readwriteF map_readwrite(fd: i64, len: i64) -> MappedFileMap for read-write
is_validF is_valid(&self) -> i64Check if mapping is valid
unmapF unmap(&self) -> i64Unmap memory region
syncF sync(&self) -> i64Sync to disk
sync_asyncF sync_async(&self) -> i64Async sync to disk
adviseF advise(&self, advice: i64) -> i64Advise kernel on access pattern
read_byteF read_byte(&self, offset: i64) -> i64Read byte at offset
write_byteF write_byte(&self, offset: i64, val: i64) -> i64Write byte at offset

FileLock Methods

MethodSignatureDescription
from_fdF from_fd(fd: i64) -> FileLockCreate from file descriptor
openF open(path: i64) -> FileLockOpen file for locking
is_validF is_valid(&self) -> i64Check if lock handle is valid
lock_sharedF lock_shared(&self) -> i64Acquire shared (read) lock
lock_exclusiveF lock_exclusive(&self) -> i64Acquire exclusive (write) lock
try_lock_sharedF try_lock_shared(&self) -> i64Try shared lock (non-blocking)
try_lock_exclusiveF try_lock_exclusive(&self) -> i64Try exclusive lock (non-blocking)
unlockF unlock(&self) -> i64Release lock
is_lockedF is_locked(&self) -> i64Check if currently locked
closeF close(&self) -> i64Close file and release lock
dropF drop(&self) -> i64Alias for close (RAII)

Usage

U std/file

F main() -> i64 {
    f := File.open_write("output.txt")
    f.write_str("Hello, file!")
    f.close()

    exists := file_exists("output.txt")  # 1
    0
}