Added os check for std.fs.setAsCwd() to work with windows

Due to the unavailability of fchdir in Windows, a call for setting the
CWD needs to either call chdir with the path string or call
SetCurrentDirectory.
Either way, since we are dealing with a Handle in Windows, a call for
GetFinalPathNameByHandle is necessary for getting the file path first.
This commit is contained in:
xEgoist 2022-10-07 14:00:48 -05:00 committed by Andrew Kelley
parent b4e3424594
commit ae39e7867d

View File

@ -1602,6 +1602,14 @@ pub const Dir = struct {
if (builtin.os.tag == .wasi) {
@compileError("changing cwd is not currently possible in WASI");
}
if (builtin.os.tag == .windows) {
var dir_path_buffer: [os.windows.PATH_MAX_WIDE]u16 = undefined;
var dir_path = try os.windows.GetFinalPathNameByHandle(self.fd, .{}, &dir_path_buffer);
if (builtin.link_libc) {
return os.chdirW(dir_path);
}
return os.windows.SetCurrentDirectory(dir_path);
}
try os.fchdir(self.fd);
}