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 {...@@ -642,10 +642,9 @@ pub fn selfExePath(out_buffer: []u8) SelfExePathError![]u8 {
642 // If ImagePathName is a symlink, then it will contain the path of the642 // If ImagePathName is a symlink, then it will contain the path of the
643 // symlink, not the path that the symlink points to. We want the path643 // symlink, not the path that the symlink points to. We want the path
644 // that the symlink points to, though, so we need to get the realpath.644 // 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;647 const wide_slice = std.fs.cwd().realpathW(pathname_w.span(), &pathname_w.data) catch |err| switch (err) {
648 const wide_slice = std.fs.cwd().realpathW(pathname_w.span(), &wide_buf) catch |err| switch (err) {
649 error.InvalidWtf8 => unreachable,648 error.InvalidWtf8 => unreachable,
650 else => |e| return e,649 else => |e| return e,
651 };650 };
lib/std/fs/Dir.zig+4-6
...@@ -1369,10 +1369,9 @@ pub fn realpath(self: Dir, pathname: []const u8, out_buffer: []u8) RealPathError...@@ -1369,10 +1369,9 @@ pub fn realpath(self: Dir, pathname: []const u8, out_buffer: []u8) RealPathError
1369 @compileError("realpath is not available on WASI");1369 @compileError("realpath is not available on WASI");
1370 }1370 }
1371 if (native_os == .windows) {1371 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;1374 const wide_slice = try self.realpathW(pathname_w.span(), &pathname_w.data);
1375 const wide_slice = try self.realpathW(pathname_w.span(), &wide_buf);
13761375
1377 const len = std.unicode.calcWtf8Len(wide_slice);1376 const len = std.unicode.calcWtf8Len(wide_slice);
1378 if (len > out_buffer.len)1377 if (len > out_buffer.len)
...@@ -1389,10 +1388,9 @@ pub fn realpath(self: Dir, pathname: []const u8, out_buffer: []u8) RealPathError...@@ -1389,10 +1388,9 @@ pub fn realpath(self: Dir, pathname: []const u8, out_buffer: []u8) RealPathError
1389/// See also `Dir.realpath`, `realpathZ`.1388/// See also `Dir.realpath`, `realpathZ`.
1390pub fn realpathZ(self: Dir, pathname: [*:0]const u8, out_buffer: []u8) RealPathError![]u8 {1389pub fn realpathZ(self: Dir, pathname: [*:0]const u8, out_buffer: []u8) RealPathError![]u8 {
1391 if (native_os == .windows) {1390 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;1393 const wide_slice = try self.realpathW(pathname_w.span(), &pathname_w.data);
1395 const wide_slice = try self.realpathW(pathname_w.span(), &wide_buf);
13961394
1397 const len = std.unicode.calcWtf8Len(wide_slice);1395 const len = std.unicode.calcWtf8Len(wide_slice);
1398 if (len > out_buffer.len)1396 if (len > out_buffer.len)
lib/std/posix.zig+4-6
...@@ -5675,10 +5675,9 @@ pub const RealPathError = error{...@@ -5675,10 +5675,9 @@ pub const RealPathError = error{
5675/// Calling this function is usually a bug.5675/// Calling this function is usually a bug.
5676pub fn realpath(pathname: []const u8, out_buffer: *[max_path_bytes]u8) RealPathError![]u8 {5676pub fn realpath(pathname: []const u8, out_buffer: *[max_path_bytes]u8) RealPathError![]u8 {
5677 if (native_os == .windows) {5677 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;5680 const wide_slice = try realpathW(pathname_w.span(), &pathname_w.data);
5681 const wide_slice = try realpathW(pathname_w.span(), &wide_buf);
56825681
5683 const end_index = std.unicode.wtf16LeToWtf8(out_buffer, wide_slice);5682 const end_index = std.unicode.wtf16LeToWtf8(out_buffer, wide_slice);
5684 return out_buffer[0..end_index];5683 return out_buffer[0..end_index];
...@@ -5694,10 +5693,9 @@ pub fn realpath(pathname: []const u8, out_buffer: *[max_path_bytes]u8) RealPathE...@@ -5694,10 +5693,9 @@ pub fn realpath(pathname: []const u8, out_buffer: *[max_path_bytes]u8) RealPathE
5694/// Calling this function is usually a bug.5693/// Calling this function is usually a bug.
5695pub fn realpathZ(pathname: [*:0]const u8, out_buffer: *[max_path_bytes]u8) RealPathError![]u8 {5694pub fn realpathZ(pathname: [*:0]const u8, out_buffer: *[max_path_bytes]u8) RealPathError![]u8 {
5696 if (native_os == .windows) {5695 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;5698 const wide_slice = try realpathW(pathname_w.span(), &pathname_w.data);
5700 const wide_slice = try realpathW(pathname_w.span(), &wide_buf);
57015699
5702 const end_index = std.unicode.wtf16LeToWtf8(out_buffer, wide_slice);5700 const end_index = std.unicode.wtf16LeToWtf8(out_buffer, wide_slice);
5703 return out_buffer[0..end_index];5701 return out_buffer[0..end_index];