authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-01-28 16:18:43-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-01-28 17:02:17-08:00
log18c6abc0ba9a58a3d25908c47df3bb9374d51c35
tree65cb6c2fbfabd6be770c3574f01efd53bb6cc5df
parent57742480414b2060411b0b07f258e9187b4e8ca0

std: finish moving os.windows.ReadLink logic to Io.Threaded

- remove error.SharingViolation from all error sets since it has the same meaning as FileBusy - add error.FileBusy to CreateFileAtomicError and ReadLinkError - update dirReadLinkWindows to use NtCreateFile and NtFsControlFile and integrate with cancelation properly. - move windows CTL_CODE constants to the proper namespace - delete os.windows.ReadLink

7 files changed, 218 insertions(+), 173 deletions(-)

lib/std/Io/Dir.zig+5-1
...@@ -940,6 +940,7 @@ pub const RenameError = error{...@@ -940,6 +940,7 @@ pub const RenameError = error{
940 /// Attempted to replace a nonempty directory.940 /// Attempted to replace a nonempty directory.
941 DirNotEmpty,941 DirNotEmpty,
942 PermissionDenied,942 PermissionDenied,
943 /// The file attempted to be moved or replaced is a running executable.
943 FileBusy,944 FileBusy,
944 DiskQuota,945 DiskQuota,
945 IsDir,946 IsDir,
...@@ -952,7 +953,6 @@ pub const RenameError = error{...@@ -952,7 +953,6 @@ pub const RenameError = error{
952 ReadOnlyFileSystem,953 ReadOnlyFileSystem,
953 CrossDevice,954 CrossDevice,
954 NoDevice,955 NoDevice,
955 SharingViolation,
956 PipeBusy,956 PipeBusy,
957 /// On Windows, `\\server` or `\\server\share` was not found.957 /// On Windows, `\\server` or `\\server\share` was not found.
958 NetworkNotFound,958 NetworkNotFound,
...@@ -1167,6 +1167,8 @@ pub const ReadLinkError = error{...@@ -1167,6 +1167,8 @@ pub const ReadLinkError = error{
1167 /// intercepts file system operations and makes them significantly slower1167 /// intercepts file system operations and makes them significantly slower
1168 /// in addition to possibly failing with this error code.1168 /// in addition to possibly failing with this error code.
1169 AntivirusInterference,1169 AntivirusInterference,
1170 /// File attempted to be opened is a running executable.
1171 FileBusy,
1170} || PathNameError || Io.Cancelable || Io.UnexpectedError;1172} || PathNameError || Io.Cancelable || Io.UnexpectedError;
11711173
1172/// Obtain target of a symbolic link.1174/// Obtain target of a symbolic link.
...@@ -1791,6 +1793,8 @@ pub const CreateFileAtomicError = error{...@@ -1791,6 +1793,8 @@ pub const CreateFileAtomicError = error{
1791 NotDir,1793 NotDir,
1792 WouldBlock,1794 WouldBlock,
1793 ReadOnlyFileSystem,1795 ReadOnlyFileSystem,
1796 /// The file attempted to be created is a running executable.
1797 FileBusy,
1794} || Io.Dir.PathNameError || Io.Cancelable || Io.UnexpectedError;1798} || Io.Dir.PathNameError || Io.Cancelable || Io.UnexpectedError;
17951799
1796/// Create an unnamed ephemeral file that can eventually be atomically1800/// Create an unnamed ephemeral file that can eventually be atomically
lib/std/Io/File.zig+1-2
...@@ -249,7 +249,6 @@ pub const CreateFlags = struct {...@@ -249,7 +249,6 @@ pub const CreateFlags = struct {
249};249};
250250
251pub const OpenError = error{251pub const OpenError = error{
252 SharingViolation,
253 PipeBusy,252 PipeBusy,
254 NoDevice,253 NoDevice,
255 /// On Windows, `\\server` or `\\server\share` was not found.254 /// On Windows, `\\server` or `\\server\share` was not found.
...@@ -757,7 +756,7 @@ pub const RealPathError = error{...@@ -757,7 +756,7 @@ pub const RealPathError = error{
757 NoSpaceLeft,756 NoSpaceLeft,
758 FileSystem,757 FileSystem,
759 DeviceBusy,758 DeviceBusy,
760 SharingViolation,759 FileBusy,
761 PipeBusy,760 PipeBusy,
762 /// On Windows, `\\server` or `\\server\share` was not found.761 /// On Windows, `\\server` or `\\server\share` was not found.
763 NetworkNotFound,762 NetworkNotFound,
lib/std/Io/Threaded.zig+201-72
...@@ -3635,7 +3635,7 @@ fn dirCreateFileWindows(...@@ -3635,7 +3635,7 @@ fn dirCreateFileWindows(
3635 // after an executable file is closed. Here we work around the3635 // after an executable file is closed. Here we work around the
3636 // kernel bug with retry attempts.3636 // kernel bug with retry attempts.
3637 syscall.finish();3637 syscall.finish();
3638 if (max_attempts - attempt == 0) return error.SharingViolation;3638 if (max_attempts - attempt == 0) return error.FileBusy;
3639 try parking_sleep.windowsRetrySleep((@as(u32, 1) << attempt) >> 1);3639 try parking_sleep.windowsRetrySleep((@as(u32, 1) << attempt) >> 1);
3640 attempt += 1;3640 attempt += 1;
3641 syscall = try .start();3641 syscall = try .start();
...@@ -3648,7 +3648,7 @@ fn dirCreateFileWindows(...@@ -3648,7 +3648,7 @@ fn dirCreateFileWindows(
3648 // call has failed. Here, we simulate the kernel bug being3648 // call has failed. Here, we simulate the kernel bug being
3649 // fixed by sleeping and retrying until the error goes away.3649 // fixed by sleeping and retrying until the error goes away.
3650 syscall.finish();3650 syscall.finish();
3651 if (max_attempts - attempt == 0) return error.SharingViolation;3651 if (max_attempts - attempt == 0) return error.FileBusy;
3652 try parking_sleep.windowsRetrySleep((@as(u32, 1) << attempt) >> 1);3652 try parking_sleep.windowsRetrySleep((@as(u32, 1) << attempt) >> 1);
3653 attempt += 1;3653 attempt += 1;
3654 syscall = try .start();3654 syscall = try .start();
...@@ -3668,10 +3668,10 @@ fn dirCreateFileWindows(...@@ -3668,10 +3668,10 @@ fn dirCreateFileWindows(
3668 .NOT_A_DIRECTORY => return syscall.fail(error.NotDir),3668 .NOT_A_DIRECTORY => return syscall.fail(error.NotDir),
3669 .USER_MAPPED_FILE => return syscall.fail(error.AccessDenied),3669 .USER_MAPPED_FILE => return syscall.fail(error.AccessDenied),
3670 .VIRUS_INFECTED, .VIRUS_DELETED => return syscall.fail(error.AntivirusInterference),3670 .VIRUS_INFECTED, .VIRUS_DELETED => return syscall.fail(error.AntivirusInterference),
3671 .INVALID_PARAMETER => |err| return syscall.ntstatusBug(err),3671 .INVALID_PARAMETER => |status| return syscall.ntstatusBug(status),
3672 .OBJECT_PATH_SYNTAX_BAD => |err| return syscall.ntstatusBug(err),3672 .OBJECT_PATH_SYNTAX_BAD => |status| return syscall.ntstatusBug(status),
3673 .INVALID_HANDLE => |err| return syscall.ntstatusBug(err),3673 .INVALID_HANDLE => |status| return syscall.ntstatusBug(status),
3674 else => |err| return syscall.unexpectedNtstatus(err),3674 else => |status| return syscall.unexpectedNtstatus(status),
3675 };3675 };
3676 errdefer windows.CloseHandle(handle);3676 errdefer windows.CloseHandle(handle);
36773677
...@@ -3819,7 +3819,6 @@ fn dirCreateFileAtomic(...@@ -3819,7 +3819,6 @@ fn dirCreateFileAtomic(
3819 error.DiskQuota,3819 error.DiskQuota,
3820 error.PathAlreadyExists,3820 error.PathAlreadyExists,
3821 error.LinkQuotaExceeded,3821 error.LinkQuotaExceeded,
3822 error.SharingViolation,
3823 error.PipeBusy,3822 error.PipeBusy,
3824 error.FileTooBig,3823 error.FileTooBig,
3825 error.DeviceBusy,3824 error.DeviceBusy,
...@@ -3889,11 +3888,9 @@ fn dirCreateFileAtomic(...@@ -3889,11 +3888,9 @@ fn dirCreateFileAtomic(
3889 error.DiskQuota,3888 error.DiskQuota,
3890 error.PathAlreadyExists,3889 error.PathAlreadyExists,
3891 error.LinkQuotaExceeded,3890 error.LinkQuotaExceeded,
3892 error.SharingViolation,
3893 error.PipeBusy,3891 error.PipeBusy,
3894 error.FileTooBig,3892 error.FileTooBig,
3895 error.FileLocksUnsupported,3893 error.FileLocksUnsupported,
3896 error.FileBusy,
3897 error.DeviceBusy,3894 error.DeviceBusy,
3898 => return error.Unexpected,3895 => return error.Unexpected,
38993896
...@@ -3926,7 +3923,6 @@ fn atomicFileInit(...@@ -3926,7 +3923,6 @@ fn atomicFileInit(
3926 error.PathAlreadyExists => continue,3923 error.PathAlreadyExists => continue,
3927 error.DeviceBusy => continue,3924 error.DeviceBusy => continue,
3928 error.FileBusy => continue,3925 error.FileBusy => continue,
3929 error.SharingViolation => continue,
39303926
3931 error.IsDir => return error.Unexpected, // No path components.3927 error.IsDir => return error.Unexpected, // No path components.
3932 error.FileTooBig => return error.Unexpected, // Creating, not opening.3928 error.FileTooBig => return error.Unexpected, // Creating, not opening.
...@@ -4236,7 +4232,7 @@ pub fn dirOpenFileWtf16(...@@ -4236,7 +4232,7 @@ pub fn dirOpenFileWtf16(
4236 // after an executable file is closed. Here we work around the4232 // after an executable file is closed. Here we work around the
4237 // kernel bug with retry attempts.4233 // kernel bug with retry attempts.
4238 syscall.finish();4234 syscall.finish();
4239 if (max_attempts - attempt == 0) return error.SharingViolation;4235 if (max_attempts - attempt == 0) return error.FileBusy;
4240 try parking_sleep.windowsRetrySleep((@as(u32, 1) << attempt) >> 1);4236 try parking_sleep.windowsRetrySleep((@as(u32, 1) << attempt) >> 1);
4241 attempt += 1;4237 attempt += 1;
4242 syscall = try .start();4238 syscall = try .start();
...@@ -4258,7 +4254,7 @@ pub fn dirOpenFileWtf16(...@@ -4258,7 +4254,7 @@ pub fn dirOpenFileWtf16(
4258 // call has failed. Here, we simulate the kernel bug being4254 // call has failed. Here, we simulate the kernel bug being
4259 // fixed by sleeping and retrying until the error goes away.4255 // fixed by sleeping and retrying until the error goes away.
4260 syscall.finish();4256 syscall.finish();
4261 if (max_attempts - attempt == 0) return error.SharingViolation;4257 if (max_attempts - attempt == 0) return error.FileBusy;
4262 try parking_sleep.windowsRetrySleep((@as(u32, 1) << attempt) >> 1);4258 try parking_sleep.windowsRetrySleep((@as(u32, 1) << attempt) >> 1);
4263 attempt += 1;4259 attempt += 1;
4264 syscall = try .start();4260 syscall = try .start();
...@@ -6464,7 +6460,7 @@ fn dirSymLinkWindows(...@@ -6464,7 +6460,7 @@ fn dirSymLinkWindows(
6464 @memcpy(buffer[@sizeOf(SYMLINK_DATA)..][0 .. final_target_path.len * 2], @as([*]const u8, @ptrCast(final_target_path)));6460 @memcpy(buffer[@sizeOf(SYMLINK_DATA)..][0 .. final_target_path.len * 2], @as([*]const u8, @ptrCast(final_target_path)));
6465 const paths_start = @sizeOf(SYMLINK_DATA) + final_target_path.len * 2;6461 const paths_start = @sizeOf(SYMLINK_DATA) + final_target_path.len * 2;
6466 @memcpy(buffer[paths_start..][0 .. final_target_path.len * 2], @as([*]const u8, @ptrCast(final_target_path)));6462 @memcpy(buffer[paths_start..][0 .. final_target_path.len * 2], @as([*]const u8, @ptrCast(final_target_path)));
6467 const rc = w.DeviceIoControl(symlink_handle, w.FSCTL.SET_REPARSE_POINT, .{ .in = buffer[0..buf_len] });6463 const rc = w.DeviceIoControl(symlink_handle, .SET_REPARSE_POINT, .{ .in = buffer[0..buf_len] });
6468 switch (rc) {6464 switch (rc) {
6469 .SUCCESS => {},6465 .SUCCESS => {},
6470 .PRIVILEGE_NOT_HELD => return error.PermissionDenied,6466 .PRIVILEGE_NOT_HELD => return error.PermissionDenied,
...@@ -6571,44 +6567,189 @@ fn dirSymLinkPosix(...@@ -6571,44 +6567,189 @@ fn dirSymLinkPosix(
6571 }6567 }
6572}6568}
65736569
6574const dirReadLink = switch (native_os) {6570fn dirReadLink(userdata: ?*anyopaque, dir: Dir, sub_path: []const u8, buffer: []u8) Dir.ReadLinkError!usize {
6575 .windows => dirReadLinkWindows,
6576 .wasi => dirReadLinkWasi,
6577 else => dirReadLinkPosix,
6578};
6579
6580fn dirReadLinkWindows(userdata: ?*anyopaque, dir: Dir, sub_path: []const u8, buffer: []u8) Dir.ReadLinkError!usize {
6581 const t: *Threaded = @ptrCast(@alignCast(userdata));6571 const t: *Threaded = @ptrCast(@alignCast(userdata));
6582 _ = t;6572 _ = t;
6583 const w = windows;6573 switch (native_os) {
6574 .windows => return dirReadLinkWindows(dir, sub_path, buffer),
6575 .wasi => return dirReadLinkWasi(dir, sub_path, buffer),
6576 else => return dirReadLinkPosix(dir, sub_path, buffer),
6577 }
6578}
65846579
6580fn dirReadLinkWindows(dir: Dir, sub_path: []const u8, buffer: []u8) Dir.ReadLinkError!usize {
6581 // This gets used once for `sub_path` and then reused again temporarily
6582 // before converting back to `buffer`.
6585 var sub_path_w_buf = try windows.sliceToPrefixedFileW(dir.handle, sub_path);6583 var sub_path_w_buf = try windows.sliceToPrefixedFileW(dir.handle, sub_path);
6584 const sub_path_w = sub_path_w_buf.span();
6585 const path_len_bytes = std.math.cast(u16, sub_path_w.len * 2) orelse return error.NameTooLong;
6586 var nt_name: windows.UNICODE_STRING = .{
6587 .Length = path_len_bytes,
6588 .MaximumLength = path_len_bytes,
6589 .Buffer = @constCast(sub_path_w.ptr),
6590 };
6591 const attr: windows.OBJECT_ATTRIBUTES = .{
6592 .Length = @sizeOf(windows.OBJECT_ATTRIBUTES),
6593 .RootDirectory = if (Dir.path.isAbsoluteWindowsWtf16(sub_path_w)) null else dir.handle,
6594 .Attributes = .{
6595 .INHERIT = false,
6596 },
6597 .ObjectName = &nt_name,
6598 .SecurityDescriptor = null,
6599 .SecurityQualityOfService = null,
6600 };
6601 var io_status_block: windows.IO_STATUS_BLOCK = undefined;
6602 var result_handle: windows.HANDLE = undefined;
65866603
6587 const syscall: Syscall = try .start();6604 // There are multiple kernel bugs being worked around with retries.
6588 const result_w = while (true) {6605 const max_attempts = 13;
6589 if (w.ReadLink(dir.handle, sub_path_w_buf.span(), &sub_path_w_buf.data)) |res| {6606 var attempt: u5 = 0;
6607
6608 var syscall: Syscall = try .start();
6609 while (true) switch (windows.ntdll.NtCreateFile(
6610 &result_handle,
6611 .{
6612 .SPECIFIC = .{ .FILE = .{
6613 .READ_ATTRIBUTES = true,
6614 } },
6615 .STANDARD = .{ .SYNCHRONIZE = true },
6616 },
6617 &attr,
6618 &io_status_block,
6619 null,
6620 .{ .NORMAL = true },
6621 .VALID_FLAGS,
6622 .OPEN,
6623 .{
6624 .DIRECTORY_FILE = false,
6625 .NON_DIRECTORY_FILE = false,
6626 .IO = .ASYNCHRONOUS,
6627 .OPEN_REPARSE_POINT = true,
6628 },
6629 null,
6630 0,
6631 )) {
6632 .SUCCESS => {
6590 syscall.finish();6633 syscall.finish();
6591 break res;6634 break;
6592 } else |err| switch (err) {6635 },
6593 error.OperationCanceled => {6636 .CANCELLED => {
6594 try syscall.checkCancel();6637 try syscall.checkCancel();
6595 continue;6638 continue;
6596 },6639 },
6597 else => |e| return syscall.fail(e),6640 .SHARING_VIOLATION => {
6598 }6641 // This occurs if the file attempting to be opened is a running
6642 // executable. However, there's a kernel bug: the error may be
6643 // incorrectly returned for an indeterminate amount of time
6644 // after an executable file is closed. Here we work around the
6645 // kernel bug with retry attempts.
6646 syscall.finish();
6647 if (max_attempts - attempt == 0) return error.FileBusy;
6648 try parking_sleep.windowsRetrySleep((@as(u32, 1) << attempt) >> 1);
6649 attempt += 1;
6650 syscall = try .start();
6651 continue;
6652 },
6653 .DELETE_PENDING => {
6654 // This error means that there *was* a file in this location on
6655 // the file system, but it was deleted. However, the OS is not
6656 // finished with the deletion operation, and so this CreateFile
6657 // call has failed. Here, we simulate the kernel bug being
6658 // fixed by sleeping and retrying until the error goes away.
6659 syscall.finish();
6660 if (max_attempts - attempt == 0) return error.FileBusy;
6661 try parking_sleep.windowsRetrySleep((@as(u32, 1) << attempt) >> 1);
6662 attempt += 1;
6663 syscall = try .start();
6664 continue;
6665 },
6666 .OBJECT_NAME_INVALID => return syscall.fail(error.BadPathName),
6667 .OBJECT_NAME_NOT_FOUND => return syscall.fail(error.FileNotFound),
6668 .OBJECT_PATH_NOT_FOUND => return syscall.fail(error.FileNotFound),
6669 .BAD_NETWORK_PATH => return syscall.fail(error.NetworkNotFound), // \\server was not found
6670 .BAD_NETWORK_NAME => return syscall.fail(error.NetworkNotFound), // \\server was found but \\server\share wasn't
6671 .NO_MEDIA_IN_DEVICE => return syscall.fail(error.FileNotFound),
6672 .ACCESS_DENIED => return syscall.fail(error.AccessDenied),
6673 .PIPE_BUSY => return syscall.fail(error.AccessDenied),
6674 .PIPE_NOT_AVAILABLE => return syscall.fail(error.FileNotFound),
6675 .USER_MAPPED_FILE => return syscall.fail(error.AccessDenied),
6676 .VIRUS_INFECTED, .VIRUS_DELETED => return syscall.fail(error.AntivirusInterference),
6677 .INVALID_PARAMETER => |status| return syscall.ntstatusBug(status),
6678 .OBJECT_PATH_SYNTAX_BAD => |status| return syscall.ntstatusBug(status),
6679 .INVALID_HANDLE => |status| return syscall.ntstatusBug(status),
6680 else => |status| return syscall.unexpectedNtstatus(status),
6599 };6681 };
6682 defer windows.CloseHandle(result_handle);
6683
6684 var reparse_buf: [windows.MAXIMUM_REPARSE_DATA_BUFFER_SIZE]u8 align(@alignOf(windows.REPARSE_DATA_BUFFER)) = undefined;
66006685
6686 syscall = try .start();
6687 while (true) switch (windows.ntdll.NtFsControlFile(
6688 result_handle,
6689 null, // event
6690 null, // APC routine
6691 null, // APC context
6692 &io_status_block,
6693 .GET_REPARSE_POINT,
6694 null, // input buffer
6695 0, // input buffer length
6696 &reparse_buf,
6697 reparse_buf.len,
6698 )) {
6699 .SUCCESS => {
6700 syscall.finish();
6701 break;
6702 },
6703 .CANCELLED => {
6704 try syscall.checkCancel();
6705 continue;
6706 },
6707 .NOT_A_REPARSE_POINT => return syscall.fail(error.NotLink),
6708 else => |status| return syscall.unexpectedNtstatus(status),
6709 };
6710
6711 const reparse_struct: *const windows.REPARSE_DATA_BUFFER = @ptrCast(@alignCast(&reparse_buf));
6712 const IoReparseTagInt = @typeInfo(windows.IO_REPARSE_TAG).@"struct".backing_integer.?;
6713 const result_w = switch (@as(IoReparseTagInt, @bitCast(reparse_struct.ReparseTag))) {
6714 @as(IoReparseTagInt, @bitCast(windows.IO_REPARSE_TAG.SYMLINK)) => r: {
6715 const buf: *const windows.SYMBOLIC_LINK_REPARSE_BUFFER = @ptrCast(@alignCast(&reparse_struct.DataBuffer[0]));
6716 const offset = buf.SubstituteNameOffset >> 1;
6717 const len = buf.SubstituteNameLength >> 1;
6718 const path_buf = @as([*]const u16, &buf.PathBuffer);
6719 const is_relative = buf.Flags & windows.SYMLINK_FLAG_RELATIVE != 0;
6720 break :r try parseReadLinkPath(path_buf[offset..][0..len], is_relative, &sub_path_w_buf.data);
6721 },
6722 @as(IoReparseTagInt, @bitCast(windows.IO_REPARSE_TAG.MOUNT_POINT)) => r: {
6723 const buf: *const windows.MOUNT_POINT_REPARSE_BUFFER = @ptrCast(@alignCast(&reparse_struct.DataBuffer[0]));
6724 const offset = buf.SubstituteNameOffset >> 1;
6725 const len = buf.SubstituteNameLength >> 1;
6726 const path_buf = @as([*]const u16, &buf.PathBuffer);
6727 break :r try parseReadLinkPath(path_buf[offset..][0..len], false, &sub_path_w_buf.data);
6728 },
6729 else => return error.UnsupportedReparsePointType,
6730 };
6601 const len = std.unicode.calcWtf8Len(result_w);6731 const len = std.unicode.calcWtf8Len(result_w);
6602 if (len > buffer.len) return error.NameTooLong;6732 if (len > buffer.len) return error.NameTooLong;
66036733
6604 return std.unicode.wtf16LeToWtf8(buffer, result_w);6734 return std.unicode.wtf16LeToWtf8(buffer, result_w);
6605}6735}
66066736
6607fn dirReadLinkWasi(userdata: ?*anyopaque, dir: Dir, sub_path: []const u8, buffer: []u8) Dir.ReadLinkError!usize {6737fn parseReadLinkPath(path: []const u16, is_relative: bool, out_buffer: []u16) error{NameTooLong}![]u16 {
6608 if (builtin.link_libc) return dirReadLinkPosix(userdata, dir, sub_path, buffer);6738 path: {
6739 if (is_relative) break :path;
6740 return windows.ntToWin32Namespace(path, out_buffer) catch |err| switch (err) {
6741 error.NameTooLong => |e| return e,
6742 error.NotNtPath => break :path,
6743 };
6744 }
6745 if (out_buffer.len < path.len) return error.NameTooLong;
6746 const dest = out_buffer[0..path.len];
6747 @memcpy(dest, path);
6748 return dest;
6749}
66096750
6610 const t: *Threaded = @ptrCast(@alignCast(userdata));6751fn dirReadLinkWasi(dir: Dir, sub_path: []const u8, buffer: []u8) Dir.ReadLinkError!usize {
6611 _ = t;6752 if (builtin.link_libc) return dirReadLinkPosix(dir, sub_path, buffer);
66126753
6613 var n: usize = undefined;6754 var n: usize = undefined;
6614 const syscall: Syscall = try .start();6755 const syscall: Syscall = try .start();
...@@ -6643,10 +6784,7 @@ fn dirReadLinkWasi(userdata: ?*anyopaque, dir: Dir, sub_path: []const u8, buffer...@@ -6643,10 +6784,7 @@ fn dirReadLinkWasi(userdata: ?*anyopaque, dir: Dir, sub_path: []const u8, buffer
6643 }6784 }
6644}6785}
66456786
6646fn dirReadLinkPosix(userdata: ?*anyopaque, dir: Dir, sub_path: []const u8, buffer: []u8) Dir.ReadLinkError!usize {6787fn dirReadLinkPosix(dir: Dir, sub_path: []const u8, buffer: []u8) Dir.ReadLinkError!usize {
6647 const t: *Threaded = @ptrCast(@alignCast(userdata));
6648 _ = t;
6649
6650 var sub_path_buffer: [posix.PATH_MAX]u8 = undefined;6788 var sub_path_buffer: [posix.PATH_MAX]u8 = undefined;
6651 const sub_path_posix = try pathToPosix(sub_path, &sub_path_buffer);6789 const sub_path_posix = try pathToPosix(sub_path, &sub_path_buffer);
66526790
...@@ -8708,45 +8846,41 @@ fn processExecutablePath(userdata: ?*anyopaque, out_buffer: []u8) process.Execut...@@ -8708,45 +8846,41 @@ fn processExecutablePath(userdata: ?*anyopaque, out_buffer: []u8) process.Execut
8708 const symlink_path = std.mem.sliceTo(&symlink_path_buf, 0);8846 const symlink_path = std.mem.sliceTo(&symlink_path_buf, 0);
8709 return Io.Dir.realPathFileAbsolute(ioBasic(t), symlink_path, out_buffer) catch |err| switch (err) {8847 return Io.Dir.realPathFileAbsolute(ioBasic(t), symlink_path, out_buffer) catch |err| switch (err) {
8710 error.NetworkNotFound => unreachable, // Windows-only8848 error.NetworkNotFound => unreachable, // Windows-only
8849 error.FileBusy => unreachable, // Windows-only
8711 else => |e| return e,8850 else => |e| return e,
8712 };8851 };
8713 },8852 },
8714 .linux, .serenity => return Io.Dir.readLinkAbsolute(ioBasic(t), "/proc/self/exe", out_buffer) catch |err| switch (err) {8853 .linux, .serenity => return Io.Dir.readLinkAbsolute(ioBasic(t), "/proc/self/exe", out_buffer) catch |err| switch (err) {
8715 error.UnsupportedReparsePointType => unreachable, // Windows-only8854 error.UnsupportedReparsePointType => unreachable, // Windows-only
8716 error.NetworkNotFound => unreachable, // Windows-only8855 error.NetworkNotFound => unreachable, // Windows-only
8856 error.FileBusy => unreachable, // Windows-only
8717 else => |e| return e,8857 else => |e| return e,
8718 },8858 },
8719 .illumos => return Io.Dir.readLinkAbsolute(ioBasic(t), "/proc/self/path/a.out", out_buffer) catch |err| switch (err) {8859 .illumos => return Io.Dir.readLinkAbsolute(ioBasic(t), "/proc/self/path/a.out", out_buffer) catch |err| switch (err) {
8720 error.UnsupportedReparsePointType => unreachable, // Windows-only8860 error.UnsupportedReparsePointType => unreachable, // Windows-only
8721 error.NetworkNotFound => unreachable, // Windows-only8861 error.NetworkNotFound => unreachable, // Windows-only
8862 error.FileBusy => unreachable, // Windows-only
8722 else => |e| return e,8863 else => |e| return e,
8723 },8864 },
8724 .freebsd, .dragonfly => {8865 .freebsd, .dragonfly => {
8725 var mib: [4]c_int = .{ posix.CTL.KERN, posix.KERN.PROC, posix.KERN.PROC_PATHNAME, -1 };8866 var mib: [4]c_int = .{ posix.CTL.KERN, posix.KERN.PROC, posix.KERN.PROC_PATHNAME, -1 };
8726 var out_len: usize = out_buffer.len;8867 var out_len: usize = out_buffer.len;
8727 const syscall: Syscall = try .start();8868 const syscall: Syscall = try .start();
8728 while (true) {8869 while (true) switch (posix.errno(posix.system.sysctl(&mib, mib.len, out_buffer.ptr, &out_len, null, 0))) {
8729 switch (posix.errno(posix.system.sysctl(&mib, mib.len, out_buffer.ptr, &out_len, null, 0))) {8870 .SUCCESS => {
8730 .SUCCESS => {8871 syscall.finish();
8731 syscall.finish();8872 return out_len - 1; // discard terminating NUL
8732 return out_len - 1; // discard terminating NUL8873 },
8733 },8874 .INTR => {
8734 .INTR => {8875 try syscall.checkCancel();
8735 try syscall.checkCancel();8876 continue;
8736 continue;8877 },
8737 },8878 .PERM => return syscall.fail(error.PermissionDenied),
8738 else => |e| {8879 .NOMEM => return syscall.fail(error.SystemResources),
8739 syscall.finish();8880 .FAULT => |err| return syscall.errnoBug(err),
8740 switch (e) {8881 .NOENT => |err| return syscall.errnoBug(err),
8741 .FAULT => |err| return errnoBug(err),8882 else => |err| return syscall.unexpectedErrno(err),
8742 .PERM => return error.PermissionDenied,8883 };
8743 .NOMEM => return error.SystemResources,
8744 .NOENT => |err| return errnoBug(err),
8745 else => |err| return posix.unexpectedErrno(err),
8746 }
8747 },
8748 }
8749 }
8750 },8884 },
8751 .netbsd => {8885 .netbsd => {
8752 var mib = [4]c_int{ posix.CTL.KERN, posix.KERN.PROC_ARGS, -1, posix.KERN.PROC_PATHNAME };8886 var mib = [4]c_int{ posix.CTL.KERN, posix.KERN.PROC_ARGS, -1, posix.KERN.PROC_PATHNAME };
...@@ -8762,16 +8896,11 @@ fn processExecutablePath(userdata: ?*anyopaque, out_buffer: []u8) process.Execut...@@ -8762,16 +8896,11 @@ fn processExecutablePath(userdata: ?*anyopaque, out_buffer: []u8) process.Execut
8762 try syscall.checkCancel();8896 try syscall.checkCancel();
8763 continue;8897 continue;
8764 },8898 },
8765 else => |e| {8899 .PERM => return syscall.fail(error.PermissionDenied),
8766 syscall.finish();8900 .NOMEM => return syscall.fail(error.SystemResources),
8767 switch (e) {8901 .FAULT => |err| return syscall.errnoBug(err),
8768 .FAULT => |err| return errnoBug(err),8902 .NOENT => |err| return syscall.errnoBug(err),
8769 .PERM => return error.PermissionDenied,8903 else => |err| return syscall.unexpectedErrno(err),
8770 .NOMEM => return error.SystemResources,
8771 .NOENT => |err| return errnoBug(err),
8772 else => |err| return posix.unexpectedErrno(err),
8773 }
8774 },
8775 }8904 }
8776 }8905 }
8777 },8906 },
lib/std/debug/SelfInfo/Windows.zig-1
...@@ -335,7 +335,6 @@ const Module = struct {...@@ -335,7 +335,6 @@ const Module = struct {
335 error.NoSpaceLeft,335 error.NoSpaceLeft,
336 error.DeviceBusy,336 error.DeviceBusy,
337 error.NoDevice,337 error.NoDevice,
338 error.SharingViolation,
339 error.PathAlreadyExists,338 error.PathAlreadyExists,
340 error.PipeBusy,339 error.PipeBusy,
341 error.NetworkNotFound,340 error.NetworkNotFound,
lib/std/os/windows.zig+10-94
...@@ -1135,19 +1135,7 @@ pub const CTL_CODE = packed struct(ULONG) {...@@ -1135,19 +1135,7 @@ pub const CTL_CODE = packed struct(ULONG) {
11351135
1136 _,1136 _,
1137 };1137 };
1138};
1139
1140pub const IOCTL = struct {
1141 pub const KSEC = struct {
1142 pub const GEN_RANDOM: CTL_CODE = .{ .DeviceType = .KSEC, .Function = 2, .Method = .BUFFERED, .Access = .ANY };
1143 };
1144 pub const MOUNTMGR = struct {
1145 pub const QUERY_POINTS: CTL_CODE = .{ .DeviceType = .MOUNTMGRCONTROLTYPE, .Function = 2, .Method = .BUFFERED, .Access = .ANY };
1146 pub const QUERY_DOS_VOLUME_PATH: CTL_CODE = .{ .DeviceType = .MOUNTMGRCONTROLTYPE, .Function = 12, .Method = .BUFFERED, .Access = .ANY };
1147 };
1148};
11491138
1150pub const FSCTL = struct {
1151 pub const SET_REPARSE_POINT: CTL_CODE = .{ .DeviceType = .FILE_SYSTEM, .Function = 41, .Method = .BUFFERED, .Access = .SPECIAL };1139 pub const SET_REPARSE_POINT: CTL_CODE = .{ .DeviceType = .FILE_SYSTEM, .Function = 41, .Method = .BUFFERED, .Access = .SPECIAL };
1152 pub const GET_REPARSE_POINT: CTL_CODE = .{ .DeviceType = .FILE_SYSTEM, .Function = 42, .Method = .BUFFERED, .Access = .ANY };1140 pub const GET_REPARSE_POINT: CTL_CODE = .{ .DeviceType = .FILE_SYSTEM, .Function = 42, .Method = .BUFFERED, .Access = .ANY };
11531141
...@@ -1177,6 +1165,16 @@ pub const FSCTL = struct {...@@ -1177,6 +1165,16 @@ pub const FSCTL = struct {
1177 };1165 };
1178};1166};
11791167
1168pub const IOCTL = struct {
1169 pub const KSEC = struct {
1170 pub const GEN_RANDOM: CTL_CODE = .{ .DeviceType = .KSEC, .Function = 2, .Method = .BUFFERED, .Access = .ANY };
1171 };
1172 pub const MOUNTMGR = struct {
1173 pub const QUERY_POINTS: CTL_CODE = .{ .DeviceType = .MOUNTMGRCONTROLTYPE, .Function = 2, .Method = .BUFFERED, .Access = .ANY };
1174 pub const QUERY_DOS_VOLUME_PATH: CTL_CODE = .{ .DeviceType = .MOUNTMGRCONTROLTYPE, .Function = 12, .Method = .BUFFERED, .Access = .ANY };
1175 };
1176};
1177
1180pub const MAXIMUM_REPARSE_DATA_BUFFER_SIZE: ULONG = 16 * 1024;1178pub const MAXIMUM_REPARSE_DATA_BUFFER_SIZE: ULONG = 16 * 1024;
11811179
1182pub const IO_REPARSE_TAG = packed struct(ULONG) {1180pub const IO_REPARSE_TAG = packed struct(ULONG) {
...@@ -2908,88 +2906,6 @@ pub fn GetCurrentDirectory(buffer: []u8) GetCurrentDirectoryError![]u8 {...@@ -2908,88 +2906,6 @@ pub fn GetCurrentDirectory(buffer: []u8) GetCurrentDirectoryError![]u8 {
2908 return buffer[0..end_index];2906 return buffer[0..end_index];
2909}2907}
29102908
2911pub const ReadLinkError = error{
2912 FileNotFound,
2913 NetworkNotFound,
2914 AccessDenied,
2915 Unexpected,
2916 NameTooLong,
2917 BadPathName,
2918 AntivirusInterference,
2919 UnsupportedReparsePointType,
2920 NotLink,
2921 OperationCanceled,
2922};
2923
2924/// `sub_path_w` will never be accessed after `out_buffer` has been written to, so it
2925/// is safe to reuse a single buffer for both.
2926pub fn ReadLink(dir: ?HANDLE, sub_path_w: []const u16, out_buffer: []u16) ReadLinkError![]u16 {
2927 const result_handle = OpenFile(sub_path_w, .{
2928 .access_mask = .{
2929 .SPECIFIC = .{ .FILE = .{
2930 .READ_ATTRIBUTES = true,
2931 } },
2932 .STANDARD = .{ .SYNCHRONIZE = true },
2933 },
2934 .dir = dir,
2935 .creation = .OPEN,
2936 .follow_symlinks = false,
2937 .filter = .any,
2938 }) catch |err| switch (err) {
2939 error.IsDir, error.NotDir => return error.Unexpected, // filter = .any
2940 error.PathAlreadyExists => return error.Unexpected, // FILE_OPEN
2941 error.WouldBlock => return error.Unexpected,
2942 error.NoDevice => return error.FileNotFound,
2943 error.PipeBusy => return error.AccessDenied,
2944 else => |e| return e,
2945 };
2946 defer CloseHandle(result_handle);
2947
2948 var reparse_buf: [MAXIMUM_REPARSE_DATA_BUFFER_SIZE]u8 align(@alignOf(REPARSE_DATA_BUFFER)) = undefined;
2949 const rc = DeviceIoControl(result_handle, FSCTL.GET_REPARSE_POINT, .{ .out = reparse_buf[0..] });
2950 switch (rc) {
2951 .SUCCESS => {},
2952 .CANCELLED => return error.OperationCanceled,
2953 .NOT_A_REPARSE_POINT => return error.NotLink,
2954 else => return unexpectedStatus(rc),
2955 }
2956
2957 const reparse_struct: *const REPARSE_DATA_BUFFER = @ptrCast(@alignCast(&reparse_buf[0]));
2958 const IoReparseTagInt = @typeInfo(IO_REPARSE_TAG).@"struct".backing_integer.?;
2959 switch (@as(IoReparseTagInt, @bitCast(reparse_struct.ReparseTag))) {
2960 @as(IoReparseTagInt, @bitCast(IO_REPARSE_TAG.SYMLINK)) => {
2961 const buf: *const SYMBOLIC_LINK_REPARSE_BUFFER = @ptrCast(@alignCast(&reparse_struct.DataBuffer[0]));
2962 const offset = buf.SubstituteNameOffset >> 1;
2963 const len = buf.SubstituteNameLength >> 1;
2964 const path_buf = @as([*]const u16, &buf.PathBuffer);
2965 const is_relative = buf.Flags & SYMLINK_FLAG_RELATIVE != 0;
2966 return parseReadLinkPath(path_buf[offset..][0..len], is_relative, out_buffer);
2967 },
2968 @as(IoReparseTagInt, @bitCast(IO_REPARSE_TAG.MOUNT_POINT)) => {
2969 const buf: *const MOUNT_POINT_REPARSE_BUFFER = @ptrCast(@alignCast(&reparse_struct.DataBuffer[0]));
2970 const offset = buf.SubstituteNameOffset >> 1;
2971 const len = buf.SubstituteNameLength >> 1;
2972 const path_buf = @as([*]const u16, &buf.PathBuffer);
2973 return parseReadLinkPath(path_buf[offset..][0..len], false, out_buffer);
2974 },
2975 else => return error.UnsupportedReparsePointType,
2976 }
2977}
2978
2979fn parseReadLinkPath(path: []const u16, is_relative: bool, out_buffer: []u16) error{NameTooLong}![]u16 {
2980 path: {
2981 if (is_relative) break :path;
2982 return ntToWin32Namespace(path, out_buffer) catch |err| switch (err) {
2983 error.NameTooLong => |e| return e,
2984 error.NotNtPath => break :path,
2985 };
2986 }
2987 if (out_buffer.len < path.len) return error.NameTooLong;
2988 const dest = out_buffer[0..path.len];
2989 @memcpy(dest, path);
2990 return dest;
2991}
2992
2993pub const DeleteFileError = error{2909pub const DeleteFileError = error{
2994 FileNotFound,2910 FileNotFound,
2995 AccessDenied,2911 AccessDenied,
lib/std/process.zig-1
...@@ -704,7 +704,6 @@ pub const ExecutablePathBaseError = error{...@@ -704,7 +704,6 @@ pub const ExecutablePathBaseError = error{
704 FileSystem,704 FileSystem,
705 BadPathName,705 BadPathName,
706 DeviceBusy,706 DeviceBusy,
707 SharingViolation,
708 PipeBusy,707 PipeBusy,
709 NotLink,708 NotLink,
710 PathAlreadyExists,709 PathAlreadyExists,
lib/std/zig/system.zig+1-2
...@@ -723,6 +723,7 @@ fn abiAndDynamicLinkerFromFile(...@@ -723,6 +723,7 @@ fn abiAndDynamicLinkerFromFile(
723 error.UnsupportedReparsePointType => unreachable, // Windows only723 error.UnsupportedReparsePointType => unreachable, // Windows only
724 error.NetworkNotFound => unreachable, // Windows only724 error.NetworkNotFound => unreachable, // Windows only
725 error.AntivirusInterference => unreachable, // Windows only725 error.AntivirusInterference => unreachable, // Windows only
726 error.FileBusy => unreachable, // Windows only
726727
727 error.AccessDenied,728 error.AccessDenied,
728 error.PermissionDenied,729 error.PermissionDenied,
...@@ -844,7 +845,6 @@ fn glibcVerFromRPath(io: Io, rpath: []const u8) !std.SemanticVersion {...@@ -844,7 +845,6 @@ fn glibcVerFromRPath(io: Io, rpath: []const u8) !std.SemanticVersion {
844 error.NameTooLong => return error.Unexpected,845 error.NameTooLong => return error.Unexpected,
845 error.BadPathName => return error.Unexpected,846 error.BadPathName => return error.Unexpected,
846 error.PipeBusy => return error.Unexpected, // Windows-only847 error.PipeBusy => return error.Unexpected, // Windows-only
847 error.SharingViolation => return error.Unexpected, // Windows-only
848 error.NetworkNotFound => return error.Unexpected, // Windows-only848 error.NetworkNotFound => return error.Unexpected, // Windows-only
849 error.AntivirusInterference => return error.Unexpected, // Windows-only849 error.AntivirusInterference => return error.Unexpected, // Windows-only
850 error.FileLocksUnsupported => return error.Unexpected, // No lock requested.850 error.FileLocksUnsupported => return error.Unexpected, // No lock requested.
...@@ -1052,7 +1052,6 @@ fn detectAbiAndDynamicLinker(io: Io, cpu: Target.Cpu, os: Target.Os, query: Targ...@@ -1052,7 +1052,6 @@ fn detectAbiAndDynamicLinker(io: Io, cpu: Target.Cpu, os: Target.Os, query: Targ
1052 error.NoSpaceLeft => return error.Unexpected,1052 error.NoSpaceLeft => return error.Unexpected,
1053 error.NameTooLong => return error.Unexpected,1053 error.NameTooLong => return error.Unexpected,
1054 error.PathAlreadyExists => return error.Unexpected,1054 error.PathAlreadyExists => return error.Unexpected,
1055 error.SharingViolation => return error.Unexpected,
1056 error.BadPathName => return error.Unexpected,1055 error.BadPathName => return error.Unexpected,
1057 error.PipeBusy => return error.Unexpected,1056 error.PipeBusy => return error.Unexpected,
1058 error.FileLocksUnsupported => return error.Unexpected,1057 error.FileLocksUnsupported => return error.Unexpected,