Landin library reference discuss source

hosted/system/windows.ldn

1--  The provider on Windows: system.ldn's files, standard streams,
2--  arguments and environment, over platform/windows (D2270).  A file is
3--  the handle Windows gives, kept in `io.file` as its word; a failure
4--  keeps the Win32 error code in the handle's last-error cell, which
5--  `last_errno` reports, and maps it to the concepts' atoms as the POSIX
6--  arm maps an errno.  Paths, arguments and variables are host strings in
7--  WTF-8 (D2268), converted at each call.
8
9import core/cmp
10import core/io
11import core/mem
12import core/text
13import platform/windows
14
15fixed if compiler.os == windows then
16    --- Keep the Win32 error `code` in the handle's last-error cell, which
17    --- `last_errno` then reports. It is for a provider built over this one,
18    --- whose own calls fail through the same cell. Zero leaves the cell as
19    --- it is.
20    public record_errno: (handle: host, code: u32) -> none =
21        record(handle, code)
22    end record_errno
23
24    --  Keep a failure's code in the handle's cell.
25    record: (self: host, code: u32) -> none =
26        if code <> 0 then keep_failure(self, i32(code & 0x7FFFFFFF)) end if
27    end record
28
29    --  Keep the code the failing call just left.
30    record_last: (self: host) -> none =
31        record(self, windows.last_error())
32    end record_last
33
34    --  A path names nothing: no file or no directory on the way, a drive
35    --  that is not there, or a component before the last that is a file.
36    absent: (code: u32) -> (yes: bool) =
37        yes = code == windows.error_file_not_found
38              or code == windows.error_path_not_found
39              or code == windows.error_invalid_drive
40              or code == windows.error_directory
41              or code == windows.error_bad_netpath
42    end absent
43
44    --  The caller may not do what it asked.
45    refused: (code: u32) -> (yes: bool) =
46        yes = code == windows.error_access_denied
47              or code == windows.error_sharing_violation
48              or code == windows.error_lock_violation
49              or code == windows.error_write_protect
50    end refused
51
52    --  Keep a failure about a path and fail as the concepts say of one.
53    path_failure: (self: host, code: u32)
54                  -> none ! mem.not_found | io.no_access | io.io_failed =
55        record(self, code)
56        fail mem.not_found when absent(code)
57        fail io.no_access when refused(code)
58        fail io.io_failed
59    end path_failure
60
61    --  A socket carries this bit beside its handle, which a handle never
62    --  has, so that a provider built over this one sends on it rather than
63    --  writing.
64    socket_mark: usize = 0x4000000000000000
65
66    --- Return the Windows handle behind a file of this provider, for a call
67    --- the portable concepts do not make. The file stays this provider's;
68    --- close it through `io.close`.
69    public handle_of: (opened: io.file) -> (result: windows.handle) =
70        result = usize(opened) & ~(socket_mark | pipe_mark)
71    end handle_of
72
73    --- Return a Windows handle as a file of this provider, which closes it
74    --- with `io.close`.
75    public file_of: (opened: windows.handle) -> (result: io.file) =
76        result = io.file(opened)
77    end file_of
78
79    --- Return a Windows socket as a file of this provider, whose `io.write`
80    --- sends on it as `net.send` does.
81    public socket_of: (opened: windows.handle) -> (result: io.file) =
82        result = io.file(opened | socket_mark)
83    end socket_of
84
85    --- Whether `opened` is a socket `socket_of` made, which a provider built
86    --- over this one writes by sending.
87    public is_socket: (opened: io.file) -> (yes: bool) =
88        yes = (usize(opened) & socket_mark) <> 0
89    end is_socket
90
91    --  Copy `path` into `room` for a call, or keep why it cannot be given.
92    path_in: (self: host, path: text.os_string, room: []mut u16)
93             -> (name: ptr u16 from room) ! io.io_failed =
94        name = windows.terminate(path, room) else (problem)
95            record(self, windows.code_of(problem))
96            fail io.io_failed
97        end
98    end path_in
99
100    --  Whether `path` names a directory.
101    names_directory: (name: ptr u16) -> (yes: bool) =
102        found: u32 = windows.get_file_attributes(name)
103        yes = found <> windows.invalid_attributes
104              and (found & windows.attribute_directory) <> 0
105    end names_directory
106
107    --  Open `path` for `access` with `disposition`: the one open every
108    --  concept's makes.  A directory is `is_a_directory`: an open to read
109    --  takes it with backup semantics, so that it is found, asks what it
110    --  opened and closes it; an open to write is refused access, and asks.
111    open: (self: host, path: text.os_string, access: u32, disposition: u32)
112          -> (opened: io.file)
113          ! io.already_exists | mem.not_found | io.no_access | io.is_a_directory
114          | io.io_failed =
115        keep_failure(self, 0)
116        mut room: windows.path_room = zeroed
117        name: ptr u16 = try path_in(self, path, room)
118        reading: bool = access == windows.generic_read
119        flags: u32 = if reading then windows.flag_backup_semantics
120                     else windows.attribute_normal end if
121        made: windows.handle = windows.create_file(name, access,
122                                                   windows.share_all, 0,
123                                                   disposition, flags, 0)
124        if made == windows.invalid_handle then
125            code: u32 = windows.last_error()
126            if code == windows.error_file_exists
127              or code == windows.error_already_exists then
128                record(self, code)
129                fail io.already_exists
130            end if
131            if code == windows.error_access_denied and names_directory(name) then
132                record(self, code)
133                fail io.is_a_directory
134            end if
135            try path_failure(self, code)
136            fail io.io_failed
137        end if
138        if reading then
139            mut found: windows.file_information = zeroed
140            if windows.get_file_information(made, addr found) == 0 then
141                code: u32 = windows.last_error()
142                _ = windows.close_handle(made)
143                record(self, code)
144                fail io.io_failed
145            end if
146            if (found.attributes & windows.attribute_directory) <> 0 then
147                _ = windows.close_handle(made)
148                record(self, windows.error_directory)
149                fail io.is_a_directory
150            end if
151        end if
152        opened = file_of(made)
153    end open
154
155    system_open_read: (self: host, path: text.os_string)
156                      -> (opened: io.file)
157                      ! mem.not_found | io.no_access | io.is_a_directory
158                      | io.io_failed =
159        opened = open(self, path, windows.generic_read, windows.open_existing)
160            else (problem)
161            fail mem.not_found when problem == mem.not_found
162            fail io.no_access when problem == io.no_access
163            fail io.is_a_directory when problem == io.is_a_directory
164            fail io.io_failed
165        end
166    end system_open_read
167
168    system_open_write: (self: host, path: text.os_string)
169                       -> (opened: io.file)
170                       ! mem.not_found | io.no_access | io.is_a_directory
171                       | io.io_failed =
172        opened = open(self, path, windows.generic_write, windows.create_always)
173            else (problem)
174            fail mem.not_found when problem == mem.not_found
175            fail io.no_access when problem == io.no_access
176            fail io.is_a_directory when problem == io.is_a_directory
177            fail io.io_failed
178        end
179    end system_open_write
180
181    --  What identifies the file `path` names: its volume and index, which
182    --  an open asking only for its attributes reads.  `absent` says no file
183    --  is there.
184    identity: type = struct
185        volume: u32
186        high: u32
187        low: u32
188    end identity
189
190    no_file: atom
191
192    identity_of: (self: host, path: text.os_string)
193                 -> (found: identity) ! no_file | io.io_failed =
194        mut room: windows.path_room = zeroed
195        name: ptr u16 = try path_in(self, path, room)
196        made: windows.handle = windows.create_file(
197            name, windows.file_read_attributes, windows.share_all, 0,
198            windows.open_existing, windows.flag_backup_semantics, 0)
199        if made == windows.invalid_handle then
200            code: u32 = windows.last_error()
201            fail no_file when absent(code)
202            record(self, code)
203            fail io.io_failed
204        end if
205        mut information: windows.file_information = zeroed
206        done: i32 = windows.get_file_information(made, addr information)
207        code: u32 = windows.last_error()
208        _ = windows.close_handle(made)
209        if done == 0 then
210            record(self, code)
211            fail io.io_failed
212        end if
213        found = (volume: information.volume, high: information.index_high,
214                 low: information.index_low)
215    end identity_of
216
217    --  A stable-path identity check, as the POSIX arm's: every input
218    --  lookup failure refuses; only an absent output is allowed.
219    system_same_file: (self: host, left: text.os_string,
220                       right: text.os_string)
221                      -> (same: bool) ! io.io_failed =
222        keep_failure(self, 0)
223        first: identity = identity_of(self, left) else (problem)
224            if problem == no_file then
225                record(self, windows.error_file_not_found)
226            end if
227            fail io.io_failed
228        end
229        same = false
230        second: identity = identity_of(self, right) else (problem)
231            return when problem == no_file
232            fail io.io_failed
233        end
234        same = first.volume == second.volume and first.high == second.high
235               and first.low == second.low
236    end system_same_file
237
238    system_close: (self: host, sink opened: io.file)
239                  -> none ! io.io_failed =
240        keep_failure(self, 0)
241        if windows.close_handle(handle_of(opened)) == 0 then
242            record_last(self)
243            fail io.io_failed
244        end if
245    end system_close
246
247    system_input: (self: host) -> (stream: io.file) =
248        _ = self.failure
249        stream = file_of(windows.get_std_handle(windows.std_input))
250    end system_input
251
252    system_out: (self: host) -> (stream: io.file) =
253        _ = self.failure
254        stream = file_of(windows.get_std_handle(windows.std_output))
255    end system_out
256
257    system_err: (self: host) -> (stream: io.file) =
258        _ = self.failure
259        stream = file_of(windows.get_std_handle(windows.std_error))
260    end system_err
261
262    system_arguments_length: (self: host) -> (count: usize) =
263        _ = self.failure
264        table: windows.argument_table = windows.arguments()
265        count = table.count
266    end system_arguments_length
267
268    system_argument: (self: host, index: usize)
269                     -> (value: text.os_string from self) ! mem.not_found =
270        _ = self.failure
271        table: windows.argument_table = windows.arguments()
272        fail mem.not_found when index >= table.count
273        value = windows.argument(table, index)
274    end system_argument
275
276    system_variables_length: (self: host) -> (count: usize) =
277        _ = self.failure
278        table: windows.environment_table = windows.environment()
279        count = table.count
280    end system_variables_length
281
282    system_variable: (self: host, index: usize)
283                     -> (item: text.os_variable from self) ! mem.not_found =
284        _ = self.failure
285        table: windows.environment_table = windows.environment()
286        fail mem.not_found when index >= table.count
287        item = windows.variable(table, index)
288    end system_variable
289
290    --  Windows compares a variable's name without regard to case, as its
291    --  own `GetEnvironmentVariableW` does, ASCII letters folded.
292    folded: (unit: u8) -> (result: u8) =
293        result = if unit >= 97 and unit <= 122 then unit - 32 else unit end if
294    end folded
295
296    same_name: (found: []u8, wanted: []u8) -> (yes: bool) =
297        yes = false
298        return when lenof found <> lenof wanted
299        for index in usize(0)..<lenof found do
300            return when folded(found[index]) <> folded(wanted[index])
301        end for
302        yes = true
303    end same_name
304
305    system_variable_named: (self: host, name: text.os_string)
306                           -> (value: text.os_string from self)
307                           ! mem.not_found =
308        _ = self.failure
309        table: windows.environment_table = windows.environment()
310        wanted: []u8 = text.os_string_bytes(name)
311        for index in usize(0)..<table.count do
312            item: text.os_variable = windows.variable(table, index)
313            if same_name(text.os_string_bytes(item.name), wanted) then
314                value = item.value
315                return
316            end if
317        end for
318        fail mem.not_found
319    end system_variable_named
320
321    host is io.files
322        (open_read: system_open_read, open_write: system_open_write,
323         same_file: system_same_file, close: system_close)
324    host is io.process
325        (input: system_input, out: system_out, err: system_err,
326         arguments_length: system_arguments_length, argument: system_argument)
327    host is io.environment
328        (variables_length: system_variables_length,
329         variable: system_variable,
330         variable_named: system_variable_named)
331
332    --  The standard streams are `GetStdHandle`'s, and the root is
333    --  platform/windows'.
334    --- Return a handle to the hosted services that do not wait. It exposes the
335    --- process arguments and the three standard streams, which a provider
336    --- built over it reads and writes. Regular open-write operations create
337    --- or truncate files; close regular files explicitly. A program that waits
338    --- on anything mints `hosted/blocking`'s `new` instead, which makes this
339    --- handle itself. The handle borrows nothing: the argument root and the
340    --- standard streams belong to the process. Each thread that calls through
341    --- the handle, or through a copy of it, records into a last-error and
342    --- last-signal cell of its own (D503), so threads sharing a handle never
343    --- see each other's failures. Use `over` for a cell of the caller's own.
344    ---
345    --- The arguments are the root that `_landin_host_initialize_arguments`
346    --- established. The hosted entry establishes it before its body runs;
347    --- calling this before any root exists traps.
348    public new: () -> (handle: host) =
349        _ = windows.arguments()
350        handle = (failure: calling_thread, signal: calling_thread)
351    end new
352
353    --- Fill `state` and return a handle that records its last error there
354    --- rather than in the calling thread's cell, whichever thread calls. It
355    --- shares the process's arguments and standard streams like any other
356    --- handle; calling this before any root exists traps, as `new` does.
357    public over: (inout state: space) -> (handle: host from state) =
358        _ = windows.arguments()
359        state = (failure: 0, signal: 0)
360        handle = (failure: addr state.failure, signal: addr state.signal)
361    end over
362end if