authorgravatar for michael@pfaff.devMichael Pfaff <michael@pfaff.dev> 2025-04-25 22:55:18-04:00
committergravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2025-10-08 17:07:38-07:00
log7bf740ee718f4b6109cd9fe7014d1784d48ada48
tree0df4edc19cd18e44d78bbd7c20fe2fdf8031dd78
parenteb46bbba34c1ccb0fbbbd755fd190e1b6eb939ec

Deprecate old realpathW correctly

- Rename modified `realpathW` to `realpathW2` - Re-add original `realpathW` - Add deprecation notice to `realpathW`

3 files changed, 39 insertions(+), 8 deletions(-)

lib/std/fs.zig+2-1
...@@ -31,6 +31,7 @@ pub const wasi = @import("fs/wasi.zig");...@@ -31,6 +31,7 @@ pub const wasi = @import("fs/wasi.zig");
31pub const realpath = posix.realpath;31pub const realpath = posix.realpath;
32pub const realpathZ = posix.realpathZ;32pub const realpathZ = posix.realpathZ;
33pub const realpathW = posix.realpathW;33pub const realpathW = posix.realpathW;
34pub const realpathW2 = posix.realpathW2;
3435
35pub const getAppDataDir = @import("fs/get_app_data_dir.zig").getAppDataDir;36pub const getAppDataDir = @import("fs/get_app_data_dir.zig").getAppDataDir;
36pub const GetAppDataDirError = @import("fs/get_app_data_dir.zig").GetAppDataDirError;37pub const GetAppDataDirError = @import("fs/get_app_data_dir.zig").GetAppDataDirError;
...@@ -644,7 +645,7 @@ pub fn selfExePath(out_buffer: []u8) SelfExePathError![]u8 {...@@ -644,7 +645,7 @@ pub fn selfExePath(out_buffer: []u8) SelfExePathError![]u8 {
644 // that the symlink points to, though, so we need to get the realpath.645 // that the symlink points to, though, so we need to get the realpath.
645 var pathname_w = try windows.wToPrefixedFileW(null, image_path_name);646 var pathname_w = try windows.wToPrefixedFileW(null, image_path_name);
646647
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().realpathW2(pathname_w.span(), &pathname_w.data) catch |err| switch (err) {
648 error.InvalidWtf8 => unreachable,649 error.InvalidWtf8 => unreachable,
649 else => |e| return e,650 else => |e| return e,
650 };651 };
lib/std/fs/Dir.zig+22-3
...@@ -1371,7 +1371,7 @@ pub fn realpath(self: Dir, pathname: []const u8, out_buffer: []u8) RealPathError...@@ -1371,7 +1371,7 @@ pub fn realpath(self: Dir, pathname: []const u8, out_buffer: []u8) RealPathError
1371 if (native_os == .windows) {1371 if (native_os == .windows) {
1372 var pathname_w = try windows.sliceToPrefixedFileW(self.fd, pathname);1372 var pathname_w = try windows.sliceToPrefixedFileW(self.fd, pathname);
13731373
1374 const wide_slice = try self.realpathW(pathname_w.span(), &pathname_w.data);1374 const wide_slice = try self.realpathW2(pathname_w.span(), &pathname_w.data);
13751375
1376 const len = std.unicode.calcWtf8Len(wide_slice);1376 const len = std.unicode.calcWtf8Len(wide_slice);
1377 if (len > out_buffer.len)1377 if (len > out_buffer.len)
...@@ -1390,7 +1390,7 @@ pub fn realpathZ(self: Dir, pathname: [*:0]const u8, out_buffer: []u8) RealPathE...@@ -1390,7 +1390,7 @@ pub fn realpathZ(self: Dir, pathname: [*:0]const u8, out_buffer: []u8) RealPathE
1390 if (native_os == .windows) {1390 if (native_os == .windows) {
1391 var pathname_w = try windows.cStrToPrefixedFileW(self.fd, pathname);1391 var pathname_w = try windows.cStrToPrefixedFileW(self.fd, pathname);
13921392
1393 const wide_slice = try self.realpathW(pathname_w.span(), &pathname_w.data);1393 const wide_slice = try self.realpathW2(pathname_w.span(), &pathname_w.data);
13941394
1395 const len = std.unicode.calcWtf8Len(wide_slice);1395 const len = std.unicode.calcWtf8Len(wide_slice);
1396 if (len > out_buffer.len)1396 if (len > out_buffer.len)
...@@ -1426,6 +1426,25 @@ pub fn realpathZ(self: Dir, pathname: [*:0]const u8, out_buffer: []u8) RealPathE...@@ -1426,6 +1426,25 @@ pub fn realpathZ(self: Dir, pathname: [*:0]const u8, out_buffer: []u8) RealPathE
1426 return result;1426 return result;
1427}1427}
14281428
1429/// Deprecated: use `realpathW2`.
1430///
1431/// Windows-only. Same as `Dir.realpath` except `pathname` is WTF16 LE encoded.
1432/// The result is encoded as [WTF-8](https://simonsapin.github.io/wtf-8/).
1433/// See also `Dir.realpath`, `realpathW`.
1434pub fn realpathW(self: Dir, pathname: []const u16, out_buffer: []u8) RealPathError![]u8 {
1435 var wide_buf: [std.os.windows.PATH_MAX_WIDE]u16 = undefined;
1436
1437 const wide_slice = try self.realpathW2(pathname, &wide_buf);
1438
1439 var big_out_buf: [fs.max_path_bytes]u8 = undefined;
1440 const end_index = std.unicode.wtf16LeToWtf8(&big_out_buf, wide_slice);
1441 if (end_index > out_buffer.len)
1442 return error.NameTooLong;
1443 const result = out_buffer[0..end_index];
1444 @memcpy(result, big_out_buf[0..end_index]);
1445 return result;
1446}
1447
1429/// Windows-only. Same as `Dir.realpath` except1448/// Windows-only. Same as `Dir.realpath` except
1430/// * `pathname` and the result are WTF-16 LE encoded1449/// * `pathname` and the result are WTF-16 LE encoded
1431/// * `pathname` is relative or has the NT namespace prefix. See `windows.wToPrefixedFileW` for details.1450/// * `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...@@ -1434,7 +1453,7 @@ pub fn realpathZ(self: Dir, pathname: [*:0]const u8, out_buffer: []u8) RealPathE
1434/// is safe to reuse a single buffer for both.1453/// is safe to reuse a single buffer for both.
1435///1454///
1436/// See also `Dir.realpath`, `realpathW`.1455/// See also `Dir.realpath`, `realpathW`.
1437pub fn realpathW(self: Dir, pathname: []const u16, out_buffer: []u16) RealPathError![]u16 {1456pub fn realpathW2(self: Dir, pathname: []const u16, out_buffer: []u16) RealPathError![]u16 {
1438 const w = windows;1457 const w = windows;
14391458
1440 const access_mask = w.GENERIC_READ | w.SYNCHRONIZE;1459 const access_mask = w.GENERIC_READ | w.SYNCHRONIZE;
lib/std/posix.zig+15-4
...@@ -5677,7 +5677,7 @@ pub fn realpath(pathname: []const u8, out_buffer: *[max_path_bytes]u8) RealPathE...@@ -5677,7 +5677,7 @@ pub fn realpath(pathname: []const u8, out_buffer: *[max_path_bytes]u8) RealPathE
5677 if (native_os == .windows) {5677 if (native_os == .windows) {
5678 var pathname_w = try windows.sliceToPrefixedFileW(null, pathname);5678 var pathname_w = try windows.sliceToPrefixedFileW(null, pathname);
56795679
5680 const wide_slice = try realpathW(pathname_w.span(), &pathname_w.data);5680 const wide_slice = try realpathW2(pathname_w.span(), &pathname_w.data);
56815681
5682 const end_index = std.unicode.wtf16LeToWtf8(out_buffer, wide_slice);5682 const end_index = std.unicode.wtf16LeToWtf8(out_buffer, wide_slice);
5683 return out_buffer[0..end_index];5683 return out_buffer[0..end_index];
...@@ -5695,7 +5695,7 @@ pub fn realpathZ(pathname: [*:0]const u8, out_buffer: *[max_path_bytes]u8) RealP...@@ -5695,7 +5695,7 @@ pub fn realpathZ(pathname: [*:0]const u8, out_buffer: *[max_path_bytes]u8) RealP
5695 if (native_os == .windows) {5695 if (native_os == .windows) {
5696 var pathname_w = try windows.cStrToPrefixedFileW(null, pathname);5696 var pathname_w = try windows.cStrToPrefixedFileW(null, pathname);
56975697
5698 const wide_slice = try realpathW(pathname_w.span(), &pathname_w.data);5698 const wide_slice = try realpathW2(pathname_w.span(), &pathname_w.data);
56995699
5700 const end_index = std.unicode.wtf16LeToWtf8(out_buffer, wide_slice);5700 const end_index = std.unicode.wtf16LeToWtf8(out_buffer, wide_slice);
5701 return out_buffer[0..end_index];5701 return out_buffer[0..end_index];
...@@ -5742,15 +5742,26 @@ pub fn realpathZ(pathname: [*:0]const u8, out_buffer: *[max_path_bytes]u8) RealP...@@ -5742,15 +5742,26 @@ pub fn realpathZ(pathname: [*:0]const u8, out_buffer: *[max_path_bytes]u8) RealP
5742 return mem.sliceTo(result_path, 0);5742 return mem.sliceTo(result_path, 0);
5743}5743}
57445744
5745/// Deprecated: use `realpathW2`.
5746///
5745/// Same as `realpath` except `pathname` is WTF16LE-encoded.5747/// Same as `realpath` except `pathname` is WTF16LE-encoded.
5746///5748///
5747/// The result is encoded as WTF16LE.5749/// The result is encoded as [WTF-8](https://simonsapin.github.io/wtf-8/).
5748///5750///
5749/// Calling this function is usually a bug.5751/// Calling this function is usually a bug.
5750pub fn realpathW(pathname: []const u16, out_buffer: *[std.os.windows.PATH_MAX_WIDE]u16) RealPathError![]u16 {5752pub fn realpathW(pathname: []const u16, out_buffer: *[max_path_bytes]u8) RealPathError![]u8 {
5751 return fs.cwd().realpathW(pathname, out_buffer);5753 return fs.cwd().realpathW(pathname, out_buffer);
5752}5754}
57535755
5756/// Same as `realpath` except `pathname` is WTF16LE-encoded.
5757///
5758/// The result is encoded as WTF16LE.
5759///
5760/// Calling this function is usually a bug.
5761pub fn realpathW2(pathname: []const u16, out_buffer: *[std.os.windows.PATH_MAX_WIDE]u16) RealPathError![]u16 {
5762 return fs.cwd().realpathW2(pathname, out_buffer);
5763}
5764
5754/// Spurious wakeups are possible and no precision of timing is guaranteed.5765/// Spurious wakeups are possible and no precision of timing is guaranteed.
5755pub fn nanosleep(seconds: u64, nanoseconds: u64) void {5766pub fn nanosleep(seconds: u64, nanoseconds: u64) void {
5756 var req = timespec{5767 var req = timespec{