authorgravatar for leecannon@leecannon.xyzLee Cannon <leecannon@leecannon.xyz> 2020-11-07 11:55:55+00:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-11-18 15:37:44+01:00
log32c998e03c38fabfb3f26ad7f88817ec8a8e5f6d
treea62ddbdf336b426f0bf72764b918461faebe4b25
parent80b1e82b1b8706b7654560013751dd4a6fc8ae2a

Switch to RtlSetCurrentDirectory_U


4 files changed, 39 insertions(+), 17 deletions(-)

lib/std/os.zig+7-1
......@@ -2297,6 +2297,9 @@ pub const ChangeCurDirError = error{
22972297 FileNotFound,
22982298 SystemResources,
22992299 NotDir,
2300
2301 /// On Windows, file paths must be valid Unicode.
2302 InvalidUtf8,
23002303} || UnexpectedError;
23012304
23022305/// Changes the current working directory of the calling process.
......@@ -2306,9 +2309,12 @@ pub fn chdir(dir_path: []const u8) ChangeCurDirError!void {
23062309 @compileError("chdir is not supported in WASI");
23072310 } else if (builtin.os.tag == .windows) {
23082311 windows.SetCurrentDirectory(dir_path) catch |err| switch (err) {
2309 error.PathNotFound, error.InvalidName, error.InvalidUtf8 => return error.FileNotFound,
2312 error.InvalidUtf8 => return error.InvalidUtf8,
23102313 error.NameTooLong => return error.NameTooLong,
2314 error.FileNotFound => return error.FileNotFound,
23112315 error.NotDir => return error.NotDir,
2316 error.NoDevice => return error.FileSystem,
2317 error.AccessDenied => return error.AccessDenied,
23122318 error.Unexpected => return error.Unexpected,
23132319 };
23142320 } else {
lib/std/os/windows.zig+28-15
......@@ -556,29 +556,42 @@ pub fn WriteFile(
556556}
557557
558558pub const SetCurrentDirectoryError = error{
559 PathNotFound,
560 InvalidName,
561 InvalidUtf8,
562559 NameTooLong,
560 InvalidUtf8,
561 FileNotFound,
563562 NotDir,
563 AccessDenied,
564 NoDevice,
564565 Unexpected,
565566};
566567
567568pub fn SetCurrentDirectory(path_name: []const u8) SetCurrentDirectoryError!void {
568 // PATH_MAX_WIDE - 1 as we need to ensure there is space for the null terminator
569 if (path_name.len >= PATH_MAX_WIDE - 1) return error.NameTooLong;
569 var path_space: PathSpace = undefined;
570 path_space.len = try std.unicode.utf8ToUtf16Le(path_space.data[0..], path_name);
571 if (path_space.len > path_space.data.len) return error.NameTooLong;
570572
571 var utf16le_buf: [PATH_MAX_WIDE]u16 = undefined;
572 const end = try std.unicode.utf8ToUtf16Le(utf16le_buf[0..], path_name);
573 utf16le_buf[end] = 0;
573 const path_len_bytes = math.cast(u16, path_space.len * 2) catch |err| switch (err) {
574 error.Overflow => return error.NameTooLong,
575 };
574576
575 if (kernel32.SetCurrentDirectoryW(@ptrCast([*:0]const u16, &utf16le_buf)) == 0) {
576 switch (kernel32.GetLastError()) {
577 .PATH_NOT_FOUND, .FILE_NOT_FOUND => return error.PathNotFound,
578 .DIRECTORY => return error.NotDir,
579 .INVALID_NAME => return error.InvalidName,
580 else => |err| return unexpectedError(err),
581 }
577 var nt_name = UNICODE_STRING{
578 .Length = path_len_bytes,
579 .MaximumLength = path_len_bytes,
580 .Buffer = @intToPtr([*]u16, @ptrToInt(&path_space.data)),
581 };
582
583 const rc = ntdll.RtlSetCurrentDirectory_U(&nt_name);
584 switch (rc) {
585 .SUCCESS => {},
586 .OBJECT_NAME_INVALID => unreachable,
587 .OBJECT_NAME_NOT_FOUND => return error.FileNotFound,
588 .OBJECT_PATH_NOT_FOUND => return error.FileNotFound,
589 .NO_MEDIA_IN_DEVICE => return error.NoDevice,
590 .INVALID_PARAMETER => unreachable,
591 .ACCESS_DENIED => return error.AccessDenied,
592 .OBJECT_PATH_SYNTAX_BAD => unreachable,
593 .NOT_A_DIRECTORY => return error.NotDir,
594 else => return unexpectedStatus(rc),
582595 }
583596}
584597
lib/std/os/windows/kernel32.zig-1
......@@ -93,7 +93,6 @@ pub extern "kernel32" fn FillConsoleOutputCharacterA(hConsoleOutput: HANDLE, cCh
9393pub extern "kernel32" fn FillConsoleOutputAttribute(hConsoleOutput: HANDLE, wAttribute: WORD, nLength: DWORD, dwWriteCoord: COORD, lpNumberOfAttrsWritten: LPDWORD) callconv(.Stdcall) BOOL;
9494pub extern "kernel32" fn SetConsoleCursorPosition(hConsoleOutput: HANDLE, dwCursorPosition: COORD) callconv(.Stdcall) BOOL;
9595
96pub extern "kernel32" fn SetCurrentDirectoryW(lpPathName: [*:0]const u16) callconv(.Stdcall) BOOL;
9796pub extern "kernel32" fn GetCurrentDirectoryW(nBufferLength: DWORD, lpBuffer: ?[*]WCHAR) callconv(.Stdcall) DWORD;
9897
9998pub extern "kernel32" fn GetCurrentThread() callconv(.Stdcall) HANDLE;
lib/std/os/windows/ntdll.zig+4
......@@ -111,3 +111,7 @@ pub extern "NtDll" fn NtWaitForKeyedEvent(
111111 Alertable: BOOLEAN,
112112 Timeout: ?*LARGE_INTEGER,
113113) callconv(.Stdcall) NTSTATUS;
114
115pub extern "NtDll" fn RtlSetCurrentDirectory_U(
116 PathName: *UNICODE_STRING
117) callconv(.Stdcall) NTSTATUS;