tobogganSe connecter

louise

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

116 lignes · 4120 octetsBrut · Historique
1/// Open files, read and written in chunks (ADR 0087): a large file
2/// streams through a fixed amount of memory, and `read_at` reads a range
3/// without reading what comes before it.
4///
5/// match stream.open_read "data.bin"
6/// { Ok(src) -> stream.read src 65536 // Ok(up to 64 KiB); empty at the end
7/// , Error(e) -> Error(e)
8/// }
9///
10/// - **One owner.** A `File` stays with the actor that opened it: it
11/// cannot be sent (`OpenFile` is not `@thread_safe`). When that actor
12/// ends, killed or not, its files are closed as it unwinds, which may be
13/// just after its monitors get `Down`. A file dropped unclosed is closed
14/// then too.
15/// - **Closed is closed.** After `close`, every call is
16/// `Error("the file is closed")`; closing again does nothing.
17/// - **The `io` pool.** Every call runs on the runtime's `io` blocking pool
18/// while the actor waits, as `std/file`'s do. A kill cannot interrupt a
19/// call in progress: the actor ends when it returns.
20/// - **New files are private:** created with mode `0600` (subject to the
21/// umask); an existing file keeps its mode.
22/// - **Durability** is explicit: `sync` the file, and `file.sync_dir` its
23/// directory after creating or renaming it.
24///
25/// Errors are the OS's message, or one about an argument (a `max` that is
26/// not positive, a negative offset).
27
28import std/ffi.{type Resource}
29import std/io.{type Stream}
30
31/// Marks open files (`Resource<OpenFile>`).
32pub type OpenFile { OpenFile }
33
34/// An open file.
35pub type File = Resource<OpenFile>
36
37/// How `open_write` treats the file at its path.
38pub type Mode
39{ /// Create it, or empty it if it exists.
40 Truncate
41 /// Create it; an existing file is an error, and is left untouched.
42, CreateNew
43 /// Create it if missing; every write goes to its end.
44, Append
45}
46
47/// The file at `path`, open for reading from its start, or why it could
48/// not be opened (a directory cannot).
49@extern c "tl_file_open_read" (blocking io)
50pub fn open_read :: path: String -> Result<File, String> / <io>
51
52/// The file at `path`, open for writing as `mode` says, or why it could
53/// not be opened. No directory is created.
54pub fn open_write :: path: String mode: Mode -> Result<File, String> / <io>
55{
56 open_write_raw
57 path
58 (match mode { Truncate -> 0, CreateNew -> 1, Append -> 2 })
59}
60
61@extern c "tl_file_open_write" (blocking io)
62fn open_write_raw :: path: String mode: Int -> Result<File, String> / <io>
63
64/// Up to `max` bytes from the current position, which moves past them:
65/// fewer only at the end of the file, empty there.
66@extern c "tl_file_handle_read" (blocking io)
67pub fn read :: file: File max: Int -> Result<Bytes, String> / <io>
68
69/// Up to `max` bytes from `offset`, without moving the position: fewer
70/// only at the end of the file, empty past it.
71@extern c "tl_file_handle_read_at" (blocking io)
72pub fn read_at ::
73 file: File
74 offset: Int
75 max: Int
76 -> Result<Bytes, String> / <io>
77
78/// All of `data`, written at the current position (at the end in
79/// `Append` mode).
80@extern c "tl_file_handle_write" (blocking io)
81pub fn write :: file: File data: Bytes -> Result<(), String> / <io>
82
83/// Move the position to `offset` bytes from the start. Past the end is
84/// allowed: a write there leaves zeros in between.
85@extern c "tl_file_handle_seek" (blocking io)
86pub fn seek :: file: File offset: Int -> Result<(), String> / <io>
87
88/// The file's size in bytes, now.
89@extern c "tl_file_handle_size" (blocking io)
90pub fn size :: file: File -> Result<Int, String> / <io>
91
92/// Flush the file's data and metadata to the device (`fsync`): what was
93/// written survives a crash once this returns `Ok`.
94@extern c "tl_file_handle_sync" (blocking io)
95pub fn sync :: file: File -> Result<(), String> / <io>
96
97/// Close the file. An error closing it is not reported: `sync` first to
98/// know the data reached the device.
99@extern c "tl_file_handle_close" (blocking io)
100pub fn close :: file: File -> () / <io>
101
102impl Stream<OpenFile>
103{
104 fn read :: file max
105 {
106 read file max
107 }
108 fn write :: file data
109 {
110 write file data
111 }
112 fn close :: file
113 {
114 close file
115 }
116}