diff options
| author | Michael Pfaff <michael@pfaff.dev> | 2025-04-25 22:55:18 -0400 |
|---|---|---|
| committer | Ryan Liptak <squeek502@hotmail.com> | 2025-10-08 17:07:38 -0700 |
| commit | 7bf740ee718f4b6109cd9fe7014d1784d48ada48 (patch) | |
| tree | 0df4edc19cd18e44d78bbd7c20fe2fdf8031dd78 /lib/std | |
| parent | eb46bbba34c1ccb0fbbbd755fd190e1b6eb939ec (diff) | |
| download | zig-7bf740ee718f4b6109cd9fe7014d1784d48ada48.tar.gz zig-7bf740ee718f4b6109cd9fe7014d1784d48ada48.zip | |
Deprecate old realpathW correctly
- Rename modified `realpathW` to `realpathW2`
- Re-add original `realpathW`
- Add deprecation notice to `realpathW`
Diffstat (limited to 'lib/std')
| -rw-r--r-- | lib/std/fs.zig | 3 | ||||
| -rw-r--r-- | lib/std/fs/Dir.zig | 25 | ||||
| -rw-r--r-- | lib/std/posix.zig | 19 |
3 files changed, 39 insertions, 8 deletions
diff --git a/lib/std/fs.zig b/lib/std/fs.zig index e8baffc3bf..39f26effc7 100644 --- a/lib/std/fs.zig +++ b/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, }; diff --git a/lib/std/fs/Dir.zig b/lib/std/fs/Dir.zig index 394ac10fd7..7c5be3003e 100644 --- a/lib/std/fs/Dir.zig +++ b/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; diff --git a/lib/std/posix.zig b/lib/std/posix.zig index 97807faa71..9ba8e010e8 100644 --- a/lib/std/posix.zig +++ b/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{ |
