1-- The provider on Windows, continued: `io.directories`,
2-- `io.file_system`, `io.terminals`, `io.entropy`, the wall clock and the
3-- words for a failure, over platform/windows, as directories.ldn,
4-- file_system.ldn, entropy.ldn, time.ldn and failures.ldn give them over
5-- platform/posix.
6
7import core/failure
8import core/io
9import core/mem
10import core/text
11import core/time
12import platform/windows
13
14fixed if compiler.os == windows then
15 --- A failure of a program over the hosted services: on Windows one of
16 --- the core library's failures, which every provider reports, the
17 --- system's own code being `last_errno`'s.
18 public reason: type = failure.reason
19
20 -- Every failure is a library one here, which `failure.words` words.
21 --- A short English phrase for a failure (D582), in lower case but for a
22 --- name's own capitals and with no final stop, so that it reads after a
23 --- subject and a colon. The phrase is static text: it borrows nothing and
24 --- is never freed.
25 public words: (problem: reason) -> (phrase: []u8) =
26 phrase = failure.words(problem)
27 end words
28
29 -- A Windows time, hundreds of nanoseconds since 1601, as core/time's
30 -- wall time since 1970.
31 wall_of: (at: windows.file_time) -> (result: time.wall_time) =
32 ticks: u64 = (u64(at.high) << 32) | u64(at.low)
33 if ticks >= windows.epoch_difference then
34 since: u64 = ticks - windows.epoch_difference
35 result = (seconds: i64(since / 10000000),
36 nanoseconds: u32((since % 10000000) * 100))
37 else
38 before: u64 = windows.epoch_difference - ticks
39 mut seconds: i64 = -i64(before / 10000000)
40 mut rest: u64 = before % 10000000
41 if rest > 0 then
42 seconds = seconds - 1
43 rest = 10000000 - rest
44 end if
45 result = (seconds: seconds, nanoseconds: u32(rest * 100))
46 end if
47 end wall_of
48
49 system_wall_now: (self: host) -> (at: time.wall_time) =
50 _ = self.failure
51 mut sample: windows.file_time = zeroed
52 windows.system_time(addr sample)
53 at = wall_of(sample)
54 end system_wall_now
55
56 host is time.wall_clock (wall_now: system_wall_now)
57
58 system_fill_random: (self: host, into: []mut u8) -> none ! io.io_failed =
59 keep_failure(self, 0)
60 mut filled: usize = 0
61 while filled < lenof into do
62 mut step: usize = lenof into - filled
63 if step > 0x40000000 then step = 0x40000000 end if
64 status: i32 = windows.random(0, addr into[filled], u32(step),
65 windows.system_preferred_generator)
66 if status <> 0 then
67 record(self, windows.error_not_supported)
68 fail io.io_failed
69 end if
70 filled += step
71 end while
72 end system_fill_random
73
74 host is io.entropy (fill_random: system_fill_random)
75
76 -- The kind of a file Windows describes by its attributes and, for a
77 -- reparse point, its tag.
78 kind_of: (attributes: u32, tag: u32) -> (named: io.entry_kind) =
79 named = if (attributes & windows.attribute_reparse_point) <> 0
80 and tag == windows.reparse_tag_symlink then io.symbolic_link
81 elsif (attributes & windows.attribute_directory) <> 0 then
82 io.subdirectory
83 else io.regular end if
84 end kind_of
85
86 -- Room for the directory's own path, from `final_path`, and a name.
87 long_room: type = [2048]u16
88
89 -- Put `opened`'s path into `room` followed by `tail`'s units and a
90 -- NUL, and return the units before the NUL: false when it does not
91 -- fit.
92 under: (opened: windows.handle, tail: []u16, room: []mut u16)
93 -> (count: usize, fits: bool) =
94 count = 0
95 fits = false
96 got: u32 = windows.final_path(opened, addr room[0], u32(lenof room), 0)
97 return when got == 0 or usize(got) >= lenof room
98 count = usize(got)
99 return when count + lenof tail + 1 > lenof room
100 for at in usize(0)..<lenof tail do
101 room[count + at] = tail[at]
102 end for
103 count += lenof tail
104 room[count] = 0
105 fits = true
106 end under
107
108 -- A pending entry: the first, which `find_first_file` read when the
109 -- directory was opened, kept in the directory's buffer until it is
110 -- asked for. `stream` holds the search's handle, `position` the
111 -- pending entry's kind, one more than its order among the kinds, or
112 -- zero when none is pending.
113 kind_number: (kind: io.entry_kind) -> (number: usize) =
114 number = if kind == io.subdirectory then 1
115 elsif kind == io.symbolic_link then 2
116 elsif kind == io.regular then 3
117 else 4 end if
118 end kind_number
119
120 kind_named: (number: usize) -> (kind: io.entry_kind) =
121 kind = if number == 1 then io.subdirectory
122 elsif number == 2 then io.symbolic_link
123 elsif number == 3 then io.regular
124 else io.special end if
125 end kind_named
126
127 -- The units of a found entry's name, before its NUL.
128 found_name: (found: windows.find_data) -> (length: usize) =
129 length = 0
130 while length < 260 and found.name[length] <> 0 do inc length end while
131 end found_name
132
133 -- Keep a found entry's name, as WTF-8, and kind in the directory.
134 keep_found: (inout opened: io.directory, found: windows.find_data) -> none =
135 length: usize = found_name(found)
136 opened.filled = windows.to_host(found.name[0..<length], opened.buffer)
137 opened.consumed = 0
138 opened.position = kind_number(kind_of(found.attributes,
139 found.reserved_0))
140 end keep_found
141
142 star: [2]u16 = [92, 42]
143
144 system_open_directory: (self: host, path: text.os_string,
145 buffer: []mut u8)
146 -> (opened: io.directory from buffer)
147 ! mem.not_found | io.no_access | io.io_failed =
148 keep_failure(self, 0)
149 opened = (handle: windows.invalid_handle, stream: windows.invalid_handle,
150 position: 0, buffer: buffer, filled: 0, consumed: 0,
151 at_end: true)
152 fail io.io_failed when lenof buffer < io.directory_buffer
153 mut room: windows.path_room = zeroed
154 name: ptr u16 = try path_in(self, path, room)
155 made: windows.handle = windows.create_file(
156 name, windows.list_directory | windows.file_read_attributes,
157 windows.share_all, 0, windows.open_existing,
158 windows.flag_backup_semantics, 0)
159 if made == windows.invalid_handle then
160 try path_failure(self, windows.last_error())
161 fail io.io_failed
162 end if
163 mut information: windows.file_information = zeroed
164 if windows.get_file_information(made, addr information) == 0 then
165 code: u32 = windows.last_error()
166 _ = windows.close_handle(made)
167 record(self, code)
168 fail io.io_failed
169 end if
170 if (information.attributes & windows.attribute_directory) == 0 then
171 _ = windows.close_handle(made)
172 record(self, windows.error_directory)
173 fail mem.not_found
174 end if
175 mut pattern: long_room = zeroed
176 (count, fits) := under(made, star[0..<2], pattern)
177 _ = count
178 if not fits then
179 _ = windows.close_handle(made)
180 record(self, windows.error_filename_exceeds_range)
181 fail io.io_failed
182 end if
183 mut found: windows.find_data = zeroed
184 search: windows.handle = windows.find_first_file(addr pattern[0],
185 addr found)
186 if search == windows.invalid_handle then
187 code: u32 = windows.last_error()
188 _ = windows.close_handle(made)
189 try path_failure(self, code)
190 fail io.io_failed
191 end if
192 opened = (handle: made, stream: search, position: 0, buffer: buffer,
193 filled: 0, consumed: 0, at_end: false)
194 keep_found(opened, found)
195 end system_open_directory
196
197 system_read_entry: (self: host, inout opened: io.directory)
198 -> (item: io.entry from opened) ! io.io_failed =
199 keep_failure(self, 0)
200 loop do
201 if opened.at_end then
202 item = (name: text.os_string_over(opened.buffer[0..<0]),
203 kind: io.special, at_end: true)
204 return
205 end if
206 if opened.position <> 0 then
207 kind: io.entry_kind = kind_named(opened.position)
208 opened.position = 0
209 name: []u8 = opened.buffer[0..<opened.filled]
210 if not (lenof name > 0 and lenof name <= 2 and name[0] == 46
211 and (lenof name == 1 or name[1] == 46)) then
212 item = (name: text.os_string_over(name), kind: kind,
213 at_end: false)
214 return
215 end if
216 end if
217 mut found: windows.find_data = zeroed
218 if windows.find_next_file(opened.stream, addr found) == 0 then
219 code: u32 = windows.last_error()
220 if code == windows.error_no_more_files then
221 opened.at_end = true
222 else
223 record(self, code)
224 fail io.io_failed
225 end if
226 else
227 keep_found(opened, found)
228 end if
229 end loop
230 end system_read_entry
231
232 system_entry_size: (self: host, opened: io.directory,
233 name: text.os_string)
234 -> (size: u64)
235 ! mem.not_found | io.no_access | io.io_failed =
236 keep_failure(self, 0)
237 size = 0
238 bytes: []u8 = text.os_string_bytes(name)
239 fail mem.not_found when lenof bytes == 0
240 or lenof bytes >= io.directory_buffer
241 for byte in bytes do
242 fail mem.not_found when byte == 0 or byte == u8('/')
243 or byte == u8('\\')
244 end for
245 mut tail: windows.path_room = zeroed
246 tail[0] = 92
247 count: usize = windows.to_units(bytes, tail[1..<lenof tail])
248 else (problem)
249 _ = problem
250 fail mem.not_found
251 end
252 mut room: long_room = zeroed
253 (count: length, fits) := under(opened.handle, tail[0..<count + 1], room)
254 _ = length
255 if not fits then
256 record(self, windows.error_filename_exceeds_range)
257 fail io.io_failed
258 end if
259 mut found: windows.attribute_data = zeroed
260 if windows.get_file_attributes_ex(addr room[0], 0, addr found) == 0 then
261 try path_failure(self, windows.last_error())
262 fail io.io_failed
263 end if
264 size = (u64(found.size_high) << 32) | u64(found.size_low)
265 end system_entry_size
266
267 system_close_directory: (self: host, sink opened: io.directory)
268 -> none ! io.io_failed =
269 keep_failure(self, 0)
270 searched: i32 = windows.find_close(opened.stream)
271 closed: i32 = windows.close_handle(opened.handle)
272 if searched == 0 or closed == 0 then
273 record_last(self)
274 fail io.io_failed
275 end if
276 end system_close_directory
277
278 system_make_directory: (self: host, path: text.os_string)
279 -> none
280 ! io.already_exists | mem.not_found | io.no_access
281 | io.io_failed =
282 keep_failure(self, 0)
283 mut room: windows.path_room = zeroed
284 name: ptr u16 = try path_in(self, path, room)
285 if windows.create_directory(name, 0) == 0 then
286 code: u32 = windows.last_error()
287 record(self, code)
288 fail io.already_exists when code == windows.error_already_exists
289 or code == windows.error_file_exists
290 try path_failure(self, code)
291 fail io.io_failed
292 end if
293 end system_make_directory
294
295 remove: (self: host, path: text.os_string, directory: bool)
296 -> none ! mem.not_found | io.no_access | io.io_failed =
297 keep_failure(self, 0)
298 mut room: windows.path_room = zeroed
299 name: ptr u16 = try path_in(self, path, room)
300 done: i32 = if directory then windows.remove_directory(name)
301 else windows.delete_file(name) end if
302 if done == 0 then
303 try path_failure(self, windows.last_error())
304 fail io.io_failed
305 end if
306 end remove
307
308 system_remove_file: (self: host, path: text.os_string)
309 -> none ! mem.not_found | io.no_access | io.io_failed =
310 try remove(self, path, false)
311 end system_remove_file
312
313 system_remove_directory: (self: host, path: text.os_string)
314 -> none
315 ! mem.not_found | io.no_access | io.io_failed =
316 try remove(self, path, true)
317 end system_remove_directory
318
319 host is io.directories
320 (open_directory: system_open_directory,
321 read_entry: system_read_entry, entry_size: system_entry_size,
322 close_directory: system_close_directory,
323 make_directory: system_make_directory,
324 remove_file: system_remove_file,
325 remove_directory: system_remove_directory)
326
327 system_open_file: (self: host, path: text.os_string,
328 options: io.open_options)
329 -> (opened: io.file)
330 ! io.already_exists | mem.not_found | io.no_access
331 | io.is_a_directory | io.io_failed =
332 keep_failure(self, 0)
333 opened = file_of(windows.no_handle)
334 if not io.valid_options(options) then
335 record(self, windows.error_invalid_parameter)
336 fail io.io_failed
337 end if
338 mut access: u32 = 0
339 if options.read then access = access | windows.generic_read end if
340 if options.append and not options.truncate then
341 access = access | windows.append_data | windows.synchronize_access
342 | windows.file_read_attributes
343 elsif options.write then
344 access = access | windows.generic_write
345 end if
346 disposition: u32 =
347 if options.create and options.exclusive then windows.create_new
348 elsif options.create and options.truncate then windows.create_always
349 elsif options.create then windows.open_always
350 elsif options.truncate then windows.truncate_existing
351 else windows.open_existing end if
352 opened = try open(self, path, access, disposition)
353 end system_open_file
354
355 system_rename: (self: host, old: text.os_string, new: text.os_string)
356 -> none ! mem.not_found | io.no_access | io.io_failed =
357 keep_failure(self, 0)
358 mut from_room: windows.path_room = zeroed
359 mut to_room: windows.path_room = zeroed
360 source: ptr u16 = try path_in(self, old, from_room)
361 target: ptr u16 = try path_in(self, new, to_room)
362 if windows.move_file(source, target,
363 windows.move_replace_existing) == 0 then
364 try path_failure(self, windows.last_error())
365 fail io.io_failed
366 end if
367 end system_rename
368
369 -- What core/io says of a file Windows describes.
370 status_from: (found: windows.file_information, kind: io.entry_kind)
371 -> (result: io.status) =
372 result = (size: (u64(found.size_high) << 32) | u64(found.size_low),
373 kind: kind, modified: wall_of(found.written),
374 read_only: (found.attributes
375 & windows.attribute_read_only) <> 0)
376 end status_from
377
378 -- The status of an open handle; `link` when it was opened not
379 -- following a link it ends in.
380 status_of_handle: (self: host, opened: windows.handle, link: bool)
381 -> (found: io.status) ! io.io_failed =
382 mut information: windows.file_information = zeroed
383 if windows.get_file_information(opened, addr information) == 0 then
384 record_last(self)
385 fail io.io_failed
386 end if
387 mut kind: io.entry_kind = kind_of(information.attributes, 0)
388 if link and (information.attributes
389 & windows.attribute_reparse_point) <> 0 then
390 kind = io.symbolic_link
391 end if
392 found = status_from(information, kind)
393 end status_of_handle
394
395 system_status_of: (self: host, path: text.os_string,
396 links: io.link_handling)
397 -> (found: io.status)
398 ! mem.not_found | io.no_access | io.io_failed =
399 keep_failure(self, 0)
400 mut room: windows.path_room = zeroed
401 name: ptr u16 = try path_in(self, path, room)
402 link: bool = links == io.not_following
403 flags: u32 = if link then windows.flag_backup_semantics
404 | windows.flag_open_reparse_point
405 else windows.flag_backup_semantics end if
406 made: windows.handle = windows.create_file(
407 name, windows.file_read_attributes, windows.share_all, 0,
408 windows.open_existing, flags, 0)
409 if made == windows.invalid_handle then
410 try path_failure(self, windows.last_error())
411 fail io.io_failed
412 end if
413 found = status_of_handle(self, made, link) else begin
414 _ = windows.close_handle(made)
415 fail io.io_failed
416 end
417 _ = windows.close_handle(made)
418 end system_status_of
419
420 system_status_of_file: (self: host, opened: io.file)
421 -> (found: io.status) ! io.io_failed =
422 keep_failure(self, 0)
423 found = try status_of_handle(self, handle_of(opened), false)
424 end system_status_of_file
425
426 system_set_read_only: (self: host, path: text.os_string,
427 read_only: bool)
428 -> none ! mem.not_found | io.no_access | io.io_failed =
429 keep_failure(self, 0)
430 mut room: windows.path_room = zeroed
431 name: ptr u16 = try path_in(self, path, room)
432 given: u32 = windows.get_file_attributes(name)
433 if given == windows.invalid_attributes then
434 try path_failure(self, windows.last_error())
435 fail io.io_failed
436 end if
437 bits: u32 = if read_only then given | windows.attribute_read_only
438 else given & ~windows.attribute_read_only end if
439 if windows.set_file_attributes(name, bits) == 0 then
440 try path_failure(self, windows.last_error())
441 fail io.io_failed
442 end if
443 end system_set_read_only
444
445 system_synchronize: (self: host, opened: io.file,
446 writing: io.synchronizing)
447 -> none ! io.io_failed =
448 keep_failure(self, 0)
449 _ = writing
450 if windows.flush_file_buffers(handle_of(opened)) == 0 then
451 record_last(self)
452 fail io.io_failed
453 end if
454 end system_synchronize
455
456 system_truncate: (self: host, opened: io.file, size: u64)
457 -> none ! io.io_failed =
458 keep_failure(self, 0)
459 mut ending: u64 = size
460 if windows.set_file_information(handle_of(opened),
461 windows.file_end_of_file_info,
462 usize(addr ending), 8) == 0 then
463 record_last(self)
464 fail io.io_failed
465 end if
466 end system_truncate
467
468 -- The handle's position, which `at_offset` keeps across a read or a
469 -- write at an offset: on a handle that does not overlap, such a call
470 -- moves it.
471 position: (opened: windows.handle) -> (at: i64) ! io.io_failed =
472 mut found: i64 = 0
473 if windows.set_file_pointer(opened, 0, usize(addr found),
474 windows.file_current) == 0 then
475 fail io.io_failed
476 end if
477 at = found
478 end position
479
480 restore: (opened: windows.handle, at: i64) -> none =
481 _ = windows.set_file_pointer(opened, at, 0, windows.file_begin)
482 end restore
483
484 system_read_at: (self: host, opened: io.file, offset: u64, into: []mut u8)
485 -> (count: usize) ! io.io_failed =
486 keep_failure(self, 0)
487 count = 0
488 return when lenof into == 0
489 made: windows.handle = handle_of(opened)
490 kept: i64 = position(made) else (problem)
491 _ = problem
492 record_last(self)
493 fail io.io_failed
494 end
495 mut asked: windows.overlapped = zeroed
496 asked.offset_low = u32(offset & 0xFFFFFFFF)
497 asked.offset_high = u32(offset >> 32)
498 mut want: usize = lenof into
499 if want > 0x40000000 then want = 0x40000000 end if
500 mut got: u32 = 0
501 done: i32 = windows.read_file(made, addr into[0], u32(want), addr got,
502 usize(addr asked))
503 code: u32 = windows.last_error()
504 restore(made, kept)
505 if done == 0 then
506 return when code == windows.error_handle_eof
507 record(self, code)
508 fail io.io_failed
509 end if
510 count = usize(got)
511 end system_read_at
512
513 system_write_at: (self: host, opened: io.file, offset: u64, bytes: []u8)
514 -> none ! io.io_failed =
515 keep_failure(self, 0)
516 made: windows.handle = handle_of(opened)
517 kept: i64 = position(made) else (problem)
518 _ = problem
519 record_last(self)
520 fail io.io_failed
521 end
522 mut written: usize = 0
523 while written < lenof bytes do
524 mut asked: windows.overlapped = zeroed
525 at: u64 = offset + u64(written)
526 asked.offset_low = u32(at & 0xFFFFFFFF)
527 asked.offset_high = u32(at >> 32)
528 mut want: usize = lenof bytes - written
529 if want > 0x40000000 then want = 0x40000000 end if
530 mut put: u32 = 0
531 if windows.write_file(made, addr bytes[written], u32(want), addr put,
532 usize(addr asked)) == 0 then
533 code: u32 = windows.last_error()
534 restore(made, kept)
535 record(self, code)
536 fail io.io_failed
537 end if
538 if put == 0 then
539 restore(made, kept)
540 fail io.io_failed
541 end if
542 written += usize(put)
543 end while
544 restore(made, kept)
545 end system_write_at
546
547 system_make_temporary: (self: host, names: any io.entropy,
548 directory: text.os_string, into: []mut u8)
549 -> (made: io.temporary from into)
550 ! mem.not_found | io.no_access | io.io_failed =
551 keep_failure(self, 0)
552 made = (opened: file_of(windows.no_handle),
553 path: text.os_string_over(into[0..<0]))
554 mut tries: usize = 0
555 while tries < io.temporary_tries do
556 inc tries
557 mut bytes: [8]u8 = zeroed
558 try names.fill_random(bytes[0..<8])
559 path: text.os_string = try io.temporary_path(directory, bytes, into)
560 opened: io.file =
561 open(self, path, windows.generic_read | windows.generic_write,
562 windows.create_new)
563 else (problem)
564 continue when problem == io.already_exists
565 fail mem.not_found when problem == mem.not_found
566 fail io.no_access when problem == io.no_access
567 fail io.io_failed
568 end
569 keep_failure(self, 0)
570 made = (opened: opened, path: path)
571 return
572 end while
573 fail io.io_failed
574 end system_make_temporary
575
576 system_current_directory: (self: host, into: []mut u8)
577 -> (path: text.os_string from into)
578 ! io.io_failed =
579 keep_failure(self, 0)
580 path = text.os_string_over(into[0..<0])
581 mut room: long_room = zeroed
582 got: u32 = windows.current_directory(u32(lenof room), addr room[0])
583 if got == 0 then
584 record_last(self)
585 fail io.io_failed
586 end if
587 if usize(got) >= lenof room
588 or windows.host_length(room[0..<usize(got)]) > lenof into then
589 record(self, windows.error_filename_exceeds_range)
590 fail io.io_failed
591 end if
592 count: usize = windows.to_host(room[0..<usize(got)], into)
593 path = text.os_string_over(into[0..<count])
594 end system_current_directory
595
596 system_is_terminal: (self: host, stream: io.file) -> (yes: bool) =
597 _ = self.failure
598 mut mode: u32 = 0
599 yes = windows.get_console_mode(handle_of(stream), addr mode) <> 0
600 end system_is_terminal
601
602 system_window_size: (self: host, stream: io.file) -> (size: io.window)
603 ! io.not_a_terminal | io.io_failed =
604 keep_failure(self, 0)
605 size = (rows: 0, columns: 0)
606 mut mode: u32 = 0
607 if windows.get_console_mode(handle_of(stream), addr mode) == 0 then
608 record_last(self)
609 fail io.not_a_terminal
610 end if
611 mut found: windows.console_screen = zeroed
612 if windows.console_screen_info(handle_of(stream), addr found) == 0 then
613 -- An input console has no screen buffer, and no size.
614 return
615 end if
616 size = (rows: u16(i32(found.bottom) - i32(found.top) + 1),
617 columns: u16(i32(found.right) - i32(found.left) + 1))
618 end system_window_size
619
620 host is io.file_system
621 (open_file: system_open_file, rename: system_rename,
622 status_of: system_status_of, status_of_file: system_status_of_file,
623 set_read_only: system_set_read_only, synchronize: system_synchronize,
624 truncate: system_truncate, read_at: system_read_at,
625 write_at: system_write_at, make_temporary: system_make_temporary,
626 current_directory: system_current_directory)
627 host is io.terminals
628 (is_terminal: system_is_terminal, window_size: system_window_size)
629end if