@@ -1370,7 +1370,16 @@ pub fn realpath(self: Dir, pathname: []const u8, out_buffer: []u8) RealPathError
13701370 }
13711371 if (native_os == .windows ) {
13721372 const pathname_w = try windows .sliceToPrefixedFileW (self .fd , pathname );
1373- return self .realpathW (pathname_w .span (), out_buffer );
1373+
1374+ var wide_buf : [windows .PATH_MAX_WIDE ]u16 = undefined ;
1375+ const wide_slice = try self .realpathW (pathname_w .span (), & wide_buf );
1376+
1377+ const len = std .unicode .calcWtf8Len (wide_slice );
1378+ if (len > out_buffer .len )
1379+ return error .NameTooLong ;
1380+
1381+ const end_index = std .unicode .wtf16LeToWtf8 (out_buffer , wide_slice );
1382+ return out_buffer [0.. end_index ];
13741383 }
13751384 const pathname_c = try posix .toPosixPath (pathname );
13761385 return self .realpathZ (& pathname_c , out_buffer );
@@ -1381,7 +1390,16 @@ pub fn realpath(self: Dir, pathname: []const u8, out_buffer: []u8) RealPathError
13811390pub fn realpathZ (self : Dir , pathname : [* :0 ]const u8 , out_buffer : []u8 ) RealPathError ! []u8 {
13821391 if (native_os == .windows ) {
13831392 const pathname_w = try windows .cStrToPrefixedFileW (self .fd , pathname );
1384- return self .realpathW (pathname_w .span (), out_buffer );
1393+
1394+ var wide_buf : [windows .PATH_MAX_WIDE ]u16 = undefined ;
1395+ const wide_slice = try self .realpathW (pathname_w .span (), & wide_buf );
1396+
1397+ const len = std .unicode .calcWtf8Len (wide_slice );
1398+ if (len > out_buffer .len )
1399+ return error .NameTooLong ;
1400+
1401+ const end_index = std .unicode .wtf16LeToWtf8 (out_buffer , wide_slice );
1402+ return out_buffer [0.. end_index ];
13851403 }
13861404
13871405 var flags : posix.O = .{};
@@ -1411,9 +1429,9 @@ pub fn realpathZ(self: Dir, pathname: [*:0]const u8, out_buffer: []u8) RealPathE
14111429}
14121430
14131431/// Windows-only. Same as `Dir.realpath` except `pathname` is WTF16 LE encoded.
1414- /// The result is encoded as [WTF-8](https://simonsapin.github.io/wtf-8/) .
1432+ /// The result is encoded as WTF16 LE .
14151433/// See also `Dir.realpath`, `realpathW`.
1416- pub fn realpathW (self : Dir , pathname : []const u16 , out_buffer : []u8 ) RealPathError ! []u8 {
1434+ pub fn realpathW (self : Dir , pathname : []const u16 , out_buffer : []u16 ) RealPathError ! []u16 {
14171435 const w = windows ;
14181436
14191437 const access_mask = w .GENERIC_READ | w .SYNCHRONIZE ;
@@ -1434,13 +1452,7 @@ pub fn realpathW(self: Dir, pathname: []const u16, out_buffer: []u8) RealPathErr
14341452 };
14351453 defer w .CloseHandle (h_file );
14361454
1437- var wide_buf : [w .PATH_MAX_WIDE ]u16 = undefined ;
1438- const wide_slice = try w .GetFinalPathNameByHandle (h_file , .{}, & wide_buf );
1439- const len = std .unicode .calcWtf8Len (wide_slice );
1440- if (len > out_buffer .len )
1441- return error .NameTooLong ;
1442- const end_index = std .unicode .wtf16LeToWtf8 (out_buffer , wide_slice );
1443- return out_buffer [0.. end_index ];
1455+ return w .GetFinalPathNameByHandle (h_file , .{}, out_buffer );
14441456}
14451457
14461458pub const RealPathAllocError = RealPathError || Allocator .Error ;
0 commit comments