From 22362568cf309a4ad3d7e48f9e8635f7cb67fba7 Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Wed, 15 Jul 2020 18:55:07 +0200 Subject: [PATCH] Refactor --- lib/std/os.zig | 15 +++++++++---- lib/std/os/windows.zig | 51 ++++++++++++++++++++++++------------------ 2 files changed, 40 insertions(+), 26 deletions(-) diff --git a/lib/std/os.zig b/lib/std/os.zig index e638e9f2e..8f86488c0 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -1524,8 +1524,8 @@ pub fn getcwd(out_buffer: []u8) GetCwdError![]u8 { /// or a directory. This value is ignored on all hosts except Windows where /// creating symlinks to different resource types, requires different flags. /// By default, symlink is assumed to point to a file. -pub const SymlinkFlags = struct{ - is_directory: bool = false, +pub const SymlinkFlags = struct { + is_directory: bool = false, }; pub const SymLinkError = error{ @@ -2372,7 +2372,8 @@ pub const ReadLinkError = error{ NotDir, InvalidUtf8, BadPathName, - /// Windows-only. + /// Windows-only. This error may occur if the opened reparse point is + /// of unsupported type. UnsupportedReparsePointType, } || UnexpectedError; @@ -4075,7 +4076,13 @@ pub fn realpathZ(pathname: [*:0]const u8, out_buffer: *[MAX_PATH_BYTES]u8) RealP var procfs_buf: ["/proc/self/fd/-2147483648".len:0]u8 = undefined; const proc_path = std.fmt.bufPrint(procfs_buf[0..], "/proc/self/fd/{}\x00", .{fd}) catch unreachable; - return readlinkZ(@ptrCast([*:0]const u8, proc_path.ptr), out_buffer); + const target = readlinkZ(@ptrCast([*:0]const u8, proc_path.ptr), out_buffer) catch |err| { + switch (err) { + error.UnsupportedReparsePointType => unreachable, // Windows only, + else => |e| return e, + } + }; + return target; } const result_path = std.c.realpath(pathname, out_buffer) orelse switch (std.c._errno().*) { EINVAL => unreachable, diff --git a/lib/std/os/windows.zig b/lib/std/os/windows.zig index ab8705048..82aea077c 100644 --- a/lib/std/os/windows.zig +++ b/lib/std/os/windows.zig @@ -1296,33 +1296,40 @@ pub fn cStrToWin32PrefixedFileW(s: [*:0]const u8) !PathSpace { /// it will get NT-style prefix `\??\` prepended automatically. For prepending /// Win32-style prefix, see `sliceToWin32PrefixedFileW` instead. pub fn sliceToPrefixedFileW(s: []const u8) !PathSpace { - // TODO https://github.com/ziglang/zig/issues/2765 - var path_space: PathSpace = undefined; - const prefix_index: usize = if (mem.startsWith(u8, s, "\\??\\")) 4 else 0; - for (s[prefix_index..]) |byte| { - switch (byte) { - '*', '?', '"', '<', '>', '|' => return error.BadPathName, - else => {}, - } - } - const start_index = if (prefix_index > 0 or !std.fs.path.isAbsolute(s)) 0 else blk: { - const prefix = [_]u16{ '\\', '?', '?', '\\' }; - mem.copy(u16, path_space.data[0..], &prefix); - break :blk prefix.len; - }; - path_space.len = start_index + try std.unicode.utf8ToUtf16Le(path_space.data[start_index..], s); - if (path_space.len > path_space.data.len) return error.NameTooLong; - path_space.ensureNtStyle(); - return path_space; + return sliceToPrefixedFileWInternal(s, PathPrefix.Nt); } /// Converts the path `s` to WTF16, null-terminated. If the path is absolute, /// it will get Win32-style extended prefix `\\?\` prepended automatically. For prepending /// NT-style prefix, see `sliceToPrefixedFileW` instead. pub fn sliceToWin32PrefixedFileW(s: []const u8) !PathSpace { + return sliceToPrefixedFileWInternal(s, PathPrefix.Win32); +} + +const PathPrefix = enum { + Win32, + Nt, + + fn toUtf8(self: PathPrefix) []const u8 { + return switch (self) { + .Win32 => "\\\\?\\", + .Nt => "\\??\\", + }; + } + + fn toUtf16(self: PathPrefix) []const u16 { + return switch (self) { + .Win32 => &[_]u16{ '\\', '\\', '?', '\\' }, + .Nt => &[_]u16{ '\\', '?', '?', '\\' }, + }; + } +}; + +fn sliceToPrefixedFileWInternal(s: []const u8, prefix: PathPrefix) !PathSpace { // TODO https://github.com/ziglang/zig/issues/2765 var path_space: PathSpace = undefined; - const prefix_index: usize = if (mem.startsWith(u8, s, "\\\\?\\")) 4 else 0; + const prefix_utf8 = prefix.toUtf8(); + const prefix_index: usize = if (mem.startsWith(u8, s, prefix_utf8)) prefix_utf8.len else 0; for (s[prefix_index..]) |byte| { switch (byte) { '*', '?', '"', '<', '>', '|' => return error.BadPathName, @@ -1330,9 +1337,9 @@ pub fn sliceToWin32PrefixedFileW(s: []const u8) !PathSpace { } } const start_index = if (prefix_index > 0 or !std.fs.path.isAbsolute(s)) 0 else blk: { - const prefix = [_]u16{ '\\', '\\', '?', '\\' }; - mem.copy(u16, path_space.data[0..], &prefix); - break :blk prefix.len; + const prefix_utf16 = prefix.toUtf16(); + mem.copy(u16, path_space.data[0..], prefix_utf16); + break :blk prefix_utf16.len; }; path_space.len = start_index + try std.unicode.utf8ToUtf16Le(path_space.data[start_index..], s); if (path_space.len > path_space.data.len) return error.NameTooLong;