authorgravatar for michael@pfaff.devMichael Pfaff <michael@pfaff.dev> 2025-04-23 10:38:35-04:00
committergravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2025-10-08 17:07:38-07:00
loga7dfc6470f0cb73c53f11590cf89d6348a81030e
treee5507b90658e5e6289456018e2b3d76e804c6ccb
parent2886a8bf45d3e340282c7f0dc8b35455e935f0b2

Reuse pathname_w buffer as out_buffer when calling realpathW


3 files changed, 10 insertions(+), 15 deletions(-)

lib/std/fs.zig+2-3
......@@ -642,10 +642,9 @@ pub fn selfExePath(out_buffer: []u8) SelfExePathError![]u8 {
642642 // If ImagePathName is a symlink, then it will contain the path of the
643643 // symlink, not the path that the symlink points to. We want the path
644644 // that the symlink points to, though, so we need to get the realpath.
645 const pathname_w = try windows.wToPrefixedFileW(null, image_path_name);
645 var pathname_w = try windows.wToPrefixedFileW(null, image_path_name);
646646
647 var wide_buf: [windows.PATH_MAX_WIDE]u16 = undefined;
648 const wide_slice = std.fs.cwd().realpathW(pathname_w.span(), &wide_buf) catch |err| switch (err) {
647 const wide_slice = std.fs.cwd().realpathW(pathname_w.span(), &pathname_w.data) catch |err| switch (err) {
649648 error.InvalidWtf8 => unreachable,
650649 else => |e| return e,
651650 };
lib/std/fs/Dir.zig+4-6
......@@ -1369,10 +1369,9 @@ pub fn realpath(self: Dir, pathname: []const u8, out_buffer: []u8) RealPathError
13691369 @compileError("realpath is not available on WASI");
13701370 }
13711371 if (native_os == .windows) {
1372 const pathname_w = try windows.sliceToPrefixedFileW(self.fd, pathname);
1372 var pathname_w = try windows.sliceToPrefixedFileW(self.fd, pathname);
13731373
1374 var wide_buf: [windows.PATH_MAX_WIDE]u16 = undefined;
1375 const wide_slice = try self.realpathW(pathname_w.span(), &wide_buf);
1374 const wide_slice = try self.realpathW(pathname_w.span(), &pathname_w.data);
13761375
13771376 const len = std.unicode.calcWtf8Len(wide_slice);
13781377 if (len > out_buffer.len)
......@@ -1389,10 +1388,9 @@ pub fn realpath(self: Dir, pathname: []const u8, out_buffer: []u8) RealPathError
13891388/// See also `Dir.realpath`, `realpathZ`.
13901389pub fn realpathZ(self: Dir, pathname: [*:0]const u8, out_buffer: []u8) RealPathError![]u8 {
13911390 if (native_os == .windows) {
1392 const pathname_w = try windows.cStrToPrefixedFileW(self.fd, pathname);
1391 var pathname_w = try windows.cStrToPrefixedFileW(self.fd, pathname);
13931392
1394 var wide_buf: [windows.PATH_MAX_WIDE]u16 = undefined;
1395 const wide_slice = try self.realpathW(pathname_w.span(), &wide_buf);
1393 const wide_slice = try self.realpathW(pathname_w.span(), &pathname_w.data);
13961394
13971395 const len = std.unicode.calcWtf8Len(wide_slice);
13981396 if (len > out_buffer.len)
lib/std/posix.zig+4-6
......@@ -5675,10 +5675,9 @@ pub const RealPathError = error{
56755675/// Calling this function is usually a bug.
56765676pub fn realpath(pathname: []const u8, out_buffer: *[max_path_bytes]u8) RealPathError![]u8 {
56775677 if (native_os == .windows) {
5678 const pathname_w = try windows.sliceToPrefixedFileW(null, pathname);
5678 var pathname_w = try windows.sliceToPrefixedFileW(null, pathname);
56795679
5680 var wide_buf: [windows.PATH_MAX_WIDE]u16 = undefined;
5681 const wide_slice = try realpathW(pathname_w.span(), &wide_buf);
5680 const wide_slice = try realpathW(pathname_w.span(), &pathname_w.data);
56825681
56835682 const end_index = std.unicode.wtf16LeToWtf8(out_buffer, wide_slice);
56845683 return out_buffer[0..end_index];
......@@ -5694,10 +5693,9 @@ pub fn realpath(pathname: []const u8, out_buffer: *[max_path_bytes]u8) RealPathE
56945693/// Calling this function is usually a bug.
56955694pub fn realpathZ(pathname: [*:0]const u8, out_buffer: *[max_path_bytes]u8) RealPathError![]u8 {
56965695 if (native_os == .windows) {
5697 const pathname_w = try windows.cStrToPrefixedFileW(null, pathname);
5696 var pathname_w = try windows.cStrToPrefixedFileW(null, pathname);
56985697
5699 var wide_buf: [windows.PATH_MAX_WIDE]u16 = undefined;
5700 const wide_slice = try realpathW(pathname_w.span(), &wide_buf);
5698 const wide_slice = try realpathW(pathname_w.span(), &pathname_w.data);
57015699
57025700 const end_index = std.unicode.wtf16LeToWtf8(out_buffer, wide_slice);
57035701 return out_buffer[0..end_index];