tobogganSe connecter

louise

The Louise language: compiler, runtime, std, tooling

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

179 lignes · 5192 octetsBrut · Historique
1// Open files (std/file/stream, ADR 0087): chunked reads and writes, the
2// write modes, ranges, seeking, sizes, the end of the file, use after
3// close, syncing, and a file through `io.Stream`. Each run works in a
4// unique temporary directory (both backends may run it at the same time)
5// and removes it.
6
7import std/bytes
8import std/ffi.{type Resource}
9import std/file
10import std/file/stream.{type File, Append, CreateNew, Truncate}
11import std/io.{type Stream}
12import std/list
13import std/option
14import std/os
15import std/random.{type random}
16import std/result
17
18pub fn main :: -> () / <io, random>
19{
20 let tmp = option.unwrap_or (os.env "TMPDIR") "/tmp"
21 let root = tmp <> "/louise_run_file_handles_" <> random.token 16
22 let _ = file.make_dirs root
23 let path = root <> "/data.txt"
24
25 // Written in two chunks, read back in three, then the end.
26 let w = result.expect (stream.open_write path Truncate) "open"
27 io.println (show (write_text w "hello "))
28 io.println (show (write_text w "world"))
29 io.println (show (stream.size w))
30 io.println (show (stream.sync w))
31 stream.close w
32 io.println (show (file.read path))
33 let r = result.expect (stream.open_read path) "open"
34 io.println (text (stream.read r 4))
35 io.println (text (stream.read r 4))
36 io.println (text (stream.read r 100))
37 io.println (text (stream.read r 4))
38 io.println (text (stream.read r 4))
39
40 // Ranges leave the position; seek moves it.
41 io.println (text (stream.read_at r 6 5))
42 io.println (text (stream.read_at r 9 100))
43 io.println (text (stream.read_at r 100 5))
44 io.println (show (stream.seek r 0))
45 io.println (text (stream.read r 5))
46 io.println (show (stream.seek r 6))
47 io.println (text (stream.read r 100))
48 io.println (show (stream.size r))
49
50 // Arguments the OS never sees.
51 io.println (text (stream.read r 0))
52 io.println (text (stream.read_at r (0 - 1) 4))
53 io.println (show (stream.seek r (0 - 1)))
54 // Writing to a file open for reading is the OS's error.
55 io.println (show (stream.write r (bytes.from_string "no")))
56 stream.close r
57
58 // Use after close is an error, not a crash; closing again is nothing.
59 io.println (text (stream.read r 4))
60 io.println (text (stream.read_at r 0 4))
61 io.println (show (stream.write r (bytes.from_string "late")))
62 io.println (show (stream.seek r 0))
63 io.println (show (stream.size r))
64 io.println (show (stream.sync r))
65 stream.close r
66
67 // Append adds at the end, wherever the position is.
68 let a = result.expect (stream.open_write path Append) "open"
69 io.println (show (stream.seek a 0))
70 io.println (show (stream.write a (bytes.from_string "!")))
71 stream.close a
72 io.println (show (file.read path))
73
74 // CreateNew refuses an existing file and leaves it untouched.
75 io.println (show (result.is_error (stream.open_write path CreateNew)))
76 io.println (show (file.read path))
77 let fresh = root <> "/fresh.txt"
78 let n = result.expect (stream.open_write fresh CreateNew) "open"
79 io.println (show (stream.write n (bytes.from_string "new")))
80 stream.close n
81 io.println (show (file.read fresh))
82
83 // Truncate empties an existing file.
84 let t = result.expect (stream.open_write fresh Truncate) "open"
85 io.println (show (stream.size t))
86 stream.close t
87
88 // A file through `io.Stream`, as a socket would be.
89 let s = result.expect (stream.open_read path) "open"
90 io.println (show (drain s 5 []))
91 io.close s
92
93 io.println (show (result.is_ok (stream.open_read (root <> "/missing"))))
94 io.println (why (stream.open_read (root <> "/missing")))
95 io.println (why (stream.open_read root))
96 io.println (show (file.sync_dir root))
97 io.println (show (file.remove_all root))
98}
99
100fn write_text :: f: File s: String -> Result<(), String> / <io>
101{
102 stream.write f (bytes.from_string s)
103}
104
105fn why :: opened: Result<File, String> -> String / <io>
106{
107 match opened
108 { Ok(f) ->
109 {
110 stream.close f
111 "opened"
112 }
113 , Error(e) -> e
114 }
115}
116
117fn text :: read: Result<Bytes, String> -> String
118{
119 match read
120 { Ok(b) -> "Ok(" <> bytes.to_string_lossy b <> ")"
121 , Error(e) -> "Error(" <> e <> ")"
122 }
123}
124
125fn drain ::
126 conn: Resource<s>
127 chunk: Int
128 acc: List<String>
129 -> Result<List<String>, String> / <io>
130 where Stream<s>
131{
132 match io.read conn chunk
133 { Ok(b) if bytes.length b == 0 -> Ok(list.reverse acc)
134 , Ok(b) -> drain conn chunk [bytes.to_string_lossy b, ..acc]
135 , Error(e) -> Error(e)
136 }
137}
138//> Ok(())
139//> Ok(())
140//> Ok(11)
141//> Ok(())
142//> Ok(hello world)
143//> Ok(hell)
144//> Ok(o wo)
145//> Ok(rld)
146//> Ok()
147//> Ok()
148//> Ok(world)
149//> Ok(ld)
150//> Ok()
151//> Ok(())
152//> Ok(hello)
153//> Ok(())
154//> Ok(world)
155//> Ok(11)
156//> Error(max must be positive, got 0)
157//> Error(offset must not be negative, got -1)
158//> Error(offset must not be negative, got -1)
159//> Error(Bad file descriptor (os error 9))
160//> Error(the file is closed)
161//> Error(the file is closed)
162//> Error(the file is closed)
163//> Error(the file is closed)
164//> Error(the file is closed)
165//> Error(the file is closed)
166//> Ok(())
167//> Ok(())
168//> Ok(hello world!)
169//> True
170//> Ok(hello world!)
171//> Ok(())
172//> Ok(new)
173//> Ok(0)
174//> Ok([hello, worl, d!])
175//> False
176//> No such file or directory (os error 2)
177//> Is a directory (os error 21)
178//> Ok(())
179//> Ok(())