| ... | ... | @@ -302,7 +302,7 @@ pub fn close(fd: fd_t) void { |
| 302 | 302 | _ = wasi.fd_close(fd); |
| 303 | 303 | return; |
| 304 | 304 | } |
| 305 | | if (comptime builtin.target.isDarwin()) { |
| 305 | if (builtin.target.isDarwin()) { |
| 306 | 306 | // This avoids the EINTR problem. |
| 307 | 307 | switch (darwin.getErrno(darwin.@"close$NOCANCEL"(fd))) { |
| 308 | 308 | .BADF => unreachable, // Always a race condition. |
| ... | ... | @@ -476,7 +476,7 @@ fn fchmodat2(dirfd: fd_t, path: []const u8, mode: mode_t, flags: u32) FChmodAtEr |
| 476 | 476 | const rc = system.openat(dirfd, &path_c, .{ .PATH = true, .NOFOLLOW = true, .CLOEXEC = true }, @as(mode_t, 0)); |
| 477 | 477 | switch (system.getErrno(rc)) { |
| 478 | 478 | .SUCCESS => { |
| 479 | | pathfd = @as(fd_t, @intCast(rc)); |
| 479 | pathfd = @intCast(rc); |
| 480 | 480 | break; |
| 481 | 481 | }, |
| 482 | 482 | .INTR => continue, |
| ... | ... | @@ -550,7 +550,7 @@ pub fn fchown(fd: fd_t, owner: ?uid_t, group: ?gid_t) FChownError!void { |
| 550 | 550 | } |
| 551 | 551 | |
| 552 | 552 | while (true) { |
| 553 | | const res = system.fchown(fd, owner orelse @as(u32, 0) -% 1, group orelse @as(u32, 0) -% 1); |
| 553 | const res = system.fchown(fd, owner orelse ~@as(uid_t, 0), group orelse ~@as(gid_t, 0)); |
| 554 | 554 | |
| 555 | 555 | switch (system.getErrno(res)) { |
| 556 | 556 | .SUCCESS => return, |
| ... | ... | @@ -596,7 +596,7 @@ pub fn reboot(cmd: RebootCommand) RebootError!void { |
| 596 | 596 | switch (system.getErrno(linux.reboot( |
| 597 | 597 | .MAGIC1, |
| 598 | 598 | .MAGIC2, |
| 599 | | @as(linux.LINUX_REBOOT.CMD, cmd), |
| 599 | cmd, |
| 600 | 600 | switch (cmd) { |
| 601 | 601 | .RESTART2 => |s| s, |
| 602 | 602 | else => null, |
| ... | ... | @@ -639,27 +639,27 @@ pub fn getrandom(buffer: []u8) GetRandomError!void { |
| 639 | 639 | std.c.versionCheck(std.SemanticVersion{ .major = 2, .minor = 25, .patch = 0 }); |
| 640 | 640 | |
| 641 | 641 | while (buf.len != 0) { |
| 642 | | const res = if (use_c) blk: { |
| 642 | const num_read: usize, const err = if (use_c) res: { |
| 643 | 643 | const rc = std.c.getrandom(buf.ptr, buf.len, 0); |
| 644 | | break :blk .{ |
| 645 | | .num_read = @as(usize, @bitCast(rc)), |
| 646 | | .err = std.c.getErrno(rc), |
| 644 | break :res .{ |
| 645 | @bitCast(rc), |
| 646 | std.c.getErrno(rc), |
| 647 | 647 | }; |
| 648 | | } else blk: { |
| 648 | } else res: { |
| 649 | 649 | const rc = linux.getrandom(buf.ptr, buf.len, 0); |
| 650 | | break :blk .{ |
| 651 | | .num_read = rc, |
| 652 | | .err = linux.getErrno(rc), |
| 650 | break :res .{ |
| 651 | rc, |
| 652 | linux.getErrno(rc), |
| 653 | 653 | }; |
| 654 | 654 | }; |
| 655 | 655 | |
| 656 | | switch (res.err) { |
| 657 | | .SUCCESS => buf = buf[res.num_read..], |
| 656 | switch (err) { |
| 657 | .SUCCESS => buf = buf[num_read..], |
| 658 | 658 | .INVAL => unreachable, |
| 659 | 659 | .FAULT => unreachable, |
| 660 | 660 | .INTR => continue, |
| 661 | 661 | .NOSYS => return getRandomBytesDevURandom(buf), |
| 662 | | else => return unexpectedErrno(res.err), |
| 662 | else => return unexpectedErrno(err), |
| 663 | 663 | } |
| 664 | 664 | } |
| 665 | 665 | return; |
| ... | ... | @@ -818,10 +818,10 @@ pub fn exit(status: u8) noreturn { |
| 818 | 818 | // exit() is only available if exitBootServices() has not been called yet. |
| 819 | 819 | // This call to exit should not fail, so we don't care about its return value. |
| 820 | 820 | if (uefi.system_table.boot_services) |bs| { |
| 821 | | _ = bs.exit(uefi.handle, @as(uefi.Status, @enumFromInt(status)), 0, null); |
| 821 | _ = bs.exit(uefi.handle, @enumFromInt(status), 0, null); |
| 822 | 822 | } |
| 823 | 823 | // If we can't exit, reboot the system instead. |
| 824 | | uefi.system_table.runtime_services.resetSystem(uefi.tables.ResetType.ResetCold, @as(uefi.Status, @enumFromInt(status)), 0, null); |
| 824 | uefi.system_table.runtime_services.resetSystem(.ResetCold, @enumFromInt(status), 0, null); |
| 825 | 825 | } |
| 826 | 826 | system.exit(status); |
| 827 | 827 | } |
| ... | ... | @@ -893,12 +893,10 @@ pub fn read(fd: fd_t, buf: []u8) ReadError!usize { |
| 893 | 893 | .macos, .ios, .watchos, .tvos => math.maxInt(i32), |
| 894 | 894 | else => math.maxInt(isize), |
| 895 | 895 | }; |
| 896 | | const adjusted_len = @min(max_count, buf.len); |
| 897 | | |
| 898 | 896 | while (true) { |
| 899 | | const rc = system.read(fd, buf.ptr, adjusted_len); |
| 897 | const rc = system.read(fd, buf.ptr, @min(buf.len, max_count)); |
| 900 | 898 | switch (errno(rc)) { |
| 901 | | .SUCCESS => return @as(usize, @intCast(rc)), |
| 899 | .SUCCESS => return @intCast(rc), |
| 902 | 900 | .INTR => continue, |
| 903 | 901 | .INVAL => unreachable, |
| 904 | 902 | .FAULT => unreachable, |
| ... | ... | @@ -932,7 +930,7 @@ pub fn read(fd: fd_t, buf: []u8) ReadError!usize { |
| 932 | 930 | pub fn readv(fd: fd_t, iov: []const iovec) ReadError!usize { |
| 933 | 931 | if (builtin.os.tag == .windows) { |
| 934 | 932 | // TODO improve this to use ReadFileScatter |
| 935 | | if (iov.len == 0) return @as(usize, 0); |
| 933 | if (iov.len == 0) return 0; |
| 936 | 934 | const first = iov[0]; |
| 937 | 935 | return read(fd, first.iov_base[0..first.iov_len]); |
| 938 | 936 | } |
| ... | ... | @@ -956,12 +954,11 @@ pub fn readv(fd: fd_t, iov: []const iovec) ReadError!usize { |
| 956 | 954 | else => |err| return unexpectedErrno(err), |
| 957 | 955 | } |
| 958 | 956 | } |
| 959 | | const iov_count = math.cast(u31, iov.len) orelse math.maxInt(u31); |
| 957 | |
| 960 | 958 | while (true) { |
| 961 | | // TODO handle the case when iov_len is too large and get rid of this @intCast |
| 962 | | const rc = system.readv(fd, iov.ptr, iov_count); |
| 959 | const rc = system.readv(fd, iov.ptr, @min(iov.len, IOV_MAX)); |
| 963 | 960 | switch (errno(rc)) { |
| 964 | | .SUCCESS => return @as(usize, @intCast(rc)), |
| 961 | .SUCCESS => return @intCast(rc), |
| 965 | 962 | .INTR => continue, |
| 966 | 963 | .INVAL => unreachable, |
| 967 | 964 | .FAULT => unreachable, |
| ... | ... | @@ -1035,15 +1032,12 @@ pub fn pread(fd: fd_t, buf: []u8, offset: u64) PReadError!usize { |
| 1035 | 1032 | .macos, .ios, .watchos, .tvos => math.maxInt(i32), |
| 1036 | 1033 | else => math.maxInt(isize), |
| 1037 | 1034 | }; |
| 1038 | | const adjusted_len = @min(max_count, buf.len); |
| 1039 | 1035 | |
| 1040 | 1036 | const pread_sym = if (lfs64_abi) system.pread64 else system.pread; |
| 1041 | | |
| 1042 | | const ioffset = @as(i64, @bitCast(offset)); // the OS treats this as unsigned |
| 1043 | 1037 | while (true) { |
| 1044 | | const rc = pread_sym(fd, buf.ptr, adjusted_len, ioffset); |
| 1038 | const rc = pread_sym(fd, buf.ptr, @min(buf.len, max_count), @bitCast(offset)); |
| 1045 | 1039 | switch (errno(rc)) { |
| 1046 | | .SUCCESS => return @as(usize, @intCast(rc)), |
| 1040 | .SUCCESS => return @intCast(rc), |
| 1047 | 1041 | .INTR => continue, |
| 1048 | 1042 | .INVAL => unreachable, |
| 1049 | 1043 | .FAULT => unreachable, |
| ... | ... | @@ -1078,7 +1072,7 @@ pub fn ftruncate(fd: fd_t, length: u64) TruncateError!void { |
| 1078 | 1072 | if (builtin.os.tag == .windows) { |
| 1079 | 1073 | var io_status_block: windows.IO_STATUS_BLOCK = undefined; |
| 1080 | 1074 | var eof_info = windows.FILE_END_OF_FILE_INFORMATION{ |
| 1081 | | .EndOfFile = @as(windows.LARGE_INTEGER, @bitCast(length)), |
| 1075 | .EndOfFile = @bitCast(length), |
| 1082 | 1076 | }; |
| 1083 | 1077 | |
| 1084 | 1078 | const rc = windows.ntdll.NtSetInformationFile( |
| ... | ... | @@ -1111,11 +1105,9 @@ pub fn ftruncate(fd: fd_t, length: u64) TruncateError!void { |
| 1111 | 1105 | } |
| 1112 | 1106 | } |
| 1113 | 1107 | |
| 1108 | const ftruncate_sym = if (lfs64_abi) system.ftruncate64 else system.ftruncate; |
| 1114 | 1109 | while (true) { |
| 1115 | | const ftruncate_sym = if (lfs64_abi) system.ftruncate64 else system.ftruncate; |
| 1116 | | |
| 1117 | | const ilen = @as(i64, @bitCast(length)); // the OS treats this as unsigned |
| 1118 | | switch (errno(ftruncate_sym(fd, ilen))) { |
| 1110 | switch (errno(ftruncate_sym(fd, @bitCast(length)))) { |
| 1119 | 1111 | .SUCCESS => return, |
| 1120 | 1112 | .INTR => continue, |
| 1121 | 1113 | .FBIG => return error.FileTooBig, |
| ... | ... | @@ -1178,15 +1170,11 @@ pub fn preadv(fd: fd_t, iov: []const iovec, offset: u64) PReadError!usize { |
| 1178 | 1170 | } |
| 1179 | 1171 | } |
| 1180 | 1172 | |
| 1181 | | const iov_count = math.cast(u31, iov.len) orelse math.maxInt(u31); |
| 1182 | | |
| 1183 | 1173 | const preadv_sym = if (lfs64_abi) system.preadv64 else system.preadv; |
| 1184 | | |
| 1185 | | const ioffset = @as(i64, @bitCast(offset)); // the OS treats this as unsigned |
| 1186 | 1174 | while (true) { |
| 1187 | | const rc = preadv_sym(fd, iov.ptr, iov_count, ioffset); |
| 1175 | const rc = preadv_sym(fd, iov.ptr, @min(iov.len, IOV_MAX), @bitCast(offset)); |
| 1188 | 1176 | switch (errno(rc)) { |
| 1189 | | .SUCCESS => return @as(usize, @bitCast(rc)), |
| 1177 | .SUCCESS => return @bitCast(rc), |
| 1190 | 1178 | .INTR => continue, |
| 1191 | 1179 | .INVAL => unreachable, |
| 1192 | 1180 | .FAULT => unreachable, |
| ... | ... | @@ -1293,12 +1281,10 @@ pub fn write(fd: fd_t, bytes: []const u8) WriteError!usize { |
| 1293 | 1281 | .macos, .ios, .watchos, .tvos => math.maxInt(i32), |
| 1294 | 1282 | else => math.maxInt(isize), |
| 1295 | 1283 | }; |
| 1296 | | const adjusted_len = @min(max_count, bytes.len); |
| 1297 | | |
| 1298 | 1284 | while (true) { |
| 1299 | | const rc = system.write(fd, bytes.ptr, adjusted_len); |
| 1285 | const rc = system.write(fd, bytes.ptr, @min(bytes.len, max_count)); |
| 1300 | 1286 | switch (errno(rc)) { |
| 1301 | | .SUCCESS => return @as(usize, @intCast(rc)), |
| 1287 | .SUCCESS => return @intCast(rc), |
| 1302 | 1288 | .INTR => continue, |
| 1303 | 1289 | .INVAL => return error.InvalidArgument, |
| 1304 | 1290 | .FAULT => unreachable, |
| ... | ... | @@ -1342,7 +1328,7 @@ pub fn write(fd: fd_t, bytes: []const u8) WriteError!usize { |
| 1342 | 1328 | pub fn writev(fd: fd_t, iov: []const iovec_const) WriteError!usize { |
| 1343 | 1329 | if (builtin.os.tag == .windows) { |
| 1344 | 1330 | // TODO improve this to use WriteFileScatter |
| 1345 | | if (iov.len == 0) return @as(usize, 0); |
| 1331 | if (iov.len == 0) return 0; |
| 1346 | 1332 | const first = iov[0]; |
| 1347 | 1333 | return write(fd, first.iov_base[0..first.iov_len]); |
| 1348 | 1334 | } |
| ... | ... | @@ -1367,11 +1353,10 @@ pub fn writev(fd: fd_t, iov: []const iovec_const) WriteError!usize { |
| 1367 | 1353 | } |
| 1368 | 1354 | } |
| 1369 | 1355 | |
| 1370 | | const iov_count = if (iov.len > IOV_MAX) IOV_MAX else @as(u31, @intCast(iov.len)); |
| 1371 | 1356 | while (true) { |
| 1372 | | const rc = system.writev(fd, iov.ptr, iov_count); |
| 1357 | const rc = system.writev(fd, iov.ptr, @min(iov.len, IOV_MAX)); |
| 1373 | 1358 | switch (errno(rc)) { |
| 1374 | | .SUCCESS => return @as(usize, @intCast(rc)), |
| 1359 | .SUCCESS => return @intCast(rc), |
| 1375 | 1360 | .INTR => continue, |
| 1376 | 1361 | .INVAL => return error.InvalidArgument, |
| 1377 | 1362 | .FAULT => unreachable, |
| ... | ... | @@ -1455,15 +1440,12 @@ pub fn pwrite(fd: fd_t, bytes: []const u8, offset: u64) PWriteError!usize { |
| 1455 | 1440 | .macos, .ios, .watchos, .tvos => math.maxInt(i32), |
| 1456 | 1441 | else => math.maxInt(isize), |
| 1457 | 1442 | }; |
| 1458 | | const adjusted_len = @min(max_count, bytes.len); |
| 1459 | 1443 | |
| 1460 | 1444 | const pwrite_sym = if (lfs64_abi) system.pwrite64 else system.pwrite; |
| 1461 | | |
| 1462 | | const ioffset = @as(i64, @bitCast(offset)); // the OS treats this as unsigned |
| 1463 | 1445 | while (true) { |
| 1464 | | const rc = pwrite_sym(fd, bytes.ptr, adjusted_len, ioffset); |
| 1446 | const rc = pwrite_sym(fd, bytes.ptr, @min(bytes.len, max_count), @bitCast(offset)); |
| 1465 | 1447 | switch (errno(rc)) { |
| 1466 | | .SUCCESS => return @as(usize, @intCast(rc)), |
| 1448 | .SUCCESS => return @intCast(rc), |
| 1467 | 1449 | .INTR => continue, |
| 1468 | 1450 | .INVAL => return error.InvalidArgument, |
| 1469 | 1451 | .FAULT => unreachable, |
| ... | ... | @@ -1515,7 +1497,7 @@ pub fn pwritev(fd: fd_t, iov: []const iovec_const, offset: u64) PWriteError!usiz |
| 1515 | 1497 | if (have_pwrite_but_not_pwritev) { |
| 1516 | 1498 | // We could loop here; but proper usage of `pwritev` must handle partial writes anyway. |
| 1517 | 1499 | // So we simply write the first vector only. |
| 1518 | | if (iov.len == 0) return @as(usize, 0); |
| 1500 | if (iov.len == 0) return 0; |
| 1519 | 1501 | const first = iov[0]; |
| 1520 | 1502 | return pwrite(fd, first.iov_base[0..first.iov_len], offset); |
| 1521 | 1503 | } |
| ... | ... | @@ -1544,13 +1526,10 @@ pub fn pwritev(fd: fd_t, iov: []const iovec_const, offset: u64) PWriteError!usiz |
| 1544 | 1526 | } |
| 1545 | 1527 | |
| 1546 | 1528 | const pwritev_sym = if (lfs64_abi) system.pwritev64 else system.pwritev; |
| 1547 | | |
| 1548 | | const iov_count = if (iov.len > IOV_MAX) IOV_MAX else @as(u31, @intCast(iov.len)); |
| 1549 | | const ioffset = @as(i64, @bitCast(offset)); // the OS treats this as unsigned |
| 1550 | 1529 | while (true) { |
| 1551 | | const rc = pwritev_sym(fd, iov.ptr, iov_count, ioffset); |
| 1530 | const rc = pwritev_sym(fd, iov.ptr, @min(iov.len, IOV_MAX), @bitCast(offset)); |
| 1552 | 1531 | switch (errno(rc)) { |
| 1553 | | .SUCCESS => return @as(usize, @intCast(rc)), |
| 1532 | .SUCCESS => return @intCast(rc), |
| 1554 | 1533 | .INTR => continue, |
| 1555 | 1534 | .INVAL => return error.InvalidArgument, |
| 1556 | 1535 | .FAULT => unreachable, |
| ... | ... | @@ -1666,11 +1645,10 @@ pub fn openZ(file_path: [*:0]const u8, flags: O, perm: mode_t) OpenError!fd_t { |
| 1666 | 1645 | } |
| 1667 | 1646 | |
| 1668 | 1647 | const open_sym = if (lfs64_abi) system.open64 else system.open; |
| 1669 | | |
| 1670 | 1648 | while (true) { |
| 1671 | 1649 | const rc = open_sym(file_path, flags, perm); |
| 1672 | 1650 | switch (errno(rc)) { |
| 1673 | | .SUCCESS => return @as(fd_t, @intCast(rc)), |
| 1651 | .SUCCESS => return @intCast(rc), |
| 1674 | 1652 | .INTR => continue, |
| 1675 | 1653 | |
| 1676 | 1654 | .FAULT => unreachable, |
| ... | ... | @@ -1863,11 +1841,10 @@ pub fn openatZ(dir_fd: fd_t, file_path: [*:0]const u8, flags: O, mode: mode_t) O |
| 1863 | 1841 | } |
| 1864 | 1842 | |
| 1865 | 1843 | const openat_sym = if (lfs64_abi) system.openat64 else system.openat; |
| 1866 | | |
| 1867 | 1844 | while (true) { |
| 1868 | 1845 | const rc = openat_sym(dir_fd, file_path, flags, mode); |
| 1869 | 1846 | switch (errno(rc)) { |
| 1870 | | .SUCCESS => return @as(fd_t, @intCast(rc)), |
| 1847 | .SUCCESS => return @intCast(rc), |
| 1871 | 1848 | .INTR => continue, |
| 1872 | 1849 | |
| 1873 | 1850 | .FAULT => unreachable, |
| ... | ... | @@ -1900,7 +1877,7 @@ pub fn openatZ(dir_fd: fd_t, file_path: [*:0]const u8, flags: O, mode: mode_t) O |
| 1900 | 1877 | pub fn dup(old_fd: fd_t) !fd_t { |
| 1901 | 1878 | const rc = system.dup(old_fd); |
| 1902 | 1879 | return switch (errno(rc)) { |
| 1903 | | .SUCCESS => return @as(fd_t, @intCast(rc)), |
| 1880 | .SUCCESS => return @intCast(rc), |
| 1904 | 1881 | .MFILE => error.ProcessFdQuotaExceeded, |
| 1905 | 1882 | .BADF => unreachable, // invalid file descriptor |
| 1906 | 1883 | else => |err| return unexpectedErrno(err), |
| ... | ... | @@ -2144,11 +2121,11 @@ pub fn getcwd(out_buffer: []u8) GetCwdError![]u8 { |
| 2144 | 2121 | return result; |
| 2145 | 2122 | } |
| 2146 | 2123 | |
| 2147 | | const err = if (builtin.link_libc) blk: { |
| 2124 | const err: E = if (builtin.link_libc) err: { |
| 2148 | 2125 | const c_err = if (std.c.getcwd(out_buffer.ptr, out_buffer.len)) |_| 0 else std.c._errno().*; |
| 2149 | | break :blk @as(E, @enumFromInt(c_err)); |
| 2150 | | } else blk: { |
| 2151 | | break :blk errno(system.getcwd(out_buffer.ptr, out_buffer.len)); |
| 2126 | break :err @enumFromInt(c_err); |
| 2127 | } else err: { |
| 2128 | break :err errno(system.getcwd(out_buffer.ptr, out_buffer.len)); |
| 2152 | 2129 | }; |
| 2153 | 2130 | switch (err) { |
| 2154 | 2131 | .SUCCESS => return mem.sliceTo(out_buffer, 0), |
| ... | ... | @@ -2880,13 +2857,13 @@ pub fn renameatW( |
| 2880 | 2857 | // supported in order to avoid either (1) using a redundant call that we can know in advance will return |
| 2881 | 2858 | // STATUS_NOT_SUPPORTED or (2) only setting IGNORE_READONLY_ATTRIBUTE when >= rs5 |
| 2882 | 2859 | // and therefore having different behavior when the Windows version is >= rs1 but < rs5. |
| 2883 | | if (comptime builtin.target.os.version_range.windows.min.isAtLeast(.win10_rs5)) { |
| 2860 | if (builtin.target.os.isAtLeast(.windows, .win10_rs5) orelse false) { |
| 2884 | 2861 | const struct_buf_len = @sizeOf(windows.FILE_RENAME_INFORMATION_EX) + (MAX_PATH_BYTES - 1); |
| 2885 | 2862 | var rename_info_buf: [struct_buf_len]u8 align(@alignOf(windows.FILE_RENAME_INFORMATION_EX)) = undefined; |
| 2886 | 2863 | const struct_len = @sizeOf(windows.FILE_RENAME_INFORMATION_EX) - 1 + new_path_w.len * 2; |
| 2887 | 2864 | if (struct_len > struct_buf_len) return error.NameTooLong; |
| 2888 | 2865 | |
| 2889 | | const rename_info = @as(*windows.FILE_RENAME_INFORMATION_EX, @ptrCast(&rename_info_buf)); |
| 2866 | const rename_info: *windows.FILE_RENAME_INFORMATION_EX = @ptrCast(&rename_info_buf); |
| 2890 | 2867 | var io_status_block: windows.IO_STATUS_BLOCK = undefined; |
| 2891 | 2868 | |
| 2892 | 2869 | var flags: windows.ULONG = windows.FILE_RENAME_POSIX_SEMANTICS | windows.FILE_RENAME_IGNORE_READONLY_ATTRIBUTE; |
| ... | ... | @@ -2897,7 +2874,7 @@ pub fn renameatW( |
| 2897 | 2874 | .FileNameLength = @intCast(new_path_w.len * 2), // already checked error.NameTooLong |
| 2898 | 2875 | .FileName = undefined, |
| 2899 | 2876 | }; |
| 2900 | | @memcpy(@as([*]u16, &rename_info.FileName)[0..new_path_w.len], new_path_w); |
| 2877 | @memcpy((&rename_info.FileName).ptr, new_path_w); |
| 2901 | 2878 | rc = windows.ntdll.NtSetInformationFile( |
| 2902 | 2879 | src_fd, |
| 2903 | 2880 | &io_status_block, |
| ... | ... | @@ -2923,7 +2900,7 @@ pub fn renameatW( |
| 2923 | 2900 | const struct_len = @sizeOf(windows.FILE_RENAME_INFORMATION) - 1 + new_path_w.len * 2; |
| 2924 | 2901 | if (struct_len > struct_buf_len) return error.NameTooLong; |
| 2925 | 2902 | |
| 2926 | | const rename_info = @as(*windows.FILE_RENAME_INFORMATION, @ptrCast(&rename_info_buf)); |
| 2903 | const rename_info: *windows.FILE_RENAME_INFORMATION = @ptrCast(&rename_info_buf); |
| 2927 | 2904 | var io_status_block: windows.IO_STATUS_BLOCK = undefined; |
| 2928 | 2905 | |
| 2929 | 2906 | rename_info.* = .{ |
| ... | ... | @@ -2932,7 +2909,7 @@ pub fn renameatW( |
| 2932 | 2909 | .FileNameLength = @intCast(new_path_w.len * 2), // already checked error.NameTooLong |
| 2933 | 2910 | .FileName = undefined, |
| 2934 | 2911 | }; |
| 2935 | | @memcpy(@as([*]u16, &rename_info.FileName)[0..new_path_w.len], new_path_w); |
| 2912 | @memcpy((&rename_info.FileName).ptr, new_path_w); |
| 2936 | 2913 | |
| 2937 | 2914 | rc = |
| 2938 | 2915 | windows.ntdll.NtSetInformationFile( |
| ... | ... | @@ -3380,7 +3357,7 @@ pub fn readlinkZ(file_path: [*:0]const u8, out_buffer: []u8) ReadLinkError![]u8 |
| 3380 | 3357 | } |
| 3381 | 3358 | const rc = system.readlink(file_path, out_buffer.ptr, out_buffer.len); |
| 3382 | 3359 | switch (errno(rc)) { |
| 3383 | | .SUCCESS => return out_buffer[0..@as(usize, @bitCast(rc))], |
| 3360 | .SUCCESS => return out_buffer[0..@bitCast(rc)], |
| 3384 | 3361 | .ACCES => return error.AccessDenied, |
| 3385 | 3362 | .FAULT => unreachable, |
| 3386 | 3363 | .INVAL => return error.NotLink, |
| ... | ... | @@ -3458,7 +3435,7 @@ pub fn readlinkatZ(dirfd: fd_t, file_path: [*:0]const u8, out_buffer: []u8) Read |
| 3458 | 3435 | } |
| 3459 | 3436 | const rc = system.readlinkat(dirfd, file_path, out_buffer.ptr, out_buffer.len); |
| 3460 | 3437 | switch (errno(rc)) { |
| 3461 | | .SUCCESS => return out_buffer[0..@as(usize, @bitCast(rc))], |
| 3438 | .SUCCESS => return out_buffer[0..@bitCast(rc)], |
| 3462 | 3439 | .ACCES => return error.AccessDenied, |
| 3463 | 3440 | .FAULT => unreachable, |
| 3464 | 3441 | .INVAL => return error.NotLink, |
| ... | ... | @@ -3570,7 +3547,7 @@ pub fn isatty(handle: fd_t) bool { |
| 3570 | 3547 | if (builtin.os.tag == .linux) { |
| 3571 | 3548 | while (true) { |
| 3572 | 3549 | var wsz: linux.winsize = undefined; |
| 3573 | | const fd = @as(usize, @bitCast(@as(isize, handle))); |
| 3550 | const fd: usize = @bitCast(@as(isize, handle)); |
| 3574 | 3551 | const rc = linux.syscall3(.ioctl, fd, linux.T.IOCGWINSZ, @intFromPtr(&wsz)); |
| 3575 | 3552 | switch (linux.getErrno(rc)) { |
| 3576 | 3553 | .SUCCESS => return true, |
| ... | ... | @@ -3614,15 +3591,15 @@ pub fn isCygwinPty(handle: fd_t) bool { |
| 3614 | 3591 | var name_info_bytes align(@alignOf(windows.FILE_NAME_INFO)) = [_]u8{0} ** (name_bytes_offset + num_name_bytes); |
| 3615 | 3592 | |
| 3616 | 3593 | var io_status_block: windows.IO_STATUS_BLOCK = undefined; |
| 3617 | | const rc = windows.ntdll.NtQueryInformationFile(handle, &io_status_block, &name_info_bytes, @as(u32, @intCast(name_info_bytes.len)), .FileNameInformation); |
| 3594 | const rc = windows.ntdll.NtQueryInformationFile(handle, &io_status_block, &name_info_bytes, @intCast(name_info_bytes.len), .FileNameInformation); |
| 3618 | 3595 | switch (rc) { |
| 3619 | 3596 | .SUCCESS => {}, |
| 3620 | 3597 | .INVALID_PARAMETER => unreachable, |
| 3621 | 3598 | else => return false, |
| 3622 | 3599 | } |
| 3623 | 3600 | |
| 3624 | | const name_info = @as(*const windows.FILE_NAME_INFO, @ptrCast(&name_info_bytes[0])); |
| 3625 | | const name_bytes = name_info_bytes[name_bytes_offset .. name_bytes_offset + @as(usize, name_info.FileNameLength)]; |
| 3601 | const name_info: *const windows.FILE_NAME_INFO = @ptrCast(&name_info_bytes); |
| 3602 | const name_bytes = name_info_bytes[name_bytes_offset .. name_bytes_offset + name_info.FileNameLength]; |
| 3626 | 3603 | const name_wide = mem.bytesAsSlice(u16, name_bytes); |
| 3627 | 3604 | // Note: The name we get from NtQueryInformationFile will be prefixed with a '\', e.g. \msys-1888ae32e00d56aa-pty0-to-master |
| 3628 | 3605 | return (mem.startsWith(u16, name_wide, &[_]u16{ '\\', 'm', 's', 'y', 's', '-' }) or |
| ... | ... | @@ -3668,9 +3645,9 @@ pub fn socket(domain: u32, socket_type: u32, protocol: u32) SocketError!socket_t |
| 3668 | 3645 | else |
| 3669 | 3646 | 0; |
| 3670 | 3647 | const rc = try windows.WSASocketW( |
| 3671 | | @as(i32, @bitCast(domain)), |
| 3672 | | @as(i32, @bitCast(filtered_sock_type)), |
| 3673 | | @as(i32, @bitCast(protocol)), |
| 3648 | @bitCast(domain), |
| 3649 | @bitCast(filtered_sock_type), |
| 3650 | @bitCast(protocol), |
| 3674 | 3651 | null, |
| 3675 | 3652 | 0, |
| 3676 | 3653 | flags, |
| ... | ... | @@ -3688,7 +3665,7 @@ pub fn socket(domain: u32, socket_type: u32, protocol: u32) SocketError!socket_t |
| 3688 | 3665 | return rc; |
| 3689 | 3666 | } |
| 3690 | 3667 | |
| 3691 | | const have_sock_flags = comptime !builtin.target.isDarwin(); |
| 3668 | const have_sock_flags = !builtin.target.isDarwin(); |
| 3692 | 3669 | const filtered_sock_type = if (!have_sock_flags) |
| 3693 | 3670 | socket_type & ~@as(u32, SOCK.NONBLOCK | SOCK.CLOEXEC) |
| 3694 | 3671 | else |
| ... | ... | @@ -3696,7 +3673,7 @@ pub fn socket(domain: u32, socket_type: u32, protocol: u32) SocketError!socket_t |
| 3696 | 3673 | const rc = system.socket(domain, filtered_sock_type, protocol); |
| 3697 | 3674 | switch (errno(rc)) { |
| 3698 | 3675 | .SUCCESS => { |
| 3699 | | const fd = @as(fd_t, @intCast(rc)); |
| 3676 | const fd: fd_t = @intCast(rc); |
| 3700 | 3677 | errdefer close(fd); |
| 3701 | 3678 | if (!have_sock_flags) { |
| 3702 | 3679 | try setSockFlags(fd, socket_type); |
| ... | ... | @@ -3984,10 +3961,10 @@ pub fn accept( |
| 3984 | 3961 | /// description of the `CLOEXEC` flag in `open` for reasons why this may be useful. |
| 3985 | 3962 | flags: u32, |
| 3986 | 3963 | ) AcceptError!socket_t { |
| 3987 | | const have_accept4 = comptime !(builtin.target.isDarwin() or builtin.os.tag == .windows); |
| 3964 | const have_accept4 = !(builtin.target.isDarwin() or builtin.os.tag == .windows); |
| 3988 | 3965 | assert(0 == (flags & ~@as(u32, SOCK.NONBLOCK | SOCK.CLOEXEC))); // Unsupported flag(s) |
| 3989 | 3966 | |
| 3990 | | const accepted_sock = while (true) { |
| 3967 | const accepted_sock: socket_t = while (true) { |
| 3991 | 3968 | const rc = if (have_accept4) |
| 3992 | 3969 | system.accept4(sock, addr, addr_size, flags) |
| 3993 | 3970 | else if (builtin.os.tag == .windows) |
| ... | ... | @@ -4014,9 +3991,7 @@ pub fn accept( |
| 4014 | 3991 | } |
| 4015 | 3992 | } else { |
| 4016 | 3993 | switch (errno(rc)) { |
| 4017 | | .SUCCESS => { |
| 4018 | | break @as(socket_t, @intCast(rc)); |
| 4019 | | }, |
| 3994 | .SUCCESS => break @intCast(rc), |
| 4020 | 3995 | .INTR => continue, |
| 4021 | 3996 | .AGAIN => return error.WouldBlock, |
| 4022 | 3997 | .BADF => unreachable, // always a race condition |
| ... | ... | @@ -4063,7 +4038,7 @@ pub const EpollCreateError = error{ |
| 4063 | 4038 | pub fn epoll_create1(flags: u32) EpollCreateError!i32 { |
| 4064 | 4039 | const rc = system.epoll_create1(flags); |
| 4065 | 4040 | switch (errno(rc)) { |
| 4066 | | .SUCCESS => return @as(i32, @intCast(rc)), |
| 4041 | .SUCCESS => return @intCast(rc), |
| 4067 | 4042 | else => |err| return unexpectedErrno(err), |
| 4068 | 4043 | |
| 4069 | 4044 | .INVAL => unreachable, |
| ... | ... | @@ -4122,9 +4097,9 @@ pub fn epoll_ctl(epfd: i32, op: u32, fd: i32, event: ?*linux.epoll_event) EpollC |
| 4122 | 4097 | pub fn epoll_wait(epfd: i32, events: []linux.epoll_event, timeout: i32) usize { |
| 4123 | 4098 | while (true) { |
| 4124 | 4099 | // TODO get rid of the @intCast |
| 4125 | | const rc = system.epoll_wait(epfd, events.ptr, @as(u32, @intCast(events.len)), timeout); |
| 4100 | const rc = system.epoll_wait(epfd, events.ptr, @intCast(events.len), timeout); |
| 4126 | 4101 | switch (errno(rc)) { |
| 4127 | | .SUCCESS => return @as(usize, @intCast(rc)), |
| 4102 | .SUCCESS => return @intCast(rc), |
| 4128 | 4103 | .INTR => continue, |
| 4129 | 4104 | .BADF => unreachable, |
| 4130 | 4105 | .FAULT => unreachable, |
| ... | ... | @@ -4143,7 +4118,7 @@ pub const EventFdError = error{ |
| 4143 | 4118 | pub fn eventfd(initval: u32, flags: u32) EventFdError!i32 { |
| 4144 | 4119 | const rc = system.eventfd(initval, flags); |
| 4145 | 4120 | switch (errno(rc)) { |
| 4146 | | .SUCCESS => return @as(i32, @intCast(rc)), |
| 4121 | .SUCCESS => return @intCast(rc), |
| 4147 | 4122 | else => |err| return unexpectedErrno(err), |
| 4148 | 4123 | |
| 4149 | 4124 | .INVAL => unreachable, // invalid parameters |
| ... | ... | @@ -4277,7 +4252,7 @@ pub const ConnectError = error{ |
| 4277 | 4252 | /// return error.WouldBlock when EAGAIN or EINPROGRESS is received. |
| 4278 | 4253 | pub fn connect(sock: socket_t, sock_addr: *const sockaddr, len: socklen_t) ConnectError!void { |
| 4279 | 4254 | if (builtin.os.tag == .windows) { |
| 4280 | | const rc = windows.ws2_32.connect(sock, sock_addr, @as(i32, @intCast(len))); |
| 4255 | const rc = windows.ws2_32.connect(sock, sock_addr, @intCast(len)); |
| 4281 | 4256 | if (rc == 0) return; |
| 4282 | 4257 | switch (windows.ws2_32.WSAGetLastError()) { |
| 4283 | 4258 | .WSAEADDRINUSE => return error.AddressInUse, |
| ... | ... | @@ -4332,7 +4307,7 @@ pub fn connect(sock: socket_t, sock_addr: *const sockaddr, len: socklen_t) Conne |
| 4332 | 4307 | pub fn getsockoptError(sockfd: fd_t) ConnectError!void { |
| 4333 | 4308 | var err_code: i32 = undefined; |
| 4334 | 4309 | var size: u32 = @sizeOf(u32); |
| 4335 | | const rc = system.getsockopt(sockfd, SOL.SOCKET, SO.ERROR, @as([*]u8, @ptrCast(&err_code)), &size); |
| 4310 | const rc = system.getsockopt(sockfd, SOL.SOCKET, SO.ERROR, @ptrCast(&err_code), &size); |
| 4336 | 4311 | assert(size == 4); |
| 4337 | 4312 | switch (errno(rc)) { |
| 4338 | 4313 | .SUCCESS => switch (@as(E, @enumFromInt(err_code))) { |
| ... | ... | @@ -4373,15 +4348,13 @@ pub const WaitPidResult = struct { |
| 4373 | 4348 | /// Use this version of the `waitpid` wrapper if you spawned your child process using explicit |
| 4374 | 4349 | /// `fork` and `execve` method. |
| 4375 | 4350 | pub fn waitpid(pid: pid_t, flags: u32) WaitPidResult { |
| 4376 | | const Status = if (builtin.link_libc) c_int else u32; |
| 4377 | | var status: Status = undefined; |
| 4378 | | const coerced_flags = if (builtin.link_libc) @as(c_int, @intCast(flags)) else flags; |
| 4351 | var status: if (builtin.link_libc) c_int else u32 = undefined; |
| 4379 | 4352 | while (true) { |
| 4380 | | const rc = system.waitpid(pid, &status, coerced_flags); |
| 4353 | const rc = system.waitpid(pid, &status, @intCast(flags)); |
| 4381 | 4354 | switch (errno(rc)) { |
| 4382 | 4355 | .SUCCESS => return .{ |
| 4383 | | .pid = @as(pid_t, @intCast(rc)), |
| 4384 | | .status = @as(u32, @bitCast(status)), |
| 4356 | .pid = @intCast(rc), |
| 4357 | .status = @bitCast(status), |
| 4385 | 4358 | }, |
| 4386 | 4359 | .INTR => continue, |
| 4387 | 4360 | .CHILD => unreachable, // The process specified does not exist. It would be a race condition to handle this error. |
| ... | ... | @@ -4392,15 +4365,13 @@ pub fn waitpid(pid: pid_t, flags: u32) WaitPidResult { |
| 4392 | 4365 | } |
| 4393 | 4366 | |
| 4394 | 4367 | pub fn wait4(pid: pid_t, flags: u32, ru: ?*rusage) WaitPidResult { |
| 4395 | | const Status = if (builtin.link_libc) c_int else u32; |
| 4396 | | var status: Status = undefined; |
| 4397 | | const coerced_flags = if (builtin.link_libc) @as(c_int, @intCast(flags)) else flags; |
| 4368 | var status: if (builtin.link_libc) c_int else u32 = undefined; |
| 4398 | 4369 | while (true) { |
| 4399 | | const rc = system.wait4(pid, &status, coerced_flags, ru); |
| 4370 | const rc = system.wait4(pid, &status, @intCast(flags), ru); |
| 4400 | 4371 | switch (errno(rc)) { |
| 4401 | 4372 | .SUCCESS => return .{ |
| 4402 | | .pid = @as(pid_t, @intCast(rc)), |
| 4403 | | .status = @as(u32, @bitCast(status)), |
| 4373 | .pid = @intCast(rc), |
| 4374 | .status = @bitCast(status), |
| 4404 | 4375 | }, |
| 4405 | 4376 | .INTR => continue, |
| 4406 | 4377 | .CHILD => unreachable, // The process specified does not exist. It would be a race condition to handle this error. |
| ... | ... | @@ -4428,7 +4399,6 @@ pub fn fstat(fd: fd_t) FStatError!Stat { |
| 4428 | 4399 | } |
| 4429 | 4400 | |
| 4430 | 4401 | const fstat_sym = if (lfs64_abi) system.fstat64 else system.fstat; |
| 4431 | | |
| 4432 | 4402 | var stat = mem.zeroes(Stat); |
| 4433 | 4403 | switch (errno(fstat_sym(fd, &stat))) { |
| 4434 | 4404 | .SUCCESS => return stat, |
| ... | ... | @@ -4512,7 +4482,6 @@ pub fn fstatatZ(dirfd: fd_t, pathname: [*:0]const u8, flags: u32) FStatAtError!S |
| 4512 | 4482 | } |
| 4513 | 4483 | |
| 4514 | 4484 | const fstatat_sym = if (lfs64_abi) system.fstatat64 else system.fstatat; |
| 4515 | | |
| 4516 | 4485 | var stat = mem.zeroes(Stat); |
| 4517 | 4486 | switch (errno(fstatat_sym(dirfd, pathname, &stat, flags))) { |
| 4518 | 4487 | .SUCCESS => return stat, |
| ... | ... | @@ -4545,7 +4514,7 @@ pub const KQueueError = error{ |
| 4545 | 4514 | pub fn kqueue() KQueueError!i32 { |
| 4546 | 4515 | const rc = system.kqueue(); |
| 4547 | 4516 | switch (errno(rc)) { |
| 4548 | | .SUCCESS => return @as(i32, @intCast(rc)), |
| 4517 | .SUCCESS => return @intCast(rc), |
| 4549 | 4518 | .MFILE => return error.ProcessFdQuotaExceeded, |
| 4550 | 4519 | .NFILE => return error.SystemFdQuotaExceeded, |
| 4551 | 4520 | else => |err| return unexpectedErrno(err), |
| ... | ... | @@ -4586,7 +4555,7 @@ pub fn kevent( |
| 4586 | 4555 | timeout, |
| 4587 | 4556 | ); |
| 4588 | 4557 | switch (errno(rc)) { |
| 4589 | | .SUCCESS => return @as(usize, @intCast(rc)), |
| 4558 | .SUCCESS => return @intCast(rc), |
| 4590 | 4559 | .ACCES => return error.AccessDenied, |
| 4591 | 4560 | .FAULT => unreachable, |
| 4592 | 4561 | .BADF => unreachable, // Always a race condition. |
| ... | ... | @@ -4610,7 +4579,7 @@ pub const INotifyInitError = error{ |
| 4610 | 4579 | pub fn inotify_init1(flags: u32) INotifyInitError!i32 { |
| 4611 | 4580 | const rc = system.inotify_init1(flags); |
| 4612 | 4581 | switch (errno(rc)) { |
| 4613 | | .SUCCESS => return @as(i32, @intCast(rc)), |
| 4582 | .SUCCESS => return @intCast(rc), |
| 4614 | 4583 | .INVAL => unreachable, |
| 4615 | 4584 | .MFILE => return error.ProcessFdQuotaExceeded, |
| 4616 | 4585 | .NFILE => return error.SystemFdQuotaExceeded, |
| ... | ... | @@ -4639,7 +4608,7 @@ pub fn inotify_add_watch(inotify_fd: i32, pathname: []const u8, mask: u32) INoti |
| 4639 | 4608 | pub fn inotify_add_watchZ(inotify_fd: i32, pathname: [*:0]const u8, mask: u32) INotifyAddWatchError!i32 { |
| 4640 | 4609 | const rc = system.inotify_add_watch(inotify_fd, pathname, mask); |
| 4641 | 4610 | switch (errno(rc)) { |
| 4642 | | .SUCCESS => return @as(i32, @intCast(rc)), |
| 4611 | .SUCCESS => return @intCast(rc), |
| 4643 | 4612 | .ACCES => return error.AccessDenied, |
| 4644 | 4613 | .BADF => unreachable, |
| 4645 | 4614 | .FAULT => unreachable, |
| ... | ... | @@ -4675,7 +4644,7 @@ pub const FanotifyInitError = error{ |
| 4675 | 4644 | pub fn fanotify_init(flags: u32, event_f_flags: u32) FanotifyInitError!i32 { |
| 4676 | 4645 | const rc = system.fanotify_init(flags, event_f_flags); |
| 4677 | 4646 | switch (errno(rc)) { |
| 4678 | | .SUCCESS => return @as(i32, @intCast(rc)), |
| 4647 | .SUCCESS => return @intCast(rc), |
| 4679 | 4648 | .INVAL => unreachable, |
| 4680 | 4649 | .MFILE => return error.ProcessFdQuotaExceeded, |
| 4681 | 4650 | .NFILE => return error.SystemFdQuotaExceeded, |
| ... | ... | @@ -4780,7 +4749,7 @@ pub const ForkError = error{SystemResources} || UnexpectedError; |
| 4780 | 4749 | pub fn fork() ForkError!pid_t { |
| 4781 | 4750 | const rc = system.fork(); |
| 4782 | 4751 | switch (errno(rc)) { |
| 4783 | | .SUCCESS => return @as(pid_t, @intCast(rc)), |
| 4752 | .SUCCESS => return @intCast(rc), |
| 4784 | 4753 | .AGAIN => return error.SystemResources, |
| 4785 | 4754 | .NOMEM => return error.SystemResources, |
| 4786 | 4755 | else => |err| return unexpectedErrno(err), |
| ... | ... | @@ -4820,12 +4789,10 @@ pub fn mmap( |
| 4820 | 4789 | offset: u64, |
| 4821 | 4790 | ) MMapError![]align(mem.page_size) u8 { |
| 4822 | 4791 | const mmap_sym = if (lfs64_abi) system.mmap64 else system.mmap; |
| 4823 | | |
| 4824 | | const ioffset: i64 = @bitCast(offset); // the OS treats this as unsigned |
| 4825 | | const rc = mmap_sym(ptr, length, prot, @bitCast(flags), fd, ioffset); |
| 4826 | | const err = if (builtin.link_libc) blk: { |
| 4792 | const rc = mmap_sym(ptr, length, prot, @bitCast(flags), fd, @bitCast(offset)); |
| 4793 | const err: E = if (builtin.link_libc) blk: { |
| 4827 | 4794 | if (rc != std.c.MAP_FAILED) return @as([*]align(mem.page_size) u8, @ptrCast(@alignCast(rc)))[0..length]; |
| 4828 | | break :blk @as(E, @enumFromInt(system._errno().*)); |
| 4795 | break :blk @enumFromInt(system._errno().*); |
| 4829 | 4796 | } else blk: { |
| 4830 | 4797 | const err = errno(rc); |
| 4831 | 4798 | if (err == .SUCCESS) return @as([*]align(mem.page_size) u8, @ptrFromInt(rc))[0..length]; |
| ... | ... | @@ -5250,7 +5217,7 @@ pub fn lseek_SET(fd: fd_t, offset: u64) SeekError!void { |
| 5250 | 5217 | } |
| 5251 | 5218 | if (builtin.os.tag == .wasi and !builtin.link_libc) { |
| 5252 | 5219 | var new_offset: wasi.filesize_t = undefined; |
| 5253 | | switch (wasi.fd_seek(fd, @as(wasi.filedelta_t, @bitCast(offset)), .SET, &new_offset)) { |
| 5220 | switch (wasi.fd_seek(fd, @bitCast(offset), .SET, &new_offset)) { |
| 5254 | 5221 | .SUCCESS => return, |
| 5255 | 5222 | .BADF => unreachable, // always a race condition |
| 5256 | 5223 | .INVAL => return error.Unseekable, |
| ... | ... | @@ -5263,9 +5230,7 @@ pub fn lseek_SET(fd: fd_t, offset: u64) SeekError!void { |
| 5263 | 5230 | } |
| 5264 | 5231 | |
| 5265 | 5232 | const lseek_sym = if (lfs64_abi) system.lseek64 else system.lseek; |
| 5266 | | |
| 5267 | | const ioffset = @as(i64, @bitCast(offset)); // the OS treats this as unsigned |
| 5268 | | switch (errno(lseek_sym(fd, ioffset, SEEK.SET))) { |
| 5233 | switch (errno(lseek_sym(fd, @bitCast(offset), SEEK.SET))) { |
| 5269 | 5234 | .SUCCESS => return, |
| 5270 | 5235 | .BADF => unreachable, // always a race condition |
| 5271 | 5236 | .INVAL => return error.Unseekable, |
| ... | ... | @@ -5280,7 +5245,7 @@ pub fn lseek_SET(fd: fd_t, offset: u64) SeekError!void { |
| 5280 | 5245 | pub fn lseek_CUR(fd: fd_t, offset: i64) SeekError!void { |
| 5281 | 5246 | if (builtin.os.tag == .linux and !builtin.link_libc and @sizeOf(usize) == 4) { |
| 5282 | 5247 | var result: u64 = undefined; |
| 5283 | | switch (errno(system.llseek(fd, @as(u64, @bitCast(offset)), &result, SEEK.CUR))) { |
| 5248 | switch (errno(system.llseek(fd, @bitCast(offset), &result, SEEK.CUR))) { |
| 5284 | 5249 | .SUCCESS => return, |
| 5285 | 5250 | .BADF => unreachable, // always a race condition |
| 5286 | 5251 | .INVAL => return error.Unseekable, |
| ... | ... | @@ -5307,9 +5272,7 @@ pub fn lseek_CUR(fd: fd_t, offset: i64) SeekError!void { |
| 5307 | 5272 | } |
| 5308 | 5273 | } |
| 5309 | 5274 | const lseek_sym = if (lfs64_abi) system.lseek64 else system.lseek; |
| 5310 | | |
| 5311 | | const ioffset = @as(i64, @bitCast(offset)); // the OS treats this as unsigned |
| 5312 | | switch (errno(lseek_sym(fd, ioffset, SEEK.CUR))) { |
| 5275 | switch (errno(lseek_sym(fd, @bitCast(offset), SEEK.CUR))) { |
| 5313 | 5276 | .SUCCESS => return, |
| 5314 | 5277 | .BADF => unreachable, // always a race condition |
| 5315 | 5278 | .INVAL => return error.Unseekable, |
| ... | ... | @@ -5324,7 +5287,7 @@ pub fn lseek_CUR(fd: fd_t, offset: i64) SeekError!void { |
| 5324 | 5287 | pub fn lseek_END(fd: fd_t, offset: i64) SeekError!void { |
| 5325 | 5288 | if (builtin.os.tag == .linux and !builtin.link_libc and @sizeOf(usize) == 4) { |
| 5326 | 5289 | var result: u64 = undefined; |
| 5327 | | switch (errno(system.llseek(fd, @as(u64, @bitCast(offset)), &result, SEEK.END))) { |
| 5290 | switch (errno(system.llseek(fd, @bitCast(offset), &result, SEEK.END))) { |
| 5328 | 5291 | .SUCCESS => return, |
| 5329 | 5292 | .BADF => unreachable, // always a race condition |
| 5330 | 5293 | .INVAL => return error.Unseekable, |
| ... | ... | @@ -5351,9 +5314,7 @@ pub fn lseek_END(fd: fd_t, offset: i64) SeekError!void { |
| 5351 | 5314 | } |
| 5352 | 5315 | } |
| 5353 | 5316 | const lseek_sym = if (lfs64_abi) system.lseek64 else system.lseek; |
| 5354 | | |
| 5355 | | const ioffset = @as(i64, @bitCast(offset)); // the OS treats this as unsigned |
| 5356 | | switch (errno(lseek_sym(fd, ioffset, SEEK.END))) { |
| 5317 | switch (errno(lseek_sym(fd, @bitCast(offset), SEEK.END))) { |
| 5357 | 5318 | .SUCCESS => return, |
| 5358 | 5319 | .BADF => unreachable, // always a race condition |
| 5359 | 5320 | .INVAL => return error.Unseekable, |
| ... | ... | @@ -5395,10 +5356,9 @@ pub fn lseek_CUR_get(fd: fd_t) SeekError!u64 { |
| 5395 | 5356 | } |
| 5396 | 5357 | } |
| 5397 | 5358 | const lseek_sym = if (lfs64_abi) system.lseek64 else system.lseek; |
| 5398 | | |
| 5399 | 5359 | const rc = lseek_sym(fd, 0, SEEK.CUR); |
| 5400 | 5360 | switch (errno(rc)) { |
| 5401 | | .SUCCESS => return @as(u64, @bitCast(rc)), |
| 5361 | .SUCCESS => return @bitCast(rc), |
| 5402 | 5362 | .BADF => unreachable, // always a race condition |
| 5403 | 5363 | .INVAL => return error.Unseekable, |
| 5404 | 5364 | .OVERFLOW => return error.Unseekable, |
| ... | ... | @@ -5421,7 +5381,7 @@ pub fn fcntl(fd: fd_t, cmd: i32, arg: usize) FcntlError!usize { |
| 5421 | 5381 | while (true) { |
| 5422 | 5382 | const rc = system.fcntl(fd, cmd, arg); |
| 5423 | 5383 | switch (errno(rc)) { |
| 5424 | | .SUCCESS => return @as(usize, @intCast(rc)), |
| 5384 | .SUCCESS => return @intCast(rc), |
| 5425 | 5385 | .INTR => continue, |
| 5426 | 5386 | .AGAIN, .ACCES => return error.Locked, |
| 5427 | 5387 | .BADF => unreachable, |
| ... | ... | @@ -5714,7 +5674,7 @@ pub fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 { |
| 5714 | 5674 | // errno values to expect when command is F.GETPATH... |
| 5715 | 5675 | else => |err| return unexpectedErrno(err), |
| 5716 | 5676 | } |
| 5717 | | const len = mem.indexOfScalar(u8, out_buffer[0..], @as(u8, 0)) orelse MAX_PATH_BYTES; |
| 5677 | const len = mem.indexOfScalar(u8, out_buffer[0..], 0) orelse MAX_PATH_BYTES; |
| 5718 | 5678 | return out_buffer[0..len]; |
| 5719 | 5679 | }, |
| 5720 | 5680 | .linux => { |
| ... | ... | @@ -5746,7 +5706,7 @@ pub fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 { |
| 5746 | 5706 | return target; |
| 5747 | 5707 | }, |
| 5748 | 5708 | .freebsd => { |
| 5749 | | if (comptime builtin.os.version_range.semver.max.order(.{ .major = 13, .minor = 0, .patch = 0 }) == .gt) { |
| 5709 | if (comptime builtin.os.isAtLeast(.freebsd, .{ .major = 13, .minor = 0, .patch = 0 }) orelse false) { |
| 5750 | 5710 | var kfile: system.kinfo_file = undefined; |
| 5751 | 5711 | kfile.structsize = system.KINFO_FILE_SIZE; |
| 5752 | 5712 | switch (errno(system.fcntl(fd, system.F.KINFO, @intFromPtr(&kfile)))) { |
| ... | ... | @@ -5785,7 +5745,7 @@ pub fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 { |
| 5785 | 5745 | }; |
| 5786 | 5746 | var i: usize = 0; |
| 5787 | 5747 | while (i < len) { |
| 5788 | | const kf: *align(1) system.kinfo_file = @as(*align(1) system.kinfo_file, @ptrCast(&buf[i])); |
| 5748 | const kf: *align(1) system.kinfo_file = @ptrCast(&buf[i]); |
| 5789 | 5749 | if (kf.fd == fd) { |
| 5790 | 5750 | len = mem.indexOfScalar(u8, &kf.path, 0) orelse MAX_PATH_BYTES; |
| 5791 | 5751 | if (len == 0) return error.NameTooLong; |
| ... | ... | @@ -5793,7 +5753,7 @@ pub fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 { |
| 5793 | 5753 | @memcpy(result, kf.path[0..len]); |
| 5794 | 5754 | return result; |
| 5795 | 5755 | } |
| 5796 | | i += @as(usize, @intCast(kf.structsize)); |
| 5756 | i += @intCast(kf.structsize); |
| 5797 | 5757 | } |
| 5798 | 5758 | return error.FileNotFound; |
| 5799 | 5759 | } |
| ... | ... | @@ -5806,7 +5766,7 @@ pub fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 { |
| 5806 | 5766 | .RANGE => return error.NameTooLong, |
| 5807 | 5767 | else => |err| return unexpectedErrno(err), |
| 5808 | 5768 | } |
| 5809 | | const len = mem.indexOfScalar(u8, out_buffer[0..], @as(u8, 0)) orelse MAX_PATH_BYTES; |
| 5769 | const len = mem.indexOfScalar(u8, out_buffer[0..], 0) orelse MAX_PATH_BYTES; |
| 5810 | 5770 | return out_buffer[0..len]; |
| 5811 | 5771 | }, |
| 5812 | 5772 | .netbsd => { |
| ... | ... | @@ -5820,7 +5780,7 @@ pub fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 { |
| 5820 | 5780 | .RANGE => return error.NameTooLong, |
| 5821 | 5781 | else => |err| return unexpectedErrno(err), |
| 5822 | 5782 | } |
| 5823 | | const len = mem.indexOfScalar(u8, out_buffer[0..], @as(u8, 0)) orelse MAX_PATH_BYTES; |
| 5783 | const len = mem.indexOfScalar(u8, out_buffer[0..], 0) orelse MAX_PATH_BYTES; |
| 5824 | 5784 | return out_buffer[0..len]; |
| 5825 | 5785 | }, |
| 5826 | 5786 | else => unreachable, // made unreachable by isGetFdPathSupportedOnTarget above |
| ... | ... | @@ -5871,14 +5831,14 @@ pub fn dl_iterate_phdr( |
| 5871 | 5831 | callback(info, size, context_ptr.*) catch |err| return @intFromError(err); |
| 5872 | 5832 | return 0; |
| 5873 | 5833 | } |
| 5874 | | }.callbackC, @as(?*anyopaque, @ptrFromInt(@intFromPtr(&context))))) { |
| 5834 | }.callbackC, @ptrCast(@constCast(&context)))) { |
| 5875 | 5835 | 0 => return, |
| 5876 | 5836 | else => |err| return @as(Error, @errorCast(@errorFromInt(@as(std.meta.Int(.unsigned, @bitSizeOf(anyerror)), @intCast(err))))), |
| 5877 | 5837 | } |
| 5878 | 5838 | } |
| 5879 | 5839 | |
| 5880 | 5840 | const elf_base = std.process.getBaseAddress(); |
| 5881 | | const ehdr = @as(*elf.Ehdr, @ptrFromInt(elf_base)); |
| 5841 | const ehdr: *elf.Ehdr = @ptrFromInt(elf_base); |
| 5882 | 5842 | // Make sure the base address points to an ELF image. |
| 5883 | 5843 | assert(mem.eql(u8, ehdr.e_ident[0..4], elf.MAGIC)); |
| 5884 | 5844 | const n_phdr = ehdr.e_phnum; |
| ... | ... | @@ -5916,12 +5876,12 @@ pub fn dl_iterate_phdr( |
| 5916 | 5876 | var dlpi_phnum: u16 = undefined; |
| 5917 | 5877 | |
| 5918 | 5878 | if (entry.l_addr != 0) { |
| 5919 | | const elf_header = @as(*elf.Ehdr, @ptrFromInt(entry.l_addr)); |
| 5920 | | dlpi_phdr = @as([*]elf.Phdr, @ptrFromInt(entry.l_addr + elf_header.e_phoff)); |
| 5879 | const elf_header: *elf.Ehdr = @ptrFromInt(entry.l_addr); |
| 5880 | dlpi_phdr = @ptrFromInt(entry.l_addr + elf_header.e_phoff); |
| 5921 | 5881 | dlpi_phnum = elf_header.e_phnum; |
| 5922 | 5882 | } else { |
| 5923 | 5883 | // This is the running ELF image |
| 5924 | | dlpi_phdr = @as([*]elf.Phdr, @ptrFromInt(elf_base + ehdr.e_phoff)); |
| 5884 | dlpi_phdr = @ptrFromInt(elf_base + ehdr.e_phoff); |
| 5925 | 5885 | dlpi_phnum = ehdr.e_phnum; |
| 5926 | 5886 | } |
| 5927 | 5887 | |
| ... | ... | @@ -5943,11 +5903,11 @@ pub const ClockGetTimeError = error{UnsupportedClock} || UnexpectedError; |
| 5943 | 5903 | pub fn clock_gettime(clk_id: i32, tp: *timespec) ClockGetTimeError!void { |
| 5944 | 5904 | if (builtin.os.tag == .wasi and !builtin.link_libc) { |
| 5945 | 5905 | var ts: timestamp_t = undefined; |
| 5946 | | switch (system.clock_time_get(@as(u32, @bitCast(clk_id)), 1, &ts)) { |
| 5906 | switch (system.clock_time_get(@bitCast(clk_id), 1, &ts)) { |
| 5947 | 5907 | .SUCCESS => { |
| 5948 | 5908 | tp.* = .{ |
| 5949 | | .tv_sec = @as(i64, @intCast(ts / std.time.ns_per_s)), |
| 5950 | | .tv_nsec = @as(isize, @intCast(ts % std.time.ns_per_s)), |
| 5909 | .tv_sec = @intCast(ts / std.time.ns_per_s), |
| 5910 | .tv_nsec = @intCast(ts % std.time.ns_per_s), |
| 5951 | 5911 | }; |
| 5952 | 5912 | }, |
| 5953 | 5913 | .INVAL => return error.UnsupportedClock, |
| ... | ... | @@ -5984,10 +5944,10 @@ pub fn clock_gettime(clk_id: i32, tp: *timespec) ClockGetTimeError!void { |
| 5984 | 5944 | pub fn clock_getres(clk_id: i32, res: *timespec) ClockGetTimeError!void { |
| 5985 | 5945 | if (builtin.os.tag == .wasi and !builtin.link_libc) { |
| 5986 | 5946 | var ts: timestamp_t = undefined; |
| 5987 | | switch (system.clock_res_get(@as(u32, @bitCast(clk_id)), &ts)) { |
| 5947 | switch (system.clock_res_get(@bitCast(clk_id), &ts)) { |
| 5988 | 5948 | .SUCCESS => res.* = .{ |
| 5989 | | .tv_sec = @as(i64, @intCast(ts / std.time.ns_per_s)), |
| 5990 | | .tv_nsec = @as(isize, @intCast(ts % std.time.ns_per_s)), |
| 5949 | .tv_sec = @intCast(ts / std.time.ns_per_s), |
| 5950 | .tv_nsec = @intCast(ts % std.time.ns_per_s), |
| 5991 | 5951 | }, |
| 5992 | 5952 | .INVAL => return error.UnsupportedClock, |
| 5993 | 5953 | else => |err| return unexpectedErrno(err), |
| ... | ... | @@ -6212,7 +6172,7 @@ pub fn res_mkquery( |
| 6212 | 6172 | // TODO determine the circumstances for this and whether or |
| 6213 | 6173 | // not this should be an error. |
| 6214 | 6174 | if (j - i - 1 > 62) unreachable; |
| 6215 | | q[i - 1] = @as(u8, @intCast(j - i)); |
| 6175 | q[i - 1] = @intCast(j - i); |
| 6216 | 6176 | } |
| 6217 | 6177 | q[i + 1] = ty; |
| 6218 | 6178 | q[i + 3] = class; |
| ... | ... | @@ -6221,10 +6181,10 @@ pub fn res_mkquery( |
| 6221 | 6181 | var ts: timespec = undefined; |
| 6222 | 6182 | clock_gettime(CLOCK.REALTIME, &ts) catch {}; |
| 6223 | 6183 | const UInt = std.meta.Int(.unsigned, @bitSizeOf(@TypeOf(ts.tv_nsec))); |
| 6224 | | const unsec = @as(UInt, @bitCast(ts.tv_nsec)); |
| 6225 | | const id = @as(u32, @truncate(unsec + unsec / 65536)); |
| 6226 | | q[0] = @as(u8, @truncate(id / 256)); |
| 6227 | | q[1] = @as(u8, @truncate(id)); |
| 6184 | const unsec: UInt = @bitCast(ts.tv_nsec); |
| 6185 | const id: u32 = @truncate(unsec + unsec / 65536); |
| 6186 | q[0] = @truncate(id / 256); |
| 6187 | q[1] = @truncate(id); |
| 6228 | 6188 | |
| 6229 | 6189 | @memcpy(buf[0..n], q[0..n]); |
| 6230 | 6190 | return n; |
| ... | ... | @@ -6330,11 +6290,11 @@ pub fn sendmsg( |
| 6330 | 6290 | else => |err| return windows.unexpectedWSAError(err), |
| 6331 | 6291 | } |
| 6332 | 6292 | } else { |
| 6333 | | return @as(usize, @intCast(rc)); |
| 6293 | return @intCast(rc); |
| 6334 | 6294 | } |
| 6335 | 6295 | } else { |
| 6336 | 6296 | switch (errno(rc)) { |
| 6337 | | .SUCCESS => return @as(usize, @intCast(rc)), |
| 6297 | .SUCCESS => return @intCast(rc), |
| 6338 | 6298 | |
| 6339 | 6299 | .ACCES => return error.AccessDenied, |
| 6340 | 6300 | .AGAIN => return error.WouldBlock, |
| ... | ... | @@ -6430,13 +6390,13 @@ pub fn sendto( |
| 6430 | 6390 | .WSANOTINITIALISED => unreachable, // A successful WSAStartup call must occur before using this function. |
| 6431 | 6391 | else => |err| return windows.unexpectedWSAError(err), |
| 6432 | 6392 | }, |
| 6433 | | else => |rc| return @as(usize, @intCast(rc)), |
| 6393 | else => |rc| return @intCast(rc), |
| 6434 | 6394 | } |
| 6435 | 6395 | } |
| 6436 | 6396 | while (true) { |
| 6437 | 6397 | const rc = system.sendto(sockfd, buf.ptr, buf.len, flags, dest_addr, addrlen); |
| 6438 | 6398 | switch (errno(rc)) { |
| 6439 | | .SUCCESS => return @as(usize, @intCast(rc)), |
| 6399 | .SUCCESS => return @intCast(rc), |
| 6440 | 6400 | |
| 6441 | 6401 | .ACCES => return error.AccessDenied, |
| 6442 | 6402 | .AGAIN => return error.WouldBlock, |
| ... | ... | @@ -6588,18 +6548,15 @@ pub fn sendfile( |
| 6588 | 6548 | } |
| 6589 | 6549 | |
| 6590 | 6550 | // Here we match BSD behavior, making a zero count value send as many bytes as possible. |
| 6591 | | const adjusted_count_tmp = if (in_len == 0) max_count else @min(in_len, @as(size_t, max_count)); |
| 6592 | | // TODO we should not need this cast; improve return type of @min |
| 6593 | | const adjusted_count = @as(usize, @intCast(adjusted_count_tmp)); |
| 6551 | const adjusted_count = if (in_len == 0) max_count else @min(in_len, max_count); |
| 6594 | 6552 | |
| 6595 | 6553 | const sendfile_sym = if (lfs64_abi) system.sendfile64 else system.sendfile; |
| 6596 | | |
| 6597 | 6554 | while (true) { |
| 6598 | | var offset: off_t = @as(off_t, @bitCast(in_offset)); |
| 6555 | var offset: off_t = @bitCast(in_offset); |
| 6599 | 6556 | const rc = sendfile_sym(out_fd, in_fd, &offset, adjusted_count); |
| 6600 | 6557 | switch (errno(rc)) { |
| 6601 | 6558 | .SUCCESS => { |
| 6602 | | const amt = @as(usize, @bitCast(rc)); |
| 6559 | const amt: usize = @bitCast(rc); |
| 6603 | 6560 | total_written += amt; |
| 6604 | 6561 | if (in_len == 0 and amt == 0) { |
| 6605 | 6562 | // We have detected EOF from `in_fd`. |
| ... | ... | @@ -6665,13 +6622,10 @@ pub fn sendfile( |
| 6665 | 6622 | hdtr = &hdtr_data; |
| 6666 | 6623 | } |
| 6667 | 6624 | |
| 6668 | | const adjusted_count = @min(in_len, max_count); |
| 6669 | | |
| 6670 | 6625 | while (true) { |
| 6671 | 6626 | var sbytes: off_t = undefined; |
| 6672 | | const offset = @as(off_t, @bitCast(in_offset)); |
| 6673 | | const err = errno(system.sendfile(in_fd, out_fd, offset, adjusted_count, hdtr, &sbytes, flags)); |
| 6674 | | const amt = @as(usize, @bitCast(sbytes)); |
| 6627 | const err = errno(system.sendfile(in_fd, out_fd, @bitCast(in_offset), @min(in_len, max_count), hdtr, &sbytes, flags)); |
| 6628 | const amt: usize = @bitCast(sbytes); |
| 6675 | 6629 | switch (err) { |
| 6676 | 6630 | .SUCCESS => return amt, |
| 6677 | 6631 | |
| ... | ... | @@ -6738,15 +6692,10 @@ pub fn sendfile( |
| 6738 | 6692 | hdtr = &hdtr_data; |
| 6739 | 6693 | } |
| 6740 | 6694 | |
| 6741 | | const adjusted_count_temporary = @min(in_len, @as(u63, max_count)); |
| 6742 | | // TODO we should not need this int cast; improve the return type of `@min` |
| 6743 | | const adjusted_count = @as(u63, @intCast(adjusted_count_temporary)); |
| 6744 | | |
| 6745 | 6695 | while (true) { |
| 6746 | | var sbytes: off_t = adjusted_count; |
| 6747 | | const signed_offset = @as(i64, @bitCast(in_offset)); |
| 6748 | | const err = errno(system.sendfile(in_fd, out_fd, signed_offset, &sbytes, hdtr, flags)); |
| 6749 | | const amt = @as(usize, @bitCast(sbytes)); |
| 6696 | var sbytes: off_t = @min(in_len, max_count); |
| 6697 | const err = errno(system.sendfile(in_fd, out_fd, @bitCast(in_offset), &sbytes, hdtr, flags)); |
| 6698 | const amt: usize = @bitCast(sbytes); |
| 6750 | 6699 | switch (err) { |
| 6751 | 6700 | .SUCCESS => return amt, |
| 6752 | 6701 | |
| ... | ... | @@ -6791,9 +6740,7 @@ pub fn sendfile( |
| 6791 | 6740 | rw: { |
| 6792 | 6741 | var buf: [8 * 4096]u8 = undefined; |
| 6793 | 6742 | // Here we match BSD behavior, making a zero count value send as many bytes as possible. |
| 6794 | | const adjusted_count_tmp = if (in_len == 0) buf.len else @min(buf.len, in_len); |
| 6795 | | // TODO we should not need this cast; improve return type of @min |
| 6796 | | const adjusted_count = @as(usize, @intCast(adjusted_count_tmp)); |
| 6743 | const adjusted_count = if (in_len == 0) buf.len else @min(buf.len, in_len); |
| 6797 | 6744 | const amt_read = try pread(in_fd, buf[0..adjusted_count], in_offset); |
| 6798 | 6745 | if (amt_read == 0) { |
| 6799 | 6746 | if (in_len == 0) { |
| ... | ... | @@ -6864,14 +6811,14 @@ pub fn copy_file_range(fd_in: fd_t, off_in: u64, fd_out: fd_t, off_out: u64, len |
| 6864 | 6811 | std.c.versionCheck(.{ .major = 2, .minor = 27, .patch = 0 })) and |
| 6865 | 6812 | has_copy_file_range_syscall.load(.Monotonic))) |
| 6866 | 6813 | { |
| 6867 | | var off_in_copy = @as(i64, @bitCast(off_in)); |
| 6868 | | var off_out_copy = @as(i64, @bitCast(off_out)); |
| 6814 | var off_in_copy: i64 = @bitCast(off_in); |
| 6815 | var off_out_copy: i64 = @bitCast(off_out); |
| 6869 | 6816 | |
| 6870 | 6817 | while (true) { |
| 6871 | 6818 | const rc = system.copy_file_range(fd_in, &off_in_copy, fd_out, &off_out_copy, len, flags); |
| 6872 | 6819 | if (builtin.os.tag == .freebsd) { |
| 6873 | 6820 | switch (system.getErrno(rc)) { |
| 6874 | | .SUCCESS => return @as(usize, @intCast(rc)), |
| 6821 | .SUCCESS => return @intCast(rc), |
| 6875 | 6822 | .BADF => return error.FilesOpenedWithWrongFlags, |
| 6876 | 6823 | .FBIG => return error.FileTooBig, |
| 6877 | 6824 | .IO => return error.InputOutput, |
| ... | ... | @@ -6884,7 +6831,7 @@ pub fn copy_file_range(fd_in: fd_t, off_in: u64, fd_out: fd_t, off_out: u64, len |
| 6884 | 6831 | } |
| 6885 | 6832 | } else { // assume linux |
| 6886 | 6833 | switch (system.getErrno(rc)) { |
| 6887 | | .SUCCESS => return @as(usize, @intCast(rc)), |
| 6834 | .SUCCESS => return @intCast(rc), |
| 6888 | 6835 | .BADF => return error.FilesOpenedWithWrongFlags, |
| 6889 | 6836 | .FBIG => return error.FileTooBig, |
| 6890 | 6837 | .IO => return error.InputOutput, |
| ... | ... | @@ -6907,11 +6854,8 @@ pub fn copy_file_range(fd_in: fd_t, off_in: u64, fd_out: fd_t, off_out: u64, len |
| 6907 | 6854 | } |
| 6908 | 6855 | |
| 6909 | 6856 | var buf: [8 * 4096]u8 = undefined; |
| 6910 | | const adjusted_count = @min(buf.len, len); |
| 6911 | | const amt_read = try pread(fd_in, buf[0..adjusted_count], off_in); |
| 6912 | | // TODO without @as the line below fails to compile for wasm32-wasi: |
| 6913 | | // error: integer value 0 cannot be coerced to type 'os.PWriteError!usize' |
| 6914 | | if (amt_read == 0) return @as(usize, 0); |
| 6857 | const amt_read = try pread(fd_in, buf[0..@min(buf.len, len)], off_in); |
| 6858 | if (amt_read == 0) return 0; |
| 6915 | 6859 | return pwrite(fd_out, buf[0..amt_read], off_out); |
| 6916 | 6860 | } |
| 6917 | 6861 | |
| ... | ... | @@ -6937,11 +6881,11 @@ pub fn poll(fds: []pollfd, timeout: i32) PollError!usize { |
| 6937 | 6881 | else => |err| return windows.unexpectedWSAError(err), |
| 6938 | 6882 | } |
| 6939 | 6883 | } else { |
| 6940 | | return @as(usize, @intCast(rc)); |
| 6884 | return @intCast(rc); |
| 6941 | 6885 | } |
| 6942 | 6886 | } else { |
| 6943 | 6887 | switch (errno(rc)) { |
| 6944 | | .SUCCESS => return @as(usize, @intCast(rc)), |
| 6888 | .SUCCESS => return @intCast(rc), |
| 6945 | 6889 | .FAULT => unreachable, |
| 6946 | 6890 | .INTR => continue, |
| 6947 | 6891 | .INVAL => unreachable, |
| ... | ... | @@ -6971,7 +6915,7 @@ pub fn ppoll(fds: []pollfd, timeout: ?*const timespec, mask: ?*const sigset_t) P |
| 6971 | 6915 | const fds_count = math.cast(nfds_t, fds.len) orelse return error.SystemResources; |
| 6972 | 6916 | const rc = system.ppoll(fds.ptr, fds_count, ts_ptr, mask); |
| 6973 | 6917 | switch (errno(rc)) { |
| 6974 | | .SUCCESS => return @as(usize, @intCast(rc)), |
| 6918 | .SUCCESS => return @intCast(rc), |
| 6975 | 6919 | .FAULT => unreachable, |
| 6976 | 6920 | .INTR => return error.SignalInterrupt, |
| 6977 | 6921 | .INVAL => unreachable, |
| ... | ... | @@ -7038,11 +6982,11 @@ pub fn recvfrom( |
| 7038 | 6982 | else => |err| return windows.unexpectedWSAError(err), |
| 7039 | 6983 | } |
| 7040 | 6984 | } else { |
| 7041 | | return @as(usize, @intCast(rc)); |
| 6985 | return @intCast(rc); |
| 7042 | 6986 | } |
| 7043 | 6987 | } else { |
| 7044 | 6988 | switch (errno(rc)) { |
| 7045 | | .SUCCESS => return @as(usize, @intCast(rc)), |
| 6989 | .SUCCESS => return @intCast(rc), |
| 7046 | 6990 | .BADF => unreachable, // always a race condition |
| 7047 | 6991 | .FAULT => unreachable, |
| 7048 | 6992 | .INVAL => unreachable, |
| ... | ... | @@ -7081,7 +7025,7 @@ pub fn dn_expand( |
| 7081 | 7025 | // loop invariants: p<end, dest<dend |
| 7082 | 7026 | if ((p[0] & 0xc0) != 0) { |
| 7083 | 7027 | if (p + 1 == end) return error.InvalidDnsPacket; |
| 7084 | | const j = ((p[0] & @as(usize, 0x3f)) << 8) | p[1]; |
| 7028 | const j = @as(usize, p[0] & 0x3f) << 8 | p[1]; |
| 7085 | 7029 | if (len == std.math.maxInt(usize)) len = @intFromPtr(p) + 2 - @intFromPtr(comp_dn.ptr); |
| 7086 | 7030 | if (j >= msg.len) return error.InvalidDnsPacket; |
| 7087 | 7031 | p = msg.ptr + j; |
| ... | ... | @@ -7135,7 +7079,7 @@ pub const SetSockOptError = error{ |
| 7135 | 7079 | /// Set a socket's options. |
| 7136 | 7080 | pub fn setsockopt(fd: socket_t, level: u32, optname: u32, opt: []const u8) SetSockOptError!void { |
| 7137 | 7081 | if (builtin.os.tag == .windows) { |
| 7138 | | const rc = windows.ws2_32.setsockopt(fd, @as(i32, @intCast(level)), @as(i32, @intCast(optname)), opt.ptr, @as(i32, @intCast(opt.len))); |
| 7082 | const rc = windows.ws2_32.setsockopt(fd, @intCast(level), @intCast(optname), opt.ptr, @intCast(opt.len)); |
| 7139 | 7083 | if (rc == windows.ws2_32.SOCKET_ERROR) { |
| 7140 | 7084 | switch (windows.ws2_32.WSAGetLastError()) { |
| 7141 | 7085 | .WSANOTINITIALISED => unreachable, |
| ... | ... | @@ -7148,7 +7092,7 @@ pub fn setsockopt(fd: socket_t, level: u32, optname: u32, opt: []const u8) SetSo |
| 7148 | 7092 | } |
| 7149 | 7093 | return; |
| 7150 | 7094 | } else { |
| 7151 | | switch (errno(system.setsockopt(fd, level, optname, opt.ptr, @as(socklen_t, @intCast(opt.len))))) { |
| 7095 | switch (errno(system.setsockopt(fd, level, optname, opt.ptr, @intCast(opt.len)))) { |
| 7152 | 7096 | .SUCCESS => {}, |
| 7153 | 7097 | .BADF => unreachable, // always a race condition |
| 7154 | 7098 | .NOTSOCK => unreachable, // always a race condition |
| ... | ... | @@ -7185,7 +7129,7 @@ pub fn memfd_createZ(name: [*:0]const u8, flags: u32) MemFdCreateError!fd_t { |
| 7185 | 7129 | const getErrno = if (use_c) std.c.getErrno else linux.getErrno; |
| 7186 | 7130 | const rc = sys.memfd_create(name, flags); |
| 7187 | 7131 | switch (getErrno(rc)) { |
| 7188 | | .SUCCESS => return @as(fd_t, @intCast(rc)), |
| 7132 | .SUCCESS => return @intCast(rc), |
| 7189 | 7133 | .FAULT => unreachable, // name has invalid memory |
| 7190 | 7134 | .INVAL => unreachable, // name/flags are faulty |
| 7191 | 7135 | .NFILE => return error.SystemFdQuotaExceeded, |
| ... | ... | @@ -7335,7 +7279,7 @@ pub fn ioctl_SIOCGIFINDEX(fd: fd_t, ifr: *ifreq) IoCtl_SIOCGIFINDEX_Error!void { |
| 7335 | 7279 | pub fn signalfd(fd: fd_t, mask: *const sigset_t, flags: u32) !fd_t { |
| 7336 | 7280 | const rc = system.signalfd(fd, mask, flags); |
| 7337 | 7281 | switch (errno(rc)) { |
| 7338 | | .SUCCESS => return @as(fd_t, @intCast(rc)), |
| 7282 | .SUCCESS => return @intCast(rc), |
| 7339 | 7283 | .BADF, .INVAL => unreachable, |
| 7340 | 7284 | .NFILE => return error.SystemFdQuotaExceeded, |
| 7341 | 7285 | .NOMEM => return error.SystemResources, |
| ... | ... | @@ -7443,7 +7387,7 @@ pub fn prctl(option: PR, args: anytype) PrctlError!u31 { |
| 7443 | 7387 | |
| 7444 | 7388 | const rc = system.prctl(@intFromEnum(option), buf[0], buf[1], buf[2], buf[3]); |
| 7445 | 7389 | switch (errno(rc)) { |
| 7446 | | .SUCCESS => return @as(u31, @intCast(rc)), |
| 7390 | .SUCCESS => return @intCast(rc), |
| 7447 | 7391 | .ACCES => return error.AccessDenied, |
| 7448 | 7392 | .BADF => return error.InvalidFileDescriptor, |
| 7449 | 7393 | .FAULT => return error.InvalidAddress, |
| ... | ... | @@ -7624,7 +7568,7 @@ pub fn perf_event_open( |
| 7624 | 7568 | ) PerfEventOpenError!fd_t { |
| 7625 | 7569 | const rc = linux.perf_event_open(attr, pid, cpu, group_fd, flags); |
| 7626 | 7570 | switch (errno(rc)) { |
| 7627 | | .SUCCESS => return @as(fd_t, @intCast(rc)), |
| 7571 | .SUCCESS => return @intCast(rc), |
| 7628 | 7572 | .@"2BIG" => return error.TooBig, |
| 7629 | 7573 | .ACCES => return error.PermissionDenied, |
| 7630 | 7574 | .BADF => unreachable, // group_fd file descriptor is not valid. |
| ... | ... | @@ -7659,7 +7603,7 @@ pub const TimerFdSetError = TimerFdGetError || error{Canceled}; |
| 7659 | 7603 | pub fn timerfd_create(clokid: i32, flags: linux.TFD) TimerFdCreateError!fd_t { |
| 7660 | 7604 | const rc = linux.timerfd_create(clokid, flags); |
| 7661 | 7605 | return switch (errno(rc)) { |
| 7662 | | .SUCCESS => @as(fd_t, @intCast(rc)), |
| 7606 | .SUCCESS => @intCast(rc), |
| 7663 | 7607 | .INVAL => unreachable, |
| 7664 | 7608 | .MFILE => return error.ProcessFdQuotaExceeded, |
| 7665 | 7609 | .NFILE => return error.SystemFdQuotaExceeded, |
| ... | ... | @@ -7702,7 +7646,6 @@ pub fn timerfd_gettime(fd: i32) TimerFdGetError!linux.itimerspec { |
| 7702 | 7646 | pub const PtraceError = error{ |
| 7703 | 7647 | DeviceBusy, |
| 7704 | 7648 | InputOutput, |
| 7705 | | Overflow, |
| 7706 | 7649 | ProcessNotFound, |
| 7707 | 7650 | PermissionDenied, |
| 7708 | 7651 | } || UnexpectedError; |
| ... | ... | @@ -7724,10 +7667,10 @@ pub fn ptrace(request: u32, pid: pid_t, addr: usize, signal: usize) PtraceError! |
| 7724 | 7667 | }, |
| 7725 | 7668 | |
| 7726 | 7669 | .macos, .ios, .tvos, .watchos => switch (errno(darwin.ptrace( |
| 7727 | | math.cast(i32, request) orelse return error.Overflow, |
| 7670 | @intCast(request), |
| 7728 | 7671 | pid, |
| 7729 | | @as(?[*]u8, @ptrFromInt(addr)), |
| 7730 | | math.cast(i32, signal) orelse return error.Overflow, |
| 7672 | @ptrFromInt(addr), |
| 7673 | @intCast(signal), |
| 7731 | 7674 | ))) { |
| 7732 | 7675 | .SUCCESS => {}, |
| 7733 | 7676 | .SRCH => error.ProcessNotFound, |