Repository navigation
Expand file tree
/
Copy pathfs.wave
More file actions
458 lines (425 loc) · 16.9 KB
/
Copy pathfs.wave
File metadata and controls
458 lines (425 loc) · 16.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
// This file is part of the Wave language project.
// Copyright (c) 2024-2026 Wave Foundation
// Copyright (c) 2024-2026 LunaStev and contributors
// SPDX-License-Identifier: Apache-2.0
// Windows filesystem provider. Native HANDLE values are carried as i64 so
// the portable descriptor layer does not truncate them.
import("std::sys::windows::fs_errors")::{windows_fs_error};
import("std::sys::windows::path_encoding")::{WidePath, wide_path, release_path, wide_path_to_utf8};
import("std::sys::windows::memory")::{sys_alloc, sys_free};
extern(system, "CreateFileW") fun win_create_file(
name: ptr<u16>, access: u32, share: u32, security: ptr<i8>, creation: u32,
attributes: u32, template: ptr<i8>
) -> ptr<i8>;
extern(system, "CloseHandle") fun win_close_handle(handle: ptr<i8>) -> i32;
extern(system, "closesocket") fun win_close_socket(socket: i64) -> i32;
extern(system, "ReadFile") fun win_read_file(
handle: ptr<i8>, buffer: ptr<u8>, length: u32, read_count: ptr<u32>, overlapped: ptr<i8>
) -> i32;
extern(system, "WriteFile") fun win_write_file(
handle: ptr<i8>, buffer: ptr<u8>, length: u32, write_count: ptr<u32>, overlapped: ptr<i8>
) -> i32;
extern(system, "FlushFileBuffers") fun win_flush_file_buffers(handle: ptr<i8>) -> i32;
extern(system, "SetFilePointerEx") fun win_set_file_pointer(
handle: ptr<i8>, distance: i64, new_position: ptr<i64>, method: u32
) -> i32;
extern(system, "GetFileType") fun win_get_file_type(handle: ptr<i8>) -> u32;
extern(system, "GetFileInformationByHandle") fun win_get_file_information(
handle: ptr<i8>, data: ptr<WinHandleFileInformation>
) -> i32;
extern(system, "GetFileAttributesW") fun win_get_file_attributes(path: ptr<u16>) -> u32;
extern(system, "GetFileAttributesExW") fun win_get_file_attributes_ex(
path: ptr<u16>, info_level: i32, data: ptr<WinFileAttributeData>
) -> i32;
extern(system, "GetCurrentDirectoryW") fun win_get_current_directory(cap: u32, buffer: ptr<u16>) -> u32;
extern(system, "SetCurrentDirectoryW") fun win_set_current_directory(path: ptr<u16>) -> i32;
extern(system, "DeleteFileW") fun win_delete_file(path: ptr<u16>) -> i32;
extern(system, "CreateDirectoryW") fun win_create_directory(path: ptr<u16>, security: ptr<i8>) -> i32;
extern(system, "RemoveDirectoryW") fun win_remove_directory(path: ptr<u16>) -> i32;
extern(system, "MoveFileExW") fun win_move_file(old_path: ptr<u16>, new_path: ptr<u16>, flags: u32) -> i32;
extern(system, "GetCurrentProcess") fun win_get_current_process() -> ptr<i8>;
extern(system, "GetStdHandle") fun win_get_std_handle(kind: u32) -> ptr<i8>;
extern(system, "DuplicateHandle") fun win_duplicate_handle(
source_process: ptr<i8>, source: ptr<i8>, target_process: ptr<i8>, target: ptr<ptr<i8>>,
access: u32, inherit: i32, options: u32
) -> i32;
pub const FS_O_RDONLY: i32 = 0;
pub const FS_O_WRONLY: i32 = 1;
pub const FS_O_RDWR: i32 = 2;
pub const FS_O_CREAT: i32 = 64;
pub const FS_O_EXCL: i32 = 128;
pub const FS_O_TRUNC: i32 = 512;
pub const FS_O_APPEND: i32 = 1024;
pub const FS_O_NONBLOCK: i32 = 2048;
pub const FS_F_OK: i32 = 0;
pub const FS_X_OK: i32 = 1;
pub const FS_W_OK: i32 = 2;
pub const FS_R_OK: i32 = 4;
pub const FS_SEEK_SET: i32 = 0;
pub const FS_SEEK_CUR: i32 = 1;
pub const FS_SEEK_END: i32 = 2;
pub const FS_F_GETFL: i32 = 3;
pub const FS_F_SETFL: i32 = 4;
const GENERIC_READ: u32 = 2147483648;
const GENERIC_WRITE: u32 = 1073741824;
const FILE_APPEND_DATA: u32 = 4;
const FILE_READ_ATTRIBUTES: u32 = 128;
const SYNCHRONIZE: u32 = 1048576;
const FILE_SHARE_ALL: u32 = 7;
const CREATE_NEW: u32 = 1;
const CREATE_ALWAYS: u32 = 2;
const OPEN_EXISTING: u32 = 3;
const OPEN_ALWAYS: u32 = 4;
const TRUNCATE_EXISTING: u32 = 5;
const FILE_ATTRIBUTE_NORMAL: u32 = 128;
const FILE_ATTRIBUTE_DIRECTORY: u32 = 16;
const INVALID_FILE_ATTRIBUTES: u32 = 4294967295;
const DUPLICATE_SAME_ACCESS: u32 = 2;
const MOVEFILE_REPLACE_EXISTING: u32 = 1;
const SYS_ERR_NOT_IMPLEMENTED: i64 = -38;
const STD_INPUT_HANDLE: u32 = 4294967286;
const STD_OUTPUT_HANDLE: u32 = 4294967285;
const STD_ERROR_HANDLE: u32 = 4294967284;
pub struct Stat {
mode: i16;
size: i64;
mtime: i64;
}
struct WinFileAttributeData {
attributes: u32;
creation_low: u32;
creation_high: u32;
access_low: u32;
access_high: u32;
write_low: u32;
write_high: u32;
size_high: u32;
size_low: u32;
}
// BY_HANDLE_FILE_INFORMATION uses DWORD fields, including both halves of
// FILETIME. Its size is 52 bytes with 4-byte alignment on amd64 and ARM64.
struct WinHandleFileInformation {
attributes: u32;
creation_low: u32;
creation_high: u32;
access_low: u32;
access_high: u32;
write_low: u32;
write_high: u32;
volume: u32;
size_high: u32;
size_low: u32;
links: u32;
index_high: u32;
index_low: u32;
}
fun win_handle_from_fd(fd: i64) -> ptr<i8> {
if (fd == 0) { return win_get_std_handle(STD_INPUT_HANDLE); }
if (fd == 1) { return win_get_std_handle(STD_OUTPUT_HANDLE); }
if (fd == 2) { return win_get_std_handle(STD_ERROR_HANDLE); }
return fd as ptr<i8>;
}
pub fun open(path: str, flags: i32, mode: i32) -> i64 {
var access_mode: i32 = flags & 3;
if (access_mode == 3 || (access_mode == FS_O_RDONLY && (flags & FS_O_TRUNC) != 0)) {
return -22;
}
var append: bool = access_mode != FS_O_RDONLY && (flags & FS_O_APPEND) != 0;
var truncate: bool = (flags & FS_O_TRUNC) != 0;
var native_path: WidePath = wide_path(path);
if (native_path.error != 0) {
return native_path.error;
}
var desired: u32 = GENERIC_READ;
if ((flags & FS_O_RDWR) != 0) {
desired = GENERIC_READ | GENERIC_WRITE;
} else if ((flags & FS_O_WRONLY) != 0) {
desired = GENERIC_WRITE;
}
if (append) {
// FILE_WRITE_DATA (also granted by GENERIC_WRITE) would allow writes
// before EOF. Let the kernel append each write, even after lseek/dup.
desired = FILE_APPEND_DATA | FILE_READ_ATTRIBUTES | SYNCHRONIZE;
if (access_mode == FS_O_RDWR) { desired = desired | GENERIC_READ; }
}
var creation: u32 = OPEN_EXISTING;
if ((flags & FS_O_CREAT) != 0 && (flags & FS_O_EXCL) != 0) {
creation = CREATE_NEW;
} else if ((flags & FS_O_CREAT) != 0 && truncate && !append) {
creation = CREATE_ALWAYS;
} else if ((flags & FS_O_CREAT) != 0) {
creation = OPEN_ALWAYS;
} else if (truncate && !append) {
creation = TRUNCATE_EXISTING;
}
// Append+truncate needs a temporary write-capable handle. Obtain the
// restricted final handle before truncating so duplication failure leaves
// existing data intact. Both handles refer to the same opened file.
var open_access: u32 = desired;
if (append && truncate) { open_access = open_access | GENERIC_WRITE; }
var handle: ptr<i8> = win_create_file(
native_path.data, open_access, FILE_SHARE_ALL, null, creation, FILE_ATTRIBUTE_NORMAL, null
);
if ((handle as i64) == -1) {
return release_path(native_path, windows_fs_error(win_file_last_error()));
}
if (append && truncate) {
var current: ptr<i8> = win_get_current_process();
var restricted: ptr<i8> = null;
if (win_duplicate_handle(current, handle, current, &restricted, desired, 0, 0) == 0) {
var error_code: u32 = win_file_last_error();
win_close_handle(handle);
return release_path(native_path, windows_fs_error(error_code));
}
var status: i64 = ftruncate(handle as i64, 0);
win_close_handle(handle);
if (status != 0) {
win_close_handle(restricted);
return release_path(native_path, status);
}
handle = restricted;
}
return release_path(native_path, handle as i64);
}
pub fun close(fd: i64) -> i64 {
if (win_close_handle(win_handle_from_fd(fd)) != 0) { return 0; }
var error_code: u32 = win_file_last_error();
if (win_close_socket(fd) == 0) { return 0; }
return windows_fs_error(error_code);
}
pub fun dup(fd: i64) -> i64 {
var current: ptr<i8> = win_get_current_process();
var duplicate: ptr<i8> = null;
if (win_duplicate_handle(current, win_handle_from_fd(fd), current, &duplicate, 0, 0, DUPLICATE_SAME_ACCESS) == 0) {
return windows_fs_error(win_file_last_error());
}
return duplicate as i64;
}
pub fun dup2(oldfd: i64, newfd: i64) -> i64 { return SYS_ERR_NOT_IMPLEMENTED; }
pub fun pipe(fds: ptr<i32>) -> i64 { return SYS_ERR_NOT_IMPLEMENTED; }
pub fun fsync(fd: i64) -> i64 {
if (win_flush_file_buffers(win_handle_from_fd(fd)) == 0) { return windows_fs_error(win_file_last_error()); }
return 0;
}
pub fun fcntl(fd: i64, cmd: i32, arg: i64) -> i64 { return SYS_ERR_NOT_IMPLEMENTED; }
pub fun read(fd: i64, buf: ptr<u8>, len: i64) -> i64 {
if (buf == null || len < 0 || len > 4294967295) { return -22; }
var count: u32 = 0;
if (win_read_file(win_handle_from_fd(fd), buf, len as u32, &count, null) == 0) {
var error_code: u32 = win_file_last_error();
if (error_code == 38 || error_code == 109) {
return 0;
}
return windows_fs_error(error_code);
}
return count as i64;
}
pub fun write(fd: i64, buf: ptr<u8>, len: i64) -> i64 {
if (buf == null || len < 0 || len > 4294967295) { return -22; }
var count: u32 = 0;
if (win_write_file(win_handle_from_fd(fd), buf, len as u32, &count, null) == 0) { return windows_fs_error(win_file_last_error()); }
return count as i64;
}
pub fun getcwd(buf: ptr<u8>, size: i64) -> i64 {
if (buf == null || size <= 0) {
return -22;
}
var capacity: u32 = win_get_current_directory(0, null);
if (capacity == 0) {
return windows_fs_error(win_file_last_error());
}
if (capacity > 2147483647) {
return -36;
}
var bytes: i64 = (capacity as i64) * 2;
var native: ptr<u16> = sys_alloc(bytes) as ptr<u16>;
if (native == null) {
return -12;
}
var count: u32 = win_get_current_directory(capacity, native);
var status: i64 = 0;
if (count == 0) {
status = windows_fs_error(win_file_last_error());
} else if (count >= capacity) {
status = -34;
} else {
status = wide_path_to_utf8(native, count as i32, buf, size);
}
sys_free(native as ptr<u8>, bytes);
return status;
}
pub fun chdir(path: str) -> i64 {
var native: WidePath = wide_path(path);
if (native.error != 0) {
return native.error;
}
if (win_set_current_directory(native.data) == 0) {
return release_path(native, windows_fs_error(win_file_last_error()));
}
return release_path(native, 0);
}
pub fun access(path: str, mode: i32) -> i64 {
if ((mode & ~7) != 0) { return -22; }
if ((mode & FS_X_OK) != 0) { return -95; }
var native: WidePath = wide_path(path);
if (native.error != 0) {
return native.error;
}
var attributes: u32 = win_get_file_attributes(native.data);
if (attributes == INVALID_FILE_ATTRIBUTES) {
return release_path(native, windows_fs_error(win_file_last_error()));
}
if (mode == FS_F_OK) { return release_path(native, 0); }
// Readonly applies to file contents, not the directory attribute hint.
if ((mode & FS_W_OK) != 0 && (attributes & FILE_ATTRIBUTE_DIRECTORY) == 0 && (attributes & 1) != 0) {
return release_path(native, -13);
}
var rights: u32 = 0;
if ((mode & FS_R_OK) != 0) { rights = rights | GENERIC_READ; }
if ((mode & FS_W_OK) != 0) { rights = rights | GENERIC_WRITE; }
// OPEN_EXISTING never creates or truncates; BACKUP_SEMANTICS permits directories.
var handle: ptr<i8> = win_create_file(native.data, rights, FILE_SHARE_ALL, null, OPEN_EXISTING, 33554432, null);
if (handle == (-1 as ptr<i8>)) {
return release_path(native, windows_fs_error(win_file_last_error()));
}
if (win_close_handle(handle) == 0) {
return release_path(native, windows_fs_error(win_file_last_error()));
}
return release_path(native, 0);
}
pub fun lseek(fd: i64, offset: i64, whence: i32) -> i64 {
var position: i64 = 0;
if (win_set_file_pointer(win_handle_from_fd(fd), offset, &position, whence as u32) == 0) { return windows_fs_error(win_file_last_error()); }
return position;
}
pub fun unlink(path: str) -> i64 {
var native: WidePath = wide_path(path);
if (native.error != 0) {
return native.error;
}
if (win_delete_file(native.data) == 0) {
return release_path(native, windows_fs_error(win_file_last_error()));
}
return release_path(native, 0);
}
pub fun mkdir(path: str, mode: i32) -> i64 {
var native: WidePath = wide_path(path);
if (native.error != 0) {
return native.error;
}
if (win_create_directory(native.data, null) == 0) {
return release_path(native, windows_fs_error(win_file_last_error()));
}
return release_path(native, 0);
}
pub fun rmdir(path: str) -> i64 {
var native: WidePath = wide_path(path);
if (native.error != 0) {
return native.error;
}
if (win_remove_directory(native.data) == 0) {
return release_path(native, windows_fs_error(win_file_last_error()));
}
return release_path(native, 0);
}
pub fun rename_path(old_path: str, new_path: str) -> i64 {
var old_native: WidePath = wide_path(old_path);
if (old_native.error != 0) {
return old_native.error;
}
var new_native: WidePath = wide_path(new_path);
if (new_native.error != 0) {
return release_path(old_native, new_native.error);
}
var status: i64 = 0;
if (win_move_file(old_native.data, new_native.data, MOVEFILE_REPLACE_EXISTING) == 0) {
status = windows_fs_error(win_file_last_error());
}
release_path(new_native, status);
return release_path(old_native, status);
}
pub fun fstat(fd: i64, st: ptr<Stat>) -> i64 {
if (st == null) { return -22; }
var handle: ptr<i8> = win_handle_from_fd(fd);
var kind: u32 = win_get_file_type(handle);
// FILE_TYPE_DISK includes directories. Pipes/consoles do not supply this
// metadata contract; never invent a regular-file mode or timestamp.
if (kind == 0) { return windows_fs_error(win_file_last_error()); }
if (kind != 1) { return -95; }
var native: WinHandleFileInformation;
if (win_get_file_information(handle, &native) == 0) { return windows_fs_error(win_file_last_error()); }
deref st.size = ((native.size_high as u64 << 32) | native.size_low as u64) as i64;
var write_ticks: u64 = (native.write_high as u64 << 32) | native.write_low as u64;
deref st.mtime = (write_ticks / 10000000) as i64 - 11644473600;
if ((native.attributes & FILE_ATTRIBUTE_DIRECTORY) != 0) {
deref st.mode = 16384 as i16;
deref st.size = 0;
} else {
deref st.mode = 32768 as i16;
}
return 0;
}
pub fun stat(path: str, st: ptr<Stat>) -> i64 {
if (st == null) { return -22; }
var native_path: WidePath = wide_path(path);
if (native_path.error != 0) {
return native_path.error;
}
var native: WinFileAttributeData;
if (win_get_file_attributes_ex(native_path.data, 0, &native) == 0) { return release_path(native_path, windows_fs_error(win_file_last_error())); }
deref st.size = ((native.size_high as u64 << 32) | native.size_low as u64) as i64;
var write_ticks: u64 = (native.write_high as u64 << 32) | native.write_low as u64;
deref st.mtime = (write_ticks / 10000000) as i64 - 11644473600;
if ((native.attributes & FILE_ATTRIBUTE_DIRECTORY) != 0) {
deref st.mode = 16384 as i16;
deref st.size = 0;
} else {
deref st.mode = 32768 as i16;
}
return release_path(native_path, 0);
}
// FILE_ID_INFO retains the full 128-bit ID, including ReFS identities.
struct WinFileIdInfo {
volume: u64;
id: array<u8, 16>;
}
extern(system, "GetFileInformationByHandleEx") fun win_file_id(
handle: ptr<i8>, info_class: i32, info: ptr<WinFileIdInfo>, size: u32
) -> i32;
extern(system, "SetFileInformationByHandle") fun win_set_file_end(
handle: ptr<i8>, info_class: i32, info: ptr<i64>, size: u32
) -> i32;
extern(system, "GetLastError") fun win_file_last_error() -> u32;
pub fun same_file(first_fd: i64, second_fd: i64) -> i64 {
var first: WinFileIdInfo;
var second: WinFileIdInfo;
// FileIdInfo = 18; FILE_ID_INFO is 24 bytes on both 64-bit targets.
if (win_file_id(win_handle_from_fd(first_fd), 18, &first, 24) == 0) {
return windows_fs_error(win_file_last_error());
}
if (win_file_id(win_handle_from_fd(second_fd), 18, &second, 24) == 0) {
return windows_fs_error(win_file_last_error());
}
if (first.volume != second.volume) {
return 0;
}
var index: i64 = 0;
while (index < 16) {
if (first.id[index] != second.id[index]) {
return 0;
}
index += 1;
}
return 1;
}
pub fun ftruncate(fd: i64, length: i64) -> i64 {
if (length < 0) {
return -22;
}
var end: i64 = length;
// FileEndOfFileInfo = 6; setting EOF does not move the file pointer.
if (win_set_file_end(win_handle_from_fd(fd), 6, &end, 8) == 0) {
return windows_fs_error(win_file_last_error());
}
return 0;
}