| author | |
| committer | |
| log | ea1b21dbdb3d5e680b133be68d174dcc0067fa1e |
| tree | 88114bd3c7235e8b0478a1e98ddc48b25d013a95 |
| parent | 51852d2587b931767a12d42ce39d5c191eea10ea |
* error.BadFd is not a valid error code. it would always be a bug to
get this error code.
* merge error.Io with existing error.InputOutput
* merge error.PathNotFound with existing error.FileNotFound.
Not all OS's support both.
* add os.File.openReadC
* add error.BadPathName for windows file operations with invalid
characters
* add os.toPosixPath to help stack allocate a null terminating byte
* add some TODOs for other functions to investigate removing the
allocator requirement
* optimize some implementations to use the alternate functions when
a null byte is already available
* add a missing error.SkipZigTest
* os.selfExePath uses a non-allocating API
* os.selfExeDirPath uses a non-allocating API
* os.path.real uses a non-allocating API
* add os.path.realAlloc and os.path.realC
* convert many windows syscalls to use the W versions (See #534)17 files changed, 319 insertions(+), 288 deletions(-)
doc/docgen.zig+3-3| ... | ... | @@ -34,10 +34,10 @@ pub fn main() !void { |
| 34 | 34 | const out_file_name = try (args_it.next(allocator) orelse @panic("expected output arg")); |
| 35 | 35 | defer allocator.free(out_file_name); |
| 36 | 36 | |
| 37 | var in_file = try os.File.openRead(allocator, in_file_name); | |
| 37 | var in_file = try os.File.openRead(in_file_name); | |
| 38 | 38 | defer in_file.close(); |
| 39 | 39 | |
| 40 | var out_file = try os.File.openWrite(allocator, out_file_name); | |
| 40 | var out_file = try os.File.openWrite(out_file_name); | |
| 41 | 41 | defer out_file.close(); |
| 42 | 42 | |
| 43 | 43 | var file_in_stream = io.FileInStream.init(&in_file); |
| ... | ... | @@ -738,7 +738,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var |
| 738 | 738 | try out.print("<pre><code class=\"zig\">{}</code></pre>", escaped_source); |
| 739 | 739 | const name_plus_ext = try std.fmt.allocPrint(allocator, "{}.zig", code.name); |
| 740 | 740 | const tmp_source_file_name = try os.path.join(allocator, tmp_dir_name, name_plus_ext); |
| 741 | try io.writeFile(allocator, tmp_source_file_name, trimmed_raw_source); | |
| 741 | try io.writeFile(tmp_source_file_name, trimmed_raw_source); | |
| 742 | 742 | |
| 743 | 743 | switch (code.id) { |
| 744 | 744 | Code.Id.Exe => |expected_outcome| { |
example/cat/main.zig+1-1| ... | ... | @@ -20,7 +20,7 @@ pub fn main() !void { |
| 20 | 20 | } else if (arg[0] == '-') { |
| 21 | 21 | return usage(exe); |
| 22 | 22 | } else { |
| 23 | var file = os.File.openRead(allocator, arg) catch |err| { | |
| 23 | var file = os.File.openRead(arg) catch |err| { | |
| 24 | 24 | warn("Unable to open file: {}\n", @errorName(err)); |
| 25 | 25 | return err; |
| 26 | 26 | }; |
src-self-hosted/compilation.zig+1-4| ... | ... | @@ -257,8 +257,6 @@ pub const Compilation = struct { |
| 257 | 257 | pub const BuildError = error{ |
| 258 | 258 | OutOfMemory, |
| 259 | 259 | EndOfStream, |
| 260 | BadFd, | |
| 261 | Io, | |
| 262 | 260 | IsDir, |
| 263 | 261 | Unexpected, |
| 264 | 262 | SystemResources, |
| ... | ... | @@ -273,7 +271,6 @@ pub const Compilation = struct { |
| 273 | 271 | NameTooLong, |
| 274 | 272 | SystemFdQuotaExceeded, |
| 275 | 273 | NoDevice, |
| 276 | PathNotFound, | |
| 277 | 274 | NoSpaceLeft, |
| 278 | 275 | NotDir, |
| 279 | 276 | FileSystem, |
| ... | ... | @@ -962,7 +959,7 @@ pub const Compilation = struct { |
| 962 | 959 | if (self.root_src_path) |root_src_path| { |
| 963 | 960 | const root_scope = blk: { |
| 964 | 961 | // TODO async/await os.path.real |
| 965 | const root_src_real_path = os.path.real(self.gpa(), root_src_path) catch |err| { | |
| 962 | const root_src_real_path = os.path.realAlloc(self.gpa(), root_src_path) catch |err| { | |
| 966 | 963 | try self.addCompileErrorCli(root_src_path, "unable to open: {}", @errorName(err)); |
| 967 | 964 | return; |
| 968 | 965 | }; |
src-self-hosted/introspect.zig+1-1| ... | ... | @@ -22,7 +22,7 @@ pub fn testZigInstallPrefix(allocator: *mem.Allocator, test_path: []const u8) ![ |
| 22 | 22 | |
| 23 | 23 | /// Caller must free result |
| 24 | 24 | pub fn findZigLibDir(allocator: *mem.Allocator) ![]u8 { |
| 25 | const self_exe_path = try os.selfExeDirPath(allocator); | |
| 25 | const self_exe_path = try os.selfExeDirPathAlloc(allocator); | |
| 26 | 26 | defer allocator.free(self_exe_path); |
| 27 | 27 | |
| 28 | 28 | var cur_path: []const u8 = self_exe_path; |
src-self-hosted/libc_installation.zig+1-1| ... | ... | @@ -453,7 +453,7 @@ fn fileExists(path: []const u8) !bool { |
| 453 | 453 | if (std.os.File.access(path)) |_| { |
| 454 | 454 | return true; |
| 455 | 455 | } else |err| switch (err) { |
| 456 | error.FileNotFound, error.PathNotFound, error.PermissionDenied => return false, | |
| 456 | error.FileNotFound, error.PermissionDenied => return false, | |
| 457 | 457 | else => return error.FileSystem, |
| 458 | 458 | } |
| 459 | 459 | } |
std/build.zig+12-6| ... | ... | @@ -1491,11 +1491,14 @@ pub const LibExeObjStep = struct { |
| 1491 | 1491 | } |
| 1492 | 1492 | |
| 1493 | 1493 | if (!is_darwin) { |
| 1494 | const rpath_arg = builder.fmt("-Wl,-rpath,{}", os.path.real(builder.allocator, builder.pathFromRoot(builder.cache_root)) catch unreachable); | |
| 1494 | const rpath_arg = builder.fmt("-Wl,-rpath,{}", try os.path.realAlloc( | |
| 1495 | builder.allocator, | |
| 1496 | builder.pathFromRoot(builder.cache_root), | |
| 1497 | )); | |
| 1495 | 1498 | defer builder.allocator.free(rpath_arg); |
| 1496 | cc_args.append(rpath_arg) catch unreachable; | |
| 1499 | try cc_args.append(rpath_arg); | |
| 1497 | 1500 | |
| 1498 | cc_args.append("-rdynamic") catch unreachable; | |
| 1501 | try cc_args.append("-rdynamic"); | |
| 1499 | 1502 | } |
| 1500 | 1503 | |
| 1501 | 1504 | for (self.full_path_libs.toSliceConst()) |full_path_lib| { |
| ... | ... | @@ -1566,11 +1569,14 @@ pub const LibExeObjStep = struct { |
| 1566 | 1569 | cc_args.append("-o") catch unreachable; |
| 1567 | 1570 | cc_args.append(output_path) catch unreachable; |
| 1568 | 1571 | |
| 1569 | const rpath_arg = builder.fmt("-Wl,-rpath,{}", os.path.real(builder.allocator, builder.pathFromRoot(builder.cache_root)) catch unreachable); | |
| 1572 | const rpath_arg = builder.fmt("-Wl,-rpath,{}", try os.path.realAlloc( | |
| 1573 | builder.allocator, | |
| 1574 | builder.pathFromRoot(builder.cache_root), | |
| 1575 | )); | |
| 1570 | 1576 | defer builder.allocator.free(rpath_arg); |
| 1571 | cc_args.append(rpath_arg) catch unreachable; | |
| 1577 | try cc_args.append(rpath_arg); | |
| 1572 | 1578 | |
| 1573 | cc_args.append("-rdynamic") catch unreachable; | |
| 1579 | try cc_args.append("-rdynamic"); | |
| 1574 | 1580 | |
| 1575 | 1581 | { |
| 1576 | 1582 | var it = self.link_libs.iterator(); |
std/debug/index.zig+1-1| ... | ... | @@ -255,7 +255,7 @@ pub fn printSourceAtAddress(debug_info: *ElfStackTrace, out_stream: var, address |
| 255 | 255 | address, |
| 256 | 256 | compile_unit_name, |
| 257 | 257 | ); |
| 258 | if (printLineFromFile(debug_info.allocator(), out_stream, line_info)) { | |
| 258 | if (printLineFromFile(out_stream, line_info)) { | |
| 259 | 259 | if (line_info.column == 0) { |
| 260 | 260 | try out_stream.write("\n"); |
| 261 | 261 | } else { |
std/event/fs.zig+24-32| ... | ... | @@ -78,8 +78,7 @@ pub async fn pwritev(loop: *Loop, fd: os.FileHandle, data: []const []const u8, o |
| 78 | 78 | builtin.Os.macosx, |
| 79 | 79 | builtin.Os.linux, |
| 80 | 80 | => return await (async pwritevPosix(loop, fd, data, offset) catch unreachable), |
| 81 | builtin.Os.windows, | |
| 82 | => return await (async pwritevWindows(loop, fd, data, offset) catch unreachable), | |
| 81 | builtin.Os.windows => return await (async pwritevWindows(loop, fd, data, offset) catch unreachable), | |
| 83 | 82 | else => @compileError("Unsupported OS"), |
| 84 | 83 | } |
| 85 | 84 | } |
| ... | ... | @@ -147,7 +146,6 @@ pub async fn pwriteWindows(loop: *Loop, fd: os.FileHandle, data: []const u8, off |
| 147 | 146 | } |
| 148 | 147 | } |
| 149 | 148 | |
| 150 | ||
| 151 | 149 | /// data - just the inner references - must live until pwritev promise completes. |
| 152 | 150 | pub async fn pwritevPosix(loop: *Loop, fd: os.FileHandle, data: []const []const u8, offset: usize) !void { |
| 153 | 151 | // workaround for https://github.com/ziglang/zig/issues/1194 |
| ... | ... | @@ -203,8 +201,7 @@ pub async fn preadv(loop: *Loop, fd: os.FileHandle, data: []const []u8, offset: |
| 203 | 201 | builtin.Os.macosx, |
| 204 | 202 | builtin.Os.linux, |
| 205 | 203 | => return await (async preadvPosix(loop, fd, data, offset) catch unreachable), |
| 206 | builtin.Os.windows, | |
| 207 | => return await (async preadvWindows(loop, fd, data, offset) catch unreachable), | |
| 204 | builtin.Os.windows => return await (async preadvWindows(loop, fd, data, offset) catch unreachable), | |
| 208 | 205 | else => @compileError("Unsupported OS"), |
| 209 | 206 | } |
| 210 | 207 | } |
| ... | ... | @@ -222,7 +219,7 @@ pub async fn preadvWindows(loop: *Loop, fd: os.FileHandle, data: []const []u8, o |
| 222 | 219 | var inner_off: usize = 0; |
| 223 | 220 | while (true) { |
| 224 | 221 | const v = data_copy[iov_i]; |
| 225 | const amt_read = try await (async preadWindows(loop, fd, v[inner_off .. v.len-inner_off], offset + off) catch unreachable); | |
| 222 | const amt_read = try await (async preadWindows(loop, fd, v[inner_off .. v.len - inner_off], offset + off) catch unreachable); | |
| 226 | 223 | off += amt_read; |
| 227 | 224 | inner_off += amt_read; |
| 228 | 225 | if (inner_off == v.len) { |
| ... | ... | @@ -340,8 +337,7 @@ pub async fn openPosix( |
| 340 | 337 | resume @handle(); |
| 341 | 338 | } |
| 342 | 339 | |
| 343 | const path_with_null = try std.cstr.addNullByte(loop.allocator, path); | |
| 344 | defer loop.allocator.free(path_with_null); | |
| 340 | const path_c = try std.os.toPosixPath(path); | |
| 345 | 341 | |
| 346 | 342 | var req_node = RequestNode{ |
| 347 | 343 | .prev = null, |
| ... | ... | @@ -349,7 +345,7 @@ pub async fn openPosix( |
| 349 | 345 | .data = Request{ |
| 350 | 346 | .msg = Request.Msg{ |
| 351 | 347 | .Open = Request.Msg.Open{ |
| 352 | .path = path_with_null[0..path.len], | |
| 348 | .path = path_c[0..path.len], | |
| 353 | 349 | .flags = flags, |
| 354 | 350 | .mode = mode, |
| 355 | 351 | .result = undefined, |
| ... | ... | @@ -408,8 +404,7 @@ pub async fn openWriteMode(loop: *Loop, path: []const u8, mode: os.File.Mode) os |
| 408 | 404 | const flags = posix.O_LARGEFILE | posix.O_WRONLY | posix.O_CREAT | posix.O_CLOEXEC | posix.O_TRUNC; |
| 409 | 405 | return await (async openPosix(loop, path, flags, os.File.default_mode) catch unreachable); |
| 410 | 406 | }, |
| 411 | builtin.Os.windows, | |
| 412 | => return os.windowsOpen( | |
| 407 | builtin.Os.windows => return os.windowsOpen( | |
| 413 | 408 | path, |
| 414 | 409 | windows.GENERIC_WRITE, |
| 415 | 410 | windows.FILE_SHARE_WRITE | windows.FILE_SHARE_READ | windows.FILE_SHARE_DELETE, |
| ... | ... | @@ -434,7 +429,7 @@ pub async fn openReadWrite( |
| 434 | 429 | |
| 435 | 430 | builtin.Os.windows => return os.windowsOpen( |
| 436 | 431 | path, |
| 437 | windows.GENERIC_WRITE|windows.GENERIC_READ, | |
| 432 | windows.GENERIC_WRITE | windows.GENERIC_READ, | |
| 438 | 433 | windows.FILE_SHARE_WRITE | windows.FILE_SHARE_READ | windows.FILE_SHARE_DELETE, |
| 439 | 434 | windows.OPEN_ALWAYS, |
| 440 | 435 | windows.FILE_ATTRIBUTE_NORMAL | windows.FILE_FLAG_OVERLAPPED, |
| ... | ... | @@ -510,8 +505,7 @@ pub const CloseOperation = struct { |
| 510 | 505 | self.loop.allocator.destroy(self); |
| 511 | 506 | } |
| 512 | 507 | }, |
| 513 | builtin.Os.windows, | |
| 514 | => { | |
| 508 | builtin.Os.windows => { | |
| 515 | 509 | if (self.os_data.handle) |handle| { |
| 516 | 510 | os.close(handle); |
| 517 | 511 | } |
| ... | ... | @@ -529,8 +523,7 @@ pub const CloseOperation = struct { |
| 529 | 523 | self.os_data.close_req_node.data.msg.Close.fd = handle; |
| 530 | 524 | self.os_data.have_fd = true; |
| 531 | 525 | }, |
| 532 | builtin.Os.windows, | |
| 533 | => { | |
| 526 | builtin.Os.windows => { | |
| 534 | 527 | self.os_data.handle = handle; |
| 535 | 528 | }, |
| 536 | 529 | else => @compileError("Unsupported OS"), |
| ... | ... | @@ -545,8 +538,7 @@ pub const CloseOperation = struct { |
| 545 | 538 | => { |
| 546 | 539 | self.os_data.have_fd = false; |
| 547 | 540 | }, |
| 548 | builtin.Os.windows, | |
| 549 | => { | |
| 541 | builtin.Os.windows => { | |
| 550 | 542 | self.os_data.handle = null; |
| 551 | 543 | }, |
| 552 | 544 | else => @compileError("Unsupported OS"), |
| ... | ... | @@ -561,8 +553,7 @@ pub const CloseOperation = struct { |
| 561 | 553 | assert(self.os_data.have_fd); |
| 562 | 554 | return self.os_data.close_req_node.data.msg.Close.fd; |
| 563 | 555 | }, |
| 564 | builtin.Os.windows, | |
| 565 | => { | |
| 556 | builtin.Os.windows => { | |
| 566 | 557 | return self.os_data.handle.?; |
| 567 | 558 | }, |
| 568 | 559 | else => @compileError("Unsupported OS"), |
| ... | ... | @@ -582,8 +573,7 @@ pub async fn writeFileMode(loop: *Loop, path: []const u8, contents: []const u8, |
| 582 | 573 | builtin.Os.linux, |
| 583 | 574 | builtin.Os.macosx, |
| 584 | 575 | => return await (async writeFileModeThread(loop, path, contents, mode) catch unreachable), |
| 585 | builtin.Os.windows, | |
| 586 | => return await (async writeFileWindows(loop, path, contents) catch unreachable), | |
| 576 | builtin.Os.windows => return await (async writeFileWindows(loop, path, contents) catch unreachable), | |
| 587 | 577 | else => @compileError("Unsupported OS"), |
| 588 | 578 | } |
| 589 | 579 | } |
| ... | ... | @@ -1000,7 +990,7 @@ pub fn Watch(comptime V: type) type { |
| 1000 | 990 | const basename_utf16le_null = try std.unicode.utf8ToUtf16LeWithNull(self.channel.loop.allocator, basename); |
| 1001 | 991 | var basename_utf16le_null_consumed = false; |
| 1002 | 992 | defer if (!basename_utf16le_null_consumed) self.channel.loop.allocator.free(basename_utf16le_null); |
| 1003 | const basename_utf16le_no_null = basename_utf16le_null[0..basename_utf16le_null.len-1]; | |
| 993 | const basename_utf16le_no_null = basename_utf16le_null[0 .. basename_utf16le_null.len - 1]; | |
| 1004 | 994 | |
| 1005 | 995 | const dir_handle = windows.CreateFileW( |
| 1006 | 996 | dirname_utf16le.ptr, |
| ... | ... | @@ -1014,9 +1004,8 @@ pub fn Watch(comptime V: type) type { |
| 1014 | 1004 | if (dir_handle == windows.INVALID_HANDLE_VALUE) { |
| 1015 | 1005 | const err = windows.GetLastError(); |
| 1016 | 1006 | switch (err) { |
| 1017 | windows.ERROR.FILE_NOT_FOUND, | |
| 1018 | windows.ERROR.PATH_NOT_FOUND, | |
| 1019 | => return error.PathNotFound, | |
| 1007 | windows.ERROR.FILE_NOT_FOUND => return error.FileNotFound, | |
| 1008 | windows.ERROR.PATH_NOT_FOUND => return error.FileNotFound, | |
| 1020 | 1009 | else => return os.unexpectedErrorWindows(err), |
| 1021 | 1010 | } |
| 1022 | 1011 | } |
| ... | ... | @@ -1102,7 +1091,10 @@ pub fn Watch(comptime V: type) type { |
| 1102 | 1091 | |
| 1103 | 1092 | // TODO handle this error not in the channel but in the setup |
| 1104 | 1093 | _ = os.windowsCreateIoCompletionPort( |
| 1105 | dir_handle, self.channel.loop.os_data.io_port, completion_key, undefined, | |
| 1094 | dir_handle, | |
| 1095 | self.channel.loop.os_data.io_port, | |
| 1096 | completion_key, | |
| 1097 | undefined, | |
| 1106 | 1098 | ) catch |err| { |
| 1107 | 1099 | await (async self.channel.put(err) catch unreachable); |
| 1108 | 1100 | return; |
| ... | ... | @@ -1122,10 +1114,10 @@ pub fn Watch(comptime V: type) type { |
| 1122 | 1114 | &event_buf, |
| 1123 | 1115 | @intCast(windows.DWORD, event_buf.len), |
| 1124 | 1116 | windows.FALSE, // watch subtree |
| 1125 | windows.FILE_NOTIFY_CHANGE_FILE_NAME | windows.FILE_NOTIFY_CHANGE_DIR_NAME | | |
| 1126 | windows.FILE_NOTIFY_CHANGE_ATTRIBUTES | windows.FILE_NOTIFY_CHANGE_SIZE | | |
| 1127 | windows.FILE_NOTIFY_CHANGE_LAST_WRITE | windows.FILE_NOTIFY_CHANGE_LAST_ACCESS | | |
| 1128 | windows.FILE_NOTIFY_CHANGE_CREATION | windows.FILE_NOTIFY_CHANGE_SECURITY, | |
| 1117 | windows.FILE_NOTIFY_CHANGE_FILE_NAME | windows.FILE_NOTIFY_CHANGE_DIR_NAME | | |
| 1118 | windows.FILE_NOTIFY_CHANGE_ATTRIBUTES | windows.FILE_NOTIFY_CHANGE_SIZE | | |
| 1119 | windows.FILE_NOTIFY_CHANGE_LAST_WRITE | windows.FILE_NOTIFY_CHANGE_LAST_ACCESS | | |
| 1120 | windows.FILE_NOTIFY_CHANGE_CREATION | windows.FILE_NOTIFY_CHANGE_SECURITY, | |
| 1129 | 1121 | null, // number of bytes transferred (unused for async) |
| 1130 | 1122 | &overlapped, |
| 1131 | 1123 | null, // completion routine - unused because we use IOCP |
| ... | ... | @@ -1152,7 +1144,7 @@ pub fn Watch(comptime V: type) type { |
| 1152 | 1144 | else => null, |
| 1153 | 1145 | }; |
| 1154 | 1146 | if (emit) |id| { |
| 1155 | const basename_utf16le = ([*]u16)(&ev.FileName)[0..ev.FileNameLength/2]; | |
| 1147 | const basename_utf16le = ([*]u16)(&ev.FileName)[0 .. ev.FileNameLength / 2]; | |
| 1156 | 1148 | const user_value = blk: { |
| 1157 | 1149 | const held = await (async dir.table_lock.acquire() catch unreachable); |
| 1158 | 1150 | defer held.release(); |
std/os/child_process.zig+1-8| ... | ... | @@ -349,14 +349,7 @@ pub const ChildProcess = struct { |
| 349 | 349 | }; |
| 350 | 350 | |
| 351 | 351 | const any_ignore = (self.stdin_behavior == StdIo.Ignore or self.stdout_behavior == StdIo.Ignore or self.stderr_behavior == StdIo.Ignore); |
| 352 | const dev_null_fd = if (any_ignore) blk: { | |
| 353 | const dev_null_path = "/dev/null"; | |
| 354 | var fixed_buffer_mem: [dev_null_path.len + 1]u8 = undefined; | |
| 355 | var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]); | |
| 356 | break :blk try os.posixOpen(&fixed_allocator.allocator, "/dev/null", posix.O_RDWR, 0); | |
| 357 | } else blk: { | |
| 358 | break :blk undefined; | |
| 359 | }; | |
| 352 | const dev_null_fd = if (any_ignore) try os.posixOpenC(c"/dev/null", posix.O_RDWR, 0) else undefined; | |
| 360 | 353 | defer { |
| 361 | 354 | if (any_ignore) os.close(dev_null_fd); |
| 362 | 355 | } |
std/os/file.zig+40-23| ... | ... | @@ -28,13 +28,26 @@ pub const File = struct { |
| 28 | 28 | |
| 29 | 29 | pub const OpenError = os.WindowsOpenError || os.PosixOpenError; |
| 30 | 30 | |
| 31 | /// Call close to clean up. | |
| 32 | pub fn openRead(path: []const u8) OpenError!File { | |
| 31 | /// `openRead` except with a null terminated path | |
| 32 | pub fn openReadC(path: [*]const u8) OpenError!File { | |
| 33 | 33 | if (is_posix) { |
| 34 | 34 | const flags = posix.O_LARGEFILE | posix.O_RDONLY; |
| 35 | const fd = try os.posixOpen(path, flags, 0); | |
| 35 | const fd = try os.posixOpenC(path, flags, 0); | |
| 36 | 36 | return openHandle(fd); |
| 37 | } else if (is_windows) { | |
| 37 | } | |
| 38 | if (is_windows) { | |
| 39 | return openRead(mem.toSliceConst(u8, path)); | |
| 40 | } | |
| 41 | @compileError("Unsupported OS"); | |
| 42 | } | |
| 43 | ||
| 44 | /// Call close to clean up. | |
| 45 | pub fn openRead(path: []const u8) OpenError!File { | |
| 46 | if (is_posix) { | |
| 47 | const path_c = try os.toPosixPath(path); | |
| 48 | return openReadC(&path_c); | |
| 49 | } | |
| 50 | if (is_windows) { | |
| 38 | 51 | const handle = try os.windowsOpen( |
| 39 | 52 | path, |
| 40 | 53 | windows.GENERIC_READ, |
| ... | ... | @@ -43,9 +56,8 @@ pub const File = struct { |
| 43 | 56 | windows.FILE_ATTRIBUTE_NORMAL, |
| 44 | 57 | ); |
| 45 | 58 | return openHandle(handle); |
| 46 | } else { | |
| 47 | @compileError("TODO implement openRead for this OS"); | |
| 48 | 59 | } |
| 60 | @compileError("Unsupported OS"); | |
| 49 | 61 | } |
| 50 | 62 | |
| 51 | 63 | /// Calls `openWriteMode` with os.File.default_mode for the mode. |
| ... | ... | @@ -103,13 +115,11 @@ pub const File = struct { |
| 103 | 115 | |
| 104 | 116 | pub const AccessError = error{ |
| 105 | 117 | PermissionDenied, |
| 106 | PathNotFound, | |
| 107 | 118 | FileNotFound, |
| 108 | 119 | NameTooLong, |
| 109 | BadMode, | |
| 110 | BadPathName, | |
| 111 | Io, | |
| 120 | InputOutput, | |
| 112 | 121 | SystemResources, |
| 122 | BadPathName, | |
| 113 | 123 | |
| 114 | 124 | /// On Windows, file paths must be valid Unicode. |
| 115 | 125 | InvalidUtf8, |
| ... | ... | @@ -127,7 +137,7 @@ pub const File = struct { |
| 127 | 137 | const err = windows.GetLastError(); |
| 128 | 138 | switch (err) { |
| 129 | 139 | windows.ERROR.FILE_NOT_FOUND => return error.FileNotFound, |
| 130 | windows.ERROR.PATH_NOT_FOUND => return error.PathNotFound, | |
| 140 | windows.ERROR.PATH_NOT_FOUND => return error.FileNotFound, | |
| 131 | 141 | windows.ERROR.ACCESS_DENIED => return error.PermissionDenied, |
| 132 | 142 | else => return os.unexpectedErrorWindows(err), |
| 133 | 143 | } |
| ... | ... | @@ -149,13 +159,13 @@ pub const File = struct { |
| 149 | 159 | posix.EROFS => return error.PermissionDenied, |
| 150 | 160 | posix.ELOOP => return error.PermissionDenied, |
| 151 | 161 | posix.ETXTBSY => return error.PermissionDenied, |
| 152 | posix.ENOTDIR => return error.NotFound, | |
| 153 | posix.ENOENT => return error.NotFound, | |
| 162 | posix.ENOTDIR => return error.FileNotFound, | |
| 163 | posix.ENOENT => return error.FileNotFound, | |
| 154 | 164 | |
| 155 | 165 | posix.ENAMETOOLONG => return error.NameTooLong, |
| 156 | 166 | posix.EINVAL => unreachable, |
| 157 | posix.EFAULT => return error.BadPathName, | |
| 158 | posix.EIO => return error.Io, | |
| 167 | posix.EFAULT => unreachable, | |
| 168 | posix.EIO => return error.InputOutput, | |
| 159 | 169 | posix.ENOMEM => return error.SystemResources, |
| 160 | 170 | else => return os.unexpectedErrorPosix(err), |
| 161 | 171 | } |
| ... | ... | @@ -197,7 +207,9 @@ pub const File = struct { |
| 197 | 207 | const err = posix.getErrno(result); |
| 198 | 208 | if (err > 0) { |
| 199 | 209 | return switch (err) { |
| 200 | posix.EBADF => error.BadFd, | |
| 210 | // We do not make this an error code because if you get EBADF it's always a bug, | |
| 211 | // since the fd could have been reused. | |
| 212 | posix.EBADF => unreachable, | |
| 201 | 213 | posix.EINVAL => error.Unseekable, |
| 202 | 214 | posix.EOVERFLOW => error.Unseekable, |
| 203 | 215 | posix.ESPIPE => error.Unseekable, |
| ... | ... | @@ -210,7 +222,7 @@ pub const File = struct { |
| 210 | 222 | if (windows.SetFilePointerEx(self.handle, amount, null, windows.FILE_CURRENT) == 0) { |
| 211 | 223 | const err = windows.GetLastError(); |
| 212 | 224 | return switch (err) { |
| 213 | windows.ERROR.INVALID_PARAMETER => error.BadFd, | |
| 225 | windows.ERROR.INVALID_PARAMETER => unreachable, | |
| 214 | 226 | else => os.unexpectedErrorWindows(err), |
| 215 | 227 | }; |
| 216 | 228 | } |
| ... | ... | @@ -227,7 +239,9 @@ pub const File = struct { |
| 227 | 239 | const err = posix.getErrno(result); |
| 228 | 240 | if (err > 0) { |
| 229 | 241 | return switch (err) { |
| 230 | posix.EBADF => error.BadFd, | |
| 242 | // We do not make this an error code because if you get EBADF it's always a bug, | |
| 243 | // since the fd could have been reused. | |
| 244 | posix.EBADF => unreachable, | |
| 231 | 245 | posix.EINVAL => error.Unseekable, |
| 232 | 246 | posix.EOVERFLOW => error.Unseekable, |
| 233 | 247 | posix.ESPIPE => error.Unseekable, |
| ... | ... | @@ -241,7 +255,7 @@ pub const File = struct { |
| 241 | 255 | if (windows.SetFilePointerEx(self.handle, ipos, null, windows.FILE_BEGIN) == 0) { |
| 242 | 256 | const err = windows.GetLastError(); |
| 243 | 257 | return switch (err) { |
| 244 | windows.ERROR.INVALID_PARAMETER => error.BadFd, | |
| 258 | windows.ERROR.INVALID_PARAMETER => unreachable, | |
| 245 | 259 | else => os.unexpectedErrorWindows(err), |
| 246 | 260 | }; |
| 247 | 261 | } |
| ... | ... | @@ -257,7 +271,9 @@ pub const File = struct { |
| 257 | 271 | const err = posix.getErrno(result); |
| 258 | 272 | if (err > 0) { |
| 259 | 273 | return switch (err) { |
| 260 | posix.EBADF => error.BadFd, | |
| 274 | // We do not make this an error code because if you get EBADF it's always a bug, | |
| 275 | // since the fd could have been reused. | |
| 276 | posix.EBADF => unreachable, | |
| 261 | 277 | posix.EINVAL => error.Unseekable, |
| 262 | 278 | posix.EOVERFLOW => error.Unseekable, |
| 263 | 279 | posix.ESPIPE => error.Unseekable, |
| ... | ... | @@ -272,7 +288,7 @@ pub const File = struct { |
| 272 | 288 | if (windows.SetFilePointerEx(self.handle, 0, &pos, windows.FILE_CURRENT) == 0) { |
| 273 | 289 | const err = windows.GetLastError(); |
| 274 | 290 | return switch (err) { |
| 275 | windows.ERROR.INVALID_PARAMETER => error.BadFd, | |
| 291 | windows.ERROR.INVALID_PARAMETER => unreachable, | |
| 276 | 292 | else => os.unexpectedErrorWindows(err), |
| 277 | 293 | }; |
| 278 | 294 | } |
| ... | ... | @@ -305,7 +321,6 @@ pub const File = struct { |
| 305 | 321 | } |
| 306 | 322 | |
| 307 | 323 | pub const ModeError = error{ |
| 308 | BadFd, | |
| 309 | 324 | SystemResources, |
| 310 | 325 | Unexpected, |
| 311 | 326 | }; |
| ... | ... | @@ -316,7 +331,9 @@ pub const File = struct { |
| 316 | 331 | const err = posix.getErrno(posix.fstat(self.handle, &stat)); |
| 317 | 332 | if (err > 0) { |
| 318 | 333 | return switch (err) { |
| 319 | posix.EBADF => error.BadFd, | |
| 334 | // We do not make this an error code because if you get EBADF it's always a bug, | |
| 335 | // since the fd could have been reused. | |
| 336 | posix.EBADF => unreachable, | |
| 320 | 337 | posix.ENOMEM => error.SystemResources, |
| 321 | 338 | else => os.unexpectedErrorPosix(err), |
| 322 | 339 | }; |
std/os/index.zig+79-97| ... | ... | @@ -436,7 +436,7 @@ pub const PosixOpenError = error{ |
| 436 | 436 | NameTooLong, |
| 437 | 437 | SystemFdQuotaExceeded, |
| 438 | 438 | NoDevice, |
| 439 | PathNotFound, | |
| 439 | FileNotFound, | |
| 440 | 440 | SystemResources, |
| 441 | 441 | NoSpaceLeft, |
| 442 | 442 | NotDir, |
| ... | ... | @@ -450,11 +450,8 @@ pub const PosixOpenError = error{ |
| 450 | 450 | /// Calls POSIX open, keeps trying if it gets interrupted, and translates |
| 451 | 451 | /// the return value into zig errors. |
| 452 | 452 | pub fn posixOpen(file_path: []const u8, flags: u32, perm: usize) PosixOpenError!i32 { |
| 453 | var path_with_null: [posix.PATH_MAX]u8 = undefined; | |
| 454 | if (file_path.len >= posix.PATH_MAX) return error.NameTooLong; | |
| 455 | mem.copy(u8, path_with_null[0..], file_path); | |
| 456 | path_with_null[file_path.len] = 0; | |
| 457 | return posixOpenC(&path_with_null, flags, perm); | |
| 453 | const file_path_c = try toPosixPath(file_path); | |
| 454 | return posixOpenC(&file_path_c, flags, perm); | |
| 458 | 455 | } |
| 459 | 456 | |
| 460 | 457 | // TODO https://github.com/ziglang/zig/issues/265 |
| ... | ... | @@ -476,7 +473,7 @@ pub fn posixOpenC(file_path: [*]const u8, flags: u32, perm: usize) !i32 { |
| 476 | 473 | posix.ENAMETOOLONG => return PosixOpenError.NameTooLong, |
| 477 | 474 | posix.ENFILE => return PosixOpenError.SystemFdQuotaExceeded, |
| 478 | 475 | posix.ENODEV => return PosixOpenError.NoDevice, |
| 479 | posix.ENOENT => return PosixOpenError.PathNotFound, | |
| 476 | posix.ENOENT => return PosixOpenError.FileNotFound, | |
| 480 | 477 | posix.ENOMEM => return PosixOpenError.SystemResources, |
| 481 | 478 | posix.ENOSPC => return PosixOpenError.NoSpaceLeft, |
| 482 | 479 | posix.ENOTDIR => return PosixOpenError.NotDir, |
| ... | ... | @@ -489,6 +486,16 @@ pub fn posixOpenC(file_path: [*]const u8, flags: u32, perm: usize) !i32 { |
| 489 | 486 | } |
| 490 | 487 | } |
| 491 | 488 | |
| 489 | /// Used to convert a slice to a null terminated slice on the stack. | |
| 490 | /// TODO well defined copy elision | |
| 491 | pub fn toPosixPath(file_path: []const u8) ![posix.PATH_MAX]u8 { | |
| 492 | var path_with_null: [posix.PATH_MAX]u8 = undefined; | |
| 493 | if (file_path.len >= posix.PATH_MAX) return error.NameTooLong; | |
| 494 | mem.copy(u8, path_with_null[0..], file_path); | |
| 495 | path_with_null[file_path.len] = 0; | |
| 496 | return path_with_null; | |
| 497 | } | |
| 498 | ||
| 492 | 499 | pub fn posixDup2(old_fd: i32, new_fd: i32) !void { |
| 493 | 500 | while (true) { |
| 494 | 501 | const err = posix.getErrno(posix.dup2(old_fd, new_fd)); |
| ... | ... | @@ -742,7 +749,6 @@ pub fn getCwdAlloc(allocator: *Allocator) ![]u8 { |
| 742 | 749 | pub const GetCwdError = error{Unexpected}; |
| 743 | 750 | |
| 744 | 751 | /// The result is a slice of out_buffer. |
| 745 | /// TODO with well defined copy elision we could make the API of this function better. | |
| 746 | 752 | pub fn getCwd(out_buffer: *[MAX_PATH_BYTES]u8) GetCwdError![]u8 { |
| 747 | 753 | switch (builtin.os) { |
| 748 | 754 | Os.windows => { |
| ... | ... | @@ -960,11 +966,8 @@ pub fn deleteFilePosixC(file_path: [*]const u8) !void { |
| 960 | 966 | } |
| 961 | 967 | |
| 962 | 968 | pub fn deleteFilePosix(file_path: []const u8) !void { |
| 963 | var path_with_null: [posix.PATH_MAX]u8 = undefined; | |
| 964 | if (file_path.len >= posix.PATH_MAX) return error.NameTooLong; | |
| 965 | mem.copy(u8, path_with_null[0..], file_path); | |
| 966 | path_with_null[file_path.len] = 0; | |
| 967 | return deleteFilePosixC(&path_with_null); | |
| 969 | const file_path_c = try toPosixPath(file_path); | |
| 970 | return deleteFilePosixC(&file_path_c); | |
| 968 | 971 | } |
| 969 | 972 | |
| 970 | 973 | /// Guaranteed to be atomic. However until https://patchwork.kernel.org/patch/9636735/ is |
| ... | ... | @@ -1120,17 +1123,9 @@ pub fn rename(old_path: []const u8, new_path: []const u8) !void { |
| 1120 | 1123 | } |
| 1121 | 1124 | } |
| 1122 | 1125 | } else { |
| 1123 | var old_path_with_null: [posix.PATH_MAX]u8 = undefined; | |
| 1124 | if (old_path.len >= posix.PATH_MAX) return error.NameTooLong; | |
| 1125 | mem.copy(u8, old_path_with_null[0..], old_path); | |
| 1126 | old_path_with_null[old_path.len] = 0; | |
| 1127 | ||
| 1128 | var new_path_with_null: [posix.PATH_MAX]u8 = undefined; | |
| 1129 | if (new_path.len >= posix.PATH_MAX) return error.NameTooLong; | |
| 1130 | mem.copy(u8, new_path_with_null[0..], new_path); | |
| 1131 | new_path_with_null[new_path.len] = 0; | |
| 1132 | ||
| 1133 | return renameC(&old_path_with_null, &new_path_with_null); | |
| 1126 | const old_path_c = try toPosixPath(old_path); | |
| 1127 | const new_path_c = try toPosixPath(new_path); | |
| 1128 | return renameC(&old_path_c, &new_path_c); | |
| 1134 | 1129 | } |
| 1135 | 1130 | } |
| 1136 | 1131 | |
| ... | ... | @@ -1156,7 +1151,7 @@ pub fn makeDirWindows(dir_path: []const u8) !void { |
| 1156 | 1151 | } |
| 1157 | 1152 | |
| 1158 | 1153 | pub fn makeDirPosixC(dir_path: [*]const u8) !void { |
| 1159 | const err = posix.getErrno(posix.mkdir(path_buf.ptr, 0o755)); | |
| 1154 | const err = posix.getErrno(posix.mkdir(dir_path, 0o755)); | |
| 1160 | 1155 | switch (err) { |
| 1161 | 1156 | 0 => return, |
| 1162 | 1157 | posix.EACCES => return error.AccessDenied, |
| ... | ... | @@ -1177,11 +1172,8 @@ pub fn makeDirPosixC(dir_path: [*]const u8) !void { |
| 1177 | 1172 | } |
| 1178 | 1173 | |
| 1179 | 1174 | pub fn makeDirPosix(dir_path: []const u8) !void { |
| 1180 | var path_with_null: [posix.PATH_MAX]u8 = undefined; | |
| 1181 | if (dir_path.len >= posix.PATH_MAX) return error.NameTooLong; | |
| 1182 | mem.copy(u8, path_with_null[0..], dir_path); | |
| 1183 | path_with_null[dir_path.len] = 0; | |
| 1184 | return makeDirPosixC(&path_with_null); | |
| 1175 | const dir_path_c = try toPosixPath(dir_path); | |
| 1176 | return makeDirPosixC(&dir_path_c); | |
| 1185 | 1177 | } |
| 1186 | 1178 | |
| 1187 | 1179 | /// Calls makeDir recursively to make an entire path. Returns success if the path |
| ... | ... | @@ -1290,7 +1282,6 @@ const DeleteTreeError = error{ |
| 1290 | 1282 | NameTooLong, |
| 1291 | 1283 | SystemFdQuotaExceeded, |
| 1292 | 1284 | NoDevice, |
| 1293 | PathNotFound, | |
| 1294 | 1285 | SystemResources, |
| 1295 | 1286 | NoSpaceLeft, |
| 1296 | 1287 | PathAlreadyExists, |
| ... | ... | @@ -1354,7 +1345,7 @@ pub fn deleteTree(allocator: *Allocator, full_path: []const u8) DeleteTreeError! |
| 1354 | 1345 | error.NameTooLong, |
| 1355 | 1346 | error.SystemFdQuotaExceeded, |
| 1356 | 1347 | error.NoDevice, |
| 1357 | error.PathNotFound, | |
| 1348 | error.FileNotFound, | |
| 1358 | 1349 | error.SystemResources, |
| 1359 | 1350 | error.NoSpaceLeft, |
| 1360 | 1351 | error.PathAlreadyExists, |
| ... | ... | @@ -1424,7 +1415,7 @@ pub const Dir = struct { |
| 1424 | 1415 | }; |
| 1425 | 1416 | |
| 1426 | 1417 | pub const OpenError = error{ |
| 1427 | PathNotFound, | |
| 1418 | FileNotFound, | |
| 1428 | 1419 | NotDir, |
| 1429 | 1420 | AccessDenied, |
| 1430 | 1421 | FileTooBig, |
| ... | ... | @@ -1443,6 +1434,7 @@ pub const Dir = struct { |
| 1443 | 1434 | Unexpected, |
| 1444 | 1435 | }; |
| 1445 | 1436 | |
| 1437 | /// TODO remove the allocator requirement from this API | |
| 1446 | 1438 | pub fn open(allocator: *Allocator, dir_path: []const u8) OpenError!Dir { |
| 1447 | 1439 | return Dir{ |
| 1448 | 1440 | .allocator = allocator, |
| ... | ... | @@ -1458,7 +1450,6 @@ pub const Dir = struct { |
| 1458 | 1450 | }, |
| 1459 | 1451 | Os.macosx, Os.ios => Handle{ |
| 1460 | 1452 | .fd = try posixOpen( |
| 1461 | allocator, | |
| 1462 | 1453 | dir_path, |
| 1463 | 1454 | posix.O_RDONLY | posix.O_NONBLOCK | posix.O_DIRECTORY | posix.O_CLOEXEC, |
| 1464 | 1455 | 0, |
| ... | ... | @@ -1470,7 +1461,6 @@ pub const Dir = struct { |
| 1470 | 1461 | }, |
| 1471 | 1462 | Os.linux => Handle{ |
| 1472 | 1463 | .fd = try posixOpen( |
| 1473 | allocator, | |
| 1474 | 1464 | dir_path, |
| 1475 | 1465 | posix.O_RDONLY | posix.O_DIRECTORY | posix.O_CLOEXEC, |
| 1476 | 1466 | 0, |
| ... | ... | @@ -1668,12 +1658,12 @@ pub fn changeCurDir(allocator: *Allocator, dir_path: []const u8) !void { |
| 1668 | 1658 | |
| 1669 | 1659 | /// Read value of a symbolic link. |
| 1670 | 1660 | /// The return value is a slice of out_buffer. |
| 1671 | pub fn readLinkC(pathname: [*]const u8, out_buffer: *[posix.PATH_MAX]u8) ![]u8 { | |
| 1661 | pub fn readLinkC(out_buffer: *[posix.PATH_MAX]u8, pathname: [*]const u8) ![]u8 { | |
| 1672 | 1662 | const rc = posix.readlink(pathname, out_buffer, out_buffer.len); |
| 1673 | 1663 | const err = posix.getErrno(rc); |
| 1674 | 1664 | switch (err) { |
| 1675 | 1665 | 0 => return out_buffer[0..rc], |
| 1676 | posix.EACCES => error.AccessDenied, | |
| 1666 | posix.EACCES => return error.AccessDenied, | |
| 1677 | 1667 | posix.EFAULT => unreachable, |
| 1678 | 1668 | posix.EINVAL => unreachable, |
| 1679 | 1669 | posix.EIO => return error.FileSystem, |
| ... | ... | @@ -1688,12 +1678,9 @@ pub fn readLinkC(pathname: [*]const u8, out_buffer: *[posix.PATH_MAX]u8) ![]u8 { |
| 1688 | 1678 | |
| 1689 | 1679 | /// Read value of a symbolic link. |
| 1690 | 1680 | /// The return value is a slice of out_buffer. |
| 1691 | pub fn readLink(file_path: []const u8, out_buffer: *[posix.PATH_MAX]u8) ![]u8 { | |
| 1692 | var path_with_null: [posix.PATH_MAX]u8 = undefined; | |
| 1693 | if (file_path.len >= posix.PATH_MAX) return error.NameTooLong; | |
| 1694 | mem.copy(u8, path_with_null[0..], file_path); | |
| 1695 | path_with_null[file_path.len] = 0; | |
| 1696 | return readLinkC(&path_with_null, out_buffer); | |
| 1681 | pub fn readLink(out_buffer: *[posix.PATH_MAX]u8, file_path: []const u8) ![]u8 { | |
| 1682 | const file_path_c = try toPosixPath(file_path); | |
| 1683 | return readLinkC(out_buffer, &file_path_c); | |
| 1697 | 1684 | } |
| 1698 | 1685 | |
| 1699 | 1686 | pub fn posix_setuid(uid: u32) !void { |
| ... | ... | @@ -2080,17 +2067,12 @@ pub fn unexpectedErrorWindows(err: windows.DWORD) UnexpectedError { |
| 2080 | 2067 | |
| 2081 | 2068 | pub fn openSelfExe() !os.File { |
| 2082 | 2069 | switch (builtin.os) { |
| 2083 | Os.linux => { | |
| 2084 | const proc_file_path = "/proc/self/exe"; | |
| 2085 | var fixed_buffer_mem: [proc_file_path.len + 1]u8 = undefined; | |
| 2086 | var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]); | |
| 2087 | return os.File.openRead(&fixed_allocator.allocator, proc_file_path); | |
| 2088 | }, | |
| 2070 | Os.linux => return os.File.openReadC(c"/proc/self/exe"), | |
| 2089 | 2071 | Os.macosx, Os.ios => { |
| 2090 | var fixed_buffer_mem: [darwin.PATH_MAX * 2]u8 = undefined; | |
| 2091 | var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]); | |
| 2092 | const self_exe_path = try selfExePath(&fixed_allocator.allocator); | |
| 2093 | return os.File.openRead(&fixed_allocator.allocator, self_exe_path); | |
| 2072 | var buf: [MAX_PATH_BYTES]u8 = undefined; | |
| 2073 | const self_exe_path = try selfExePath(&buf); | |
| 2074 | buf[self_exe_path.len] = 0; | |
| 2075 | return os.File.openReadC(self_exe_path.ptr); | |
| 2094 | 2076 | }, |
| 2095 | 2077 | else => @compileError("Unsupported OS"), |
| 2096 | 2078 | } |
| ... | ... | @@ -2099,7 +2081,7 @@ pub fn openSelfExe() !os.File { |
| 2099 | 2081 | test "openSelfExe" { |
| 2100 | 2082 | switch (builtin.os) { |
| 2101 | 2083 | Os.linux, Os.macosx, Os.ios => (try openSelfExe()).close(), |
| 2102 | else => return, // Unsupported OS. | |
| 2084 | else => return error.SkipZigTest, // Unsupported OS | |
| 2103 | 2085 | } |
| 2104 | 2086 | } |
| 2105 | 2087 | |
| ... | ... | @@ -2108,69 +2090,67 @@ test "openSelfExe" { |
| 2108 | 2090 | /// If you only want an open file handle, use openSelfExe. |
| 2109 | 2091 | /// This function may return an error if the current executable |
| 2110 | 2092 | /// was deleted after spawning. |
| 2111 | /// Caller owns returned memory. | |
| 2112 | pub fn selfExePath(allocator: *mem.Allocator) ![]u8 { | |
| 2093 | /// Returned value is a slice of out_buffer. | |
| 2094 | /// | |
| 2095 | /// On Linux, depends on procfs being mounted. If the currently executing binary has | |
| 2096 | /// been deleted, the file path looks something like `/a/b/c/exe (deleted)`. | |
| 2097 | pub fn selfExePath(out_buffer: *[MAX_PATH_BYTES]u8) ![]u8 { | |
| 2113 | 2098 | switch (builtin.os) { |
| 2114 | Os.linux => { | |
| 2115 | // If the currently executing binary has been deleted, | |
| 2116 | // the file path looks something like `/a/b/c/exe (deleted)` | |
| 2117 | return readLink(allocator, "/proc/self/exe"); | |
| 2118 | }, | |
| 2099 | Os.linux => return readLink(out_buffer, "/proc/self/exe"), | |
| 2119 | 2100 | Os.windows => { |
| 2120 | var out_path = try Buffer.initSize(allocator, 0xff); | |
| 2121 | errdefer out_path.deinit(); | |
| 2122 | while (true) { | |
| 2123 | const dword_len = try math.cast(windows.DWORD, out_path.len()); | |
| 2124 | const copied_amt = windows.GetModuleFileNameA(null, out_path.ptr(), dword_len); | |
| 2125 | if (copied_amt <= 0) { | |
| 2126 | const err = windows.GetLastError(); | |
| 2127 | return switch (err) { | |
| 2128 | else => unexpectedErrorWindows(err), | |
| 2129 | }; | |
| 2130 | } | |
| 2131 | if (copied_amt < out_path.len()) { | |
| 2132 | out_path.shrink(copied_amt); | |
| 2133 | return out_path.toOwnedSlice(); | |
| 2101 | var utf16le_buf: [windows_util.PATH_MAX_WIDE]u16 = undefined; | |
| 2102 | const casted_len = @intCast(windows.DWORD, utf16le_buf.len); // TODO shouldn't need this cast | |
| 2103 | const rc = windows.GetModuleFileNameW(null, &utf16le_buf, casted_len); | |
| 2104 | assert(rc <= utf16le_buf.len); | |
| 2105 | if (rc == 0) { | |
| 2106 | const err = windows.GetLastError(); | |
| 2107 | switch (err) { | |
| 2108 | else => return unexpectedErrorWindows(err), | |
| 2134 | 2109 | } |
| 2135 | const new_len = (out_path.len() << 1) | 0b1; | |
| 2136 | try out_path.resize(new_len); | |
| 2137 | 2110 | } |
| 2111 | const utf16le_slice = utf16le_buf[0..rc]; | |
| 2112 | // Trust that Windows gives us valid UTF-16LE. | |
| 2113 | const end_index = std.unicode.utf16leToUtf8(out_buffer, utf16le_slice) catch unreachable; | |
| 2114 | return out_buffer[0..end_index]; | |
| 2138 | 2115 | }, |
| 2139 | 2116 | Os.macosx, Os.ios => { |
| 2140 | var u32_len: u32 = 0; | |
| 2141 | const ret1 = c._NSGetExecutablePath(undefined, &u32_len); | |
| 2142 | assert(ret1 != 0); | |
| 2143 | const bytes = try allocator.alloc(u8, u32_len); | |
| 2144 | errdefer allocator.free(bytes); | |
| 2145 | const ret2 = c._NSGetExecutablePath(bytes.ptr, &u32_len); | |
| 2146 | assert(ret2 == 0); | |
| 2147 | return bytes; | |
| 2117 | var u32_len: u32 = @intCast(u32, out_buffer.len); // TODO shouldn't need this cast | |
| 2118 | const rc = c._NSGetExecutablePath(out_buffer, &u32_len); | |
| 2119 | if (rc != 0) return error.NameTooLong; | |
| 2120 | return out_buffer[0..u32_len]; | |
| 2148 | 2121 | }, |
| 2149 | 2122 | else => @compileError("Unsupported OS"), |
| 2150 | 2123 | } |
| 2151 | 2124 | } |
| 2152 | 2125 | |
| 2153 | /// Get the directory path that contains the current executable. | |
| 2126 | /// `selfExeDirPath` except allocates the result on the heap. | |
| 2154 | 2127 | /// Caller owns returned memory. |
| 2155 | pub fn selfExeDirPath(allocator: *mem.Allocator) ![]u8 { | |
| 2128 | pub fn selfExeDirPathAlloc(allocator: *Allocator) ![]u8 { | |
| 2129 | var buf: [MAX_PATH_BYTES]u8 = undefined; | |
| 2130 | return mem.dupe(allocator, u8, try selfExeDirPath(&buf)); | |
| 2131 | } | |
| 2132 | ||
| 2133 | /// Get the directory path that contains the current executable. | |
| 2134 | /// Returned value is a slice of out_buffer. | |
| 2135 | pub fn selfExeDirPath(out_buffer: *[MAX_PATH_BYTES]u8) ![]const u8 { | |
| 2156 | 2136 | switch (builtin.os) { |
| 2157 | 2137 | Os.linux => { |
| 2158 | 2138 | // If the currently executing binary has been deleted, |
| 2159 | 2139 | // the file path looks something like `/a/b/c/exe (deleted)` |
| 2160 | 2140 | // This path cannot be opened, but it's valid for determining the directory |
| 2161 | 2141 | // the executable was in when it was run. |
| 2162 | const full_exe_path = try readLink(allocator, "/proc/self/exe"); | |
| 2163 | errdefer allocator.free(full_exe_path); | |
| 2164 | const dir = path.dirname(full_exe_path) orelse "."; | |
| 2165 | return allocator.shrink(u8, full_exe_path, dir.len); | |
| 2142 | const full_exe_path = try readLinkC(out_buffer, c"/proc/self/exe"); | |
| 2143 | // Assume that /proc/self/exe has an absolute path, and therefore dirname | |
| 2144 | // will not return null. | |
| 2145 | return path.dirname(full_exe_path).?; | |
| 2166 | 2146 | }, |
| 2167 | 2147 | Os.windows, Os.macosx, Os.ios => { |
| 2168 | const self_exe_path = try selfExePath(allocator); | |
| 2169 | errdefer allocator.free(self_exe_path); | |
| 2170 | const dirname = os.path.dirname(self_exe_path) orelse "."; | |
| 2171 | return allocator.shrink(u8, self_exe_path, dirname.len); | |
| 2148 | const self_exe_path = try selfExePath(out_buffer); | |
| 2149 | // Assume that the OS APIs return absolute paths, and therefore dirname | |
| 2150 | // will not return null. | |
| 2151 | return path.dirname(self_exe_path).?; | |
| 2172 | 2152 | }, |
| 2173 | else => @compileError("unimplemented: std.os.selfExeDirPath for " ++ @tagName(builtin.os)), | |
| 2153 | else => @compileError("Unsupported OS"), | |
| 2174 | 2154 | } |
| 2175 | 2155 | } |
| 2176 | 2156 | |
| ... | ... | @@ -2991,7 +2971,9 @@ pub fn posixFStat(fd: i32) !posix.Stat { |
| 2991 | 2971 | const err = posix.getErrno(posix.fstat(fd, &stat)); |
| 2992 | 2972 | if (err > 0) { |
| 2993 | 2973 | return switch (err) { |
| 2994 | posix.EBADF => error.BadFd, | |
| 2974 | // We do not make this an error code because if you get EBADF it's always a bug, | |
| 2975 | // since the fd could have been reused. | |
| 2976 | posix.EBADF => unreachable, | |
| 2995 | 2977 | posix.ENOMEM => error.SystemResources, |
| 2996 | 2978 | else => os.unexpectedErrorPosix(err), |
| 2997 | 2979 | }; |
std/os/path.zig+127-91| ... | ... | @@ -11,6 +11,7 @@ const math = std.math; |
| 11 | 11 | const posix = os.posix; |
| 12 | 12 | const windows = os.windows; |
| 13 | 13 | const cstr = std.cstr; |
| 14 | const windows_util = @import("windows/util.zig"); | |
| 14 | 15 | |
| 15 | 16 | pub const sep_windows = '\\'; |
| 16 | 17 | pub const sep_posix = '/'; |
| ... | ... | @@ -1075,113 +1076,148 @@ fn testRelativeWindows(from: []const u8, to: []const u8, expected_output: []cons |
| 1075 | 1076 | assert(mem.eql(u8, result, expected_output)); |
| 1076 | 1077 | } |
| 1077 | 1078 | |
| 1078 | /// Return the canonicalized absolute pathname. | |
| 1079 | /// Expands all symbolic links and resolves references to `.`, `..`, and | |
| 1080 | /// extra `/` characters in ::pathname. | |
| 1081 | /// Caller must deallocate result. | |
| 1082 | /// TODO rename this to realAlloc and provide real with no allocator. See #1392 | |
| 1083 | pub fn real(allocator: *Allocator, pathname: []const u8) ![]u8 { | |
| 1084 | switch (builtin.os) { | |
| 1085 | Os.windows => { | |
| 1086 | const pathname_buf = try allocator.alloc(u8, pathname.len + 1); | |
| 1087 | defer allocator.free(pathname_buf); | |
| 1088 | ||
| 1089 | mem.copy(u8, pathname_buf, pathname); | |
| 1090 | pathname_buf[pathname.len] = 0; | |
| 1091 | ||
| 1092 | const h_file = windows.CreateFileA(pathname_buf.ptr, windows.GENERIC_READ, windows.FILE_SHARE_READ, null, windows.OPEN_EXISTING, windows.FILE_ATTRIBUTE_NORMAL, null); | |
| 1093 | if (h_file == windows.INVALID_HANDLE_VALUE) { | |
| 1094 | const err = windows.GetLastError(); | |
| 1095 | return switch (err) { | |
| 1096 | windows.ERROR.FILE_NOT_FOUND => error.FileNotFound, | |
| 1097 | windows.ERROR.ACCESS_DENIED => error.AccessDenied, | |
| 1098 | windows.ERROR.FILENAME_EXCED_RANGE => error.NameTooLong, | |
| 1099 | else => os.unexpectedErrorWindows(err), | |
| 1100 | }; | |
| 1101 | } | |
| 1102 | defer os.close(h_file); | |
| 1103 | var buf = try allocator.alloc(u8, 256); | |
| 1104 | errdefer allocator.free(buf); | |
| 1105 | while (true) { | |
| 1106 | const buf_len = math.cast(windows.DWORD, buf.len) catch return error.NameTooLong; | |
| 1107 | const result = windows.GetFinalPathNameByHandleA(h_file, buf.ptr, buf_len, windows.VOLUME_NAME_DOS); | |
| 1108 | ||
| 1109 | if (result == 0) { | |
| 1110 | const err = windows.GetLastError(); | |
| 1111 | return switch (err) { | |
| 1112 | windows.ERROR.PATH_NOT_FOUND => error.FileNotFound, | |
| 1113 | windows.ERROR.NOT_ENOUGH_MEMORY => error.OutOfMemory, | |
| 1114 | windows.ERROR.INVALID_PARAMETER => unreachable, | |
| 1115 | else => os.unexpectedErrorWindows(err), | |
| 1116 | }; | |
| 1117 | } | |
| 1079 | pub const RealError = error{ | |
| 1080 | FileNotFound, | |
| 1081 | AccessDenied, | |
| 1082 | NameTooLong, | |
| 1083 | NotSupported, | |
| 1084 | NotDir, | |
| 1085 | SymLinkLoop, | |
| 1086 | InputOutput, | |
| 1087 | FileTooBig, | |
| 1088 | IsDir, | |
| 1089 | ProcessFdQuotaExceeded, | |
| 1090 | SystemFdQuotaExceeded, | |
| 1091 | NoDevice, | |
| 1092 | SystemResources, | |
| 1093 | NoSpaceLeft, | |
| 1094 | FileSystem, | |
| 1095 | BadPathName, | |
| 1096 | ||
| 1097 | /// On Windows, file paths must be valid Unicode. | |
| 1098 | InvalidUtf8, | |
| 1099 | ||
| 1100 | /// TODO remove this possibility | |
| 1101 | PathAlreadyExists, | |
| 1102 | ||
| 1103 | /// TODO remove this possibility | |
| 1104 | Unexpected, | |
| 1105 | }; | |
| 1118 | 1106 | |
| 1119 | if (result > buf.len) { | |
| 1120 | buf = try allocator.realloc(u8, buf, result); | |
| 1121 | continue; | |
| 1122 | } | |
| 1107 | /// Call from Windows-specific code if you already have a UTF-16LE encoded, null terminated string. | |
| 1108 | /// Otherwise use `real` or `realC`. | |
| 1109 | pub fn realW(out_buffer: *[os.MAX_PATH_BYTES]u8, pathname: [*]const u16) RealError![]u8 { | |
| 1110 | const h_file = windows.CreateFileW( | |
| 1111 | pathname, | |
| 1112 | windows.GENERIC_READ, | |
| 1113 | windows.FILE_SHARE_READ, | |
| 1114 | null, | |
| 1115 | windows.OPEN_EXISTING, | |
| 1116 | windows.FILE_ATTRIBUTE_NORMAL, | |
| 1117 | null, | |
| 1118 | ); | |
| 1119 | if (h_file == windows.INVALID_HANDLE_VALUE) { | |
| 1120 | const err = windows.GetLastError(); | |
| 1121 | switch (err) { | |
| 1122 | windows.ERROR.FILE_NOT_FOUND => return error.FileNotFound, | |
| 1123 | windows.ERROR.ACCESS_DENIED => return error.AccessDenied, | |
| 1124 | windows.ERROR.FILENAME_EXCED_RANGE => return error.NameTooLong, | |
| 1125 | else => return os.unexpectedErrorWindows(err), | |
| 1126 | } | |
| 1127 | } | |
| 1128 | defer os.close(h_file); | |
| 1129 | var utf16le_buf: [windows_util.PATH_MAX_WIDE]u16 = undefined; | |
| 1130 | const casted_len = @intCast(windows.DWORD, utf16le_buf.len); // TODO shouldn't need this cast | |
| 1131 | const result = windows.GetFinalPathNameByHandleW(h_file, &utf16le_buf, casted_len, windows.VOLUME_NAME_DOS); | |
| 1132 | assert(result <= utf16le_buf.len); | |
| 1133 | if (result == 0) { | |
| 1134 | const err = windows.GetLastError(); | |
| 1135 | switch (err) { | |
| 1136 | windows.ERROR.FILE_NOT_FOUND => return error.FileNotFound, | |
| 1137 | windows.ERROR.PATH_NOT_FOUND => return error.FileNotFound, | |
| 1138 | windows.ERROR.NOT_ENOUGH_MEMORY => return error.SystemResources, | |
| 1139 | windows.ERROR.FILENAME_EXCED_RANGE => return error.NameTooLong, | |
| 1140 | windows.ERROR.INVALID_PARAMETER => unreachable, | |
| 1141 | else => return os.unexpectedErrorWindows(err), | |
| 1142 | } | |
| 1143 | } | |
| 1144 | const utf16le_slice = utf16le_buf[0..result]; | |
| 1123 | 1145 | |
| 1124 | // windows returns \\?\ prepended to the path | |
| 1125 | // we strip it because nobody wants \\?\ prepended to their path | |
| 1126 | const final_len = x: { | |
| 1127 | if (result > 4 and mem.startsWith(u8, buf, "\\\\?\\")) { | |
| 1128 | var i: usize = 4; | |
| 1129 | while (i < result) : (i += 1) { | |
| 1130 | buf[i - 4] = buf[i]; | |
| 1131 | } | |
| 1132 | break :x result - 4; | |
| 1133 | } else { | |
| 1134 | break :x result; | |
| 1135 | } | |
| 1136 | }; | |
| 1137 | ||
| 1138 | return allocator.shrink(u8, buf, final_len); | |
| 1139 | } | |
| 1146 | // windows returns \\?\ prepended to the path | |
| 1147 | // we strip it because nobody wants \\?\ prepended to their path | |
| 1148 | const prefix = []u16{ '\\', '\\', '?', '\\' }; | |
| 1149 | const start_index = if (mem.startsWith(u16, utf16le_slice, prefix)) prefix.len else 0; | |
| 1150 | ||
| 1151 | // Trust that Windows gives us valid UTF-16LE. | |
| 1152 | const end_index = std.unicode.utf16leToUtf8(out_buffer, utf16le_slice[start_index..]) catch unreachable; | |
| 1153 | return out_buffer[0..end_index]; | |
| 1154 | } | |
| 1155 | ||
| 1156 | /// See `real` | |
| 1157 | /// Use this when you have a null terminated pointer path. | |
| 1158 | pub fn realC(out_buffer: *[os.MAX_PATH_BYTES]u8, pathname: [*]const u8) RealError![]u8 { | |
| 1159 | switch (builtin.os) { | |
| 1160 | Os.windows => { | |
| 1161 | const pathname_w = try windows_util.cStrToPrefixedFileW(pathname); | |
| 1162 | return realW(out_buffer, pathname_w); | |
| 1140 | 1163 | }, |
| 1141 | 1164 | Os.macosx, Os.ios => { |
| 1142 | // TODO instead of calling the libc function here, port the implementation | |
| 1143 | // to Zig, and then remove the NameTooLong error possibility. | |
| 1144 | const pathname_buf = try allocator.alloc(u8, pathname.len + 1); | |
| 1145 | defer allocator.free(pathname_buf); | |
| 1146 | ||
| 1147 | const result_buf = try allocator.alloc(u8, posix.PATH_MAX); | |
| 1148 | errdefer allocator.free(result_buf); | |
| 1149 | ||
| 1150 | mem.copy(u8, pathname_buf, pathname); | |
| 1151 | pathname_buf[pathname.len] = 0; | |
| 1152 | ||
| 1153 | const err = posix.getErrno(posix.realpath(pathname_buf.ptr, result_buf.ptr)); | |
| 1154 | if (err > 0) { | |
| 1155 | return switch (err) { | |
| 1156 | posix.EINVAL => unreachable, | |
| 1157 | posix.EBADF => unreachable, | |
| 1158 | posix.EFAULT => unreachable, | |
| 1159 | posix.EACCES => error.AccessDenied, | |
| 1160 | posix.ENOENT => error.FileNotFound, | |
| 1161 | posix.ENOTSUP => error.NotSupported, | |
| 1162 | posix.ENOTDIR => error.NotDir, | |
| 1163 | posix.ENAMETOOLONG => error.NameTooLong, | |
| 1164 | posix.ELOOP => error.SymLinkLoop, | |
| 1165 | posix.EIO => error.InputOutput, | |
| 1166 | else => os.unexpectedErrorPosix(err), | |
| 1167 | }; | |
| 1165 | // TODO instead of calling the libc function here, port the implementation to Zig | |
| 1166 | const err = posix.getErrno(posix.realpath(pathname, out_buffer)); | |
| 1167 | switch (err) { | |
| 1168 | 0 => return mem.toSlice(u8, out_buffer), | |
| 1169 | posix.EINVAL => unreachable, | |
| 1170 | posix.EBADF => unreachable, | |
| 1171 | posix.EFAULT => unreachable, | |
| 1172 | posix.EACCES => return error.AccessDenied, | |
| 1173 | posix.ENOENT => return error.FileNotFound, | |
| 1174 | posix.ENOTSUP => return error.NotSupported, | |
| 1175 | posix.ENOTDIR => return error.NotDir, | |
| 1176 | posix.ENAMETOOLONG => return error.NameTooLong, | |
| 1177 | posix.ELOOP => return error.SymLinkLoop, | |
| 1178 | posix.EIO => return error.InputOutput, | |
| 1179 | else => return os.unexpectedErrorPosix(err), | |
| 1168 | 1180 | } |
| 1169 | return allocator.shrink(u8, result_buf, cstr.len(result_buf.ptr)); | |
| 1170 | 1181 | }, |
| 1171 | 1182 | Os.linux => { |
| 1172 | const fd = try os.posixOpen(allocator, pathname, posix.O_PATH | posix.O_NONBLOCK | posix.O_CLOEXEC, 0); | |
| 1183 | const fd = try os.posixOpenC(pathname, posix.O_PATH | posix.O_NONBLOCK | posix.O_CLOEXEC, 0); | |
| 1173 | 1184 | defer os.close(fd); |
| 1174 | 1185 | |
| 1175 | 1186 | var buf: ["/proc/self/fd/-2147483648".len]u8 = undefined; |
| 1176 | const proc_path = fmt.bufPrint(buf[0..], "/proc/self/fd/{}", fd) catch unreachable; | |
| 1187 | const proc_path = fmt.bufPrint(buf[0..], "/proc/self/fd/{}\x00", fd) catch unreachable; | |
| 1177 | 1188 | |
| 1178 | return os.readLink(allocator, proc_path); | |
| 1189 | return os.readLinkC(out_buffer, proc_path.ptr); | |
| 1179 | 1190 | }, |
| 1180 | 1191 | else => @compileError("TODO implement os.path.real for " ++ @tagName(builtin.os)), |
| 1181 | 1192 | } |
| 1182 | 1193 | } |
| 1183 | 1194 | |
| 1195 | /// Return the canonicalized absolute pathname. | |
| 1196 | /// Expands all symbolic links and resolves references to `.`, `..`, and | |
| 1197 | /// extra `/` characters in ::pathname. | |
| 1198 | /// The return value is a slice of out_buffer, and not necessarily from the beginning. | |
| 1199 | pub fn real(out_buffer: *[os.MAX_PATH_BYTES]u8, pathname: []const u8) RealError![]u8 { | |
| 1200 | switch (builtin.os) { | |
| 1201 | Os.windows => { | |
| 1202 | const pathname_w = try windows_util.sliceToPrefixedFileW(pathname); | |
| 1203 | return realW(out_buffer, &pathname_w); | |
| 1204 | }, | |
| 1205 | Os.macosx, Os.ios, Os.linux => { | |
| 1206 | const pathname_c = try os.toPosixPath(pathname); | |
| 1207 | return realC(out_buffer, &pathname_c); | |
| 1208 | }, | |
| 1209 | else => @compileError("Unsupported OS"), | |
| 1210 | } | |
| 1211 | } | |
| 1212 | ||
| 1213 | /// `real`, except caller must free the returned memory. | |
| 1214 | pub fn realAlloc(allocator: *Allocator, pathname: []const u8) ![]u8 { | |
| 1215 | var buf: [os.MAX_PATH_BYTES]u8 = undefined; | |
| 1216 | return mem.dupe(allocator, u8, try real(&buf, pathname)); | |
| 1217 | } | |
| 1218 | ||
| 1184 | 1219 | test "os.path.real" { |
| 1185 | 1220 | // at least call it so it gets compiled |
| 1186 | _ = real(debug.global_allocator, "some_path"); | |
| 1221 | var buf: [os.MAX_PATH_BYTES]u8 = undefined; | |
| 1222 | std.debug.assertError(real(&buf, "definitely_bogus_does_not_exist1234"), error.FileNotFound); | |
| 1187 | 1223 | } |
std/os/test.zig+1-1| ... | ... | @@ -17,7 +17,7 @@ test "makePath, put some files in it, deleteTree" { |
| 17 | 17 | if (os.Dir.open(a, "os_test_tmp")) |dir| { |
| 18 | 18 | @panic("expected error"); |
| 19 | 19 | } else |err| { |
| 20 | assert(err == error.PathNotFound); | |
| 20 | assert(err == error.FileNotFound); | |
| 21 | 21 | } |
| 22 | 22 | } |
| 23 | 23 |
std/os/windows/kernel32.zig+11-3| ... | ... | @@ -4,8 +4,8 @@ pub extern "kernel32" stdcallcc fn CancelIoEx(hFile: HANDLE, lpOverlapped: LPOVE |
| 4 | 4 | |
| 5 | 5 | pub extern "kernel32" stdcallcc fn CloseHandle(hObject: HANDLE) BOOL; |
| 6 | 6 | |
| 7 | pub extern "kernel32" stdcallcc fn CreateDirectoryA( lpPathName: [*]const u8, lpSecurityAttributes: ?*SECURITY_ATTRIBUTES) BOOL; | |
| 8 | pub extern "kernel32" stdcallcc fn CreateDirectoryW( lpPathName: [*]const u16, lpSecurityAttributes: ?*SECURITY_ATTRIBUTES) BOOL; | |
| 7 | pub extern "kernel32" stdcallcc fn CreateDirectoryA(lpPathName: [*]const u8, lpSecurityAttributes: ?*SECURITY_ATTRIBUTES) BOOL; | |
| 8 | pub extern "kernel32" stdcallcc fn CreateDirectoryW(lpPathName: [*]const u16, lpSecurityAttributes: ?*SECURITY_ATTRIBUTES) BOOL; | |
| 9 | 9 | |
| 10 | 10 | pub extern "kernel32" stdcallcc fn CreateFileA( |
| 11 | 11 | lpFileName: [*]const u8, // TODO null terminated pointer type |
| ... | ... | @@ -89,7 +89,8 @@ pub extern "kernel32" stdcallcc fn GetFileSizeEx(hFile: HANDLE, lpFileSize: *LAR |
| 89 | 89 | pub extern "kernel32" stdcallcc fn GetFileAttributesA(lpFileName: [*]const CHAR) DWORD; |
| 90 | 90 | pub extern "kernel32" stdcallcc fn GetFileAttributesW(lpFileName: [*]const WCHAR) DWORD; |
| 91 | 91 | |
| 92 | pub extern "kernel32" stdcallcc fn GetModuleFileNameA(hModule: ?HMODULE, lpFilename: LPSTR, nSize: DWORD) DWORD; | |
| 92 | pub extern "kernel32" stdcallcc fn GetModuleFileNameA(hModule: ?HMODULE, lpFilename: [*]u8, nSize: DWORD) DWORD; | |
| 93 | pub extern "kernel32" stdcallcc fn GetModuleFileNameW(hModule: ?HMODULE, lpFilename: [*]u16, nSize: DWORD) DWORD; | |
| 93 | 94 | |
| 94 | 95 | pub extern "kernel32" stdcallcc fn GetLastError() DWORD; |
| 95 | 96 | |
| ... | ... | @@ -107,6 +108,13 @@ pub extern "kernel32" stdcallcc fn GetFinalPathNameByHandleA( |
| 107 | 108 | dwFlags: DWORD, |
| 108 | 109 | ) DWORD; |
| 109 | 110 | |
| 111 | pub extern "kernel32" stdcallcc fn GetFinalPathNameByHandleW( | |
| 112 | hFile: HANDLE, | |
| 113 | lpszFilePath: [*]u16, | |
| 114 | cchFilePath: DWORD, | |
| 115 | dwFlags: DWORD, | |
| 116 | ) DWORD; | |
| 117 | ||
| 110 | 118 | pub extern "kernel32" stdcallcc fn GetOverlappedResult(hFile: HANDLE, lpOverlapped: *OVERLAPPED, lpNumberOfBytesTransferred: *DWORD, bWait: BOOL) BOOL; |
| 111 | 119 | |
| 112 | 120 | pub extern "kernel32" stdcallcc fn GetProcessHeap() ?HANDLE; |
std/os/windows/util.zig+13-13| ... | ... | @@ -97,12 +97,12 @@ pub const OpenError = error{ |
| 97 | 97 | SharingViolation, |
| 98 | 98 | PathAlreadyExists, |
| 99 | 99 | |
| 100 | /// When all the path components are found but the file component is not. | |
| 100 | /// When any of the path components can not be found or the file component can not | |
| 101 | /// be found. Some operating systems distinguish between path components not found and | |
| 102 | /// file components not found, but they are collapsed into FileNotFound to gain | |
| 103 | /// consistency across operating systems. | |
| 101 | 104 | FileNotFound, |
| 102 | 105 | |
| 103 | /// When one or more path components are not found. | |
| 104 | PathNotFound, | |
| 105 | ||
| 106 | 106 | AccessDenied, |
| 107 | 107 | PipeBusy, |
| 108 | 108 | NameTooLong, |
| ... | ... | @@ -136,7 +136,7 @@ pub fn windowsOpen( |
| 136 | 136 | windows.ERROR.ALREADY_EXISTS => return OpenError.PathAlreadyExists, |
| 137 | 137 | windows.ERROR.FILE_EXISTS => return OpenError.PathAlreadyExists, |
| 138 | 138 | windows.ERROR.FILE_NOT_FOUND => return OpenError.FileNotFound, |
| 139 | windows.ERROR.PATH_NOT_FOUND => return OpenError.PathNotFound, | |
| 139 | windows.ERROR.PATH_NOT_FOUND => return OpenError.FileNotFound, | |
| 140 | 140 | windows.ERROR.ACCESS_DENIED => return OpenError.AccessDenied, |
| 141 | 141 | windows.ERROR.PIPE_BUSY => return OpenError.PipeBusy, |
| 142 | 142 | else => return os.unexpectedErrorWindows(err), |
| ... | ... | @@ -216,9 +216,8 @@ pub fn windowsFindFirstFile( |
| 216 | 216 | if (handle == windows.INVALID_HANDLE_VALUE) { |
| 217 | 217 | const err = windows.GetLastError(); |
| 218 | 218 | switch (err) { |
| 219 | windows.ERROR.FILE_NOT_FOUND, | |
| 220 | windows.ERROR.PATH_NOT_FOUND, | |
| 221 | => return error.PathNotFound, | |
| 219 | windows.ERROR.FILE_NOT_FOUND => return error.FileNotFound, | |
| 220 | windows.ERROR.PATH_NOT_FOUND => return error.FileNotFound, | |
| 222 | 221 | else => return os.unexpectedErrorWindows(err), |
| 223 | 222 | } |
| 224 | 223 | } |
| ... | ... | @@ -284,13 +283,13 @@ pub fn windowsGetQueuedCompletionStatus(completion_port: windows.HANDLE, bytes_t |
| 284 | 283 | return WindowsWaitResult.Normal; |
| 285 | 284 | } |
| 286 | 285 | |
| 287 | pub fn cStrToPrefixedFileW(s: [*]const u8) ![PATH_MAX_WIDE+1]u16 { | |
| 286 | pub fn cStrToPrefixedFileW(s: [*]const u8) ![PATH_MAX_WIDE + 1]u16 { | |
| 288 | 287 | return sliceToPrefixedFileW(mem.toSliceConst(u8, s)); |
| 289 | 288 | } |
| 290 | 289 | |
| 291 | pub fn sliceToPrefixedFileW(s: []const u8) ![PATH_MAX_WIDE+1]u16 { | |
| 290 | pub fn sliceToPrefixedFileW(s: []const u8) ![PATH_MAX_WIDE + 1]u16 { | |
| 292 | 291 | // TODO well defined copy elision |
| 293 | var result: [PATH_MAX_WIDE+1]u16 = undefined; | |
| 292 | var result: [PATH_MAX_WIDE + 1]u16 = undefined; | |
| 294 | 293 | |
| 295 | 294 | // > File I/O functions in the Windows API convert "/" to "\" as part of |
| 296 | 295 | // > converting the name to an NT-style name, except when using the "\\?\" |
| ... | ... | @@ -298,12 +297,13 @@ pub fn sliceToPrefixedFileW(s: []const u8) ![PATH_MAX_WIDE+1]u16 { |
| 298 | 297 | // from https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#maximum-path-length-limitation |
| 299 | 298 | // Because we want the larger maximum path length for absolute paths, we |
| 300 | 299 | // disallow forward slashes in zig std lib file functions on Windows. |
| 301 | for (s) |byte| switch (byte) { | |
| 300 | for (s) |byte| | |
| 301 | switch (byte) { | |
| 302 | 302 | '/', '*', '?', '"', '<', '>', '|' => return error.BadPathName, |
| 303 | 303 | else => {}, |
| 304 | 304 | }; |
| 305 | 305 | const start_index = if (mem.startsWith(u8, s, "\\\\") or !os.path.isAbsolute(s)) 0 else blk: { |
| 306 | const prefix = []u16{'\\', '\\', '?', '\\'}; | |
| 306 | const prefix = []u16{ '\\', '\\', '?', '\\' }; | |
| 307 | 307 | mem.copy(u16, result[0..], prefix); |
| 308 | 308 | break :blk prefix.len; |
| 309 | 309 | }; |
std/unicode.zig+1-1| ... | ... | @@ -495,7 +495,7 @@ pub fn utf16leToUtf8Alloc(allocator: *mem.Allocator, utf16le: []const u16) ![]u8 |
| 495 | 495 | } |
| 496 | 496 | |
| 497 | 497 | /// Asserts that the output buffer is big enough. |
| 498 | /// Returns end index. | |
| 498 | /// Returns end byte index into utf8. | |
| 499 | 499 | pub fn utf16leToUtf8(utf8: []u8, utf16le: []const u16) !usize { |
| 500 | 500 | var end_index: usize = 0; |
| 501 | 501 | var it = Utf16LeIterator.init(utf16le); |
test/cases/merge_error_sets.zig+2-2| ... | ... | @@ -1,5 +1,5 @@ |
| 1 | 1 | const A = error{ |
| 2 | PathNotFound, | |
| 2 | FileNotFound, | |
| 3 | 3 | NotDir, |
| 4 | 4 | }; |
| 5 | 5 | const B = error{OutOfMemory}; |
| ... | ... | @@ -15,7 +15,7 @@ test "merge error sets" { |
| 15 | 15 | @panic("unexpected"); |
| 16 | 16 | } else |err| switch (err) { |
| 17 | 17 | error.OutOfMemory => @panic("unexpected"), |
| 18 | error.PathNotFound => @panic("unexpected"), | |
| 18 | error.FileNotFound => @panic("unexpected"), | |
| 19 | 19 | error.NotDir => {}, |
| 20 | 20 | } |
| 21 | 21 | } |