1-- Target-neutral I/O capabilities and their adapters. The memory world
2-- that provides them over the caller's storage is memory.ldn's, time is
3-- core/time's, the network core/net's and the fakes of a system's
4-- providers core/io/fake's; the hosted provider is hosted/system, selected
5-- explicitly.
6
7import core/mem
8import core/text
9
10--- The provider refused access to the requested file.
11public no_access: atom
12--- An I/O operation failed. Provider-specific diagnostic detail, if offered,
13--- must be read from that provider.
14public io_failed: atom
15--- The path names a directory where a file was to be opened.
16public is_a_directory: atom
17--- The name a file or directory was to be made at is already taken.
18public already_exists: atom
19
20--- A directory entry that is a regular file.
21public regular: atom
22--- A directory entry that is itself a directory.
23public subdirectory: atom
24--- A directory entry that is a symbolic link, which a listing does not
25--- follow.
26public symbolic_link: atom
27--- A directory entry that is anything else: a device, a pipe or a socket.
28public special: atom
29--- What a directory entry names: `regular`, `subdirectory`,
30--- `symbolic_link` or `special`.
31public entry_kind: type = regular | subdirectory | symbolic_link | special
32
33-- A word wide enough for any provider's identifier: a POSIX descriptor,
34-- a slot index, or a handle a system gives as a pointer.
35file_value: type = distinct usize
36
37--- Provider-defined file identifier. Use a handle only with the provider that
38--- created it and obey that provider's close rules.
39public file: type = file_value
40
41--- Output capability. `write` writes every byte or fails, and a failure may
42--- follow partial output. `write_some` makes one attempt: it reports
43--- positive progress for nonempty input and zero for empty input, and a
44--- failed attempt makes no progress.
45---
46-- Four narrow concepts and their composition: spec.md's D268 and D1952.
47--- The capability is four narrow concepts over one provider type, so a
48--- provider supplies only what it has: a UART is a writer and a read-only
49--- image is a reader, while system and memory providers also supply files and
50--- process entries. A consumer names the narrowest concept it uses;
51--- diagnostics stream through any io.writer. `world` composes all four for
52--- roots that have everything. A provider is a handle: a small copyable
53--- value, passed by value, that refers to its state, so a program and its
54--- logger share one without locking it. An erased `any io.world` is built
55--- from the address of a handle binding and dispatches every entry, and it
56--- converts to an `any` of each concept it is built on: where an
57--- `any io.writer` is wanted, pass the world (D1952).
58public writer: type = concept (provider: type)
59 --- Write the entire byte slice or fail. Failure may follow partial
60 --- output; do not blindly retry the whole slice.
61 write: (self: provider, opened: file, bytes: []u8)
62 -> none ! io_failed
63 --- Attempt one transfer. Nonempty success returns positive progress,
64 --- empty success returns zero, and failure transfers no bytes.
65 write_some: (self: provider, opened: file, bytes: []u8)
66 -> (count: usize) ! io_failed
67end writer
68
69--- Input capability. `read` puts at most `lenof into` bytes in the caller's
70--- slice and returns the count; zero for a nonempty slice means end of
71--- input. The slice's storage stays the caller's.
72public reader: type = concept (provider: type)
73 --- Read at most the destination length and return progress; zero into a
74 --- nonempty destination is end of input, and an empty one always reads
75 --- zero.
76 read: (self: provider, opened: file, into: []mut u8)
77 -> (count: usize) ! io_failed
78end reader
79
80--- File lifecycle and identity capability. A path is an OS string, as
81--- the system gives and takes it (`text.os_string`); a literal one is
82--- `text.os_string_over("name")`. A successful open returns a
83--- provider-specific handle; close consumes the handle even when reporting
84--- failure.
85public files: type = concept (provider: type)
86 --- Open a path for reading; return a handle owned by this provider.
87 --- A path that names a directory is `is_a_directory`: a directory is
88 --- read through `directories`.
89 open_read: (self: provider, path: text.os_string)
90 -> (opened: file)
91 ! mem.not_found | no_access | is_a_directory | io_failed
92 --- Open a path for writing; creation and truncation follow the
93 --- provider contract. A path that names a directory is
94 --- `is_a_directory`.
95 open_write: (self: provider, path: text.os_string)
96 -> (opened: file)
97 ! mem.not_found | no_access | is_a_directory | io_failed
98 --- Report whether two paths name the same file. The left path is an
99 --- existing input: if it names no file the provider can examine, the
100 --- call fails with `io_failed`. The right path may be an output not yet
101 --- created: if it does not exist, the answer is false.
102 same_file: (self: provider, left: text.os_string,
103 right: text.os_string)
104 -> (same: bool) ! io_failed
105 --- Consume a file handle, including when closure reports a failure.
106 close: (self: provider, sink opened: file)
107 -> none ! io_failed
108end files
109
110--- Standard streams and argument access. An argument is an OS string
111--- whose storage belongs to the provider; stream identifiers have
112--- provider-specific lifetime and closing rules.
113public process: type = concept (provider: type)
114 --- Return the standard-input handle, which a `reader` reads to end of
115 --- input.
116 input: (self: provider) -> (stream: file)
117 --- Return the standard-output handle.
118 out: (self: provider) -> (stream: file)
119 --- Return the standard-error handle.
120 err: (self: provider) -> (stream: file)
121 --- Return the count of available arguments.
122 arguments_length: (self: provider) -> (count: usize)
123 --- Lend an argument by index; the provider retains its backing. Report
124 --- mem.not_found when out of range.
125 argument: (self: provider, index: usize)
126 -> (value: text.os_string from self) ! mem.not_found
127end process
128
129--- The process's environment: its variables, each a name and a value that
130--- are OS strings whose storage belongs to the provider, in the order
131--- the provider keeps them. It is read-only: a process does not change
132--- its own environment, which is not safe beside threads, and gives a
133--- child the one it builds instead (`programs`). It is a concept of its
134--- own, outside `world`, so a provider that has no environment, and a
135--- firmware image that never asks for one, carries none.
136public environment: type = concept (provider: type)
137 --- Return the count of the environment's variables.
138 variables_length: (self: provider) -> (count: usize)
139 --- Lend the environment variable at an index; the provider retains its
140 --- backing. Report mem.not_found when out of range.
141 variable: (self: provider, index: usize)
142 -> (item: text.os_variable from self) ! mem.not_found
143 --- Lend the value of the first variable named `name`, compared as the
144 --- system stores names, with no normalization. Report mem.not_found when
145 --- none has that name.
146 variable_named: (self: provider, name: text.os_string)
147 -> (value: text.os_string from self) ! mem.not_found
148end environment
149
150--- Composition of reader, writer, files and process over one provider type.
151--- Consumers should request the narrowest capability they use.
152--- `directories`, `environment`, `programs`, `clock`, `wall_clock`,
153--- `entropy`, `network`, `file_system` and `terminals` are not part of it:
154--- a program that lists a directory, reads its environment, runs another
155--- program, reads the time, wants random bytes, uses the network, renames
156--- a file or asks for a terminal names that concept as well.
157public world: type = concept (provider: type) is reader, writer, files, process
158end world
159
160--- The fewest bytes a directory's buffer may have: room for any entry any
161--- provider here gives, with its record and its terminator. The hosted
162--- provider refuses a smaller buffer when the directory is opened.
163public directory_buffer: usize = 1024
164
165--- An open directory: a value its caller keeps and edits, passing it
166--- `inout` to `read_entry`, which refills `buffer` with entries as the
167--- caller reads them. The buffer is the caller's, so each entry's name is
168--- lent from the directory and is good until the next read; copy a name to
169--- keep it. The other fields belong to the provider that opened it: its
170--- own handle and stream for the directory, and how much of the buffer it
171--- has filled and read. Close it through the same provider. A directory
172--- dropped without closing keeps what its provider holds for it until the
173--- process ends: there are no destructors, and a `defer` closes it on
174--- every exit.
175public directory: type = struct
176 handle: usize
177 stream: usize
178 position: usize
179 buffer: []mut u8
180 filled: usize
181 consumed: usize
182 at_end: bool
183end directory
184
185--- One entry of a directory: its name, an OS string as the system stores
186--- it, which need not be UTF-8, and what it is. `read_entry` returns an
187--- entry whose `at_end` is true at the end of the directory, its name then
188--- empty; every other entry's `at_end` is false.
189public entry: type = struct
190 name: text.os_string
191 kind: entry_kind
192 at_end: bool
193end entry
194
195--- Directory listing and the names in a tree: open a directory and read its
196--- entries, ask an entry's size, make and remove directories, and remove
197--- files. Paths are OS strings, as `files` takes them, and an entry's
198--- name is one too, so a name read is given back unconverted. A listing
199--- leaves out `.` and `..`, which every directory holds and which are no
200--- entries of its own. It gives its entries in the system's order, and no
201--- provider promises an order.
202public directories: type = concept (provider: type)
203 --- Open the directory at `path` and read it into the caller's
204 --- `buffer`. A missing path and a path through or to something that
205 --- is not a directory are `mem.not_found`.
206 open_directory: (self: provider, path: text.os_string,
207 buffer: []mut u8)
208 -> (opened: directory from buffer)
209 ! mem.not_found | no_access | io_failed
210 --- The directory's next entry, lent from it until the next read, or
211 --- an entry whose `at_end` is true at its end.
212 read_entry: (self: provider, inout opened: directory)
213 -> (item: entry from opened) ! io_failed
214 --- The size in bytes of the entry `name` in the open directory, not
215 --- following a symbolic link. A name no entry can have is
216 --- `mem.not_found`: an empty one, one with a `/` or a NUL, or one longer
217 --- than `directory_buffer` allows. So is an entry removed since it was
218 --- read.
219 entry_size: (self: provider, opened: directory,
220 name: text.os_string)
221 -> (size: u64) ! mem.not_found | no_access | io_failed
222 --- Close the directory, which is consumed even when this fails.
223 close_directory: (self: provider, sink opened: directory)
224 -> none ! io_failed
225 --- Make a directory at `path`. A path whose parent is missing is
226 --- `mem.not_found`; a name already taken is `already_exists`.
227 make_directory: (self: provider, path: text.os_string)
228 -> none ! already_exists | mem.not_found | no_access | io_failed
229 --- Remove the file `path` names, not a directory.
230 remove_file: (self: provider, path: text.os_string)
231 -> none ! mem.not_found | no_access | io_failed
232 --- Remove the empty directory `path` names.
233 remove_directory: (self: provider, path: text.os_string)
234 -> none ! mem.not_found | no_access | io_failed
235end directories
236
237--- Open a path for reading through `files` evidence. The caller must close
238--- a successful handle using the same provider. A path that names a
239--- directory fails `is_a_directory`.
240public open_read: (provider: type is files, host: provider,
241 path: text.os_string)
242 -> (opened: file)
243 ! mem.not_found | no_access | is_a_directory | io_failed =
244 opened = try provider.open_read(host, path)
245end open_read
246
247--- Open a path for writing through `files` evidence. Creation and
248--- truncation behavior belongs to the provider; close a successful handle
249--- through that provider. A path that names a directory fails
250--- `is_a_directory`.
251public open_write: (provider: type is files, host: provider,
252 path: text.os_string)
253 -> (opened: file)
254 ! mem.not_found | no_access | is_a_directory | io_failed =
255 opened = try provider.open_write(host, path)
256end open_write
257
258--- Ask whether two paths name the same file. The provider applies its own
259--- identity rules; a failure is `io_failed`.
260public same_file: (provider: type is files, host: provider,
261 left: text.os_string, right: text.os_string)
262 -> (same: bool) ! io_failed =
263 same = try provider.same_file(host, left, right)
264end same_file
265
266--- Close a provider handle. Treat it as consumed even if closing reports
267--- `io_failed`; do not retry a consumed handle.
268public close: (provider: type is files, host: provider,
269 sink opened: file) -> none ! io_failed =
270 try provider.close(host, opened)
271end close
272
273--- Open the directory at a path through `directories` evidence, reading it
274--- into the caller's `buffer`; close a successful directory through the
275--- same provider.
276public open_directory: (provider: type is directories, host: provider,
277 path: text.os_string, buffer: []mut u8)
278 -> (opened: directory from buffer)
279 ! mem.not_found | no_access | io_failed =
280 opened = try provider.open_directory(host, path, buffer)
281end open_directory
282
283--- Read the directory's next entry, whose name is lent from the directory
284--- until the next read; an entry whose `at_end` is true is the end of the
285--- directory.
286public read_entry: (provider: type is directories, host: provider,
287 inout opened: directory)
288 -> (item: entry from opened) ! io_failed =
289 item = try provider.read_entry(host, opened)
290end read_entry
291
292--- The size in bytes of the entry `name` in an open directory, without
293--- following a symbolic link.
294public entry_size: (provider: type is directories, host: provider,
295 opened: directory, name: text.os_string)
296 -> (size: u64) ! mem.not_found | no_access | io_failed =
297 size = try provider.entry_size(host, opened, name)
298end entry_size
299
300--- Close a directory. Treat it as consumed even if closing reports
301--- `io_failed`.
302public close_directory: (provider: type is directories, host: provider,
303 sink opened: directory) -> none ! io_failed =
304 try provider.close_directory(host, opened)
305end close_directory
306
307--- Make a directory at a path.
308public make_directory: (provider: type is directories, host: provider,
309 path: text.os_string)
310 -> none
311 ! already_exists | mem.not_found | no_access | io_failed =
312 try provider.make_directory(host, path)
313end make_directory
314
315--- Remove the file a path names.
316public remove_file: (provider: type is directories, host: provider,
317 path: text.os_string)
318 -> none ! mem.not_found | no_access | io_failed =
319 try provider.remove_file(host, path)
320end remove_file
321
322--- Remove the empty directory a path names.
323public remove_directory: (provider: type is directories, host: provider,
324 path: text.os_string)
325 -> none ! mem.not_found | no_access | io_failed =
326 try provider.remove_directory(host, path)
327end remove_directory
328
329--- Read up to the destination length through `reader` evidence. Returns the
330--- number of bytes placed in the buffer, or zero at end of input.
331public read: (provider: type is reader, host: provider,
332 opened: file, into: []mut u8)
333 -> (count: usize) ! io_failed =
334 count = try provider.read(host, opened, into)
335end read
336
337--- Write the complete byte slice through `writer` evidence. `io_failed` may
338--- follow partial output; callers must not assume retrying the whole buffer
339--- is safe.
340public write: (provider: type is writer, host: provider,
341 opened: file, bytes: []u8) -> none ! io_failed =
342 try provider.write(host, opened, bytes)
343end write
344
345-- Where `write_format` hands its store: the file, through the writer's
346-- handle.
347file_outlet: type (provider: type) = struct
348 host: provider
349 opened: file
350end file_outlet
351
352file_take: (provider: type is writer, self: ptr mut file_outlet(provider),
353 bytes: []u8) -> none ! mem.out_of_memory =
354 provider.write(self.val.host, self.val.opened, bytes) else (problem)
355 _ = problem
356 fail mem.out_of_memory
357 end
358end file_take
359
360(provider: type is writer) file_outlet(provider) is text.outlet
361 (take: file_take)
362
363-- The bytes `write_format` formats into on the stack before it writes
364-- them: a line no longer than this is one `write`.
365format_store: usize = 128
366
367--- Write `template` to the file with each `{}` replaced by the next of
368--- `values`, as `text.format` writes them: `io.write_format(host,
369--- io.out(host), "{} of {}\n", [any(addr done), any(addr total)])`. The
370--- caller supplies no buffer and has nothing to flush: the text is formatted
371--- into 128 bytes on the stack, which are written whenever they fill and at
372--- the end. So a line that fits is one write, and a value of any length is
373--- written in pieces. The template is checked against its values first, so
374--- `text.invalid_format` writes nothing. A failed write is `io_failed` and
375--- may follow partial output, as `write`'s may. A conformance of the
376--- program's own that asks for more room at once than the 128 bytes, or
377--- right-aligns a piece of a value longer than them that was already written,
378--- is `io_failed` too, since its text cannot be written. The values are
379--- `text.formattable`'s types; floats are not formattable yet. For many
380--- writes to one file, a `buffered_writer` gathers them into fewer.
381public write_format: (provider: type is writer, host: provider,
382 opened: file, template: utf8,
383 values: []any text.formattable)
384 -> none ! io_failed | text.invalid_format =
385 mut store: [format_store]u8 = zeroed
386 mut target: file_outlet(provider) = (host: host, opened: opened)
387 onto: any text.outlet = any(addr target)
388 text.format_onto(store[0..<format_store], onto, template, values)
389 else (problem)
390 fail text.invalid_format when problem == text.invalid_format
391 fail io_failed
392 end
393end write_format
394
395--- Make one write attempt and return its progress. Nonempty success is
396--- positive; empty input returns zero; failure transfers no bytes.
397public write_some: (provider: type is writer, host: provider,
398 opened: file, bytes: []u8) -> (count: usize) ! io_failed =
399 count = try provider.write_some(host, opened, bytes)
400end write_some
401
402--- Return the provider's standard-input identifier. Read it through
403--- `reader` evidence; zero from a nonempty read is end of input.
404public input: (provider: type is process, host: provider)
405 -> (stream: file) =
406 stream = provider.input(host)
407end input
408
409--- Return the provider's standard-output identifier.
410public out: (provider: type is process, host: provider)
411 -> (stream: file) =
412 stream = provider.out(host)
413end out
414
415--- Return the provider's standard-error identifier.
416public err: (provider: type is process, host: provider)
417 -> (stream: file) =
418 stream = provider.err(host)
419end err
420
421--- Return the number of arguments after the program's name, which is not
422--- one of them: C's `argc - 1`.
423public arguments_length: (provider: type is process, host: provider)
424 -> (count: usize) =
425 count = provider.arguments_length(host)
426end arguments_length
427
428--- Lend the process argument at `index` as an OS string; index zero is the
429--- first argument after the program's name, C's `argv[1]`. An index that
430--- names no argument reports `mem.not_found`. The provider's argument backing
431--- must outlive the string's use. Convert it to `utf8` with the checked
432--- `text.from_os_string`, or take its bytes with `text.os_string_bytes`;
433--- passed on as a path, it is not converted at all.
434public argument: (provider: type is process, host: provider, index: usize)
435 -> (value: text.os_string from host) ! mem.not_found =
436 value = try provider.argument(host, index)
437end argument
438
439--- Return the number of variables in the provider's environment.
440public variables_length: (provider: type is environment, host: provider)
441 -> (count: usize) =
442 count = provider.variables_length(host)
443end variables_length
444
445--- Lend the environment variable at `index`, from zero, its name and value
446--- OS strings. Reports `mem.not_found` for an index at or past the count;
447--- the provider's backing must outlive its use. Every index below
448--- `variables_length` gives one, so a loop over them lists the whole
449--- environment.
450public variable: (provider: type is environment, host: provider,
451 index: usize)
452 -> (item: text.os_variable from host) ! mem.not_found =
453 item = try provider.variable(host, index)
454end variable
455
456--- Lend the value of the environment variable named `name`, the first one
457--- when the environment names it twice. Reports `mem.not_found` when no
458--- variable has that name. `text.from_os_string` is the value's checked
459--- conversion to `utf8`.
460public variable_named: (provider: type is environment, host: provider,
461 name: text.os_string)
462 -> (value: text.os_string from host)
463 ! mem.not_found =
464 value = try provider.variable_named(host, name)
465end variable_named