From 0f248e0988b24bf4fcdaadd0e47127462206711e Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Sat, 3 Oct 2020 12:31:17 +0200 Subject: std: Make file copy ops use zero-copy mechanisms Use copy_file_range on Linux (if available), fcopyfile on Darwin, sendfile on *BSDs (and on Linux kernels without copy_file_range). --- lib/std/os.zig | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 91 insertions(+), 10 deletions(-) (limited to 'lib/std/os.zig') diff --git a/lib/std/os.zig b/lib/std/os.zig index c06ce4ed00..c8d27eaa85 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -4981,19 +4981,15 @@ pub const CopyFileRangeError = error{ pub fn copy_file_range(fd_in: fd_t, off_in: u64, fd_out: fd_t, off_out: u64, len: usize, flags: u32) CopyFileRangeError!usize { const use_c = std.c.versionCheck(.{ .major = 2, .minor = 27, .patch = 0 }).ok; - // TODO support for other systems than linux - const try_syscall = comptime std.Target.current.os.isAtLeast(.linux, .{ .major = 4, .minor = 5 }) != false; - - if (use_c or try_syscall) { + if (std.Target.current.os.tag == .linux and + (use_c or has_copy_file_range_syscall.get() != 0)) + { const sys = if (use_c) std.c else linux; var off_in_copy = @bitCast(i64, off_in); var off_out_copy = @bitCast(i64, off_out); const rc = sys.copy_file_range(fd_in, &off_in_copy, fd_out, &off_out_copy, len, flags); - - // TODO avoid wasting a syscall every time if kernel is too old and returns ENOSYS https://github.com/ziglang/zig/issues/1018 - switch (sys.getErrno(rc)) { 0 => return @intCast(usize, rc), EBADF => unreachable, @@ -5005,9 +5001,14 @@ pub fn copy_file_range(fd_in: fd_t, off_in: u64, fd_out: fd_t, off_out: u64, len EOVERFLOW => return error.Unseekable, EPERM => return error.PermissionDenied, ETXTBSY => return error.FileBusy, - EINVAL => {}, // these may not be regular files, try fallback - EXDEV => {}, // support for cross-filesystem copy added in Linux 5.3, use fallback - ENOSYS => {}, // syscall added in Linux 4.5, use fallback + // these may not be regular files, try fallback + EINVAL => {}, + // support for cross-filesystem copy added in Linux 5.3, use fallback + EXDEV => {}, + // syscall added in Linux 4.5, use fallback + ENOSYS => { + has_copy_file_range_syscall.set(0); + }, else => |err| return unexpectedErrno(err), } } @@ -5021,6 +5022,86 @@ pub fn copy_file_range(fd_in: fd_t, off_in: u64, fd_out: fd_t, off_out: u64, len return pwrite(fd_out, buf[0..amt_read], off_out); } +var has_copy_file_range_syscall = std.atomic.Int(u1).init(1); + +pub const CopyFileOptions = struct { + /// Size in bytes of the source files, if available saves a call to stat(). + file_size: ?u64 = null, +}; + +pub const CopyFileError = error{ + SystemResources, + FileTooBig, + InputOutput, + IsDir, + OutOfMemory, + NoSpaceLeft, + Unseekable, + PermissionDenied, + FileBusy, +} || FStatError || SendFileError; + +/// Transfer all the data between two file descriptors in the most efficient way. +/// No metadata is transferred over. +pub fn copy_file(fd_in: fd_t, fd_out: fd_t, options: CopyFileOptions) CopyFileError!void { + if (comptime std.Target.current.isDarwin()) { + const rc = system.fcopyfile(fd_in, fd_out, null, system.COPYFILE_DATA); + switch (errno(rc)) { + 0 => return, + EINVAL => unreachable, + ENOMEM => return error.SystemResources, + // The source file was not a directory, symbolic link, or regular file. + // Try with the fallback path before giving up. + ENOTSUP => {}, + else => |err| return unexpectedErrno(err), + } + } + + const src_file_size = options.file_size orelse + @bitCast(u64, (try fstat(fd_in)).size); + var remaining = src_file_size; + + if (std.Target.current.os.tag == .linux) { + // Try copy_file_range first as that works at the FS level and is the + // most efficient method (if available). + if (has_copy_file_range_syscall.get() != 0) { + cfr_loop: while (remaining > 0) { + const copy_amt = math.cast(usize, remaining) catch math.maxInt(usize); + const rc = linux.copy_file_range(fd_in, null, fd_out, null, copy_amt, 0); + switch (errno(rc)) { + 0 => {}, + EBADF => unreachable, + EFBIG => return error.FileTooBig, + EIO => return error.InputOutput, + EISDIR => return error.IsDir, + ENOMEM => return error.OutOfMemory, + ENOSPC => return error.NoSpaceLeft, + EOVERFLOW => return error.Unseekable, + EPERM => return error.PermissionDenied, + ETXTBSY => return error.FileBusy, + // these may not be regular files, try fallback + EINVAL => break :cfr_loop, + // support for cross-filesystem copy added in Linux 5.3, use fallback + EXDEV => break :cfr_loop, + // syscall added in Linux 4.5, use fallback + ENOSYS => { + has_copy_file_range_syscall.set(0); + break :cfr_loop; + }, + else => |err| return unexpectedErrno(err), + } + remaining -= rc; + } + return; + } + } + + // Sendfile is a zero-copy mechanism iff the OS supports it, otherwise the + // fallback code will copy the contents chunk by chunk. + const empty_iovec = [0]iovec_const{}; + _ = try sendfile(fd_out, fd_in, 0, remaining, &empty_iovec, &empty_iovec, 0); +} + pub const PollError = error{ /// The kernel had no space to allocate file descriptor tables. SystemResources, -- cgit v1.2.3 From 8b4f5f039df65980d0a0ec6add1caf4c9bf2468c Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Sat, 3 Oct 2020 19:51:22 +0200 Subject: Alternative strategy to avoid calling stat() This is an optimization as it avoids an extra syscall, but it's also a workaround for fstat being not available on Windows. --- lib/std/fs.zig | 2 +- lib/std/os.zig | 39 +++++++++++++++++++++------------------ 2 files changed, 22 insertions(+), 19 deletions(-) (limited to 'lib/std/os.zig') diff --git a/lib/std/fs.zig b/lib/std/fs.zig index 75f0709df6..c00976e01c 100644 --- a/lib/std/fs.zig +++ b/lib/std/fs.zig @@ -1823,7 +1823,7 @@ pub const Dir = struct { var atomic_file = try dest_dir.atomicFile(dest_path, .{ .mode = mode }); defer atomic_file.deinit(); - try os.copy_file(in_file.handle, atomic_file.file.handle, .{ .file_size = size }); + try os.copy_file(in_file.handle, atomic_file.file.handle, .{}); return atomic_file.finish(); } diff --git a/lib/std/os.zig b/lib/std/os.zig index c8d27eaa85..60845a3b87 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -5024,12 +5024,10 @@ pub fn copy_file_range(fd_in: fd_t, off_in: u64, fd_out: fd_t, off_out: u64, len var has_copy_file_range_syscall = std.atomic.Int(u1).init(1); -pub const CopyFileOptions = struct { - /// Size in bytes of the source files, if available saves a call to stat(). - file_size: ?u64 = null, -}; +pub const CopyFileOptions = struct {}; pub const CopyFileError = error{ + BadFileHandle, SystemResources, FileTooBig, InputOutput, @@ -5057,20 +5055,18 @@ pub fn copy_file(fd_in: fd_t, fd_out: fd_t, options: CopyFileOptions) CopyFileEr } } - const src_file_size = options.file_size orelse - @bitCast(u64, (try fstat(fd_in)).size); - var remaining = src_file_size; - if (std.Target.current.os.tag == .linux) { // Try copy_file_range first as that works at the FS level and is the // most efficient method (if available). if (has_copy_file_range_syscall.get() != 0) { - cfr_loop: while (remaining > 0) { - const copy_amt = math.cast(usize, remaining) catch math.maxInt(usize); - const rc = linux.copy_file_range(fd_in, null, fd_out, null, copy_amt, 0); + cfr_loop: while (true) { + // The kernel checks `file_pos+count` for overflow, use a 32 bit + // value so that the syscall won't return EINVAL except for + // impossibly large files. + const rc = linux.copy_file_range(fd_in, null, fd_out, null, math.maxInt(u32), 0); switch (errno(rc)) { 0 => {}, - EBADF => unreachable, + EBADF => return error.BadFileHandle, EFBIG => return error.FileTooBig, EIO => return error.InputOutput, EISDIR => return error.IsDir, @@ -5079,27 +5075,34 @@ pub fn copy_file(fd_in: fd_t, fd_out: fd_t, options: CopyFileOptions) CopyFileEr EOVERFLOW => return error.Unseekable, EPERM => return error.PermissionDenied, ETXTBSY => return error.FileBusy, - // these may not be regular files, try fallback + // These may not be regular files, try fallback EINVAL => break :cfr_loop, - // support for cross-filesystem copy added in Linux 5.3, use fallback + // Support for cross-filesystem copy added in Linux 5.3, use fallback EXDEV => break :cfr_loop, - // syscall added in Linux 4.5, use fallback + // Syscall added in Linux 4.5, use fallback ENOSYS => { has_copy_file_range_syscall.set(0); break :cfr_loop; }, else => |err| return unexpectedErrno(err), } - remaining -= rc; + // Terminate when no data was copied + if (rc == 0) return; } - return; + // This point is reached when an error occurred, hopefully no data + // was transferred yet } } // Sendfile is a zero-copy mechanism iff the OS supports it, otherwise the // fallback code will copy the contents chunk by chunk. const empty_iovec = [0]iovec_const{}; - _ = try sendfile(fd_out, fd_in, 0, remaining, &empty_iovec, &empty_iovec, 0); + var offset: u64 = 0; + sendfile_loop: while (true) { + const amt = try sendfile(fd_out, fd_in, offset, 0, &empty_iovec, &empty_iovec, 0); + if (amt == 0) break :sendfile_loop; + offset += amt; + } } pub const PollError = error{ -- cgit v1.2.3 From a419a1aabce63fedcc77a1118d596864fcff46e3 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Tue, 6 Oct 2020 09:38:59 +0200 Subject: Move copy_file to fs namespace Now it is a private API. Also handle short writes in copy_file_range fallback implementation. --- lib/std/fs.zig | 48 ++++++++++++++++++++++++- lib/std/os.zig | 110 ++++++++++++--------------------------------------------- 2 files changed, 69 insertions(+), 89 deletions(-) (limited to 'lib/std/os.zig') diff --git a/lib/std/fs.zig b/lib/std/fs.zig index c00976e01c..92f68590f9 100644 --- a/lib/std/fs.zig +++ b/lib/std/fs.zig @@ -1823,7 +1823,7 @@ pub const Dir = struct { var atomic_file = try dest_dir.atomicFile(dest_path, .{ .mode = mode }); defer atomic_file.deinit(); - try os.copy_file(in_file.handle, atomic_file.file.handle, .{}); + try copy_file(in_file.handle, atomic_file.file.handle); return atomic_file.finish(); } @@ -2263,6 +2263,52 @@ pub fn realpathAlloc(allocator: *Allocator, pathname: []const u8) ![]u8 { return allocator.dupe(u8, try os.realpath(pathname, &buf)); } +const CopyFileError = error{SystemResources} || os.CopyFileRangeError || os.SendFileError; + +/// Transfer all the data between two file descriptors in the most efficient way. +/// No metadata is transferred over. +fn copy_file(fd_in: os.fd_t, fd_out: os.fd_t) CopyFileError!void { + if (comptime std.Target.current.isDarwin()) { + const rc = os.system.fcopyfile(fd_in, fd_out, null, os.system.COPYFILE_DATA); + switch (errno(rc)) { + 0 => return, + EINVAL => unreachable, + ENOMEM => return error.SystemResources, + // The source file was not a directory, symbolic link, or regular file. + // Try with the fallback path before giving up. + ENOTSUP => {}, + else => |err| return unexpectedErrno(err), + } + } + + if (std.Target.current.os.tag == .linux) { + // Try copy_file_range first as that works at the FS level and is the + // most efficient method (if available). + var offset: u64 = 0; + cfr_loop: while (true) { + // The kernel checks `offset+count` for overflow, use a 32 bit + // value so that the syscall won't return EINVAL except for + // impossibly large files. + const amt = try os.copy_file_range(fd_in, offset, fd_out, offset, math.maxInt(u32), 0); + // Terminate when no data was copied + if (amt == 0) break :cfr_loop; + offset += amt; + } + return; + } + + // Sendfile is a zero-copy mechanism iff the OS supports it, otherwise the + // fallback code will copy the contents chunk by chunk. + const empty_iovec = [0]os.iovec_const{}; + var offset: u64 = 0; + sendfile_loop: while (true) { + const amt = try os.sendfile(fd_out, fd_in, offset, 0, &empty_iovec, &empty_iovec, 0); + // Terminate when no data was copied + if (amt == 0) break :sendfile_loop; + offset += amt; + } +} + test "" { if (builtin.os.tag != .wasi) { _ = makeDirAbsolute; diff --git a/lib/std/os.zig b/lib/std/os.zig index 60845a3b87..e4e0b1d4a1 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -4945,6 +4945,7 @@ pub fn sendfile( pub const CopyFileRangeError = error{ FileTooBig, InputOutput, + InvalidFileDescriptor, IsDir, OutOfMemory, NoSpaceLeft, @@ -4978,6 +4979,11 @@ pub const CopyFileRangeError = error{ /// Other systems fall back to calling `pread` / `pwrite`. /// /// Maximum offsets on Linux are `math.maxInt(i64)`. +var has_copy_file_range_syscall = init: { + const kernel_has_syscall = comptime std.Target.current.os.isAtLeast(.linux, .{ .major = 4, .minor = 5 }) orelse true; + break :init std.atomic.Int(u1).init(@boolToInt(kernel_has_syscall)); +}; + pub fn copy_file_range(fd_in: fd_t, off_in: u64, fd_out: fd_t, off_out: u64, len: usize, flags: u32) CopyFileRangeError!usize { const use_c = std.c.versionCheck(.{ .major = 2, .minor = 27, .patch = 0 }).ok; @@ -4992,7 +4998,7 @@ pub fn copy_file_range(fd_in: fd_t, off_in: u64, fd_out: fd_t, off_out: u64, len const rc = sys.copy_file_range(fd_in, &off_in_copy, fd_out, &off_out_copy, len, flags); switch (sys.getErrno(rc)) { 0 => return @intCast(usize, rc), - EBADF => unreachable, + EBADF => return error.InvalidFileDescriptor, EFBIG => return error.FileTooBig, EIO => return error.InputOutput, EISDIR => return error.IsDir, @@ -5013,96 +5019,24 @@ pub fn copy_file_range(fd_in: fd_t, off_in: u64, fd_out: fd_t, off_out: u64, len } } - var buf: [8 * 4096]u8 = undefined; - const adjusted_count = math.min(buf.len, len); - const amt_read = try pread(fd_in, buf[0..adjusted_count], off_in); - // TODO without @as the line below fails to compile for wasm32-wasi: - // error: integer value 0 cannot be coerced to type 'os.PWriteError!usize' - if (amt_read == 0) return @as(usize, 0); - return pwrite(fd_out, buf[0..amt_read], off_out); -} - -var has_copy_file_range_syscall = std.atomic.Int(u1).init(1); - -pub const CopyFileOptions = struct {}; - -pub const CopyFileError = error{ - BadFileHandle, - SystemResources, - FileTooBig, - InputOutput, - IsDir, - OutOfMemory, - NoSpaceLeft, - Unseekable, - PermissionDenied, - FileBusy, -} || FStatError || SendFileError; + var buf: [2 * 4096]u8 = undefined; -/// Transfer all the data between two file descriptors in the most efficient way. -/// No metadata is transferred over. -pub fn copy_file(fd_in: fd_t, fd_out: fd_t, options: CopyFileOptions) CopyFileError!void { - if (comptime std.Target.current.isDarwin()) { - const rc = system.fcopyfile(fd_in, fd_out, null, system.COPYFILE_DATA); - switch (errno(rc)) { - 0 => return, - EINVAL => unreachable, - ENOMEM => return error.SystemResources, - // The source file was not a directory, symbolic link, or regular file. - // Try with the fallback path before giving up. - ENOTSUP => {}, - else => |err| return unexpectedErrno(err), - } - } - - if (std.Target.current.os.tag == .linux) { - // Try copy_file_range first as that works at the FS level and is the - // most efficient method (if available). - if (has_copy_file_range_syscall.get() != 0) { - cfr_loop: while (true) { - // The kernel checks `file_pos+count` for overflow, use a 32 bit - // value so that the syscall won't return EINVAL except for - // impossibly large files. - const rc = linux.copy_file_range(fd_in, null, fd_out, null, math.maxInt(u32), 0); - switch (errno(rc)) { - 0 => {}, - EBADF => return error.BadFileHandle, - EFBIG => return error.FileTooBig, - EIO => return error.InputOutput, - EISDIR => return error.IsDir, - ENOMEM => return error.OutOfMemory, - ENOSPC => return error.NoSpaceLeft, - EOVERFLOW => return error.Unseekable, - EPERM => return error.PermissionDenied, - ETXTBSY => return error.FileBusy, - // These may not be regular files, try fallback - EINVAL => break :cfr_loop, - // Support for cross-filesystem copy added in Linux 5.3, use fallback - EXDEV => break :cfr_loop, - // Syscall added in Linux 4.5, use fallback - ENOSYS => { - has_copy_file_range_syscall.set(0); - break :cfr_loop; - }, - else => |err| return unexpectedErrno(err), - } - // Terminate when no data was copied - if (rc == 0) return; - } - // This point is reached when an error occurred, hopefully no data - // was transferred yet - } + var total_copied: usize = 0; + var read_off = off_in; + var write_off = off_out; + while (total_copied < len) { + const adjusted_count = math.min(buf.len, len - total_copied); + const amt_read = try pread(fd_in, buf[0..adjusted_count], read_off); + if (amt_read == 0) break; + const amt_written = try pwrite(fd_out, buf[0..amt_read], write_off); + // pwrite may write less than the specified amount, handle the remaining + // chunk of data in the next iteration + read_off += amt_written; + write_off += amt_written; + total_copied += amt_written; } - // Sendfile is a zero-copy mechanism iff the OS supports it, otherwise the - // fallback code will copy the contents chunk by chunk. - const empty_iovec = [0]iovec_const{}; - var offset: u64 = 0; - sendfile_loop: while (true) { - const amt = try sendfile(fd_out, fd_in, offset, 0, &empty_iovec, &empty_iovec, 0); - if (amt == 0) break :sendfile_loop; - offset += amt; - } + return total_copied; } pub const PollError = error{ -- cgit v1.2.3 From 1f7ec0de706eded887bc8dbcf5f45a5546d1be5c Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Tue, 6 Oct 2020 11:57:23 +0200 Subject: Address review comments & fix compilation errors --- lib/std/fs.zig | 18 +++++++++--------- lib/std/os.zig | 14 +++++++------- 2 files changed, 16 insertions(+), 16 deletions(-) (limited to 'lib/std/os.zig') diff --git a/lib/std/fs.zig b/lib/std/fs.zig index 92f68590f9..6b4f9384dd 100644 --- a/lib/std/fs.zig +++ b/lib/std/fs.zig @@ -2270,14 +2270,14 @@ const CopyFileError = error{SystemResources} || os.CopyFileRangeError || os.Send fn copy_file(fd_in: os.fd_t, fd_out: os.fd_t) CopyFileError!void { if (comptime std.Target.current.isDarwin()) { const rc = os.system.fcopyfile(fd_in, fd_out, null, os.system.COPYFILE_DATA); - switch (errno(rc)) { + switch (os.errno(rc)) { 0 => return, - EINVAL => unreachable, - ENOMEM => return error.SystemResources, - // The source file was not a directory, symbolic link, or regular file. + os.EINVAL => unreachable, + os.ENOMEM => return error.SystemResources, + // The source file is not a directory, symbolic link, or regular file. // Try with the fallback path before giving up. - ENOTSUP => {}, - else => |err| return unexpectedErrno(err), + os.ENOTSUP => {}, + else => |err| return os.unexpectedErrno(err), } } @@ -2286,9 +2286,9 @@ fn copy_file(fd_in: os.fd_t, fd_out: os.fd_t) CopyFileError!void { // most efficient method (if available). var offset: u64 = 0; cfr_loop: while (true) { - // The kernel checks `offset+count` for overflow, use a 32 bit - // value so that the syscall won't return EINVAL except for - // impossibly large files. + // The kernel checks the u64 value `offset+count` for overflow, use + // a 32 bit value so that the syscall won't return EINVAL except for + // impossibly large files (> 2^64-1 - 2^32-1). const amt = try os.copy_file_range(fd_in, offset, fd_out, offset, math.maxInt(u32), 0); // Terminate when no data was copied if (amt == 0) break :cfr_loop; diff --git a/lib/std/os.zig b/lib/std/os.zig index e4e0b1d4a1..0e81b73f8c 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -4954,6 +4954,11 @@ pub const CopyFileRangeError = error{ FileBusy, } || PReadError || PWriteError || UnexpectedError; +var has_copy_file_range_syscall = init: { + const kernel_has_syscall = comptime std.Target.current.os.isAtLeast(.linux, .{ .major = 4, .minor = 5 }) orelse true; + break :init std.atomic.Int(bool).init(kernel_has_syscall); +}; + /// Transfer data between file descriptors at specified offsets. /// Returns the number of bytes written, which can less than requested. /// @@ -4979,16 +4984,11 @@ pub const CopyFileRangeError = error{ /// Other systems fall back to calling `pread` / `pwrite`. /// /// Maximum offsets on Linux are `math.maxInt(i64)`. -var has_copy_file_range_syscall = init: { - const kernel_has_syscall = comptime std.Target.current.os.isAtLeast(.linux, .{ .major = 4, .minor = 5 }) orelse true; - break :init std.atomic.Int(u1).init(@boolToInt(kernel_has_syscall)); -}; - pub fn copy_file_range(fd_in: fd_t, off_in: u64, fd_out: fd_t, off_out: u64, len: usize, flags: u32) CopyFileRangeError!usize { const use_c = std.c.versionCheck(.{ .major = 2, .minor = 27, .patch = 0 }).ok; if (std.Target.current.os.tag == .linux and - (use_c or has_copy_file_range_syscall.get() != 0)) + (use_c or has_copy_file_range_syscall.get())) { const sys = if (use_c) std.c else linux; @@ -5013,7 +5013,7 @@ pub fn copy_file_range(fd_in: fd_t, off_in: u64, fd_out: fd_t, off_out: u64, len EXDEV => {}, // syscall added in Linux 4.5, use fallback ENOSYS => { - has_copy_file_range_syscall.set(0); + has_copy_file_range_syscall.set(false); }, else => |err| return unexpectedErrno(err), } -- cgit v1.2.3 From 03762da2af8753ecf7f4bc2005dd00d570c51ae7 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Wed, 7 Oct 2020 11:13:26 +0200 Subject: New review round --- lib/std/c/darwin.zig | 2 -- lib/std/fs.zig | 5 +++-- lib/std/os.zig | 33 ++++++++++++--------------------- 3 files changed, 15 insertions(+), 25 deletions(-) (limited to 'lib/std/os.zig') diff --git a/lib/std/c/darwin.zig b/lib/std/c/darwin.zig index 5b305d60e6..93efdd061c 100644 --- a/lib/std/c/darwin.zig +++ b/lib/std/c/darwin.zig @@ -24,8 +24,6 @@ pub const COPYFILE_XATTR = 1 << 2; pub const COPYFILE_DATA = 1 << 3; pub const copyfile_state_t = *@Type(.Opaque); -pub extern "c" fn copyfile_state_alloc() copyfile_state_t; -pub extern "c" fn copyfile_state_free(state: copyfile_state_t) c_int; pub extern "c" fn fcopyfile(from: fd_t, to: fd_t, state: ?copyfile_state_t, flags: u32) c_int; pub extern "c" fn @"realpath$DARWIN_EXTSN"(noalias file_name: [*:0]const u8, noalias resolved_name: [*]u8) ?[*:0]u8; diff --git a/lib/std/fs.zig b/lib/std/fs.zig index 6b4f9384dd..5f49bb3766 100644 --- a/lib/std/fs.zig +++ b/lib/std/fs.zig @@ -2265,8 +2265,9 @@ pub fn realpathAlloc(allocator: *Allocator, pathname: []const u8) ![]u8 { const CopyFileError = error{SystemResources} || os.CopyFileRangeError || os.SendFileError; -/// Transfer all the data between two file descriptors in the most efficient way. -/// No metadata is transferred over. +// Transfer all the data between two file descriptors in the most efficient way. +// The copy starts at offset 0, the initial offsets are preserved. +// No metadata is transferred over. fn copy_file(fd_in: os.fd_t, fd_out: os.fd_t) CopyFileError!void { if (comptime std.Target.current.isDarwin()) { const rc = os.system.fcopyfile(fd_in, fd_out, null, os.system.COPYFILE_DATA); diff --git a/lib/std/os.zig b/lib/std/os.zig index 0e81b73f8c..6e25451879 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -4945,7 +4945,9 @@ pub fn sendfile( pub const CopyFileRangeError = error{ FileTooBig, InputOutput, - InvalidFileDescriptor, + /// `fd_in` is not open for reading; or `fd_out` is not open for writing; + /// or the `O_APPEND` flag is set for `fd_out`. + FilesOpenedWithWrongFlags, IsDir, OutOfMemory, NoSpaceLeft, @@ -4955,7 +4957,7 @@ pub const CopyFileRangeError = error{ } || PReadError || PWriteError || UnexpectedError; var has_copy_file_range_syscall = init: { - const kernel_has_syscall = comptime std.Target.current.os.isAtLeast(.linux, .{ .major = 4, .minor = 5 }) orelse true; + const kernel_has_syscall = std.Target.current.os.isAtLeast(.linux, .{ .major = 4, .minor = 5 }) orelse true; break :init std.atomic.Int(bool).init(kernel_has_syscall); }; @@ -4998,7 +5000,7 @@ pub fn copy_file_range(fd_in: fd_t, off_in: u64, fd_out: fd_t, off_out: u64, len const rc = sys.copy_file_range(fd_in, &off_in_copy, fd_out, &off_out_copy, len, flags); switch (sys.getErrno(rc)) { 0 => return @intCast(usize, rc), - EBADF => return error.InvalidFileDescriptor, + EBADF => return error.FilesOpenedWithWrongFlags, EFBIG => return error.FileTooBig, EIO => return error.InputOutput, EISDIR => return error.IsDir, @@ -5019,24 +5021,13 @@ pub fn copy_file_range(fd_in: fd_t, off_in: u64, fd_out: fd_t, off_out: u64, len } } - var buf: [2 * 4096]u8 = undefined; - - var total_copied: usize = 0; - var read_off = off_in; - var write_off = off_out; - while (total_copied < len) { - const adjusted_count = math.min(buf.len, len - total_copied); - const amt_read = try pread(fd_in, buf[0..adjusted_count], read_off); - if (amt_read == 0) break; - const amt_written = try pwrite(fd_out, buf[0..amt_read], write_off); - // pwrite may write less than the specified amount, handle the remaining - // chunk of data in the next iteration - read_off += amt_written; - write_off += amt_written; - total_copied += amt_written; - } - - return total_copied; + var buf: [8 * 4096]u8 = undefined; + const adjusted_count = math.min(buf.len, len); + const amt_read = try pread(fd_in, buf[0..adjusted_count], off_in); + // TODO without @as the line below fails to compile for wasm32-wasi: + // error: integer value 0 cannot be coerced to type 'os.PWriteError!usize' + if (amt_read == 0) return @as(usize, 0); + return pwrite(fd_out, buf[0..amt_read], off_out); } pub const PollError = error{ -- cgit v1.2.3 From 2ab0c7391a871a3063f825e08e02ea2a8e9269e9 Mon Sep 17 00:00:00 2001 From: Vignesh Rajagopalan Date: Mon, 12 Oct 2020 14:29:43 +0530 Subject: Rename .macosx to .macos --- lib/std/c.zig | 8 ++++---- lib/std/debug.zig | 4 ++-- lib/std/dynamic_library.zig | 4 ++-- lib/std/event/loop.zig | 24 ++++++++++++------------ lib/std/fs.zig | 10 +++++----- lib/std/fs/get_app_data_dir.zig | 2 +- lib/std/fs/test.zig | 2 +- lib/std/fs/watch.zig | 8 ++++---- lib/std/os.zig | 18 +++++++++--------- lib/std/os/bits.zig | 2 +- lib/std/os/test.zig | 2 +- lib/std/packed_int_array.zig | 4 ++-- lib/std/process.zig | 6 +++--- lib/std/special/compiler_rt/clear_cache.zig | 2 +- lib/std/target.zig | 16 ++++++++-------- lib/std/time.zig | 2 +- lib/std/zig/cross_target.zig | 6 +++--- lib/std/zig/system.zig | 2 +- src/codegen/llvm.zig | 2 +- src/link/MachO.zig | 2 +- src/target.zig | 6 +++--- src/type.zig | 2 +- src/zig_llvm.cpp | 4 +++- test/stack_traces.zig | 2 +- test/stage2/test.zig | 2 +- test/tests.zig | 2 +- 26 files changed, 73 insertions(+), 71 deletions(-) (limited to 'lib/std/os.zig') diff --git a/lib/std/c.zig b/lib/std/c.zig index a75fcaa84b..1fd3c593cc 100644 --- a/lib/std/c.zig +++ b/lib/std/c.zig @@ -22,7 +22,7 @@ pub usingnamespace @import("os/bits.zig"); pub usingnamespace switch (std.Target.current.os.tag) { .linux => @import("c/linux.zig"), .windows => @import("c/windows.zig"), - .macosx, .ios, .tvos, .watchos => @import("c/darwin.zig"), + .macos, .ios, .tvos, .watchos => @import("c/darwin.zig"), .freebsd, .kfreebsd => @import("c/freebsd.zig"), .netbsd => @import("c/netbsd.zig"), .dragonfly => @import("c/dragonfly.zig"), @@ -122,7 +122,7 @@ pub extern "c" fn readlink(noalias path: [*:0]const u8, noalias buf: [*]u8, bufs pub extern "c" fn readlinkat(dirfd: fd_t, noalias path: [*:0]const u8, noalias buf: [*]u8, bufsize: usize) isize; pub usingnamespace switch (builtin.os.tag) { - .macosx, .ios, .watchos, .tvos => struct { + .macos, .ios, .watchos, .tvos => struct { pub const realpath = @"realpath$DARWIN_EXTSN"; pub const fstatat = @"fstatat$INODE64"; }, @@ -189,7 +189,7 @@ pub usingnamespace switch (builtin.os.tag) { pub const sigprocmask = __sigprocmask14; pub const stat = __stat50; }, - .macosx, .ios, .watchos, .tvos => struct { + .macos, .ios, .watchos, .tvos => struct { // XXX: close -> close$NOCANCEL // XXX: getdirentries -> _getdirentries64 pub extern "c" fn clock_getres(clk_id: c_int, tp: *timespec) c_int; @@ -252,7 +252,7 @@ pub usingnamespace switch (builtin.os.tag) { .linux, .freebsd, .kfreebsd, .netbsd, .openbsd => struct { pub extern "c" fn malloc_usable_size(?*const c_void) usize; }, - .macosx, .ios, .watchos, .tvos => struct { + .macos, .ios, .watchos, .tvos => struct { pub extern "c" fn malloc_size(?*const c_void) usize; }, else => struct {}, diff --git a/lib/std/debug.zig b/lib/std/debug.zig index be57c0b2fd..8dc2938894 100644 --- a/lib/std/debug.zig +++ b/lib/std/debug.zig @@ -654,7 +654,7 @@ pub fn openSelfDebugInfo(allocator: *mem.Allocator) anyerror!DebugInfo { .freebsd, .netbsd, .dragonfly, - .macosx, + .macos, .windows, => return DebugInfo.init(allocator), else => @compileError("openSelfDebugInfo unsupported for this platform"), @@ -1320,7 +1320,7 @@ const SymbolInfo = struct { }; pub const ModuleDebugInfo = switch (builtin.os.tag) { - .macosx, .ios, .watchos, .tvos => struct { + .macos, .ios, .watchos, .tvos => struct { base_address: usize, mapped_memory: []const u8, symbols: []const MachoSymbol, diff --git a/lib/std/dynamic_library.zig b/lib/std/dynamic_library.zig index 238854f07f..07be46a8e1 100644 --- a/lib/std/dynamic_library.zig +++ b/lib/std/dynamic_library.zig @@ -19,7 +19,7 @@ const max = std.math.max; pub const DynLib = switch (builtin.os.tag) { .linux => if (builtin.link_libc) DlDynlib else ElfDynLib, .windows => WindowsDynLib, - .macosx, .tvos, .watchos, .ios, .freebsd => DlDynlib, + .macos, .tvos, .watchos, .ios, .freebsd => DlDynlib, else => void, }; @@ -404,7 +404,7 @@ test "dynamic_library" { const libname = switch (builtin.os.tag) { .linux, .freebsd => "invalid_so.so", .windows => "invalid_dll.dll", - .macosx, .tvos, .watchos, .ios => "invalid_dylib.dylib", + .macos, .tvos, .watchos, .ios => "invalid_dylib.dylib", else => return error.SkipZigTest, }; diff --git a/lib/std/event/loop.zig b/lib/std/event/loop.zig index a064f711e2..5b10335535 100644 --- a/lib/std/event/loop.zig +++ b/lib/std/event/loop.zig @@ -66,7 +66,7 @@ pub const Loop = struct { }; pub const EventFd = switch (builtin.os.tag) { - .macosx, .freebsd, .netbsd, .dragonfly => KEventFd, + .macos, .freebsd, .netbsd, .dragonfly => KEventFd, .linux => struct { base: ResumeNode, epoll_op: u32, @@ -85,7 +85,7 @@ pub const Loop = struct { }; pub const Basic = switch (builtin.os.tag) { - .macosx, .freebsd, .netbsd, .dragonfly => KEventBasic, + .macos, .freebsd, .netbsd, .dragonfly => KEventBasic, .linux => struct { base: ResumeNode, }, @@ -259,7 +259,7 @@ pub const Loop = struct { self.extra_threads[extra_thread_index] = try Thread.spawn(self, workerRun); } }, - .macosx, .freebsd, .netbsd, .dragonfly => { + .macos, .freebsd, .netbsd, .dragonfly => { self.os_data.kqfd = try os.kqueue(); errdefer os.close(self.os_data.kqfd); @@ -384,7 +384,7 @@ pub const Loop = struct { while (self.available_eventfd_resume_nodes.pop()) |node| os.close(node.data.eventfd); os.close(self.os_data.epollfd); }, - .macosx, .freebsd, .netbsd, .dragonfly => { + .macos, .freebsd, .netbsd, .dragonfly => { os.close(self.os_data.kqfd); }, .windows => { @@ -478,7 +478,7 @@ pub const Loop = struct { .linux => { self.linuxWaitFd(fd, os.EPOLLET | os.EPOLLONESHOT | os.EPOLLIN); }, - .macosx, .freebsd, .netbsd, .dragonfly => { + .macos, .freebsd, .netbsd, .dragonfly => { self.bsdWaitKev(@intCast(usize, fd), os.EVFILT_READ, os.EV_ONESHOT); }, else => @compileError("Unsupported OS"), @@ -490,7 +490,7 @@ pub const Loop = struct { .linux => { self.linuxWaitFd(fd, os.EPOLLET | os.EPOLLONESHOT | os.EPOLLOUT); }, - .macosx, .freebsd, .netbsd, .dragonfly => { + .macos, .freebsd, .netbsd, .dragonfly => { self.bsdWaitKev(@intCast(usize, fd), os.EVFILT_WRITE, os.EV_ONESHOT); }, else => @compileError("Unsupported OS"), @@ -502,7 +502,7 @@ pub const Loop = struct { .linux => { self.linuxWaitFd(fd, os.EPOLLET | os.EPOLLONESHOT | os.EPOLLOUT | os.EPOLLIN); }, - .macosx, .freebsd, .netbsd, .dragonfly => { + .macos, .freebsd, .netbsd, .dragonfly => { self.bsdWaitKev(@intCast(usize, fd), os.EVFILT_READ, os.EV_ONESHOT); self.bsdWaitKev(@intCast(usize, fd), os.EVFILT_WRITE, os.EV_ONESHOT); }, @@ -571,7 +571,7 @@ pub const Loop = struct { const eventfd_node = &resume_stack_node.data; eventfd_node.base.handle = next_tick_node.data; switch (builtin.os.tag) { - .macosx, .freebsd, .netbsd, .dragonfly => { + .macos, .freebsd, .netbsd, .dragonfly => { const kevent_array = @as(*const [1]os.Kevent, &eventfd_node.kevent); const empty_kevs = &[0]os.Kevent{}; _ = os.kevent(self.os_data.kqfd, kevent_array, empty_kevs, null) catch { @@ -633,7 +633,7 @@ pub const Loop = struct { if (!builtin.single_threaded) { switch (builtin.os.tag) { .linux, - .macosx, + .macos, .freebsd, .netbsd, .dragonfly, @@ -725,7 +725,7 @@ pub const Loop = struct { } return; }, - .macosx, .freebsd, .netbsd, .dragonfly => { + .macos, .freebsd, .netbsd, .dragonfly => { const final_kevent = @as(*const [1]os.Kevent, &self.os_data.final_kevent); const empty_kevs = &[0]os.Kevent{}; // cannot fail because we already added it and this just enables it @@ -1218,7 +1218,7 @@ pub const Loop = struct { } } }, - .macosx, .freebsd, .netbsd, .dragonfly => { + .macos, .freebsd, .netbsd, .dragonfly => { var eventlist: [1]os.Kevent = undefined; const empty_kevs = &[0]os.Kevent{}; const count = os.kevent(self.os_data.kqfd, empty_kevs, eventlist[0..], null) catch unreachable; @@ -1344,7 +1344,7 @@ pub const Loop = struct { const OsData = switch (builtin.os.tag) { .linux => LinuxOsData, - .macosx, .freebsd, .netbsd, .dragonfly => KEventData, + .macos, .freebsd, .netbsd, .dragonfly => KEventData, .windows => struct { io_port: windows.HANDLE, extra_thread_count: usize, diff --git a/lib/std/fs.zig b/lib/std/fs.zig index 0f8c6e0d24..6fd188e1f9 100644 --- a/lib/std/fs.zig +++ b/lib/std/fs.zig @@ -39,7 +39,7 @@ pub const Watch = @import("fs/watch.zig").Watch; /// fit into a UTF-8 encoded array of this length. /// The byte count includes room for a null sentinel byte. pub const MAX_PATH_BYTES = switch (builtin.os.tag) { - .linux, .macosx, .ios, .freebsd, .netbsd, .dragonfly => os.PATH_MAX, + .linux, .macos, .ios, .freebsd, .netbsd, .dragonfly => os.PATH_MAX, // Each UTF-16LE character may be expanded to 3 UTF-8 bytes. // If it would require 4 UTF-8 bytes, then there would be a surrogate // pair in the UTF-16LE, and we (over)account 3 bytes for it that way. @@ -303,7 +303,7 @@ pub const Dir = struct { const IteratorError = error{AccessDenied} || os.UnexpectedError; pub const Iterator = switch (builtin.os.tag) { - .macosx, .ios, .freebsd, .netbsd, .dragonfly => struct { + .macos, .ios, .freebsd, .netbsd, .dragonfly => struct { dir: Dir, seek: i64, buf: [8192]u8, // TODO align(@alignOf(os.dirent)), @@ -318,7 +318,7 @@ pub const Dir = struct { /// with subsequent calls to `next`, as well as when this `Dir` is deinitialized. pub fn next(self: *Self) Error!?Entry { switch (builtin.os.tag) { - .macosx, .ios => return self.nextDarwin(), + .macos, .ios => return self.nextDarwin(), .freebsd, .netbsd, .dragonfly => return self.nextBsd(), else => @compileError("unimplemented"), } @@ -615,7 +615,7 @@ pub const Dir = struct { pub fn iterate(self: Dir) Iterator { switch (builtin.os.tag) { - .macosx, .ios, .freebsd, .netbsd, .dragonfly => return Iterator{ + .macos, .ios, .freebsd, .netbsd, .dragonfly => return Iterator{ .dir = self, .seek = 0, .index = 0, @@ -1302,7 +1302,7 @@ pub const Dir = struct { error.AccessDenied => |e| switch (builtin.os.tag) { // non-Linux POSIX systems return EPERM when trying to delete a directory, so // we need to handle that case specifically and translate the error - .macosx, .ios, .freebsd, .netbsd, .dragonfly => { + .macos, .ios, .freebsd, .netbsd, .dragonfly => { // Don't follow symlinks to match unlinkat (which acts on symlinks rather than follows them) const fstat = os.fstatatZ(self.fd, sub_path_c, os.AT_SYMLINK_NOFOLLOW) catch return e; const is_dir = fstat.mode & os.S_IFMT == os.S_IFDIR; diff --git a/lib/std/fs/get_app_data_dir.zig b/lib/std/fs/get_app_data_dir.zig index 17d365f684..6f8dae07c5 100644 --- a/lib/std/fs/get_app_data_dir.zig +++ b/lib/std/fs/get_app_data_dir.zig @@ -42,7 +42,7 @@ pub fn getAppDataDir(allocator: *mem.Allocator, appname: []const u8) GetAppDataD else => return error.AppDataDirUnavailable, } }, - .macosx => { + .macos => { const home_dir = os.getenv("HOME") orelse { // TODO look in /etc/passwd return error.AppDataDirUnavailable; diff --git a/lib/std/fs/test.zig b/lib/std/fs/test.zig index 60cd380444..860da8ef16 100644 --- a/lib/std/fs/test.zig +++ b/lib/std/fs/test.zig @@ -143,7 +143,7 @@ fn contains(entries: *const std.ArrayList(Dir.Entry), el: Dir.Entry) bool { test "Dir.realpath smoke test" { switch (builtin.os.tag) { - .linux, .windows, .macosx, .ios, .watchos, .tvos => {}, + .linux, .windows, .macos, .ios, .watchos, .tvos => {}, else => return error.SkipZigTest, } diff --git a/lib/std/fs/watch.zig b/lib/std/fs/watch.zig index 269cdec6d8..8b709c2d4b 100644 --- a/lib/std/fs/watch.zig +++ b/lib/std/fs/watch.zig @@ -49,7 +49,7 @@ pub fn Watch(comptime V: type) type { const OsData = switch (builtin.os.tag) { // TODO https://github.com/ziglang/zig/issues/3778 - .macosx, .freebsd, .netbsd, .dragonfly => KqOsData, + .macos, .freebsd, .netbsd, .dragonfly => KqOsData, .linux => LinuxOsData, .windows => WindowsOsData, @@ -160,7 +160,7 @@ pub fn Watch(comptime V: type) type { return self; }, - .macosx, .freebsd, .netbsd, .dragonfly => { + .macos, .freebsd, .netbsd, .dragonfly => { self.* = Self{ .allocator = allocator, .channel = channel, @@ -178,7 +178,7 @@ pub fn Watch(comptime V: type) type { /// All addFile calls and removeFile calls must have completed. pub fn deinit(self: *Self) void { switch (builtin.os.tag) { - .macosx, .freebsd, .netbsd, .dragonfly => { + .macos, .freebsd, .netbsd, .dragonfly => { // TODO we need to cancel the frames before destroying the lock self.os_data.table_lock.deinit(); var it = self.os_data.file_table.iterator(); @@ -229,7 +229,7 @@ pub fn Watch(comptime V: type) type { pub fn addFile(self: *Self, file_path: []const u8, value: V) !?V { switch (builtin.os.tag) { - .macosx, .freebsd, .netbsd, .dragonfly => return addFileKEvent(self, file_path, value), + .macos, .freebsd, .netbsd, .dragonfly => return addFileKEvent(self, file_path, value), .linux => return addFileLinux(self, file_path, value), .windows => return addFileWindows(self, file_path, value), else => @compileError("Unsupported OS"), diff --git a/lib/std/os.zig b/lib/std/os.zig index 9f6c012090..bc7dae031c 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -62,7 +62,7 @@ pub const system = if (@hasDecl(root, "os") and root.os != @This()) else if (builtin.link_libc) std.c else switch (builtin.os.tag) { - .macosx, .ios, .watchos, .tvos => darwin, + .macos, .ios, .watchos, .tvos => darwin, .freebsd => freebsd, .linux => linux, .netbsd => netbsd, @@ -354,7 +354,7 @@ pub fn read(fd: fd_t, buf: []u8) ReadError!usize { // Prevents EINVAL. const max_count = switch (std.Target.current.os.tag) { .linux => 0x7ffff000, - .macosx, .ios, .watchos, .tvos => math.maxInt(i32), + .macos, .ios, .watchos, .tvos => math.maxInt(i32), else => math.maxInt(isize), }; const adjusted_len = math.min(max_count, buf.len); @@ -582,7 +582,7 @@ pub fn ftruncate(fd: fd_t, length: u64) TruncateError!void { /// On these systems, the read races with concurrent writes to the same file descriptor. pub fn preadv(fd: fd_t, iov: []const iovec, offset: u64) PReadError!usize { const have_pread_but_not_preadv = switch (std.Target.current.os.tag) { - .windows, .macosx, .ios, .watchos, .tvos => true, + .windows, .macos, .ios, .watchos, .tvos => true, else => false, }; if (have_pread_but_not_preadv) { @@ -709,7 +709,7 @@ pub fn write(fd: fd_t, bytes: []const u8) WriteError!usize { const max_count = switch (std.Target.current.os.tag) { .linux => 0x7ffff000, - .macosx, .ios, .watchos, .tvos => math.maxInt(i32), + .macos, .ios, .watchos, .tvos => math.maxInt(i32), else => math.maxInt(isize), }; const adjusted_len = math.min(max_count, bytes.len); @@ -863,7 +863,7 @@ pub fn pwrite(fd: fd_t, bytes: []const u8, offset: u64) PWriteError!usize { // Prevent EINVAL. const max_count = switch (std.Target.current.os.tag) { .linux => 0x7ffff000, - .macosx, .ios, .watchos, .tvos => math.maxInt(i32), + .macos, .ios, .watchos, .tvos => math.maxInt(i32), else => math.maxInt(isize), }; const adjusted_len = math.min(max_count, bytes.len); @@ -915,7 +915,7 @@ pub fn pwrite(fd: fd_t, bytes: []const u8, offset: u64) PWriteError!usize { /// If `iov.len` is larger than will fit in a `u31`, a partial write will occur. pub fn pwritev(fd: fd_t, iov: []const iovec_const, offset: u64) PWriteError!usize { const have_pwrite_but_not_pwritev = switch (std.Target.current.os.tag) { - .windows, .macosx, .ios, .watchos, .tvos => true, + .windows, .macos, .ios, .watchos, .tvos => true, else => false, }; @@ -4091,7 +4091,7 @@ pub fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 { const end_index = std.unicode.utf16leToUtf8(out_buffer, wide_slice) catch unreachable; return out_buffer[0..end_index]; }, - .macosx, .ios, .watchos, .tvos => { + .macos, .ios, .watchos, .tvos => { // On macOS, we can use F_GETPATH fcntl command to query the OS for // the path to the file descriptor. @memset(out_buffer, 0, MAX_PATH_BYTES); @@ -4688,7 +4688,7 @@ pub fn sendfile( }); const max_count = switch (std.Target.current.os.tag) { .linux => 0x7ffff000, - .macosx, .ios, .watchos, .tvos => math.maxInt(i32), + .macos, .ios, .watchos, .tvos => math.maxInt(i32), else => math.maxInt(size_t), }; @@ -4846,7 +4846,7 @@ pub fn sendfile( } } }, - .macosx, .ios, .tvos, .watchos => sf: { + .macos, .ios, .tvos, .watchos => sf: { var hdtr_data: std.c.sf_hdtr = undefined; var hdtr: ?*std.c.sf_hdtr = null; if (headers.len != 0 or trailers.len != 0) { diff --git a/lib/std/os/bits.zig b/lib/std/os/bits.zig index 177b7daad2..3647f67f2a 100644 --- a/lib/std/os/bits.zig +++ b/lib/std/os/bits.zig @@ -12,7 +12,7 @@ const std = @import("std"); const root = @import("root"); pub usingnamespace switch (std.Target.current.os.tag) { - .macosx, .ios, .tvos, .watchos => @import("bits/darwin.zig"), + .macos, .ios, .tvos, .watchos => @import("bits/darwin.zig"), .dragonfly => @import("bits/dragonfly.zig"), .freebsd => @import("bits/freebsd.zig"), .linux => @import("bits/linux.zig"), diff --git a/lib/std/os/test.zig b/lib/std/os/test.zig index aee1b9549a..58c2b311b1 100644 --- a/lib/std/os/test.zig +++ b/lib/std/os/test.zig @@ -360,7 +360,7 @@ fn iter_fn(info: *dl_phdr_info, size: usize, counter: *usize) IterFnError!void { } test "dl_iterate_phdr" { - if (builtin.os.tag == .windows or builtin.os.tag == .wasi or builtin.os.tag == .macosx) + if (builtin.os.tag == .windows or builtin.os.tag == .wasi or builtin.os.tag == .macos) return error.SkipZigTest; var counter: usize = 0; diff --git a/lib/std/packed_int_array.zig b/lib/std/packed_int_array.zig index f25ff0b1b8..2f2b671692 100644 --- a/lib/std/packed_int_array.zig +++ b/lib/std/packed_int_array.zig @@ -618,7 +618,7 @@ test "PackedIntArray at end of available memory" { if (we_are_testing_this_with_stage1_which_leaks_comptime_memory) return error.SkipZigTest; switch (builtin.os.tag) { - .linux, .macosx, .ios, .freebsd, .netbsd, .windows => {}, + .linux, .macos, .ios, .freebsd, .netbsd, .windows => {}, else => return, } const PackedArray = PackedIntArray(u3, 8); @@ -639,7 +639,7 @@ test "PackedIntSlice at end of available memory" { if (we_are_testing_this_with_stage1_which_leaks_comptime_memory) return error.SkipZigTest; switch (builtin.os.tag) { - .linux, .macosx, .ios, .freebsd, .netbsd, .windows => {}, + .linux, .macos, .ios, .freebsd, .netbsd, .windows => {}, else => return, } const PackedSlice = PackedIntSlice(u11); diff --git a/lib/std/process.zig b/lib/std/process.zig index 2813d8cbab..3a499f3e24 100644 --- a/lib/std/process.zig +++ b/lib/std/process.zig @@ -585,7 +585,7 @@ pub const UserInfo = struct { /// POSIX function which gets a uid from username. pub fn getUserInfo(name: []const u8) !UserInfo { return switch (builtin.os.tag) { - .linux, .macosx, .watchos, .tvos, .ios, .freebsd, .netbsd => posixGetUserInfo(name), + .linux, .macos, .watchos, .tvos, .ios, .freebsd, .netbsd => posixGetUserInfo(name), else => @compileError("Unsupported OS"), }; } @@ -688,7 +688,7 @@ pub fn getBaseAddress() usize { const phdr = os.system.getauxval(std.elf.AT_PHDR); return phdr - @sizeOf(std.elf.Ehdr); }, - .macosx, .freebsd, .netbsd => { + .macos, .freebsd, .netbsd => { return @ptrToInt(&std.c._mh_execute_header); }, .windows => return @ptrToInt(os.windows.kernel32.GetModuleHandleW(null)), @@ -733,7 +733,7 @@ pub fn getSelfExeSharedLibPaths(allocator: *Allocator) error{OutOfMemory}![][:0] }.callback); return paths.toOwnedSlice(); }, - .macosx, .ios, .watchos, .tvos => { + .macos, .ios, .watchos, .tvos => { var paths = List.init(allocator); errdefer { const slice = paths.toOwnedSlice(); diff --git a/lib/std/special/compiler_rt/clear_cache.zig b/lib/std/special/compiler_rt/clear_cache.zig index d862f739d3..4b00721868 100644 --- a/lib/std/special/compiler_rt/clear_cache.zig +++ b/lib/std/special/compiler_rt/clear_cache.zig @@ -44,7 +44,7 @@ pub fn clear_cache(start: usize, end: usize) callconv(.C) void { else => false, }; const apple = switch (os) { - .ios, .macosx, .watchos, .tvos => true, + .ios, .macos, .watchos, .tvos => true, else => false, }; if (x86) { diff --git a/lib/std/target.zig b/lib/std/target.zig index 99617c6d0e..4b9a6b4458 100644 --- a/lib/std/target.zig +++ b/lib/std/target.zig @@ -31,7 +31,7 @@ pub const Target = struct { kfreebsd, linux, lv2, - macosx, + macos, netbsd, openbsd, solaris, @@ -61,7 +61,7 @@ pub const Target = struct { pub fn isDarwin(tag: Tag) bool { return switch (tag) { - .ios, .macosx, .watchos, .tvos => true, + .ios, .macos, .watchos, .tvos => true, else => false, }; } @@ -234,7 +234,7 @@ pub const Target = struct { .max = .{ .major = 12, .minor = 1 }, }, }, - .macosx => return .{ + .macos => return .{ .semver = .{ .min = .{ .major = 10, .minor = 13 }, .max = .{ .major = 10, .minor = 15, .patch = 3 }, @@ -312,7 +312,7 @@ pub const Target = struct { .windows => return TaggedVersionRange{ .windows = self.version_range.windows }, .freebsd, - .macosx, + .macos, .ios, .tvos, .watchos, @@ -341,7 +341,7 @@ pub const Target = struct { return switch (os.tag) { .freebsd, .netbsd, - .macosx, + .macos, .ios, .tvos, .watchos, @@ -450,7 +450,7 @@ pub const Target = struct { .other, => return .eabi, .openbsd, - .macosx, + .macos, .freebsd, .ios, .tvos, @@ -1277,7 +1277,7 @@ pub const Target = struct { .ios, .tvos, .watchos, - .macosx, + .macos, .uefi, .windows, .emscripten, @@ -1450,7 +1450,7 @@ pub const Target = struct { .ios, .tvos, .watchos, - .macosx, + .macos, .uefi, .windows, .emscripten, diff --git a/lib/std/time.zig b/lib/std/time.zig index ae128f6b0c..344f192784 100644 --- a/lib/std/time.zig +++ b/lib/std/time.zig @@ -143,7 +143,7 @@ pub const Timer = struct { /// be less precise frequency: switch (builtin.os.tag) { .windows => u64, - .macosx, .ios, .tvos, .watchos => os.darwin.mach_timebase_info_data, + .macos, .ios, .tvos, .watchos => os.darwin.mach_timebase_info_data, else => void, }, resolution: u64, diff --git a/lib/std/zig/cross_target.zig b/lib/std/zig/cross_target.zig index f1ae3457a5..3d39c8338b 100644 --- a/lib/std/zig/cross_target.zig +++ b/lib/std/zig/cross_target.zig @@ -137,7 +137,7 @@ pub const CrossTarget = struct { }, .freebsd, - .macosx, + .macos, .ios, .tvos, .watchos, @@ -578,7 +578,7 @@ pub const CrossTarget = struct { const os = switch (self.getOsTag()) { .windows => "windows", .linux => "linux", - .macosx => "macos", + .macos => "macos", else => return error.UnsupportedVcpkgOperatingSystem, }; @@ -718,7 +718,7 @@ pub const CrossTarget = struct { => return error.InvalidOperatingSystemVersion, .freebsd, - .macosx, + .macos, .ios, .tvos, .watchos, diff --git a/lib/std/zig/system.zig b/lib/std/zig/system.zig index b1947058cc..33ef137eb6 100644 --- a/lib/std/zig/system.zig +++ b/lib/std/zig/system.zig @@ -267,7 +267,7 @@ pub const NativeTargetInfo = struct { os.version_range.windows.max = @intToEnum(Target.Os.WindowsVersion, version); os.version_range.windows.min = @intToEnum(Target.Os.WindowsVersion, version); }, - .macosx => { + .macos => { var scbuf: [32]u8 = undefined; var size: usize = undefined; diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index 01fa0baf02..f87a7a940c 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -69,7 +69,7 @@ pub fn targetTriple(allocator: *Allocator, target: std.Target) ![]u8 { .kfreebsd => "kfreebsd", .linux => "linux", .lv2 => "lv2", - .macosx => "macosx", + .macos => "macosx", .netbsd => "netbsd", .openbsd => "openbsd", .solaris => "solaris", diff --git a/src/link/MachO.zig b/src/link/MachO.zig index 9700a0bdb4..0d3de89d58 100644 --- a/src/link/MachO.zig +++ b/src/link/MachO.zig @@ -525,7 +525,7 @@ fn linkWithLLD(self: *MachO, comp: *Compilation) !void { try argv.append(darwinArchString(target.cpu.arch)); switch (target.os.tag) { - .macosx => { + .macos => { try argv.append("-macosx_version_min"); }, .ios, .tvos, .watchos => switch (target.cpu.arch) { diff --git a/src/target.zig b/src/target.zig index d009cf8556..ce3a200d2b 100644 --- a/src/target.zig +++ b/src/target.zig @@ -123,14 +123,14 @@ pub fn cannotDynamicLink(target: std.Target) bool { /// since this is the stable syscall interface. pub fn osRequiresLibC(target: std.Target) bool { return switch (target.os.tag) { - .freebsd, .netbsd, .dragonfly, .macosx, .ios, .watchos, .tvos => true, + .freebsd, .netbsd, .dragonfly, .macos, .ios, .watchos, .tvos => true, else => false, }; } pub fn libcNeedsLibUnwind(target: std.Target) bool { return switch (target.os.tag) { - .macosx, + .macos, .ios, .watchos, .tvos, @@ -197,7 +197,7 @@ pub fn osToLLVM(os_tag: std.Target.Os.Tag) llvm.OSType { .kfreebsd => .KFreeBSD, .linux => .Linux, .lv2 => .Lv2, - .macosx => .MacOSX, + .macos => .MacOSX, .netbsd => .NetBSD, .openbsd => .OpenBSD, .solaris => .Solaris, diff --git a/src/type.zig b/src/type.zig index c22edaa561..f07df290fc 100644 --- a/src/type.zig +++ b/src/type.zig @@ -3104,7 +3104,7 @@ pub const CType = enum { }, .linux, - .macosx, + .macos, .freebsd, .netbsd, .dragonfly, diff --git a/src/zig_llvm.cpp b/src/zig_llvm.cpp index 78082d16ba..decd11e0ab 100644 --- a/src/zig_llvm.cpp +++ b/src/zig_llvm.cpp @@ -817,7 +817,9 @@ const char *ZigLLVMGetVendorTypeName(ZigLLVM_VendorType vendor) { } const char *ZigLLVMGetOSTypeName(ZigLLVM_OSType os) { - return (const char*)Triple::getOSTypeName((Triple::OSType)os).bytes_begin(); + const char* name = (const char*)Triple::getOSTypeName((Triple::OSType)os).bytes_begin(); + if (strcmp(name, "macosx") == 0) return "macos"; + return name; } const char *ZigLLVMGetEnvironmentTypeName(ZigLLVM_EnvironmentType env_type) { diff --git a/test/stack_traces.zig b/test/stack_traces.zig index 496be05138..640cc0904c 100644 --- a/test/stack_traces.zig +++ b/test/stack_traces.zig @@ -308,7 +308,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void { }, ); }, - .macosx => { + .macos => { cases.addCase( "return", source_return, diff --git a/test/stage2/test.zig b/test/stage2/test.zig index 2e3b570570..5f5fe76d17 100644 --- a/test/stage2/test.zig +++ b/test/stage2/test.zig @@ -13,7 +13,7 @@ const linux_x64 = std.zig.CrossTarget{ const macosx_x64 = std.zig.CrossTarget{ .cpu_arch = .x86_64, - .os_tag = .macosx, + .os_tag = .macos, }; const linux_riscv64 = std.zig.CrossTarget{ diff --git a/test/tests.zig b/test/tests.zig index 58b2b50094..09001b02a1 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -234,7 +234,7 @@ const test_targets = blk: { TestTarget{ .target = .{ .cpu_arch = .x86_64, - .os_tag = .macosx, + .os_tag = .macos, .abi = .gnu, }, // https://github.com/ziglang/zig/issues/3295 -- cgit v1.2.3