1-- Files, directories and the console: kernel32's calls on a path or an
2-- open file's handle, raw. A path is a terminated wide string, which
3-- wide.ldn's `terminate` makes from a host string.
4
5fixed if compiler.os == windows then
6 --- `CreateFileW`'s access rights, `GENERIC_READ` and `GENERIC_WRITE`, and
7 --- `FILE_READ_ATTRIBUTES`, which opens a file only to ask about it.
8 public generic_read: u32 = 0x80000000
9 --- `GENERIC_WRITE`: the right to write.
10 public generic_write: u32 = 0x40000000
11 --- `FILE_READ_ATTRIBUTES`: the right to ask about a file, alone.
12 public file_read_attributes: u32 = 0x80
13 --- `FILE_WRITE_ATTRIBUTES`: the right to change what is kept of a file.
14 public file_write_attributes: u32 = 0x100
15
16 --- `FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE`: other opens
17 --- may read, write, rename and delete the file while this one is open,
18 --- as on a POSIX system.
19 public share_all: u32 = 7
20
21 --- `CreateFileW`'s dispositions.
22 public create_new: u32 = 1
23 --- `CREATE_ALWAYS`: make the file, or empty one that is there.
24 public create_always: u32 = 2
25 --- `OPEN_EXISTING`: open the file only if it is there.
26 public open_existing: u32 = 3
27 --- `OPEN_ALWAYS`: open the file, making it when it is not there.
28 public open_always: u32 = 4
29 --- `TRUNCATE_EXISTING`: empty the file, which must be there.
30 public truncate_existing: u32 = 5
31
32 --- `CreateFileW`'s attributes and flags: a plain file,
33 --- `FILE_FLAG_BACKUP_SEMANTICS`, which a directory is opened with,
34 --- `FILE_FLAG_OVERLAPPED`, `FILE_FLAG_WRITE_THROUGH`, and
35 --- `FILE_FLAG_OPEN_REPARSE_POINT`, which opens a link rather than what it
36 --- names.
37 public attribute_normal: u32 = 0x80
38 --- `FILE_FLAG_BACKUP_SEMANTICS`: needed to open a directory.
39 public flag_backup_semantics: u32 = 0x02000000
40 --- `FILE_FLAG_OVERLAPPED`: calls on the handle may end later.
41 public flag_overlapped: u32 = 0x40000000
42 --- `FILE_FLAG_OPEN_REPARSE_POINT`: open a link, not what it names.
43 public flag_open_reparse_point: u32 = 0x00200000
44
45 --- A file's attribute bits: read-only, a directory and a reparse point,
46 --- which a symbolic link is; and what `get_file_attributes` gives when it
47 --- fails, all ones.
48 public attribute_read_only: u32 = 0x1
49 --- `FILE_ATTRIBUTE_DIRECTORY`: the file is a directory.
50 public attribute_directory: u32 = 0x10
51 --- `FILE_ATTRIBUTE_REPARSE_POINT`: the file is a link or another reparse
52 --- point.
53 public attribute_reparse_point: u32 = 0x400
54 --- `INVALID_FILE_ATTRIBUTES`: what `get_file_attributes` gives when it
55 --- fails.
56 public invalid_attributes: u32 = 0xFFFFFFFF
57
58 --- Open or make the file `name` names, `CreateFileW`: its handle, or
59 --- `invalid_handle` with the reason in `last_error`. `security` is zero,
60 --- so the handle is not inherited by a child, and `template` is zero.
61 public extern(c) link(symbol: "CreateFileW") create_file:
62 (name: ptr u16, access: u32, share: u32, security: usize,
63 disposition: u32, flags: u32, template: usize) -> (result: handle)
64
65 --- A time as Windows keeps it: hundreds of nanoseconds since 1601,
66 --- `FILETIME`'s two halves.
67 public file_time: type = struct
68 low: u32
69 high: u32
70 end file_time
71
72 --- What `get_file_information` fills: `BY_HANDLE_FILE_INFORMATION`.
73 public file_information: type = struct
74 attributes: u32
75 created: file_time
76 accessed: file_time
77 written: file_time
78 volume: u32
79 size_high: u32
80 size_low: u32
81 links: u32
82 index_high: u32
83 index_low: u32
84 end file_information
85
86 --- Fill `found` with what the system keeps of `opened`,
87 --- `GetFileInformationByHandle`: nonzero, or zero with the reason in
88 --- `last_error`.
89 public extern(c) link(symbol: "GetFileInformationByHandle") get_file_information:
90 (opened: handle, found: ptr mut file_information) -> (done: i32)
91
92 --- A path's attribute bits, `GetFileAttributesW`, or `invalid_attributes`
93 --- with the reason in `last_error`.
94 public extern(c) link(symbol: "GetFileAttributesW") get_file_attributes:
95 (name: ptr u16) -> (attributes: u32)
96
97 --- Set a path's attribute bits, `SetFileAttributesW`: nonzero, or zero
98 --- with the reason in `last_error`.
99 public extern(c) link(symbol: "SetFileAttributesW") set_file_attributes:
100 (name: ptr u16, attributes: u32) -> (done: i32)
101
102 --- Make the directory `name` names, `CreateDirectoryW`, with the
103 --- default security: nonzero, or zero with the reason in `last_error`.
104 public extern(c) link(symbol: "CreateDirectoryW") create_directory:
105 (name: ptr u16, security: usize) -> (done: i32)
106
107 --- Remove the empty directory `name` names, `RemoveDirectoryW`.
108 public extern(c) link(symbol: "RemoveDirectoryW") remove_directory:
109 (name: ptr u16) -> (done: i32)
110
111 --- Remove the file `name` names, `DeleteFileW`. A file another handle
112 --- holds open with `share_all` is removed when the last is closed.
113 public extern(c) link(symbol: "DeleteFileW") delete_file:
114 (name: ptr u16) -> (done: i32)
115
116 --- `MoveFileExW`'s flags: replace a file the new name holds, and do not
117 --- return before the move is on the disk.
118 public move_replace_existing: u32 = 1
119
120 --- Rename `old` to `new`, `MoveFileExW`: nonzero, or zero with the
121 --- reason in `last_error`.
122 public extern(c) link(symbol: "MoveFileExW") move_file:
123 (old: ptr u16, new: ptr u16, flags: u32) -> (done: i32)
124
125 --- The working directory into `into`'s `size` units with a NUL,
126 --- `GetCurrentDirectoryW`: the units written, the NUL not counted; the
127 --- units it needs, the NUL counted, when they do not fit; or zero with
128 --- the reason in `last_error`.
129 public extern(c) link(symbol: "GetCurrentDirectoryW") current_directory:
130 (size: u32, into: ptr mut u16) -> (count: u32)
131
132 --- Move `opened`'s position, `SetFilePointerEx`, from the start, the
133 --- current position or the end, by `distance`; the new position goes to
134 --- `moved` when it is not zero.
135 public file_begin: u32 = 0
136 --- `FILE_CURRENT`: `set_file_pointer` moves from the current position.
137 public file_current: u32 = 1
138 --- Move `opened`'s position by `distance` from where `method` says,
139 --- `SetFilePointerEx`; the new position goes to the `i64` at `moved`
140 --- unless it is zero.
141 public extern(c) link(symbol: "SetFilePointerEx") set_file_pointer:
142 (opened: handle, distance: i64, moved: usize, method: u32)
143 -> (done: i32)
144
145 --- Write what the system holds of `opened` to its disk,
146 --- `FlushFileBuffers`.
147 public extern(c) link(symbol: "FlushFileBuffers") flush_file_buffers:
148 (opened: handle) -> (done: i32)
149
150 --- The record an overlapped call takes: its offset, and the event that
151 --- is set when it ends, or zero. `OVERLAPPED`.
152 public overlapped: type = struct
153 internal: usize
154 internal_high: usize
155 offset_low: u32
156 offset_high: u32
157 event: handle
158 end overlapped
159
160 --- How an overlapped call on `opened` ended, `GetOverlappedResult`: the
161 --- bytes it moved in `moved`, waiting for it when `wait` is nonzero.
162 public extern(c) link(symbol: "GetOverlappedResult") overlapped_result:
163 (opened: handle, record: ptr mut overlapped, moved: ptr mut u32,
164 wait: i32) -> (done: i32)
165
166 --- What `find_first_file` fills for each entry: `WIN32_FIND_DATAW`.
167 public find_data: type = struct
168 attributes: u32
169 created: file_time
170 accessed: file_time
171 written: file_time
172 size_high: u32
173 size_low: u32
174 reserved_0: u32
175 reserved_1: u32
176 name: [260]u16
177 alternate: [14]u16
178 end find_data
179
180 --- Begin a directory's listing, `FindFirstFileW`, with `pattern` such as
181 --- `dir\*`: the search's handle, its first entry in `found`, or
182 --- `invalid_handle` with the reason in `last_error`,
183 --- `error_file_not_found` for an empty match.
184 public extern(c) link(symbol: "FindFirstFileW") find_first_file:
185 (pattern: ptr u16, found: ptr mut find_data) -> (search: handle)
186
187 --- The search's next entry, `FindNextFileW`: nonzero, or zero with
188 --- `error_no_more_files` at the end.
189 public extern(c) link(symbol: "FindNextFileW") find_next_file:
190 (search: handle, found: ptr mut find_data) -> (done: i32)
191
192 --- End a search, `FindClose`.
193 public extern(c) link(symbol: "FindClose") find_close:
194 (search: handle) -> (done: i32)
195
196 --- The mode of a console handle, `GetConsoleMode`: nonzero for a
197 --- console, zero with the reason in `last_error` for anything else.
198 public extern(c) link(symbol: "GetConsoleMode") get_console_mode:
199 (console: handle, mode: ptr mut u32) -> (done: i32)
200
201 --- A console's screen buffer, `CONSOLE_SCREEN_BUFFER_INFO`: its size,
202 --- the cursor, the attributes, the window's corners and its largest
203 --- size, each coordinate 16 bits.
204 public console_screen: type = struct
205 size_x: i16
206 size_y: i16
207 cursor_x: i16
208 cursor_y: i16
209 attributes: u16
210 left: i16
211 top: i16
212 right: i16
213 bottom: i16
214 largest_x: i16
215 largest_y: i16
216 end console_screen
217
218 --- Fill `found` for the console screen buffer `console`,
219 --- `GetConsoleScreenBufferInfo`.
220 public extern(c) link(symbol: "GetConsoleScreenBufferInfo") console_screen_info:
221 (console: handle, found: ptr mut console_screen) -> (done: i32)
222
223 --- `FileEndOfFileInfo`, the class `set_file_information` takes to give
224 --- a file a size without moving its position.
225 public file_end_of_file_info: u32 = 6
226
227 --- Set what the information class `class` says of `opened` from the
228 --- `size` bytes at `information`, `SetFileInformationByHandle`.
229 public extern(c) link(symbol: "SetFileInformationByHandle") set_file_information:
230 (opened: handle, class: u32, information: usize, size: u32)
231 -> (done: i32)
232
233 --- The path `opened` was reached by after every link, into `into`'s
234 --- `size` units with a NUL, `GetFinalPathNameByHandleW`: the units
235 --- written, the NUL not counted, in the `\\?\` form; the units it needs
236 --- when they do not fit; or zero with the reason in `last_error`.
237 public extern(c) link(symbol: "GetFinalPathNameByHandleW") final_path:
238 (opened: handle, into: ptr mut u16, size: u32, flags: u32)
239 -> (count: u32)
240
241 --- What `get_file_attributes_ex` fills: `WIN32_FILE_ATTRIBUTE_DATA`.
242 public attribute_data: type = struct
243 attributes: u32
244 created: file_time
245 accessed: file_time
246 written: file_time
247 size_high: u32
248 size_low: u32
249 end attribute_data
250
251 --- Fill `found` with a path's attributes, times and size, not following
252 --- a link it ends in, `GetFileAttributesExW` with
253 --- `GetFileExInfoStandard`: nonzero, or zero with the reason in
254 --- `last_error`.
255 public extern(c) link(symbol: "GetFileAttributesExW") get_file_attributes_ex:
256 (name: ptr u16, level: u32, found: ptr mut attribute_data)
257 -> (done: i32)
258
259 --- `FILE_LIST_DIRECTORY`, the access a directory is opened to list with.
260 public list_directory: u32 = 1
261
262 --- `FILE_APPEND_DATA` and `SYNCHRONIZE`: an open to append, whose every
263 --- write goes to the file's end.
264 public append_data: u32 = 4
265 --- `SYNCHRONIZE`: the right to wait for the handle.
266 public synchronize_access: u32 = 0x100000
267
268 --- `IO_REPARSE_TAG_SYMLINK`, the tag `find_data`'s `reserved_0` holds
269 --- for a symbolic link.
270 public reparse_tag_symlink: u32 = 0xA000000C
271end if