// Open files (std/file/stream, ADR 0087): chunked reads and writes, the // write modes, ranges, seeking, sizes, the end of the file, use after // close, syncing, and a file through `io.Stream`. Each run works in a // unique temporary directory (both backends may run it at the same time) // and removes it. import std/bytes import std/ffi.{type Resource} import std/file import std/file/stream.{type File, Append, CreateNew, Truncate} import std/io.{type Stream} import std/list import std/option import std/os import std/random.{type random} import std/result pub fn main :: -> () / { let tmp = option.unwrap_or (os.env "TMPDIR") "/tmp" let root = tmp <> "/louise_run_file_handles_" <> random.token 16 let _ = file.make_dirs root let path = root <> "/data.txt" // Written in two chunks, read back in three, then the end. let w = result.expect (stream.open_write path Truncate) "open" io.println (show (write_text w "hello ")) io.println (show (write_text w "world")) io.println (show (stream.size w)) io.println (show (stream.sync w)) stream.close w io.println (show (file.read path)) let r = result.expect (stream.open_read path) "open" io.println (text (stream.read r 4)) io.println (text (stream.read r 4)) io.println (text (stream.read r 100)) io.println (text (stream.read r 4)) io.println (text (stream.read r 4)) // Ranges leave the position; seek moves it. io.println (text (stream.read_at r 6 5)) io.println (text (stream.read_at r 9 100)) io.println (text (stream.read_at r 100 5)) io.println (show (stream.seek r 0)) io.println (text (stream.read r 5)) io.println (show (stream.seek r 6)) io.println (text (stream.read r 100)) io.println (show (stream.size r)) // Arguments the OS never sees. io.println (text (stream.read r 0)) io.println (text (stream.read_at r (0 - 1) 4)) io.println (show (stream.seek r (0 - 1))) // Writing to a file open for reading is the OS's error. io.println (show (stream.write r (bytes.from_string "no"))) stream.close r // Use after close is an error, not a crash; closing again is nothing. io.println (text (stream.read r 4)) io.println (text (stream.read_at r 0 4)) io.println (show (stream.write r (bytes.from_string "late"))) io.println (show (stream.seek r 0)) io.println (show (stream.size r)) io.println (show (stream.sync r)) stream.close r // Append adds at the end, wherever the position is. let a = result.expect (stream.open_write path Append) "open" io.println (show (stream.seek a 0)) io.println (show (stream.write a (bytes.from_string "!"))) stream.close a io.println (show (file.read path)) // CreateNew refuses an existing file and leaves it untouched. io.println (show (result.is_error (stream.open_write path CreateNew))) io.println (show (file.read path)) let fresh = root <> "/fresh.txt" let n = result.expect (stream.open_write fresh CreateNew) "open" io.println (show (stream.write n (bytes.from_string "new"))) stream.close n io.println (show (file.read fresh)) // Truncate empties an existing file. let t = result.expect (stream.open_write fresh Truncate) "open" io.println (show (stream.size t)) stream.close t // A file through `io.Stream`, as a socket would be. let s = result.expect (stream.open_read path) "open" io.println (show (drain s 5 [])) io.close s io.println (show (result.is_ok (stream.open_read (root <> "/missing")))) io.println (why (stream.open_read (root <> "/missing"))) io.println (why (stream.open_read root)) io.println (show (file.sync_dir root)) io.println (show (file.remove_all root)) } fn write_text :: f: File s: String -> Result<(), String> / { stream.write f (bytes.from_string s) } fn why :: opened: Result -> String / { match opened { Ok(f) -> { stream.close f "opened" } , Error(e) -> e } } fn text :: read: Result -> String { match read { Ok(b) -> "Ok(" <> bytes.to_string_lossy b <> ")" , Error(e) -> "Error(" <> e <> ")" } } fn drain :: conn: Resource chunk: Int acc: List -> Result, String> / where Stream { match io.read conn chunk { Ok(b) if bytes.length b == 0 -> Ok(list.reverse acc) , Ok(b) -> drain conn chunk [bytes.to_string_lossy b, ..acc] , Error(e) -> Error(e) } } //> Ok(()) //> Ok(()) //> Ok(11) //> Ok(()) //> Ok(hello world) //> Ok(hell) //> Ok(o wo) //> Ok(rld) //> Ok() //> Ok() //> Ok(world) //> Ok(ld) //> Ok() //> Ok(()) //> Ok(hello) //> Ok(()) //> Ok(world) //> Ok(11) //> Error(max must be positive, got 0) //> Error(offset must not be negative, got -1) //> Error(offset must not be negative, got -1) //> Error(Bad file descriptor (os error 9)) //> Error(the file is closed) //> Error(the file is closed) //> Error(the file is closed) //> Error(the file is closed) //> Error(the file is closed) //> Error(the file is closed) //> Ok(()) //> Ok(()) //> Ok(hello world!) //> True //> Ok(hello world!) //> Ok(()) //> Ok(new) //> Ok(0) //> Ok([hello, worl, d!]) //> False //> No such file or directory (os error 2) //> Is a directory (os error 21) //> Ok(()) //> Ok(())