/// Open files, read and written in chunks (ADR 0087): a large file /// streams through a fixed amount of memory, and `read_at` reads a range /// without reading what comes before it. /// /// match stream.open_read "data.bin" /// { Ok(src) -> stream.read src 65536 // Ok(up to 64 KiB); empty at the end /// , Error(e) -> Error(e) /// } /// /// - **One owner.** A `File` stays with the actor that opened it: it /// cannot be sent (`OpenFile` is not `@thread_safe`). When that actor /// ends, killed or not, its files are closed as it unwinds, which may be /// just after its monitors get `Down`. A file dropped unclosed is closed /// then too. /// - **Closed is closed.** After `close`, every call is /// `Error("the file is closed")`; closing again does nothing. /// - **The `io` pool.** Every call runs on the runtime's `io` blocking pool /// while the actor waits, as `std/file`'s do. A kill cannot interrupt a /// call in progress: the actor ends when it returns. /// - **New files are private:** created with mode `0600` (subject to the /// umask); an existing file keeps its mode. /// - **Durability** is explicit: `sync` the file, and `file.sync_dir` its /// directory after creating or renaming it. /// /// Errors are the OS's message, or one about an argument (a `max` that is /// not positive, a negative offset). import std/ffi.{type Resource} import std/io.{type Stream} /// Marks open files (`Resource`). pub type OpenFile { OpenFile } /// An open file. pub type File = Resource /// How `open_write` treats the file at its path. pub type Mode { /// Create it, or empty it if it exists. Truncate /// Create it; an existing file is an error, and is left untouched. , CreateNew /// Create it if missing; every write goes to its end. , Append } /// The file at `path`, open for reading from its start, or why it could /// not be opened (a directory cannot). @extern c "tl_file_open_read" (blocking io) pub fn open_read :: path: String -> Result / /// The file at `path`, open for writing as `mode` says, or why it could /// not be opened. No directory is created. pub fn open_write :: path: String mode: Mode -> Result / { open_write_raw path (match mode { Truncate -> 0, CreateNew -> 1, Append -> 2 }) } @extern c "tl_file_open_write" (blocking io) fn open_write_raw :: path: String mode: Int -> Result / /// Up to `max` bytes from the current position, which moves past them: /// fewer only at the end of the file, empty there. @extern c "tl_file_handle_read" (blocking io) pub fn read :: file: File max: Int -> Result / /// Up to `max` bytes from `offset`, without moving the position: fewer /// only at the end of the file, empty past it. @extern c "tl_file_handle_read_at" (blocking io) pub fn read_at :: file: File offset: Int max: Int -> Result / /// All of `data`, written at the current position (at the end in /// `Append` mode). @extern c "tl_file_handle_write" (blocking io) pub fn write :: file: File data: Bytes -> Result<(), String> / /// Move the position to `offset` bytes from the start. Past the end is /// allowed: a write there leaves zeros in between. @extern c "tl_file_handle_seek" (blocking io) pub fn seek :: file: File offset: Int -> Result<(), String> / /// The file's size in bytes, now. @extern c "tl_file_handle_size" (blocking io) pub fn size :: file: File -> Result / /// Flush the file's data and metadata to the device (`fsync`): what was /// written survives a crash once this returns `Ok`. @extern c "tl_file_handle_sync" (blocking io) pub fn sync :: file: File -> Result<(), String> / /// Close the file. An error closing it is not reported: `sync` first to /// know the data reached the device. @extern c "tl_file_handle_close" (blocking io) pub fn close :: file: File -> () / impl Stream { fn read :: file max { read file max } fn write :: file data { write file data } fn close :: file { close file } }