authorgravatar for leecannon@leecannon.xyzLee Cannon <leecannon@leecannon.xyz> 2020-11-08 18:06:45+00:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-11-18 15:37:44+01:00
log64feae3ac35af700e5f3d2e36f9482d1b58496ef
tree48da254dfac6daa457d471ae2479b63197a0b782
parentdbbe709dc7a759a8cc1e780ec1d3dbef9d23ea16

Move utf8->utf16 up one level into os.zig


2 files changed, 11 insertions(+), 9 deletions(-)

lib/std/os.zig+6-1
...@@ -2297,6 +2297,7 @@ pub const ChangeCurDirError = error{...@@ -2297,6 +2297,7 @@ pub const ChangeCurDirError = error{
2297 FileNotFound,2297 FileNotFound,
2298 SystemResources,2298 SystemResources,
2299 NotDir,2299 NotDir,
2300 BadPathName,
2300 2301
2301 /// On Windows, file paths must be valid Unicode.2302 /// On Windows, file paths must be valid Unicode.
2302 InvalidUtf8,2303 InvalidUtf8,
...@@ -2308,7 +2309,11 @@ pub fn chdir(dir_path: []const u8) ChangeCurDirError!void {...@@ -2308,7 +2309,11 @@ pub fn chdir(dir_path: []const u8) ChangeCurDirError!void {
2308 if (builtin.os.tag == .wasi) {2309 if (builtin.os.tag == .wasi) {
2309 @compileError("chdir is not supported in WASI");2310 @compileError("chdir is not supported in WASI");
2310 } else if (builtin.os.tag == .windows) {2311 } else if (builtin.os.tag == .windows) {
2311 windows.SetCurrentDirectory(dir_path) catch |err| switch (err) {2312 var utf16_dir_path: [windows.PATH_MAX_WIDE]u16 = undefined;
2313 const len = try std.unicode.utf8ToUtf16Le(utf16_dir_path[0..], dir_path);
2314 if (len > utf16_dir_path.len) return error.NameTooLong;
2315
2316 windows.SetCurrentDirectory(utf16_dir_path[0..len]) catch |err| switch (err) {
2312 error.NoDevice => return error.FileSystem,2317 error.NoDevice => return error.FileSystem,
2313 else => |e| return e,2318 else => |e| return e,
2314 };2319 };
lib/std/os/windows.zig+5-8
...@@ -562,28 +562,25 @@ pub const SetCurrentDirectoryError = error{...@@ -562,28 +562,25 @@ pub const SetCurrentDirectoryError = error{
562 NotDir,562 NotDir,
563 AccessDenied,563 AccessDenied,
564 NoDevice,564 NoDevice,
565 BadPathName,
565 Unexpected,566 Unexpected,
566};567};
567568
568pub fn SetCurrentDirectory(path_name: []const u8) SetCurrentDirectoryError!void {569pub fn SetCurrentDirectory(path_name: []const u16) SetCurrentDirectoryError!void {
569 var path_space: PathSpace = undefined;570 const path_len_bytes = math.cast(u16, path_name.len * 2) catch |err| switch (err) {
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;
572
573 const path_len_bytes = math.cast(u16, path_space.len * 2) catch |err| switch (err) {
574 error.Overflow => return error.NameTooLong,571 error.Overflow => return error.NameTooLong,
575 };572 };
576 573
577 var nt_name = UNICODE_STRING{574 var nt_name = UNICODE_STRING{
578 .Length = path_len_bytes,575 .Length = path_len_bytes,
579 .MaximumLength = path_len_bytes,576 .MaximumLength = path_len_bytes,
580 .Buffer = @intToPtr([*]u16, @ptrToInt(&path_space.data)),577 .Buffer = @intToPtr([*]u16, @ptrToInt(path_name.ptr)),
581 };578 };
582 579
583 const rc = ntdll.RtlSetCurrentDirectory_U(&nt_name);580 const rc = ntdll.RtlSetCurrentDirectory_U(&nt_name);
584 switch (rc) {581 switch (rc) {
585 .SUCCESS => {},582 .SUCCESS => {},
586 .OBJECT_NAME_INVALID => unreachable,583 .OBJECT_NAME_INVALID => return error.BadPathName,
587 .OBJECT_NAME_NOT_FOUND => return error.FileNotFound,584 .OBJECT_NAME_NOT_FOUND => return error.FileNotFound,
588 .OBJECT_PATH_NOT_FOUND => return error.FileNotFound,585 .OBJECT_PATH_NOT_FOUND => return error.FileNotFound,
589 .NO_MEDIA_IN_DEVICE => return error.NoDevice,586 .NO_MEDIA_IN_DEVICE => return error.NoDevice,