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{
22972297 FileNotFound,
22982298 SystemResources,
22992299 NotDir,
2300 BadPathName,
23002301
23012302 /// On Windows, file paths must be valid Unicode.
23022303 InvalidUtf8,
......@@ -2308,7 +2309,11 @@ pub fn chdir(dir_path: []const u8) ChangeCurDirError!void {
23082309 if (builtin.os.tag == .wasi) {
23092310 @compileError("chdir is not supported in WASI");
23102311 } 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) {
23122317 error.NoDevice => return error.FileSystem,
23132318 else => |e| return e,
23142319 };
lib/std/os/windows.zig+5-8
......@@ -562,28 +562,25 @@ pub const SetCurrentDirectoryError = error{
562562 NotDir,
563563 AccessDenied,
564564 NoDevice,
565 BadPathName,
565566 Unexpected,
566567};
567568
568pub fn SetCurrentDirectory(path_name: []const u8) SetCurrentDirectoryError!void {
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;
572
573 const path_len_bytes = math.cast(u16, path_space.len * 2) catch |err| switch (err) {
569pub fn SetCurrentDirectory(path_name: []const u16) SetCurrentDirectoryError!void {
570 const path_len_bytes = math.cast(u16, path_name.len * 2) catch |err| switch (err) {
574571 error.Overflow => return error.NameTooLong,
575572 };
576573
577574 var nt_name = UNICODE_STRING{
578575 .Length = path_len_bytes,
579576 .MaximumLength = path_len_bytes,
580 .Buffer = @intToPtr([*]u16, @ptrToInt(&path_space.data)),
577 .Buffer = @intToPtr([*]u16, @ptrToInt(path_name.ptr)),
581578 };
582579
583580 const rc = ntdll.RtlSetCurrentDirectory_U(&nt_name);
584581 switch (rc) {
585582 .SUCCESS => {},
586 .OBJECT_NAME_INVALID => unreachable,
583 .OBJECT_NAME_INVALID => return error.BadPathName,
587584 .OBJECT_NAME_NOT_FOUND => return error.FileNotFound,
588585 .OBJECT_PATH_NOT_FOUND => return error.FileNotFound,
589586 .NO_MEDIA_IN_DEVICE => return error.NoDevice,