Skip to content

Commit 7bf740e

Browse files
mpfaffsqueek502
authored andcommitted
Deprecate old realpathW correctly
- Rename modified `realpathW` to `realpathW2` - Re-add original `realpathW` - Add deprecation notice to `realpathW`
1 parent eb46bbb commit 7bf740e

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

lib/std/fs.zig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub const wasi = @import("fs/wasi.zig");
3131
pub const realpath = posix.realpath;
3232
pub const realpathZ = posix.realpathZ;
3333
pub const realpathW = posix.realpathW;
34+
pub const realpathW2 = posix.realpathW2;
3435

3536
pub const getAppDataDir = @import("fs/get_app_data_dir.zig").getAppDataDir;
3637
pub const GetAppDataDirError = @import("fs/get_app_data_dir.zig").GetAppDataDirError;
@@ -644,7 +645,7 @@ pub fn selfExePath(out_buffer: []u8) SelfExePathError![]u8 {
644645
// that the symlink points to, though, so we need to get the realpath.
645646
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) {
648649
error.InvalidWtf8 => unreachable,
649650
else => |e| return e,
650651
};

lib/std/fs/Dir.zig

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,7 +1371,7 @@ pub fn realpath(self: Dir, pathname: []const u8, out_buffer: []u8) RealPathError
13711371
if (native_os == .windows) {
13721372
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

13761376
const len = std.unicode.calcWtf8Len(wide_slice);
13771377
if (len > out_buffer.len)
@@ -1390,7 +1390,7 @@ pub fn realpathZ(self: Dir, pathname: [*:0]const u8, out_buffer: []u8) RealPathE
13901390
if (native_os == .windows) {
13911391
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

13951395
const len = std.unicode.calcWtf8Len(wide_slice);
13961396
if (len > out_buffer.len)
@@ -1426,6 +1426,25 @@ pub fn realpathZ(self: Dir, pathname: [*:0]const u8, out_buffer: []u8) RealPathE
14261426
return result;
14271427
}
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`.
1434+
pub 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+
14291448
/// Windows-only. Same as `Dir.realpath` except
14301449
/// * `pathname` and the result are WTF-16 LE encoded
14311450
/// * `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
14341453
/// is safe to reuse a single buffer for both.
14351454
///
14361455
/// See also `Dir.realpath`, `realpathW`.
1437-
pub fn realpathW(self: Dir, pathname: []const u16, out_buffer: []u16) RealPathError![]u16 {
1456+
pub fn realpathW2(self: Dir, pathname: []const u16, out_buffer: []u16) RealPathError![]u16 {
14381457
const w = windows;
14391458

14401459
const access_mask = w.GENERIC_READ | w.SYNCHRONIZE;

lib/std/posix.zig

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5677,7 +5677,7 @@ pub fn realpath(pathname: []const u8, out_buffer: *[max_path_bytes]u8) RealPathE
56775677
if (native_os == .windows) {
56785678
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

56825682
const end_index = std.unicode.wtf16LeToWtf8(out_buffer, wide_slice);
56835683
return out_buffer[0..end_index];
@@ -5695,7 +5695,7 @@ pub fn realpathZ(pathname: [*:0]const u8, out_buffer: *[max_path_bytes]u8) RealP
56955695
if (native_os == .windows) {
56965696
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

57005700
const end_index = std.unicode.wtf16LeToWtf8(out_buffer, wide_slice);
57015701
return out_buffer[0..end_index];
@@ -5742,15 +5742,26 @@ pub fn realpathZ(pathname: [*:0]const u8, out_buffer: *[max_path_bytes]u8) RealP
57425742
return mem.sliceTo(result_path, 0);
57435743
}
57445744

5745+
/// Deprecated: use `realpathW2`.
5746+
///
57455747
/// Same as `realpath` except `pathname` is WTF16LE-encoded.
57465748
///
5747-
/// The result is encoded as WTF16LE.
5749+
/// The result is encoded as [WTF-8](https://simonsapin.github.io/wtf-8/).
57485750
///
57495751
/// Calling this function is usually a bug.
5750-
pub fn realpathW(pathname: []const u16, out_buffer: *[std.os.windows.PATH_MAX_WIDE]u16) RealPathError![]u16 {
5752+
pub fn realpathW(pathname: []const u16, out_buffer: *[max_path_bytes]u8) RealPathError![]u8 {
57515753
return fs.cwd().realpathW(pathname, out_buffer);
57525754
}
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.
5761+
pub 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+
57545765
/// Spurious wakeups are possible and no precision of timing is guaranteed.
57555766
pub fn nanosleep(seconds: u64, nanoseconds: u64) void {
57565767
var req = timespec{

0 commit comments

Comments
 (0)