authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-08-12 10:38:31-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-29 14:30:36-07:00
log612b1772014aa016dddd4703818973c8dec6104f
tree05d6643fe7c475eef29a82f006016a25b2ba44df
parent1b88ce3b0cdeec7d23ded22e04aee8fc43f7bb5f

Merge pull request #16783 from squeek502/fs-too-many-parent-dirs

Windows: Fix `TooManyParentDirs` handling for paths that shouldn't be cwd-relative

7 files changed, 133 insertions(+), 72 deletions(-)

lib/std/child_process.zig+1-1
......@@ -963,7 +963,7 @@ fn windowsCreateProcessPathExt(
963963 try dir_buf.append(allocator, 0);
964964 defer dir_buf.shrinkRetainingCapacity(dir_path_len);
965965 const dir_path_z = dir_buf.items[0 .. dir_buf.items.len - 1 :0];
966 const prefixed_path = try windows.wToPrefixedFileW(dir_path_z);
966 const prefixed_path = try windows.wToPrefixedFileW(null, dir_path_z);
967967 break :dir fs.cwd().openDirW(prefixed_path.span().ptr, .{}, true) catch return error.FileNotFound;
968968 };
969969 defer dir.close();
lib/std/dynamic_library.zig+2-2
......@@ -319,12 +319,12 @@ pub const WindowsDynLib = struct {
319319 dll: windows.HMODULE,
320320
321321 pub fn open(path: []const u8) !WindowsDynLib {
322 const path_w = try windows.sliceToPrefixedFileW(path);
322 const path_w = try windows.sliceToPrefixedFileW(null, path);
323323 return openW(path_w.span().ptr);
324324 }
325325
326326 pub fn openZ(path_c: [*:0]const u8) !WindowsDynLib {
327 const path_w = try windows.cStrToPrefixedFileW(path_c);
327 const path_w = try windows.cStrToPrefixedFileW(null, path_c);
328328 return openW(path_w.span().ptr);
329329 }
330330
lib/std/fs.zig+28-22
......@@ -1118,7 +1118,7 @@ pub const Dir = struct {
11181118 /// Asserts that the path parameter has no null bytes.
11191119 pub fn openFile(self: Dir, sub_path: []const u8, flags: File.OpenFlags) File.OpenError!File {
11201120 if (builtin.os.tag == .windows) {
1121 const path_w = try os.windows.sliceToPrefixedFileW(sub_path);
1121 const path_w = try os.windows.sliceToPrefixedFileW(self.fd, sub_path);
11221122 return self.openFileW(path_w.span(), flags);
11231123 }
11241124 if (builtin.os.tag == .wasi and !builtin.link_libc) {
......@@ -1156,7 +1156,7 @@ pub const Dir = struct {
11561156 /// Same as `openFile` but the path parameter is null-terminated.
11571157 pub fn openFileZ(self: Dir, sub_path: [*:0]const u8, flags: File.OpenFlags) File.OpenError!File {
11581158 if (builtin.os.tag == .windows) {
1159 const path_w = try os.windows.cStrToPrefixedFileW(sub_path);
1159 const path_w = try os.windows.cStrToPrefixedFileW(self.fd, sub_path);
11601160 return self.openFileW(path_w.span(), flags);
11611161 }
11621162
......@@ -1282,7 +1282,7 @@ pub const Dir = struct {
12821282 /// Asserts that the path parameter has no null bytes.
12831283 pub fn createFile(self: Dir, sub_path: []const u8, flags: File.CreateFlags) File.OpenError!File {
12841284 if (builtin.os.tag == .windows) {
1285 const path_w = try os.windows.sliceToPrefixedFileW(sub_path);
1285 const path_w = try os.windows.sliceToPrefixedFileW(self.fd, sub_path);
12861286 return self.createFileW(path_w.span(), flags);
12871287 }
12881288 if (builtin.os.tag == .wasi and !builtin.link_libc) {
......@@ -1323,7 +1323,7 @@ pub const Dir = struct {
13231323 /// Same as `createFile` but the path parameter is null-terminated.
13241324 pub fn createFileZ(self: Dir, sub_path_c: [*:0]const u8, flags: File.CreateFlags) File.OpenError!File {
13251325 if (builtin.os.tag == .windows) {
1326 const path_w = try os.windows.cStrToPrefixedFileW(sub_path_c);
1326 const path_w = try os.windows.cStrToPrefixedFileW(self.fd, sub_path_c);
13271327 return self.createFileW(path_w.span(), flags);
13281328 }
13291329
......@@ -1513,7 +1513,7 @@ pub const Dir = struct {
15131513 @compileError("realpath is not available on WASI");
15141514 }
15151515 if (builtin.os.tag == .windows) {
1516 const pathname_w = try os.windows.sliceToPrefixedFileW(pathname);
1516 const pathname_w = try os.windows.sliceToPrefixedFileW(self.fd, pathname);
15171517 return self.realpathW(pathname_w.span(), out_buffer);
15181518 }
15191519 const pathname_c = try os.toPosixPath(pathname);
......@@ -1524,7 +1524,7 @@ pub const Dir = struct {
15241524 /// See also `Dir.realpath`, `realpathZ`.
15251525 pub fn realpathZ(self: Dir, pathname: [*:0]const u8, out_buffer: []u8) ![]u8 {
15261526 if (builtin.os.tag == .windows) {
1527 const pathname_w = try os.windows.cStrToPrefixedFileW(pathname);
1527 const pathname_w = try os.windows.cStrToPrefixedFileW(self.fd, pathname);
15281528 return self.realpathW(pathname_w.span(), out_buffer);
15291529 }
15301530
......@@ -1656,7 +1656,7 @@ pub const Dir = struct {
16561656 /// Asserts that the path parameter has no null bytes.
16571657 pub fn openDir(self: Dir, sub_path: []const u8, args: OpenDirOptions) OpenError!Dir {
16581658 if (builtin.os.tag == .windows) {
1659 const sub_path_w = try os.windows.sliceToPrefixedFileW(sub_path);
1659 const sub_path_w = try os.windows.sliceToPrefixedFileW(self.fd, sub_path);
16601660 return self.openDirW(sub_path_w.span().ptr, args, false);
16611661 } else if (builtin.os.tag == .wasi and !builtin.link_libc) {
16621662 return self.openDirWasi(sub_path, args);
......@@ -1672,7 +1672,7 @@ pub const Dir = struct {
16721672 /// Asserts that the path parameter has no null bytes.
16731673 pub fn openIterableDir(self: Dir, sub_path: []const u8, args: OpenDirOptions) OpenError!IterableDir {
16741674 if (builtin.os.tag == .windows) {
1675 const sub_path_w = try os.windows.sliceToPrefixedFileW(sub_path);
1675 const sub_path_w = try os.windows.sliceToPrefixedFileW(self.fd, sub_path);
16761676 return IterableDir{ .dir = try self.openDirW(sub_path_w.span().ptr, args, true) };
16771677 } else if (builtin.os.tag == .wasi and !builtin.link_libc) {
16781678 return IterableDir{ .dir = try self.openDirWasi(sub_path, args) };
......@@ -1732,7 +1732,7 @@ pub const Dir = struct {
17321732 /// Same as `openDir` except the parameter is null-terminated.
17331733 pub fn openDirZ(self: Dir, sub_path_c: [*:0]const u8, args: OpenDirOptions, iterable: bool) OpenError!Dir {
17341734 if (builtin.os.tag == .windows) {
1735 const sub_path_w = try os.windows.cStrToPrefixedFileW(sub_path_c);
1735 const sub_path_w = try os.windows.cStrToPrefixedFileW(self.fd, sub_path_c);
17361736 return self.openDirW(sub_path_w.span().ptr, args, iterable);
17371737 }
17381738 const symlink_flags: u32 = if (args.no_follow) os.O.NOFOLLOW else 0x0;
......@@ -1831,7 +1831,7 @@ pub const Dir = struct {
18311831 /// Asserts that the path parameter has no null bytes.
18321832 pub fn deleteFile(self: Dir, sub_path: []const u8) DeleteFileError!void {
18331833 if (builtin.os.tag == .windows) {
1834 const sub_path_w = try os.windows.sliceToPrefixedFileW(sub_path);
1834 const sub_path_w = try os.windows.sliceToPrefixedFileW(self.fd, sub_path);
18351835 return self.deleteFileW(sub_path_w.span());
18361836 } else if (builtin.os.tag == .wasi and !builtin.link_libc) {
18371837 os.unlinkat(self.fd, sub_path, 0) catch |err| switch (err) {
......@@ -1894,7 +1894,7 @@ pub const Dir = struct {
18941894 /// Asserts that the path parameter has no null bytes.
18951895 pub fn deleteDir(self: Dir, sub_path: []const u8) DeleteDirError!void {
18961896 if (builtin.os.tag == .windows) {
1897 const sub_path_w = try os.windows.sliceToPrefixedFileW(sub_path);
1897 const sub_path_w = try os.windows.sliceToPrefixedFileW(self.fd, sub_path);
18981898 return self.deleteDirW(sub_path_w.span());
18991899 } else if (builtin.os.tag == .wasi and !builtin.link_libc) {
19001900 os.unlinkat(self.fd, sub_path, os.AT.REMOVEDIR) catch |err| switch (err) {
......@@ -1959,8 +1959,8 @@ pub const Dir = struct {
19591959 return self.symLinkWasi(target_path, sym_link_path, flags);
19601960 }
19611961 if (builtin.os.tag == .windows) {
1962 const target_path_w = try os.windows.sliceToPrefixedFileW(target_path);
1963 const sym_link_path_w = try os.windows.sliceToPrefixedFileW(sym_link_path);
1962 const target_path_w = try os.windows.sliceToPrefixedFileW(self.fd, target_path);
1963 const sym_link_path_w = try os.windows.sliceToPrefixedFileW(self.fd, sym_link_path);
19641964 return self.symLinkW(target_path_w.span(), sym_link_path_w.span(), flags);
19651965 }
19661966 const target_path_c = try os.toPosixPath(target_path);
......@@ -1986,8 +1986,8 @@ pub const Dir = struct {
19861986 flags: SymLinkFlags,
19871987 ) !void {
19881988 if (builtin.os.tag == .windows) {
1989 const target_path_w = try os.windows.cStrToPrefixedFileW(target_path_c);
1990 const sym_link_path_w = try os.windows.cStrToPrefixedFileW(sym_link_path_c);
1989 const target_path_w = try os.windows.cStrToPrefixedFileW(self.fd, target_path_c);
1990 const sym_link_path_w = try os.windows.cStrToPrefixedFileW(self.fd, sym_link_path_c);
19911991 return self.symLinkW(target_path_w.span(), sym_link_path_w.span(), flags);
19921992 }
19931993 return os.symlinkatZ(target_path_c, self.fd, sym_link_path_c);
......@@ -2012,7 +2012,7 @@ pub const Dir = struct {
20122012 return self.readLinkWasi(sub_path, buffer);
20132013 }
20142014 if (builtin.os.tag == .windows) {
2015 const sub_path_w = try os.windows.sliceToPrefixedFileW(sub_path);
2015 const sub_path_w = try os.windows.sliceToPrefixedFileW(self.fd, sub_path);
20162016 return self.readLinkW(sub_path_w.span(), buffer);
20172017 }
20182018 const sub_path_c = try os.toPosixPath(sub_path);
......@@ -2027,7 +2027,7 @@ pub const Dir = struct {
20272027 /// Same as `readLink`, except the `pathname` parameter is null-terminated.
20282028 pub fn readLinkZ(self: Dir, sub_path_c: [*:0]const u8, buffer: []u8) ![]u8 {
20292029 if (builtin.os.tag == .windows) {
2030 const sub_path_w = try os.windows.cStrToPrefixedFileW(sub_path_c);
2030 const sub_path_w = try os.windows.cStrToPrefixedFileW(self.fd, sub_path_c);
20312031 return self.readLinkW(sub_path_w.span(), buffer);
20322032 }
20332033 return os.readlinkatZ(self.fd, sub_path_c, buffer);
......@@ -2501,7 +2501,10 @@ pub const Dir = struct {
25012501 /// open it and handle the error for file not found.
25022502 pub fn access(self: Dir, sub_path: []const u8, flags: File.OpenFlags) AccessError!void {
25032503 if (builtin.os.tag == .windows) {
2504 const sub_path_w = try os.windows.sliceToPrefixedFileW(sub_path);
2504 const sub_path_w = os.windows.sliceToPrefixedFileW(self.fd, sub_path) catch |err| switch (err) {
2505 error.AccessDenied => return error.PermissionDenied,
2506 else => |e| return e,
2507 };
25052508 return self.accessW(sub_path_w.span().ptr, flags);
25062509 }
25072510 const path_c = try os.toPosixPath(sub_path);
......@@ -2511,7 +2514,10 @@ pub const Dir = struct {
25112514 /// Same as `access` except the path parameter is null-terminated.
25122515 pub fn accessZ(self: Dir, sub_path: [*:0]const u8, flags: File.OpenFlags) AccessError!void {
25132516 if (builtin.os.tag == .windows) {
2514 const sub_path_w = try os.windows.cStrToPrefixedFileW(sub_path);
2517 const sub_path_w = os.windows.cStrToPrefixedFileW(self.fd, sub_path) catch |err| switch (err) {
2518 error.AccessDenied => return error.PermissionDenied,
2519 else => |e| return e,
2520 };
25152521 return self.accessW(sub_path_w.span().ptr, flags);
25162522 }
25172523 const os_mode = switch (flags.mode) {
......@@ -2894,8 +2900,8 @@ pub fn symLinkAbsolute(target_path: []const u8, sym_link_path: []const u8, flags
28942900 assert(path.isAbsolute(target_path));
28952901 assert(path.isAbsolute(sym_link_path));
28962902 if (builtin.os.tag == .windows) {
2897 const target_path_w = try os.windows.sliceToPrefixedFileW(target_path);
2898 const sym_link_path_w = try os.windows.sliceToPrefixedFileW(sym_link_path);
2903 const target_path_w = try os.windows.sliceToPrefixedFileW(null, target_path);
2904 const sym_link_path_w = try os.windows.sliceToPrefixedFileW(null, sym_link_path);
28992905 return os.windows.CreateSymbolicLink(null, sym_link_path_w.span(), target_path_w.span(), flags.is_directory);
29002906 }
29012907 return os.symlink(target_path, sym_link_path);
......@@ -2945,7 +2951,7 @@ pub fn openSelfExe(flags: File.OpenFlags) OpenSelfExeError!File {
29452951 }
29462952 if (builtin.os.tag == .windows) {
29472953 const wide_slice = selfExePathW();
2948 const prefixed_path_w = try os.windows.wToPrefixedFileW(wide_slice);
2954 const prefixed_path_w = try os.windows.wToPrefixedFileW(null, wide_slice);
29492955 return cwd().openFileW(prefixed_path_w.span(), flags);
29502956 }
29512957 // Use of MAX_PATH_BYTES here is valid as the resulting path is immediately
lib/std/fs/test.zig+21
......@@ -101,6 +101,27 @@ test "openDir cwd parent .." {
101101 defer dir.close();
102102}
103103
104test "openDir non-cwd parent .." {
105 if (builtin.os.tag == .wasi) return error.SkipZigTest;
106
107 var tmp = tmpDir(.{});
108 defer tmp.cleanup();
109
110 var subdir = try tmp.dir.makeOpenPath("subdir", .{});
111 defer subdir.close();
112
113 var dir = try subdir.openDir("..", .{});
114 defer dir.close();
115
116 const expected_path = try tmp.dir.realpathAlloc(testing.allocator, ".");
117 defer testing.allocator.free(expected_path);
118
119 const actual_path = try dir.realpathAlloc(testing.allocator, ".");
120 defer testing.allocator.free(actual_path);
121
122 try testing.expectEqualStrings(expected_path, actual_path);
123}
124
104125test "readLinkAbsolute" {
105126 if (builtin.os.tag == .wasi) return error.SkipZigTest;
106127
lib/std/os.zig+37-31
......@@ -1467,7 +1467,7 @@ pub const OpenError = error{
14671467/// See also `openZ`.
14681468pub fn open(file_path: []const u8, flags: u32, perm: mode_t) OpenError!fd_t {
14691469 if (builtin.os.tag == .windows) {
1470 const file_path_w = try windows.sliceToPrefixedFileW(file_path);
1470 const file_path_w = try windows.sliceToPrefixedFileW(null, file_path);
14711471 return openW(file_path_w.span(), flags, perm);
14721472 } else if (builtin.os.tag == .wasi and !builtin.link_libc) {
14731473 return openat(wasi.AT.FDCWD, file_path, flags, perm);
......@@ -1480,7 +1480,7 @@ pub fn open(file_path: []const u8, flags: u32, perm: mode_t) OpenError!fd_t {
14801480/// See also `open`.
14811481pub fn openZ(file_path: [*:0]const u8, flags: u32, perm: mode_t) OpenError!fd_t {
14821482 if (builtin.os.tag == .windows) {
1483 const file_path_w = try windows.cStrToPrefixedFileW(file_path);
1483 const file_path_w = try windows.cStrToPrefixedFileW(null, file_path);
14841484 return openW(file_path_w.span(), flags, perm);
14851485 } else if (builtin.os.tag == .wasi and !builtin.link_libc) {
14861486 return open(mem.sliceTo(file_path, 0), flags, perm);
......@@ -1571,7 +1571,7 @@ pub fn openW(file_path_w: []const u16, flags: u32, perm: mode_t) OpenError!fd_t
15711571/// See also `openatZ`.
15721572pub fn openat(dir_fd: fd_t, file_path: []const u8, flags: u32, mode: mode_t) OpenError!fd_t {
15731573 if (builtin.os.tag == .windows) {
1574 const file_path_w = try windows.sliceToPrefixedFileW(file_path);
1574 const file_path_w = try windows.sliceToPrefixedFileW(dir_fd, file_path);
15751575 return openatW(dir_fd, file_path_w.span(), flags, mode);
15761576 } else if (builtin.os.tag == .wasi and !builtin.link_libc) {
15771577 // `mode` is ignored on WASI, which does not support unix-style file permissions
......@@ -1693,7 +1693,7 @@ pub fn openatWasi(
16931693/// See also `openat`.
16941694pub fn openatZ(dir_fd: fd_t, file_path: [*:0]const u8, flags: u32, mode: mode_t) OpenError!fd_t {
16951695 if (builtin.os.tag == .windows) {
1696 const file_path_w = try windows.cStrToPrefixedFileW(file_path);
1696 const file_path_w = try windows.cStrToPrefixedFileW(dir_fd, file_path);
16971697 return openatW(dir_fd, file_path_w.span(), flags, mode);
16981698 } else if (builtin.os.tag == .wasi and !builtin.link_libc) {
16991699 return openat(dir_fd, mem.sliceTo(file_path, 0), flags, mode);
......@@ -2308,7 +2308,7 @@ pub fn unlink(file_path: []const u8) UnlinkError!void {
23082308 else => |e| return e,
23092309 };
23102310 } else if (builtin.os.tag == .windows) {
2311 const file_path_w = try windows.sliceToPrefixedFileW(file_path);
2311 const file_path_w = try windows.sliceToPrefixedFileW(null, file_path);
23122312 return unlinkW(file_path_w.span());
23132313 } else {
23142314 const file_path_c = try toPosixPath(file_path);
......@@ -2319,7 +2319,7 @@ pub fn unlink(file_path: []const u8) UnlinkError!void {
23192319/// Same as `unlink` except the parameter is a null terminated UTF8-encoded string.
23202320pub fn unlinkZ(file_path: [*:0]const u8) UnlinkError!void {
23212321 if (builtin.os.tag == .windows) {
2322 const file_path_w = try windows.cStrToPrefixedFileW(file_path);
2322 const file_path_w = try windows.cStrToPrefixedFileW(null, file_path);
23232323 return unlinkW(file_path_w.span());
23242324 } else if (builtin.os.tag == .wasi and !builtin.link_libc) {
23252325 return unlink(mem.sliceTo(file_path, 0));
......@@ -2357,7 +2357,7 @@ pub const UnlinkatError = UnlinkError || error{
23572357/// Asserts that the path parameter has no null bytes.
23582358pub fn unlinkat(dirfd: fd_t, file_path: []const u8, flags: u32) UnlinkatError!void {
23592359 if (builtin.os.tag == .windows) {
2360 const file_path_w = try windows.sliceToPrefixedFileW(file_path);
2360 const file_path_w = try windows.sliceToPrefixedFileW(dirfd, file_path);
23612361 return unlinkatW(dirfd, file_path_w.span(), flags);
23622362 } else if (builtin.os.tag == .wasi and !builtin.link_libc) {
23632363 return unlinkatWasi(dirfd, file_path, flags);
......@@ -2402,7 +2402,7 @@ pub fn unlinkatWasi(dirfd: fd_t, file_path: []const u8, flags: u32) UnlinkatErro
24022402/// Same as `unlinkat` but `file_path` is a null-terminated string.
24032403pub fn unlinkatZ(dirfd: fd_t, file_path_c: [*:0]const u8, flags: u32) UnlinkatError!void {
24042404 if (builtin.os.tag == .windows) {
2405 const file_path_w = try windows.cStrToPrefixedFileW(file_path_c);
2405 const file_path_w = try windows.cStrToPrefixedFileW(dirfd, file_path_c);
24062406 return unlinkatW(dirfd, file_path_w.span(), flags);
24072407 } else if (builtin.os.tag == .wasi and !builtin.link_libc) {
24082408 return unlinkat(dirfd, mem.sliceTo(file_path_c, 0), flags);
......@@ -2471,8 +2471,8 @@ pub fn rename(old_path: []const u8, new_path: []const u8) RenameError!void {
24712471 if (builtin.os.tag == .wasi and !builtin.link_libc) {
24722472 return renameat(wasi.AT.FDCWD, old_path, wasi.AT.FDCWD, new_path);
24732473 } else if (builtin.os.tag == .windows) {
2474 const old_path_w = try windows.sliceToPrefixedFileW(old_path);
2475 const new_path_w = try windows.sliceToPrefixedFileW(new_path);
2474 const old_path_w = try windows.sliceToPrefixedFileW(null, old_path);
2475 const new_path_w = try windows.sliceToPrefixedFileW(null, new_path);
24762476 return renameW(old_path_w.span().ptr, new_path_w.span().ptr);
24772477 } else {
24782478 const old_path_c = try toPosixPath(old_path);
......@@ -2484,8 +2484,8 @@ pub fn rename(old_path: []const u8, new_path: []const u8) RenameError!void {
24842484/// Same as `rename` except the parameters are null-terminated byte arrays.
24852485pub fn renameZ(old_path: [*:0]const u8, new_path: [*:0]const u8) RenameError!void {
24862486 if (builtin.os.tag == .windows) {
2487 const old_path_w = try windows.cStrToPrefixedFileW(old_path);
2488 const new_path_w = try windows.cStrToPrefixedFileW(new_path);
2487 const old_path_w = try windows.cStrToPrefixedFileW(null, old_path);
2488 const new_path_w = try windows.cStrToPrefixedFileW(null, new_path);
24892489 return renameW(old_path_w.span().ptr, new_path_w.span().ptr);
24902490 } else if (builtin.os.tag == .wasi and !builtin.link_libc) {
24912491 return rename(mem.sliceTo(old_path, 0), mem.sliceTo(new_path, 0));
......@@ -2529,8 +2529,8 @@ pub fn renameat(
25292529 new_path: []const u8,
25302530) RenameError!void {
25312531 if (builtin.os.tag == .windows) {
2532 const old_path_w = try windows.sliceToPrefixedFileW(old_path);
2533 const new_path_w = try windows.sliceToPrefixedFileW(new_path);
2532 const old_path_w = try windows.sliceToPrefixedFileW(old_dir_fd, old_path);
2533 const new_path_w = try windows.sliceToPrefixedFileW(new_dir_fd, new_path);
25342534 return renameatW(old_dir_fd, old_path_w.span(), new_dir_fd, new_path_w.span(), windows.TRUE);
25352535 } else if (builtin.os.tag == .wasi and !builtin.link_libc) {
25362536 const old: RelativePathWasi = .{ .dir_fd = old_dir_fd, .relative_path = old_path };
......@@ -2579,8 +2579,8 @@ pub fn renameatZ(
25792579 new_path: [*:0]const u8,
25802580) RenameError!void {
25812581 if (builtin.os.tag == .windows) {
2582 const old_path_w = try windows.cStrToPrefixedFileW(old_path);
2583 const new_path_w = try windows.cStrToPrefixedFileW(new_path);
2582 const old_path_w = try windows.cStrToPrefixedFileW(old_dir_fd, old_path);
2583 const new_path_w = try windows.cStrToPrefixedFileW(new_dir_fd, new_path);
25842584 return renameatW(old_dir_fd, old_path_w.span(), new_dir_fd, new_path_w.span(), windows.TRUE);
25852585 } else if (builtin.os.tag == .wasi and !builtin.link_libc) {
25862586 return renameat(old_dir_fd, mem.sliceTo(old_path, 0), new_dir_fd, mem.sliceTo(new_path, 0));
......@@ -2673,7 +2673,7 @@ pub fn renameatW(
26732673
26742674pub fn mkdirat(dir_fd: fd_t, sub_dir_path: []const u8, mode: u32) MakeDirError!void {
26752675 if (builtin.os.tag == .windows) {
2676 const sub_dir_path_w = try windows.sliceToPrefixedFileW(sub_dir_path);
2676 const sub_dir_path_w = try windows.sliceToPrefixedFileW(dir_fd, sub_dir_path);
26772677 return mkdiratW(dir_fd, sub_dir_path_w.span(), mode);
26782678 } else if (builtin.os.tag == .wasi and !builtin.link_libc) {
26792679 return mkdiratWasi(dir_fd, sub_dir_path, mode);
......@@ -2708,7 +2708,7 @@ pub fn mkdiratWasi(dir_fd: fd_t, sub_dir_path: []const u8, mode: u32) MakeDirErr
27082708
27092709pub fn mkdiratZ(dir_fd: fd_t, sub_dir_path: [*:0]const u8, mode: u32) MakeDirError!void {
27102710 if (builtin.os.tag == .windows) {
2711 const sub_dir_path_w = try windows.cStrToPrefixedFileW(sub_dir_path);
2711 const sub_dir_path_w = try windows.cStrToPrefixedFileW(dir_fd, sub_dir_path);
27122712 return mkdiratW(dir_fd, sub_dir_path_w.span().ptr, mode);
27132713 } else if (builtin.os.tag == .wasi and !builtin.link_libc) {
27142714 return mkdirat(dir_fd, mem.sliceTo(sub_dir_path, 0), mode);
......@@ -2779,7 +2779,7 @@ pub fn mkdir(dir_path: []const u8, mode: u32) MakeDirError!void {
27792779 if (builtin.os.tag == .wasi and !builtin.link_libc) {
27802780 return mkdirat(wasi.AT.FDCWD, dir_path, mode);
27812781 } else if (builtin.os.tag == .windows) {
2782 const dir_path_w = try windows.sliceToPrefixedFileW(dir_path);
2782 const dir_path_w = try windows.sliceToPrefixedFileW(null, dir_path);
27832783 return mkdirW(dir_path_w.span(), mode);
27842784 } else {
27852785 const dir_path_c = try toPosixPath(dir_path);
......@@ -2790,7 +2790,7 @@ pub fn mkdir(dir_path: []const u8, mode: u32) MakeDirError!void {
27902790/// Same as `mkdir` but the parameter is a null-terminated UTF8-encoded string.
27912791pub fn mkdirZ(dir_path: [*:0]const u8, mode: u32) MakeDirError!void {
27922792 if (builtin.os.tag == .windows) {
2793 const dir_path_w = try windows.cStrToPrefixedFileW(dir_path);
2793 const dir_path_w = try windows.cStrToPrefixedFileW(null, dir_path);
27942794 return mkdirW(dir_path_w.span(), mode);
27952795 } else if (builtin.os.tag == .wasi and !builtin.link_libc) {
27962796 return mkdir(mem.sliceTo(dir_path, 0), mode);
......@@ -2857,7 +2857,7 @@ pub fn rmdir(dir_path: []const u8) DeleteDirError!void {
28572857 else => |e| return e,
28582858 };
28592859 } else if (builtin.os.tag == .windows) {
2860 const dir_path_w = try windows.sliceToPrefixedFileW(dir_path);
2860 const dir_path_w = try windows.sliceToPrefixedFileW(null, dir_path);
28612861 return rmdirW(dir_path_w.span());
28622862 } else {
28632863 const dir_path_c = try toPosixPath(dir_path);
......@@ -2868,7 +2868,7 @@ pub fn rmdir(dir_path: []const u8) DeleteDirError!void {
28682868/// Same as `rmdir` except the parameter is null-terminated.
28692869pub fn rmdirZ(dir_path: [*:0]const u8) DeleteDirError!void {
28702870 if (builtin.os.tag == .windows) {
2871 const dir_path_w = try windows.cStrToPrefixedFileW(dir_path);
2871 const dir_path_w = try windows.cStrToPrefixedFileW(null, dir_path);
28722872 return rmdirW(dir_path_w.span());
28732873 } else if (builtin.os.tag == .wasi and !builtin.link_libc) {
28742874 return rmdir(mem.sliceTo(dir_path, 0));
......@@ -3006,7 +3006,7 @@ pub fn readlink(file_path: []const u8, out_buffer: []u8) ReadLinkError![]u8 {
30063006 if (builtin.os.tag == .wasi and !builtin.link_libc) {
30073007 return readlinkat(wasi.AT.FDCWD, file_path, out_buffer);
30083008 } else if (builtin.os.tag == .windows) {
3009 const file_path_w = try windows.sliceToPrefixedFileW(file_path);
3009 const file_path_w = try windows.sliceToPrefixedFileW(null, file_path);
30103010 return readlinkW(file_path_w.span(), out_buffer);
30113011 } else {
30123012 const file_path_c = try toPosixPath(file_path);
......@@ -3052,7 +3052,7 @@ pub fn readlinkat(dirfd: fd_t, file_path: []const u8, out_buffer: []u8) ReadLink
30523052 return readlinkatWasi(dirfd, file_path, out_buffer);
30533053 }
30543054 if (builtin.os.tag == .windows) {
3055 const file_path_w = try windows.sliceToPrefixedFileW(file_path);
3055 const file_path_w = try windows.sliceToPrefixedFileW(dirfd, file_path);
30563056 return readlinkatW(dirfd, file_path_w.span(), out_buffer);
30573057 }
30583058 const file_path_c = try toPosixPath(file_path);
......@@ -3089,7 +3089,7 @@ pub fn readlinkatW(dirfd: fd_t, file_path: []const u16, out_buffer: []u8) ReadLi
30893089/// See also `readlinkat`.
30903090pub fn readlinkatZ(dirfd: fd_t, file_path: [*:0]const u8, out_buffer: []u8) ReadLinkError![]u8 {
30913091 if (builtin.os.tag == .windows) {
3092 const file_path_w = try windows.cStrToPrefixedFileW(file_path);
3092 const file_path_w = try windows.cStrToPrefixedFileW(dirfd, file_path);
30933093 return readlinkatW(dirfd, file_path_w.span(), out_buffer);
30943094 } else if (builtin.os.tag == .wasi and !builtin.link_libc) {
30953095 return readlinkat(dirfd, mem.sliceTo(file_path, 0), out_buffer);
......@@ -4446,7 +4446,10 @@ pub const AccessError = error{
44464446/// TODO currently this assumes `mode` is `F.OK` on Windows.
44474447pub fn access(path: []const u8, mode: u32) AccessError!void {
44484448 if (builtin.os.tag == .windows) {
4449 const path_w = try windows.sliceToPrefixedFileW(path);
4449 const path_w = windows.sliceToPrefixedFileW(null, path) catch |err| switch (err) {
4450 error.AccessDenied => return error.PermissionDenied,
4451 else => |e| return e,
4452 };
44504453 _ = try windows.GetFileAttributesW(path_w.span().ptr);
44514454 return;
44524455 } else if (builtin.os.tag == .wasi and !builtin.link_libc) {
......@@ -4459,7 +4462,10 @@ pub fn access(path: []const u8, mode: u32) AccessError!void {
44594462/// Same as `access` except `path` is null-terminated.
44604463pub fn accessZ(path: [*:0]const u8, mode: u32) AccessError!void {
44614464 if (builtin.os.tag == .windows) {
4462 const path_w = try windows.cStrToPrefixedFileW(path);
4465 const path_w = windows.cStrToPrefixedFileW(null, path) catch |err| switch (err) {
4466 error.AccessDenied => return error.PermissionDenied,
4467 else => |e| return e,
4468 };
44634469 _ = try windows.GetFileAttributesW(path_w.span().ptr);
44644470 return;
44654471 } else if (builtin.os.tag == .wasi and !builtin.link_libc) {
......@@ -4503,7 +4509,7 @@ pub fn accessW(path: [*:0]const u16, mode: u32) windows.GetFileAttributesError!v
45034509/// TODO currently this ignores `mode` and `flags` on Windows.
45044510pub fn faccessat(dirfd: fd_t, path: []const u8, mode: u32, flags: u32) AccessError!void {
45054511 if (builtin.os.tag == .windows) {
4506 const path_w = try windows.sliceToPrefixedFileW(path);
4512 const path_w = try windows.sliceToPrefixedFileW(dirfd, path);
45074513 return faccessatW(dirfd, path_w.span().ptr, mode, flags);
45084514 } else if (builtin.os.tag == .wasi and !builtin.link_libc) {
45094515 var resolved = RelativePathWasi{ .dir_fd = dirfd, .relative_path = path };
......@@ -4546,7 +4552,7 @@ pub fn faccessat(dirfd: fd_t, path: []const u8, mode: u32, flags: u32) AccessErr
45464552/// Same as `faccessat` except the path parameter is null-terminated.
45474553pub fn faccessatZ(dirfd: fd_t, path: [*:0]const u8, mode: u32, flags: u32) AccessError!void {
45484554 if (builtin.os.tag == .windows) {
4549 const path_w = try windows.cStrToPrefixedFileW(path);
4555 const path_w = try windows.cStrToPrefixedFileW(dirfd, path);
45504556 return faccessatW(dirfd, path_w.span().ptr, mode, flags);
45514557 } else if (builtin.os.tag == .wasi and !builtin.link_libc) {
45524558 return faccessat(dirfd, mem.sliceTo(path, 0), mode, flags);
......@@ -5082,7 +5088,7 @@ pub const RealPathError = error{
50825088/// See also `realpathZ` and `realpathW`.
50835089pub fn realpath(pathname: []const u8, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {
50845090 if (builtin.os.tag == .windows) {
5085 const pathname_w = try windows.sliceToPrefixedFileW(pathname);
5091 const pathname_w = try windows.sliceToPrefixedFileW(null, pathname);
50865092 return realpathW(pathname_w.span(), out_buffer);
50875093 } else if (builtin.os.tag == .wasi and !builtin.link_libc) {
50885094 @compileError("WASI does not support os.realpath");
......@@ -5094,7 +5100,7 @@ pub fn realpath(pathname: []const u8, out_buffer: *[MAX_PATH_BYTES]u8) RealPathE
50945100/// Same as `realpath` except `pathname` is null-terminated.
50955101pub fn realpathZ(pathname: [*:0]const u8, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {
50965102 if (builtin.os.tag == .windows) {
5097 const pathname_w = try windows.cStrToPrefixedFileW(pathname);
5103 const pathname_w = try windows.cStrToPrefixedFileW(null, pathname);
50985104 return realpathW(pathname_w.span(), out_buffer);
50995105 } else if (builtin.os.tag == .wasi and !builtin.link_libc) {
51005106 return realpath(mem.sliceTo(pathname, 0), out_buffer);
lib/std/os/windows.zig+42-14
......@@ -170,7 +170,7 @@ pub fn CreatePipe(rd: *HANDLE, wr: *HANDLE, sattr: *const SECURITY_ATTRIBUTES) C
170170}
171171
172172pub fn CreateEventEx(attributes: ?*SECURITY_ATTRIBUTES, name: []const u8, flags: DWORD, desired_access: DWORD) !HANDLE {
173 const nameW = try sliceToPrefixedFileW(name);
173 const nameW = try sliceToPrefixedFileW(null, name);
174174 return CreateEventExW(attributes, nameW.span().ptr, flags, desired_access);
175175}
176176
......@@ -1007,8 +1007,8 @@ pub fn DeleteFile(sub_path_w: []const u16, options: DeleteFileOptions) DeleteFil
10071007pub const MoveFileError = error{ FileNotFound, AccessDenied, Unexpected };
10081008
10091009pub fn MoveFileEx(old_path: []const u8, new_path: []const u8, flags: DWORD) MoveFileError!void {
1010 const old_path_w = try sliceToPrefixedFileW(old_path);
1011 const new_path_w = try sliceToPrefixedFileW(new_path);
1010 const old_path_w = try sliceToPrefixedFileW(null, old_path);
1011 const new_path_w = try sliceToPrefixedFileW(null, new_path);
10121012 return MoveFileExW(old_path_w.span().ptr, new_path_w.span().ptr, flags);
10131013}
10141014
......@@ -1317,7 +1317,7 @@ pub const GetFileAttributesError = error{
13171317};
13181318
13191319pub fn GetFileAttributes(filename: []const u8) GetFileAttributesError!DWORD {
1320 const filename_w = try sliceToPrefixedFileW(filename);
1320 const filename_w = try sliceToPrefixedFileW(null, filename);
13211321 return GetFileAttributesW(filename_w.span().ptr);
13221322}
13231323
......@@ -2120,16 +2120,16 @@ pub fn normalizePath(comptime T: type, path: []T) RemoveDotDirsError!usize {
21202120
21212121/// Same as `sliceToPrefixedFileW` but accepts a pointer
21222122/// to a null-terminated path.
2123pub fn cStrToPrefixedFileW(s: [*:0]const u8) !PathSpace {
2124 return sliceToPrefixedFileW(mem.sliceTo(s, 0));
2123pub fn cStrToPrefixedFileW(dir: ?HANDLE, s: [*:0]const u8) !PathSpace {
2124 return sliceToPrefixedFileW(dir, mem.sliceTo(s, 0));
21252125}
21262126
21272127/// Same as `wToPrefixedFileW` but accepts a UTF-8 encoded path.
2128pub fn sliceToPrefixedFileW(path: []const u8) !PathSpace {
2128pub fn sliceToPrefixedFileW(dir: ?HANDLE, path: []const u8) !PathSpace {
21292129 var temp_path: PathSpace = undefined;
21302130 temp_path.len = try std.unicode.utf8ToUtf16Le(&temp_path.data, path);
21312131 temp_path.data[temp_path.len] = 0;
2132 return wToPrefixedFileW(temp_path.span());
2132 return wToPrefixedFileW(dir, temp_path.span());
21332133}
21342134
21352135/// Converts the `path` to WTF16, null-terminated. If the path contains any
......@@ -2139,11 +2139,11 @@ pub fn sliceToPrefixedFileW(path: []const u8) !PathSpace {
21392139/// Similar to RtlDosPathNameToNtPathName_U with a few differences:
21402140/// - Does not allocate on the heap.
21412141/// - Relative paths are kept as relative unless they contain too many ..
2142/// components, in which case they are treated as drive-relative and resolved
2143/// against the CWD.
2142/// components, in which case they are resolved against the `dir` if it
2143/// is non-null, or the CWD if it is null.
21442144/// - Special case device names like COM1, NUL, etc are not handled specially (TODO)
21452145/// - . and space are not stripped from the end of relative paths (potential TODO)
2146pub fn wToPrefixedFileW(path: [:0]const u16) !PathSpace {
2146pub fn wToPrefixedFileW(dir: ?HANDLE, path: [:0]const u16) !PathSpace {
21472147 const nt_prefix = [_]u16{ '\\', '?', '?', '\\' };
21482148 switch (getNamespacePrefix(u16, path)) {
21492149 // TODO: Figure out a way to design an API that can avoid the copy for .nt,
......@@ -2194,8 +2194,7 @@ pub fn wToPrefixedFileW(path: [:0]const u16) !PathSpace {
21942194
21952195 @memcpy(path_space.data[0..path.len], path);
21962196 // Try to normalize, but if we get too many parent directories,
2197 // then this is effectively a 'drive relative' path, so we need to
2198 // start over and use RtlGetFullPathName_U instead.
2197 // then we need to start over and use RtlGetFullPathName_U instead.
21992198 path_space.len = normalizePath(u16, path_space.data[0..path.len]) catch |err| switch (err) {
22002199 error.TooManyParentDirs => break :relative,
22012200 };
......@@ -2224,8 +2223,37 @@ pub fn wToPrefixedFileW(path: [:0]const u16) !PathSpace {
22242223 else => nt_prefix.len,
22252224 };
22262225 const buf_len = @as(u32, @intCast(path_space.data.len - path_buf_offset));
2226 const path_to_get: [:0]const u16 = path_to_get: {
2227 // If dir is null, then we don't need to bother with GetFinalPathNameByHandle because
2228 // RtlGetFullPathName_U will resolve relative paths against the CWD for us.
2229 if (path_type != .relative or dir == null) {
2230 break :path_to_get path;
2231 }
2232 // We can also skip GetFinalPathNameByHandle if the handle matches
2233 // the handle returned by fs.cwd()
2234 if (dir.? == std.fs.cwd().fd) {
2235 break :path_to_get path;
2236 }
2237 // At this point, we know we have a relative path that had too many
2238 // `..` components to be resolved by normalizePath, so we need to
2239 // convert it into an absolute path and let RtlGetFullPathName_U
2240 // canonicalize it. We do this by getting the path of the `dir`
2241 // and appending the relative path to it.
2242 var dir_path_buf: [PATH_MAX_WIDE:0]u16 = undefined;
2243 const dir_path = try GetFinalPathNameByHandle(dir.?, .{}, &dir_path_buf);
2244 if (dir_path.len + 1 + path.len > PATH_MAX_WIDE) {
2245 return error.NameTooLong;
2246 }
2247 // We don't have to worry about potentially doubling up path separators
2248 // here since RtlGetFullPathName_U will handle canonicalizing it.
2249 dir_path_buf[dir_path.len] = '\\';
2250 @memcpy(dir_path_buf[dir_path.len + 1 ..][0..path.len], path);
2251 const full_len = dir_path.len + 1 + path.len;
2252 dir_path_buf[full_len] = 0;
2253 break :path_to_get dir_path_buf[0..full_len :0];
2254 };
22272255 const path_byte_len = ntdll.RtlGetFullPathName_U(
2228 path.ptr,
2256 path_to_get.ptr,
22292257 buf_len * 2,
22302258 path_space.data[path_buf_offset..].ptr,
22312259 null,
lib/std/os/windows/test.zig+2-2
......@@ -28,7 +28,7 @@ fn RtlDosPathNameToNtPathName_U(path: [:0]const u16) !windows.PathSpace {
2828fn testToPrefixedFileNoOracle(comptime path: []const u8, comptime expected_path: []const u8) !void {
2929 const path_utf16 = std.unicode.utf8ToUtf16LeStringLiteral(path);
3030 const expected_path_utf16 = std.unicode.utf8ToUtf16LeStringLiteral(expected_path);
31 const actual_path = try windows.wToPrefixedFileW(path_utf16);
31 const actual_path = try windows.wToPrefixedFileW(null, path_utf16);
3232 std.testing.expectEqualSlices(u16, expected_path_utf16, actual_path.span()) catch |e| {
3333 std.debug.print("got '{s}', expected '{s}'\n", .{ std.unicode.fmtUtf16le(actual_path.span()), std.unicode.fmtUtf16le(expected_path_utf16) });
3434 return e;
......@@ -45,7 +45,7 @@ fn testToPrefixedFileWithOracle(comptime path: []const u8, comptime expected_pat
4545/// Test that the Zig conversion matches the conversion that RtlDosPathNameToNtPathName_U does.
4646fn testToPrefixedFileOnlyOracle(comptime path: []const u8) !void {
4747 const path_utf16 = std.unicode.utf8ToUtf16LeStringLiteral(path);
48 const zig_result = try windows.wToPrefixedFileW(path_utf16);
48 const zig_result = try windows.wToPrefixedFileW(null, path_utf16);
4949 const win32_api_result = try RtlDosPathNameToNtPathName_U(path_utf16);
5050 std.testing.expectEqualSlices(u16, win32_api_result.span(), zig_result.span()) catch |e| {
5151 std.debug.print("got '{s}', expected '{s}'\n", .{ std.unicode.fmtUtf16le(zig_result.span()), std.unicode.fmtUtf16le(win32_api_result.span()) });