Commit 7bf740ee71

Michael Pfaff <michael@pfaff.dev>
2025-04-26 04:55:18
Deprecate old realpathW correctly
- Rename modified `realpathW` to `realpathW2` - Re-add original `realpathW` - Add deprecation notice to `realpathW`
1 parent eb46bbb
Changed files (3)
lib/std/fs/Dir.zig
@@ -1371,7 +1371,7 @@ pub fn realpath(self: Dir, pathname: []const u8, out_buffer: []u8) RealPathError
     if (native_os == .windows) {
         var pathname_w = try windows.sliceToPrefixedFileW(self.fd, pathname);
 
-        const wide_slice = try self.realpathW(pathname_w.span(), &pathname_w.data);
+        const wide_slice = try self.realpathW2(pathname_w.span(), &pathname_w.data);
 
         const len = std.unicode.calcWtf8Len(wide_slice);
         if (len > out_buffer.len)
@@ -1390,7 +1390,7 @@ pub fn realpathZ(self: Dir, pathname: [*:0]const u8, out_buffer: []u8) RealPathE
     if (native_os == .windows) {
         var pathname_w = try windows.cStrToPrefixedFileW(self.fd, pathname);
 
-        const wide_slice = try self.realpathW(pathname_w.span(), &pathname_w.data);
+        const wide_slice = try self.realpathW2(pathname_w.span(), &pathname_w.data);
 
         const len = std.unicode.calcWtf8Len(wide_slice);
         if (len > out_buffer.len)
@@ -1426,6 +1426,25 @@ pub fn realpathZ(self: Dir, pathname: [*:0]const u8, out_buffer: []u8) RealPathE
     return result;
 }
 
+/// Deprecated: use `realpathW2`.
+///
+/// Windows-only. Same as `Dir.realpath` except `pathname` is WTF16 LE encoded.
+/// The result is encoded as [WTF-8](https://simonsapin.github.io/wtf-8/).
+/// See also `Dir.realpath`, `realpathW`.
+pub fn realpathW(self: Dir, pathname: []const u16, out_buffer: []u8) RealPathError![]u8 {
+    var wide_buf: [std.os.windows.PATH_MAX_WIDE]u16 = undefined;
+
+    const wide_slice = try self.realpathW2(pathname, &wide_buf);
+
+    var big_out_buf: [fs.max_path_bytes]u8 = undefined;
+    const end_index = std.unicode.wtf16LeToWtf8(&big_out_buf, wide_slice);
+    if (end_index > out_buffer.len)
+        return error.NameTooLong;
+    const result = out_buffer[0..end_index];
+    @memcpy(result, big_out_buf[0..end_index]);
+    return result;
+}
+
 /// Windows-only. Same as `Dir.realpath` except
 /// * `pathname` and the result are WTF-16 LE encoded
 /// * `pathname` is relative or has the NT namespace prefix. See `windows.wToPrefixedFileW` for details.
@@ -1434,7 +1453,7 @@ pub fn realpathZ(self: Dir, pathname: [*:0]const u8, out_buffer: []u8) RealPathE
 /// is safe to reuse a single buffer for both.
 ///
 /// See also `Dir.realpath`, `realpathW`.
-pub fn realpathW(self: Dir, pathname: []const u16, out_buffer: []u16) RealPathError![]u16 {
+pub fn realpathW2(self: Dir, pathname: []const u16, out_buffer: []u16) RealPathError![]u16 {
     const w = windows;
 
     const access_mask = w.GENERIC_READ | w.SYNCHRONIZE;
lib/std/fs.zig
@@ -31,6 +31,7 @@ pub const wasi = @import("fs/wasi.zig");
 pub const realpath = posix.realpath;
 pub const realpathZ = posix.realpathZ;
 pub const realpathW = posix.realpathW;
+pub const realpathW2 = posix.realpathW2;
 
 pub const getAppDataDir = @import("fs/get_app_data_dir.zig").getAppDataDir;
 pub const GetAppDataDirError = @import("fs/get_app_data_dir.zig").GetAppDataDirError;
@@ -644,7 +645,7 @@ pub fn selfExePath(out_buffer: []u8) SelfExePathError![]u8 {
             // that the symlink points to, though, so we need to get the realpath.
             var pathname_w = try windows.wToPrefixedFileW(null, image_path_name);
 
-            const wide_slice = std.fs.cwd().realpathW(pathname_w.span(), &pathname_w.data) catch |err| switch (err) {
+            const wide_slice = std.fs.cwd().realpathW2(pathname_w.span(), &pathname_w.data) catch |err| switch (err) {
                 error.InvalidWtf8 => unreachable,
                 else => |e| return e,
             };
lib/std/posix.zig
@@ -5677,7 +5677,7 @@ pub fn realpath(pathname: []const u8, out_buffer: *[max_path_bytes]u8) RealPathE
     if (native_os == .windows) {
         var pathname_w = try windows.sliceToPrefixedFileW(null, pathname);
 
-        const wide_slice = try realpathW(pathname_w.span(), &pathname_w.data);
+        const wide_slice = try realpathW2(pathname_w.span(), &pathname_w.data);
 
         const end_index = std.unicode.wtf16LeToWtf8(out_buffer, wide_slice);
         return out_buffer[0..end_index];
@@ -5695,7 +5695,7 @@ pub fn realpathZ(pathname: [*:0]const u8, out_buffer: *[max_path_bytes]u8) RealP
     if (native_os == .windows) {
         var pathname_w = try windows.cStrToPrefixedFileW(null, pathname);
 
-        const wide_slice = try realpathW(pathname_w.span(), &pathname_w.data);
+        const wide_slice = try realpathW2(pathname_w.span(), &pathname_w.data);
 
         const end_index = std.unicode.wtf16LeToWtf8(out_buffer, wide_slice);
         return out_buffer[0..end_index];
@@ -5742,15 +5742,26 @@ pub fn realpathZ(pathname: [*:0]const u8, out_buffer: *[max_path_bytes]u8) RealP
     return mem.sliceTo(result_path, 0);
 }
 
+/// Deprecated: use `realpathW2`.
+///
 /// Same as `realpath` except `pathname` is WTF16LE-encoded.
 ///
-/// The result is encoded as WTF16LE.
+/// The result is encoded as [WTF-8](https://simonsapin.github.io/wtf-8/).
 ///
 /// Calling this function is usually a bug.
-pub fn realpathW(pathname: []const u16, out_buffer: *[std.os.windows.PATH_MAX_WIDE]u16) RealPathError![]u16 {
+pub fn realpathW(pathname: []const u16, out_buffer: *[max_path_bytes]u8) RealPathError![]u8 {
     return fs.cwd().realpathW(pathname, out_buffer);
 }
 
+/// Same as `realpath` except `pathname` is WTF16LE-encoded.
+///
+/// The result is encoded as WTF16LE.
+///
+/// Calling this function is usually a bug.
+pub fn realpathW2(pathname: []const u16, out_buffer: *[std.os.windows.PATH_MAX_WIDE]u16) RealPathError![]u16 {
+    return fs.cwd().realpathW2(pathname, out_buffer);
+}
+
 /// Spurious wakeups are possible and no precision of timing is guaranteed.
 pub fn nanosleep(seconds: u64, nanoseconds: u64) void {
     var req = timespec{