Skip to content

Commit 2569835

Browse files
committed
Deprecate old realpathW correctly
- Rename modified `realpathW` to `realpathW2` - Re-add original `realpathW` - Add deprecation notice to `realpathW`
1 parent 7d25ecd commit 2569835

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

lib/std/fs.zig

+2-1
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;
@@ -653,7 +654,7 @@ pub fn selfExePath(out_buffer: []u8) SelfExePathError![]u8 {
653654
// that the symlink points to, though, so we need to get the realpath.
654655
var pathname_w = try windows.wToPrefixedFileW(null, image_path_name);
655656

656-
const wide_slice = std.fs.cwd().realpathW(pathname_w.span(), &pathname_w.data) catch |err| switch (err) {
657+
const wide_slice = std.fs.cwd().realpathW2(pathname_w.span(), &pathname_w.data) catch |err| switch (err) {
657658
error.InvalidWtf8 => unreachable,
658659
else => |e| return e,
659660
};

lib/std/fs/Dir.zig

+22-3
Original file line numberDiff line numberDiff line change
@@ -1284,7 +1284,7 @@ pub fn realpath(self: Dir, pathname: []const u8, out_buffer: []u8) RealPathError
12841284
if (native_os == .windows) {
12851285
var pathname_w = try windows.sliceToPrefixedFileW(self.fd, pathname);
12861286

1287-
const wide_slice = try self.realpathW(pathname_w.span(), &pathname_w.data);
1287+
const wide_slice = try self.realpathW2(pathname_w.span(), &pathname_w.data);
12881288

12891289
const len = std.unicode.calcWtf8Len(wide_slice);
12901290
if (len > out_buffer.len)
@@ -1303,7 +1303,7 @@ pub fn realpathZ(self: Dir, pathname: [*:0]const u8, out_buffer: []u8) RealPathE
13031303
if (native_os == .windows) {
13041304
var pathname_w = try windows.cStrToPrefixedFileW(self.fd, pathname);
13051305

1306-
const wide_slice = try self.realpathW(pathname_w.span(), &pathname_w.data);
1306+
const wide_slice = try self.realpathW2(pathname_w.span(), &pathname_w.data);
13071307

13081308
const len = std.unicode.calcWtf8Len(wide_slice);
13091309
if (len > out_buffer.len)
@@ -1339,6 +1339,25 @@ pub fn realpathZ(self: Dir, pathname: [*:0]const u8, out_buffer: []u8) RealPathE
13391339
return result;
13401340
}
13411341

1342+
/// Deprecated: use `realpathW2`.
1343+
///
1344+
/// Windows-only. Same as `Dir.realpath` except `pathname` is WTF16 LE encoded.
1345+
/// The result is encoded as [WTF-8](https://simonsapin.github.io/wtf-8/).
1346+
/// See also `Dir.realpath`, `realpathW`.
1347+
pub fn realpathW(self: Dir, pathname: []const u16, out_buffer: []u8) RealPathError![]u8 {
1348+
var wide_buf: [std.os.windows.PATH_MAX_WIDE]u16 = undefined;
1349+
1350+
const wide_slice = try self.realpathW2(pathname, &wide_buf);
1351+
1352+
var big_out_buf: [fs.max_path_bytes]u8 = undefined;
1353+
const end_index = std.unicode.wtf16LeToWtf8(&big_out_buf, wide_slice);
1354+
if (end_index > out_buffer.len)
1355+
return error.NameTooLong;
1356+
const result = out_buffer[0..end_index];
1357+
@memcpy(result, big_out_buf[0..end_index]);
1358+
return result;
1359+
}
1360+
13421361
/// Windows-only. Same as `Dir.realpath` except
13431362
/// * `pathname` and the result are WTF-16 LE encoded
13441363
/// * `pathname` is relative or has the NT namespace prefix. See `windows.wToPrefixedFileW` for details.
@@ -1347,7 +1366,7 @@ pub fn realpathZ(self: Dir, pathname: [*:0]const u8, out_buffer: []u8) RealPathE
13471366
/// is safe to reuse a single buffer for both.
13481367
///
13491368
/// See also `Dir.realpath`, `realpathW`.
1350-
pub fn realpathW(self: Dir, pathname: []const u16, out_buffer: []u16) RealPathError![]u16 {
1369+
pub fn realpathW2(self: Dir, pathname: []const u16, out_buffer: []u16) RealPathError![]u16 {
13511370
const w = windows;
13521371

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

lib/std/posix.zig

+15-4
Original file line numberDiff line numberDiff line change
@@ -5535,7 +5535,7 @@ pub fn realpath(pathname: []const u8, out_buffer: *[max_path_bytes]u8) RealPathE
55355535
if (native_os == .windows) {
55365536
var pathname_w = try windows.sliceToPrefixedFileW(null, pathname);
55375537

5538-
const wide_slice = try realpathW(pathname_w.span(), &pathname_w.data);
5538+
const wide_slice = try realpathW2(pathname_w.span(), &pathname_w.data);
55395539

55405540
const end_index = std.unicode.wtf16LeToWtf8(out_buffer, wide_slice);
55415541
return out_buffer[0..end_index];
@@ -5553,7 +5553,7 @@ pub fn realpathZ(pathname: [*:0]const u8, out_buffer: *[max_path_bytes]u8) RealP
55535553
if (native_os == .windows) {
55545554
var pathname_w = try windows.cStrToPrefixedFileW(null, pathname);
55555555

5556-
const wide_slice = try realpathW(pathname_w.span(), &pathname_w.data);
5556+
const wide_slice = try realpathW2(pathname_w.span(), &pathname_w.data);
55575557

55585558
const end_index = std.unicode.wtf16LeToWtf8(out_buffer, wide_slice);
55595559
return out_buffer[0..end_index];
@@ -5600,15 +5600,26 @@ pub fn realpathZ(pathname: [*:0]const u8, out_buffer: *[max_path_bytes]u8) RealP
56005600
return mem.sliceTo(result_path, 0);
56015601
}
56025602

5603+
/// Deprecated: use `realpathW2`.
5604+
///
56035605
/// Same as `realpath` except `pathname` is WTF16LE-encoded.
56045606
///
5605-
/// The result is encoded as WTF16LE.
5607+
/// The result is encoded as [WTF-8](https://simonsapin.github.io/wtf-8/).
56065608
///
56075609
/// Calling this function is usually a bug.
5608-
pub fn realpathW(pathname: []const u16, out_buffer: *[std.os.windows.PATH_MAX_WIDE]u16) RealPathError![]u16 {
5610+
pub fn realpathW(pathname: []const u16, out_buffer: *[max_path_bytes]u8) RealPathError![]u8 {
56095611
return fs.cwd().realpathW(pathname, out_buffer);
56105612
}
56115613

5614+
/// Same as `realpath` except `pathname` is WTF16LE-encoded.
5615+
///
5616+
/// The result is encoded as WTF16LE.
5617+
///
5618+
/// Calling this function is usually a bug.
5619+
pub fn realpathW2(pathname: []const u16, out_buffer: *[std.os.windows.PATH_MAX_WIDE]u16) RealPathError![]u16 {
5620+
return fs.cwd().realpathW2(pathname, out_buffer);
5621+
}
5622+
56125623
/// Spurious wakeups are possible and no precision of timing is guaranteed.
56135624
pub fn nanosleep(seconds: u64, nanoseconds: u64) void {
56145625
var req = timespec{

0 commit comments

Comments
 (0)