From 17b0166e004e81d9c433576f4c642e743fc527b7 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 24 May 2019 18:27:18 -0400 Subject: do Jay's suggestion with posix/os API naming & layout --- std/debug.zig | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'std/debug.zig') diff --git a/std/debug.zig b/std/debug.zig index 45abfda88e..e30efebdbc 100644 --- a/std/debug.zig +++ b/std/debug.zig @@ -893,7 +893,7 @@ fn openSelfDebugInfoWindows(allocator: *mem.Allocator) !DebugInfo { if (this_record_len % 4 != 0) { const round_to_next_4 = (this_record_len | 0x3) + 1; const march_forward_bytes = round_to_next_4 - this_record_len; - try dbi.seekForward(march_forward_bytes); + try dbi.seekBy(march_forward_bytes); this_record_len += march_forward_bytes; } @@ -1116,7 +1116,7 @@ fn printLineFromFileAnyOs(out_stream: var, line_info: LineInfo) !void { defer f.close(); // TODO fstat and make sure that the file has the correct size - var buf: [os.page_size]u8 = undefined; + var buf: [mem.page_size]u8 = undefined; var line: usize = 1; var column: usize = 1; var abs_index: usize = 0; @@ -1938,7 +1938,7 @@ fn getLineNumberInfoDwarf(di: *DwarfInfo, compile_unit: CompileUnit, target_addr }, else => { const fwd_amt = math.cast(isize, op_size - 1) catch return error.InvalidDebugInfo; - try di.dwarf_seekable_stream.seekForward(fwd_amt); + try di.dwarf_seekable_stream.seekBy(fwd_amt); }, } } else if (opcode >= opcode_base) { @@ -1990,7 +1990,7 @@ fn getLineNumberInfoDwarf(di: *DwarfInfo, compile_unit: CompileUnit, target_addr else => { if (opcode - 1 >= standard_opcode_lengths.len) return error.InvalidDebugInfo; const len_bytes = standard_opcode_lengths[opcode - 1]; - try di.dwarf_seekable_stream.seekForward(len_bytes); + try di.dwarf_seekable_stream.seekBy(len_bytes); }, } } -- cgit v1.2.3 From ca6debcaf4a4f85b7aff94c7b5fe821530b0f195 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 24 May 2019 22:52:07 -0400 Subject: starting to fix the regressions --- std/atomic/queue.zig | 4 +- std/atomic/stack.zig | 4 +- std/build.zig | 5 +- std/c.zig | 2 +- std/child_process.zig | 92 ++++------- std/coff.zig | 5 +- std/crypto/throughput_test.zig | 2 +- std/debug.zig | 13 +- std/dynamic_library.zig | 28 ++-- std/elf.zig | 5 +- std/event/fs.zig | 43 ++--- std/event/group.zig | 2 +- std/event/loop.zig | 4 +- std/event/net.zig | 15 +- std/event/rwlock.zig | 4 +- std/fs.zig | 367 ++++++++++++++++++++--------------------- std/io.zig | 12 +- std/io/c_out_stream.zig | 39 ++--- std/io/test.zig | 23 +-- std/os.zig | 110 ++++++++++-- std/os/bits/linux.zig | 12 +- std/os/test.zig | 7 +- std/os/windows.zig | 26 +++ std/pdb.zig | 11 +- std/special/build_runner.zig | 5 +- std/time.zig | 284 +++++++++++-------------------- std/zig/bench.zig | 4 +- 27 files changed, 547 insertions(+), 581 deletions(-) (limited to 'std/debug.zig') diff --git a/std/atomic/queue.zig b/std/atomic/queue.zig index 431a96e64b..34cd0d22f8 100644 --- a/std/atomic/queue.zig +++ b/std/atomic/queue.zig @@ -220,7 +220,7 @@ fn startPuts(ctx: *Context) u8 { var put_count: usize = puts_per_thread; var r = std.rand.DefaultPrng.init(0xdeadbeef); while (put_count != 0) : (put_count -= 1) { - std.os.time.sleep(1); // let the os scheduler be our fuzz + std.time.sleep(1); // let the os scheduler be our fuzz const x = @bitCast(i32, r.random.scalar(u32)); const node = ctx.allocator.create(Queue(i32).Node) catch unreachable; node.* = Queue(i32).Node{ @@ -239,7 +239,7 @@ fn startGets(ctx: *Context) u8 { const last = @atomicLoad(u8, &ctx.puts_done, builtin.AtomicOrder.SeqCst) == 1; while (ctx.queue.get()) |node| { - std.os.time.sleep(1); // let the os scheduler be our fuzz + std.time.sleep(1); // let the os scheduler be our fuzz _ = @atomicRmw(isize, &ctx.get_sum, builtin.AtomicRmwOp.Add, node.data, builtin.AtomicOrder.SeqCst); _ = @atomicRmw(usize, &ctx.get_count, builtin.AtomicRmwOp.Add, 1, builtin.AtomicOrder.SeqCst); } diff --git a/std/atomic/stack.zig b/std/atomic/stack.zig index 8ae6c997aa..773bdf6f1b 100644 --- a/std/atomic/stack.zig +++ b/std/atomic/stack.zig @@ -154,7 +154,7 @@ fn startPuts(ctx: *Context) u8 { var put_count: usize = puts_per_thread; var r = std.rand.DefaultPrng.init(0xdeadbeef); while (put_count != 0) : (put_count -= 1) { - std.os.time.sleep(1); // let the os scheduler be our fuzz + std.time.sleep(1); // let the os scheduler be our fuzz const x = @bitCast(i32, r.random.scalar(u32)); const node = ctx.allocator.create(Stack(i32).Node) catch unreachable; node.* = Stack(i32).Node{ @@ -172,7 +172,7 @@ fn startGets(ctx: *Context) u8 { const last = @atomicLoad(u8, &ctx.puts_done, builtin.AtomicOrder.SeqCst) == 1; while (ctx.stack.pop()) |node| { - std.os.time.sleep(1); // let the os scheduler be our fuzz + std.time.sleep(1); // let the os scheduler be our fuzz _ = @atomicRmw(isize, &ctx.get_sum, builtin.AtomicRmwOp.Add, node.data, builtin.AtomicOrder.SeqCst); _ = @atomicRmw(usize, &ctx.get_count, builtin.AtomicRmwOp.Add, 1, builtin.AtomicOrder.SeqCst); } diff --git a/std/build.zig b/std/build.zig index ff64c6cb93..0f2d6093c4 100644 --- a/std/build.zig +++ b/std/build.zig @@ -14,6 +14,7 @@ const Term = os.ChildProcess.Term; const BufSet = std.BufSet; const BufMap = std.BufMap; const fmt_lib = std.fmt; +const File = std.fs.File; pub const FmtStep = @import("build/fmt.zig").FmtStep; @@ -668,10 +669,10 @@ pub const Builder = struct { } fn copyFile(self: *Builder, source_path: []const u8, dest_path: []const u8) !void { - return self.copyFileMode(source_path, dest_path, os.File.default_mode); + return self.copyFileMode(source_path, dest_path, File.default_mode); } - fn copyFileMode(self: *Builder, source_path: []const u8, dest_path: []const u8, mode: os.File.Mode) !void { + fn copyFileMode(self: *Builder, source_path: []const u8, dest_path: []const u8, mode: File.Mode) !void { if (self.verbose) { warn("cp {} {}\n", source_path, dest_path); } diff --git a/std/c.zig b/std/c.zig index 3a4ac32bab..c1398bccab 100644 --- a/std/c.zig +++ b/std/c.zig @@ -75,7 +75,7 @@ pub extern "c" fn sysctlnametomib(name: [*]const u8, mibp: ?*c_int, sizep: ?*usi pub extern "c" fn bind(socket: fd_t, address: ?*const sockaddr, address_len: socklen_t) c_int; pub extern "c" fn socket(domain: c_int, sock_type: c_int, protocol: c_int) c_int; pub extern "c" fn kill(pid: pid_t, sig: c_int) c_int; -pub extern "c" fn getdirentries(fd: fd_t, buf_ptr: [*]u8, nbytes: usize, basep: *i64) usize; +pub extern "c" fn getdirentries(fd: fd_t, buf_ptr: [*]u8, nbytes: usize, basep: *i64) isize; pub extern "c" fn openat(fd: c_int, path: [*]const u8, flags: c_int) c_int; pub extern "c" fn setgid(ruid: c_uint, euid: c_uint) c_int; pub extern "c" fn setuid(uid: c_uint) c_int; diff --git a/std/child_process.zig b/std/child_process.zig index 9a0f20d1c5..fa20a5e814 100644 --- a/std/child_process.zig +++ b/std/child_process.zig @@ -3,7 +3,7 @@ const cstr = std.cstr; const unicode = std.unicode; const io = std.io; const os = std.os; -const posix = os.posix; +const File = std.fs.File; const windows = os.windows; const mem = std.mem; const debug = std.debug; @@ -23,9 +23,9 @@ pub const ChildProcess = struct { pub allocator: *mem.Allocator, - pub stdin: ?os.File, - pub stdout: ?os.File, - pub stderr: ?os.File, + pub stdin: ?File, + pub stdout: ?File, + pub stderr: ?File, pub term: ?(SpawnError!Term), @@ -148,12 +148,7 @@ pub const ChildProcess = struct { return term; } - if (!windows.TerminateProcess(self.handle, exit_code)) { - const err = windows.GetLastError(); - return switch (err) { - else => os.unexpectedErrorWindows(err), - }; - } + try windows.TerminateProcess(self.handle, exit_code); try self.waitUnwrappedWindows(); return self.term.?; } @@ -163,16 +158,7 @@ pub const ChildProcess = struct { self.cleanupStreams(); return term; } - const ret = posix.kill(self.pid, posix.SIGTERM); - const err = posix.getErrno(ret); - if (err > 0) { - return switch (err) { - posix.EINVAL => unreachable, - posix.EPERM => error.PermissionDenied, - posix.ESRCH => error.ProcessNotFound, - else => os.unexpectedErrorPosix(err), - }; - } + try os.kill(self.pid, os.SIGTERM); self.waitUnwrapped(); return self.term.?; } @@ -267,19 +253,9 @@ pub const ChildProcess = struct { } fn waitUnwrapped(self: *ChildProcess) void { - var status: i32 = undefined; - while (true) { - const err = posix.getErrno(posix.waitpid(self.pid, &status, 0)); - if (err > 0) { - switch (err) { - posix.EINTR => continue, - else => unreachable, - } - } - self.cleanupStreams(); - self.handleWaitResult(status); - return; - } + const status = os.waitpid(self.pid, 0); + self.cleanupStreams(); + self.handleWaitResult(status); } fn handleWaitResult(self: *ChildProcess, status: i32) void { @@ -324,34 +300,34 @@ pub const ChildProcess = struct { } fn statusToTerm(status: i32) Term { - return if (posix.WIFEXITED(status)) - Term{ .Exited = posix.WEXITSTATUS(status) } - else if (posix.WIFSIGNALED(status)) - Term{ .Signal = posix.WTERMSIG(status) } - else if (posix.WIFSTOPPED(status)) - Term{ .Stopped = posix.WSTOPSIG(status) } + return if (os.WIFEXITED(status)) + Term{ .Exited = os.WEXITSTATUS(status) } + else if (os.WIFSIGNALED(status)) + Term{ .Signal = os.WTERMSIG(status) } + else if (os.WIFSTOPPED(status)) + Term{ .Stopped = os.WSTOPSIG(status) } else Term{ .Unknown = status }; } fn spawnPosix(self: *ChildProcess) !void { - const stdin_pipe = if (self.stdin_behavior == StdIo.Pipe) try makePipe() else undefined; + const stdin_pipe = if (self.stdin_behavior == StdIo.Pipe) try os.pipe() else undefined; errdefer if (self.stdin_behavior == StdIo.Pipe) { destroyPipe(stdin_pipe); }; - const stdout_pipe = if (self.stdout_behavior == StdIo.Pipe) try makePipe() else undefined; + const stdout_pipe = if (self.stdout_behavior == StdIo.Pipe) try os.pipe() else undefined; errdefer if (self.stdout_behavior == StdIo.Pipe) { destroyPipe(stdout_pipe); }; - const stderr_pipe = if (self.stderr_behavior == StdIo.Pipe) try makePipe() else undefined; + const stderr_pipe = if (self.stderr_behavior == StdIo.Pipe) try os.pipe() else undefined; errdefer if (self.stderr_behavior == StdIo.Pipe) { destroyPipe(stderr_pipe); }; const any_ignore = (self.stdin_behavior == StdIo.Ignore or self.stdout_behavior == StdIo.Ignore or self.stderr_behavior == StdIo.Ignore); - const dev_null_fd = if (any_ignore) try os.posixOpenC(c"/dev/null", posix.O_RDWR, 0) else undefined; + const dev_null_fd = if (any_ignore) try os.openC(c"/dev/null", os.O_RDWR, 0) else undefined; defer { if (any_ignore) os.close(dev_null_fd); } @@ -372,7 +348,7 @@ pub const ChildProcess = struct { // This pipe is used to communicate errors between the time of fork // and execve from the child process to the parent process. - const err_pipe = try makePipe(); + const err_pipe = try os.pipe(); errdefer destroyPipe(err_pipe); const pid_result = try posix.fork(); @@ -413,17 +389,17 @@ pub const ChildProcess = struct { // we are the parent const pid = @intCast(i32, pid_result); if (self.stdin_behavior == StdIo.Pipe) { - self.stdin = os.File.openHandle(stdin_pipe[1]); + self.stdin = File.openHandle(stdin_pipe[1]); } else { self.stdin = null; } if (self.stdout_behavior == StdIo.Pipe) { - self.stdout = os.File.openHandle(stdout_pipe[0]); + self.stdout = File.openHandle(stdout_pipe[0]); } else { self.stdout = null; } if (self.stderr_behavior == StdIo.Pipe) { - self.stderr = os.File.openHandle(stderr_pipe[0]); + self.stderr = File.openHandle(stderr_pipe[0]); } else { self.stderr = null; } @@ -608,17 +584,17 @@ pub const ChildProcess = struct { }; if (g_hChildStd_IN_Wr) |h| { - self.stdin = os.File.openHandle(h); + self.stdin = File.openHandle(h); } else { self.stdin = null; } if (g_hChildStd_OUT_Rd) |h| { - self.stdout = os.File.openHandle(h); + self.stdout = File.openHandle(h); } else { self.stdout = null; } if (g_hChildStd_ERR_Rd) |h| { - self.stderr = os.File.openHandle(h); + self.stderr = File.openHandle(h); } else { self.stderr = null; } @@ -751,18 +727,6 @@ fn windowsMakePipeOut(rd: *?windows.HANDLE, wr: *?windows.HANDLE, sattr: *const wr.* = wr_h; } -fn makePipe() ![2]i32 { - var fds: [2]i32 = undefined; - const err = posix.getErrno(posix.pipe(&fds)); - if (err > 0) { - return switch (err) { - posix.EMFILE, posix.ENFILE => error.SystemResources, - else => os.unexpectedErrorPosix(err), - }; - } - return fds; -} - fn destroyPipe(pipe: [2]i32) void { os.close(pipe[0]); os.close(pipe[1]); @@ -778,12 +742,12 @@ fn forkChildErrReport(fd: i32, err: ChildProcess.SpawnError) noreturn { const ErrInt = @IntType(false, @sizeOf(anyerror) * 8); fn writeIntFd(fd: i32, value: ErrInt) !void { - const stream = &os.File.openHandle(fd).outStream().stream; + const stream = &File.openHandle(fd).outStream().stream; stream.writeIntNative(ErrInt, value) catch return error.SystemResources; } fn readIntFd(fd: i32) !ErrInt { - const stream = &os.File.openHandle(fd).inStream().stream; + const stream = &File.openHandle(fd).inStream().stream; return stream.readIntNative(ErrInt) catch return error.SystemResources; } diff --git a/std/coff.zig b/std/coff.zig index b5b5733989..87f3f089de 100644 --- a/std/coff.zig +++ b/std/coff.zig @@ -3,6 +3,7 @@ const std = @import("std.zig"); const io = std.io; const mem = std.mem; const os = std.os; +const File = std.fs.File; const ArrayList = std.ArrayList; @@ -28,7 +29,7 @@ pub const CoffError = error{ }; pub const Coff = struct { - in_file: os.File, + in_file: File, allocator: *mem.Allocator, coff_header: CoffHeader, @@ -77,7 +78,7 @@ pub const Coff = struct { try self.loadOptionalHeader(&file_stream); } - fn loadOptionalHeader(self: *Coff, file_stream: *os.File.InStream) !void { + fn loadOptionalHeader(self: *Coff, file_stream: *File.InStream) !void { const in = &file_stream.stream; self.pe_header.magic = try in.readIntLittle(u16); // For now we're only interested in finding the reference to the .pdb, diff --git a/std/crypto/throughput_test.zig b/std/crypto/throughput_test.zig index 73a2a86124..aee06571a0 100644 --- a/std/crypto/throughput_test.zig +++ b/std/crypto/throughput_test.zig @@ -1,6 +1,6 @@ const builtin = @import("builtin"); const std = @import("std"); -const time = std.os.time; +const time = std.time; const Timer = time.Timer; const crypto = @import("../crypto.zig"); diff --git a/std/debug.zig b/std/debug.zig index e30efebdbc..83daad6dc5 100644 --- a/std/debug.zig +++ b/std/debug.zig @@ -12,6 +12,7 @@ const windows = os.windows; const ArrayList = std.ArrayList; const builtin = @import("builtin"); const maxInt = std.math.maxInt; +const File = std.fs.File; const leb = @import("debug/leb128.zig"); @@ -36,10 +37,10 @@ const Module = struct { /// Tries to write to stderr, unbuffered, and ignores any error returned. /// Does not append a newline. -var stderr_file: os.File = undefined; -var stderr_file_out_stream: os.File.OutStream = undefined; +var stderr_file: File = undefined; +var stderr_file_out_stream: File.OutStream = undefined; -var stderr_stream: ?*io.OutStream(os.File.WriteError) = null; +var stderr_stream: ?*io.OutStream(File.WriteError) = null; var stderr_mutex = std.Mutex.init(); pub fn warn(comptime fmt: []const u8, args: ...) void { const held = stderr_mutex.acquire(); @@ -48,7 +49,7 @@ pub fn warn(comptime fmt: []const u8, args: ...) void { stderr.print(fmt, args) catch return; } -pub fn getStderrStream() !*io.OutStream(os.File.WriteError) { +pub fn getStderrStream() !*io.OutStream(File.WriteError) { if (stderr_stream) |st| { return st; } else { @@ -1003,7 +1004,7 @@ pub fn openElfDebugInfo( fn openSelfDebugInfoLinux(allocator: *mem.Allocator) !DwarfInfo { const S = struct { - var self_exe_file: os.File = undefined; + var self_exe_file: File = undefined; var self_exe_mmap_seekable: io.SliceSeekableInStream = undefined; }; @@ -1112,7 +1113,7 @@ fn openSelfDebugInfoMacOs(allocator: *mem.Allocator) !DebugInfo { } fn printLineFromFileAnyOs(out_stream: var, line_info: LineInfo) !void { - var f = try os.File.openRead(line_info.file_name); + var f = try File.openRead(line_info.file_name); defer f.close(); // TODO fstat and make sure that the file has the correct size diff --git a/std/dynamic_library.zig b/std/dynamic_library.zig index 14bde98f1a..2832b54dbb 100644 --- a/std/dynamic_library.zig +++ b/std/dynamic_library.zig @@ -1,5 +1,4 @@ const builtin = @import("builtin"); -const Os = builtin.Os; const std = @import("std.zig"); const mem = std.mem; @@ -8,14 +7,13 @@ const os = std.os; const assert = std.debug.assert; const testing = std.testing; const elf = std.elf; -const linux = os.linux; const windows = os.windows; const win_util = @import("os/windows/util.zig"); const maxInt = std.math.maxInt; pub const DynLib = switch (builtin.os) { - Os.linux => LinuxDynLib, - Os.windows => WindowsDynLib, + .linux => LinuxDynLib, + .windows => WindowsDynLib, else => void, }; @@ -110,20 +108,20 @@ pub const LinuxDynLib = struct { /// Trusts the file pub fn open(allocator: *mem.Allocator, path: []const u8) !DynLib { - const fd = try std.os.posixOpen(path, 0, linux.O_RDONLY | linux.O_CLOEXEC); + const fd = try os.open(path, 0, os.O_RDONLY | os.O_CLOEXEC); errdefer std.os.close(fd); const size = @intCast(usize, (try std.os.posixFStat(fd)).size); - const addr = linux.mmap( + const addr = os.mmap( null, size, - linux.PROT_READ | linux.PROT_EXEC, - linux.MAP_PRIVATE | linux.MAP_LOCKED, + os.PROT_READ | os.PROT_EXEC, + os.MAP_PRIVATE | os.MAP_LOCKED, fd, 0, ); - errdefer _ = linux.munmap(addr, size); + errdefer os.munmap(addr, size); const bytes = @intToPtr([*]align(mem.page_size) u8, addr)[0..size]; @@ -136,7 +134,7 @@ pub const LinuxDynLib = struct { } pub fn close(self: *DynLib) void { - _ = linux.munmap(self.map_addr, self.map_size); + os.munmap(self.map_addr, self.map_size); std.os.close(self.fd); self.* = undefined; } @@ -149,7 +147,7 @@ pub const LinuxDynLib = struct { pub const ElfLib = struct { strings: [*]u8, syms: [*]elf.Sym, - hashtab: [*]linux.Elf_Symndx, + hashtab: [*]os.Elf_Symndx, versym: ?[*]u16, verdef: ?*elf.Verdef, base: usize, @@ -184,7 +182,7 @@ pub const ElfLib = struct { var maybe_strings: ?[*]u8 = null; var maybe_syms: ?[*]elf.Sym = null; - var maybe_hashtab: ?[*]linux.Elf_Symndx = null; + var maybe_hashtab: ?[*]os.Elf_Symndx = null; var maybe_versym: ?[*]u16 = null; var maybe_verdef: ?*elf.Verdef = null; @@ -195,7 +193,7 @@ pub const ElfLib = struct { switch (dynv[i]) { elf.DT_STRTAB => maybe_strings = @intToPtr([*]u8, p), elf.DT_SYMTAB => maybe_syms = @intToPtr([*]elf.Sym, p), - elf.DT_HASH => maybe_hashtab = @intToPtr([*]linux.Elf_Symndx, p), + elf.DT_HASH => maybe_hashtab = @intToPtr([*]os.Elf_Symndx, p), elf.DT_VERSYM => maybe_versym = @intToPtr([*]u16, p), elf.DT_VERDEF => maybe_verdef = @intToPtr(*elf.Verdef, p), else => {}, @@ -283,8 +281,8 @@ pub const WindowsDynLib = struct { test "dynamic_library" { const libname = switch (builtin.os) { - Os.linux => "invalid_so.so", - Os.windows => "invalid_dll.dll", + .linux => "invalid_so.so", + .windows => "invalid_dll.dll", else => return, }; diff --git a/std/elf.zig b/std/elf.zig index 898f9d83f8..c605a177a5 100644 --- a/std/elf.zig +++ b/std/elf.zig @@ -6,6 +6,7 @@ const math = std.math; const mem = std.mem; const debug = std.debug; const InStream = std.stream.InStream; +const File = std.fs.File; pub const AT_NULL = 0; pub const AT_IGNORE = 1; @@ -367,7 +368,7 @@ pub const Elf = struct { string_section: *SectionHeader, section_headers: []SectionHeader, allocator: *mem.Allocator, - prealloc_file: os.File, + prealloc_file: File, /// Call close when done. pub fn openPath(elf: *Elf, allocator: *mem.Allocator, path: []const u8) !void { @@ -375,7 +376,7 @@ pub const Elf = struct { } /// Call close when done. - pub fn openFile(elf: *Elf, allocator: *mem.Allocator, file: os.File) !void { + pub fn openFile(elf: *Elf, allocator: *mem.Allocator, file: File) !void { @compileError("TODO implement"); } diff --git a/std/event/fs.zig b/std/event/fs.zig index f3118fe97a..346a6c6b69 100644 --- a/std/event/fs.zig +++ b/std/event/fs.zig @@ -9,6 +9,7 @@ const posix = os.posix; const windows = os.windows; const Loop = event.Loop; const fd_t = posix.fd_t; +const File = std.fs.File; pub const RequestNode = std.atomic.Queue(Request).Node; @@ -52,20 +53,20 @@ pub const Request = struct { /// must be null terminated. TODO https://github.com/ziglang/zig/issues/265 path: []const u8, flags: u32, - mode: os.File.Mode, + mode: File.Mode, result: Error!fd_t, - pub const Error = os.File.OpenError; + pub const Error = File.OpenError; }; pub const WriteFile = struct { /// must be null terminated. TODO https://github.com/ziglang/zig/issues/265 path: []const u8, contents: []const u8, - mode: os.File.Mode, + mode: File.Mode, result: Error!void, - pub const Error = os.File.OpenError || os.File.WriteError; + pub const Error = File.OpenError || File.WriteError; }; pub const Close = struct { @@ -74,7 +75,7 @@ pub const Request = struct { }; }; -pub const PWriteVError = error{OutOfMemory} || os.File.WriteError; +pub const PWriteVError = error{OutOfMemory} || File.WriteError; /// data - just the inner references - must live until pwritev promise completes. pub async fn pwritev(loop: *Loop, fd: fd_t, data: []const []const u8, offset: usize) PWriteVError!void { @@ -209,7 +210,7 @@ pub async fn pwritevPosix( return req_node.data.msg.PWriteV.result; } -pub const PReadVError = error{OutOfMemory} || os.File.ReadError; +pub const PReadVError = error{OutOfMemory} || File.ReadError; /// data - just the inner references - must live until preadv promise completes. pub async fn preadv(loop: *Loop, fd: fd_t, data: []const []u8, offset: usize) PReadVError!usize { @@ -361,8 +362,8 @@ pub async fn openPosix( loop: *Loop, path: []const u8, flags: u32, - mode: os.File.Mode, -) os.File.OpenError!fd_t { + mode: File.Mode, +) File.OpenError!fd_t { // workaround for https://github.com/ziglang/zig/issues/1194 suspend { resume @handle(); @@ -401,11 +402,11 @@ pub async fn openPosix( return req_node.data.msg.Open.result; } -pub async fn openRead(loop: *Loop, path: []const u8) os.File.OpenError!fd_t { +pub async fn openRead(loop: *Loop, path: []const u8) File.OpenError!fd_t { switch (builtin.os) { builtin.Os.macosx, builtin.Os.linux, builtin.Os.freebsd, builtin.Os.netbsd => { const flags = posix.O_LARGEFILE | posix.O_RDONLY | posix.O_CLOEXEC; - return await (async openPosix(loop, path, flags, os.File.default_mode) catch unreachable); + return await (async openPosix(loop, path, flags, File.default_mode) catch unreachable); }, builtin.Os.windows => return os.windowsOpen( @@ -422,12 +423,12 @@ pub async fn openRead(loop: *Loop, path: []const u8) os.File.OpenError!fd_t { /// Creates if does not exist. Truncates the file if it exists. /// Uses the default mode. -pub async fn openWrite(loop: *Loop, path: []const u8) os.File.OpenError!fd_t { - return await (async openWriteMode(loop, path, os.File.default_mode) catch unreachable); +pub async fn openWrite(loop: *Loop, path: []const u8) File.OpenError!fd_t { + return await (async openWriteMode(loop, path, File.default_mode) catch unreachable); } /// Creates if does not exist. Truncates the file if it exists. -pub async fn openWriteMode(loop: *Loop, path: []const u8, mode: os.File.Mode) os.File.OpenError!fd_t { +pub async fn openWriteMode(loop: *Loop, path: []const u8, mode: File.Mode) File.OpenError!fd_t { switch (builtin.os) { builtin.Os.macosx, builtin.Os.linux, @@ -435,7 +436,7 @@ pub async fn openWriteMode(loop: *Loop, path: []const u8, mode: os.File.Mode) os builtin.Os.netbsd, => { const flags = posix.O_LARGEFILE | posix.O_WRONLY | posix.O_CREAT | posix.O_CLOEXEC | posix.O_TRUNC; - return await (async openPosix(loop, path, flags, os.File.default_mode) catch unreachable); + return await (async openPosix(loop, path, flags, File.default_mode) catch unreachable); }, builtin.Os.windows => return os.windowsOpen( path, @@ -452,8 +453,8 @@ pub async fn openWriteMode(loop: *Loop, path: []const u8, mode: os.File.Mode) os pub async fn openReadWrite( loop: *Loop, path: []const u8, - mode: os.File.Mode, -) os.File.OpenError!fd_t { + mode: File.Mode, +) File.OpenError!fd_t { switch (builtin.os) { builtin.Os.macosx, builtin.Os.linux, builtin.Os.freebsd, builtin.Os.netbsd => { const flags = posix.O_LARGEFILE | posix.O_RDWR | posix.O_CREAT | posix.O_CLOEXEC; @@ -605,11 +606,11 @@ pub const CloseOperation = struct { /// contents must remain alive until writeFile completes. /// TODO make this atomic or provide writeFileAtomic and rename this one to writeFileTruncate pub async fn writeFile(loop: *Loop, path: []const u8, contents: []const u8) !void { - return await (async writeFileMode(loop, path, contents, os.File.default_mode) catch unreachable); + return await (async writeFileMode(loop, path, contents, File.default_mode) catch unreachable); } /// contents must remain alive until writeFile completes. -pub async fn writeFileMode(loop: *Loop, path: []const u8, contents: []const u8, mode: os.File.Mode) !void { +pub async fn writeFileMode(loop: *Loop, path: []const u8, contents: []const u8, mode: File.Mode) !void { switch (builtin.os) { builtin.Os.linux, builtin.Os.macosx, @@ -634,7 +635,7 @@ async fn writeFileWindows(loop: *Loop, path: []const u8, contents: []const u8) ! try await (async pwriteWindows(loop, handle, contents, 0) catch unreachable); } -async fn writeFileModeThread(loop: *Loop, path: []const u8, contents: []const u8, mode: os.File.Mode) !void { +async fn writeFileModeThread(loop: *Loop, path: []const u8, contents: []const u8, mode: File.Mode) !void { // workaround for https://github.com/ziglang/zig/issues/1194 suspend { resume @handle(); @@ -1363,7 +1364,7 @@ async fn testFsWatch(loop: *Loop) !void { defer if (!ev_consumed) cancel ev; // overwrite line 2 - const fd = try await try async openReadWrite(loop, file_path, os.File.default_mode); + const fd = try await try async openReadWrite(loop, file_path, File.default_mode); { defer os.close(fd); @@ -1390,7 +1391,7 @@ pub const OutStream = struct { loop: *Loop, offset: usize, - pub const Error = os.File.WriteError; + pub const Error = File.WriteError; pub const Stream = event.io.OutStream(Error); pub fn init(loop: *Loop, fd: fd_t, offset: usize) OutStream { diff --git a/std/event/group.zig b/std/event/group.zig index 455d1bd60c..143efd76c3 100644 --- a/std/event/group.zig +++ b/std/event/group.zig @@ -155,7 +155,7 @@ async fn testGroup(loop: *Loop) void { } async fn sleepALittle(count: *usize) void { - std.os.time.sleep(1 * std.os.time.millisecond); + std.time.sleep(1 * std.time.millisecond); _ = @atomicRmw(usize, count, AtomicRmwOp.Add, 1, AtomicOrder.SeqCst); } diff --git a/std/event/loop.zig b/std/event/loop.zig index 76b1f6455b..71bc45d48f 100644 --- a/std/event/loop.zig +++ b/std/event/loop.zig @@ -789,13 +789,13 @@ pub const Loop = struct { msg.result = os.posix_preadv(msg.fd, msg.iov.ptr, msg.iov.len, msg.offset); }, @TagType(fs.Request.Msg).Open => |*msg| { - msg.result = os.posixOpenC(msg.path.ptr, msg.flags, msg.mode); + msg.result = os.openC(msg.path.ptr, msg.flags, msg.mode); }, @TagType(fs.Request.Msg).Close => |*msg| os.close(msg.fd), @TagType(fs.Request.Msg).WriteFile => |*msg| blk: { const flags = posix.O_LARGEFILE | posix.O_WRONLY | posix.O_CREAT | posix.O_CLOEXEC | posix.O_TRUNC; - const fd = os.posixOpenC(msg.path.ptr, flags, msg.mode) catch |err| { + const fd = os.openC(msg.path.ptr, flags, msg.mode) catch |err| { msg.result = err; break :blk; }; diff --git a/std/event/net.zig b/std/event/net.zig index d4b79d6872..bb492f1715 100644 --- a/std/event/net.zig +++ b/std/event/net.zig @@ -6,11 +6,12 @@ const mem = std.mem; const os = std.os; const posix = os.posix; const Loop = std.event.Loop; +const File = std.fs.File; const fd_t = posix.fd_t; pub const Server = struct { - handleRequestFn: async<*mem.Allocator> fn (*Server, *const std.net.Address, os.File) void, + handleRequestFn: async<*mem.Allocator> fn (*Server, *const std.net.Address, File) void, loop: *Loop, sockfd: ?i32, @@ -42,7 +43,7 @@ pub const Server = struct { pub fn listen( self: *Server, address: *const std.net.Address, - handleRequestFn: async<*mem.Allocator> fn (*Server, *const std.net.Address, os.File) void, + handleRequestFn: async<*mem.Allocator> fn (*Server, *const std.net.Address, File) void, ) !void { self.handleRequestFn = handleRequestFn; @@ -83,7 +84,7 @@ pub const Server = struct { suspend; // we will get resumed by epoll_wait in the event loop continue; } - var socket = os.File.openHandle(accepted_fd); + var socket = File.openHandle(accepted_fd); _ = async self.handleRequestFn(self, &accepted_addr, socket) catch |err| switch (err) { error.OutOfMemory => { socket.close(); @@ -250,7 +251,7 @@ pub async fn readv(loop: *Loop, fd: fd_t, data: []const []u8) !usize { return await (async readvPosix(loop, fd, iovecs.ptr, data.len) catch unreachable); } -pub async fn connect(loop: *Loop, _address: *const std.net.Address) !os.File { +pub async fn connect(loop: *Loop, _address: *const std.net.Address) !File { var address = _address.*; // TODO https://github.com/ziglang/zig/issues/1592 const sockfd = try os.posixSocket(posix.AF_INET, posix.SOCK_STREAM | posix.SOCK_CLOEXEC | posix.SOCK_NONBLOCK, posix.PROTO_tcp); @@ -260,7 +261,7 @@ pub async fn connect(loop: *Loop, _address: *const std.net.Address) !os.File { try await try async loop.linuxWaitFd(sockfd, posix.EPOLLIN | posix.EPOLLOUT | posix.EPOLLET); try os.posixGetSockOptConnectError(sockfd); - return os.File.openHandle(sockfd); + return File.openHandle(sockfd); } test "listen on a port, send bytes, receive bytes" { @@ -276,7 +277,7 @@ test "listen on a port, send bytes, receive bytes" { tcp_server: Server, const Self = @This(); - async<*mem.Allocator> fn handler(tcp_server: *Server, _addr: *const std.net.Address, _socket: os.File) void { + async<*mem.Allocator> fn handler(tcp_server: *Server, _addr: *const std.net.Address, _socket: File) void { const self = @fieldParentPtr(Self, "tcp_server", tcp_server); var socket = _socket; // TODO https://github.com/ziglang/zig/issues/1592 defer socket.close(); @@ -289,7 +290,7 @@ test "listen on a port, send bytes, receive bytes" { cancel @handle(); } } - async fn errorableHandler(self: *Self, _addr: *const std.net.Address, _socket: os.File) !void { + async fn errorableHandler(self: *Self, _addr: *const std.net.Address, _socket: File) !void { const addr = _addr.*; // TODO https://github.com/ziglang/zig/issues/1592 var socket = _socket; // TODO https://github.com/ziglang/zig/issues/1592 diff --git a/std/event/rwlock.zig b/std/event/rwlock.zig index 76b364fedc..00f3c0bc60 100644 --- a/std/event/rwlock.zig +++ b/std/event/rwlock.zig @@ -271,7 +271,7 @@ async fn writeRunner(lock: *RwLock) void { var i: usize = 0; while (i < shared_test_data.len) : (i += 1) { - std.os.time.sleep(100 * std.os.time.microsecond); + std.time.sleep(100 * std.time.microsecond); const lock_promise = async lock.acquireWrite() catch @panic("out of memory"); const handle = await lock_promise; defer handle.release(); @@ -286,7 +286,7 @@ async fn writeRunner(lock: *RwLock) void { async fn readRunner(lock: *RwLock) void { suspend; // resumed by onNextTick - std.os.time.sleep(1); + std.time.sleep(1); var i: usize = 0; while (i < shared_test_data.len) : (i += 1) { diff --git a/std/fs.zig b/std/fs.zig index 50d0b0df70..91e4aca824 100644 --- a/std/fs.zig +++ b/std/fs.zig @@ -11,6 +11,7 @@ pub const deleteFile = os.unlink; pub const deleteFileC = os.unlinkC; pub const rename = os.rename; pub const renameC = os.renameC; +pub const renameW = os.renameW; pub const changeCurDir = os.chdir; pub const changeCurDirC = os.chdirC; pub const realpath = os.realpath; @@ -25,24 +26,24 @@ pub const GetAppDataDirError = @import("fs/get_app_data_dir.zig").GetAppDataDirE /// fit into a UTF-8 encoded array of this length. /// path being too long if it is this 0long pub const MAX_PATH_BYTES = switch (builtin.os) { - .linux, .macosx, .ios, .freebsd, .netbsd => posix.PATH_MAX, + .linux, .macosx, .ios, .freebsd, .netbsd => 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. // +1 for the null byte at the end, which can be encoded in 1 byte. - .windows => posix.PATH_MAX_WIDE * 3 + 1, + .windows => os.windows.PATH_MAX_WIDE * 3 + 1, else => @compileError("Unsupported OS"), }; /// The result is a slice of `out_buffer`, from index `0`. pub fn getCwd(out_buffer: *[MAX_PATH_BYTES]u8) ![]u8 { - return posix.getcwd(out_buffer); + return os.getcwd(out_buffer); } /// Caller must free the returned memory. pub fn getCwdAlloc(allocator: *Allocator) ![]u8 { var buf: [MAX_PATH_BYTES]u8 = undefined; - return mem.dupe(allocator, u8, try posix.getcwd(&buf)); + return mem.dupe(allocator, u8, try os.getcwd(&buf)); } test "getCwdAlloc" { @@ -90,7 +91,7 @@ pub fn atomicSymLink(allocator: *Allocator, existing_path: []const u8, new_path: /// in the same directory as dest_path. /// Destination file will have the same mode as the source file. pub fn copyFile(source_path: []const u8, dest_path: []const u8) !void { - var in_file = try os.File.openRead(source_path); + var in_file = try File.openRead(source_path); defer in_file.close(); const mode = try in_file.mode(); @@ -113,7 +114,7 @@ pub fn copyFile(source_path: []const u8, dest_path: []const u8) !void { /// merged and readily available, /// there is a possibility of power loss or application termination leaving temporary files present pub fn copyFileMode(source_path: []const u8, dest_path: []const u8, mode: File.Mode) !void { - var in_file = try os.File.openRead(source_path); + var in_file = try File.openRead(source_path); defer in_file.close(); var atomic_file = try AtomicFile.init(dest_path, mode); @@ -130,12 +131,12 @@ pub fn copyFileMode(source_path: []const u8, dest_path: []const u8, mode: File.M } pub const AtomicFile = struct { - file: os.File, + file: File, tmp_path_buf: [MAX_PATH_BYTES]u8, dest_path: []const u8, finished: bool, - const InitError = os.File.OpenError; + const InitError = File.OpenError; /// dest_path must remain valid for the lifetime of AtomicFile /// call finish to atomically replace dest_path with contents @@ -161,7 +162,7 @@ pub const AtomicFile = struct { try getRandomBytes(rand_buf[0..]); b64_fs_encoder.encode(tmp_path_buf[dirname_component_len..tmp_path_len], rand_buf); - const file = os.File.openWriteNoClobberC(&tmp_path_buf, mode) catch |err| switch (err) { + const file = File.openWriteNoClobberC(&tmp_path_buf, mode) catch |err| switch (err) { error.PathAlreadyExists => continue, // TODO zig should figure out that this error set does not include PathAlreadyExists since // it is handled in the above switch @@ -190,16 +191,13 @@ pub const AtomicFile = struct { assert(!self.finished); self.file.close(); self.finished = true; - if (is_posix) { - const dest_path_c = try toPosixPath(self.dest_path); - return renameC(&self.tmp_path_buf, &dest_path_c); - } else if (is_windows) { - const dest_path_w = try posix.sliceToPrefixedFileW(self.dest_path); - const tmp_path_w = try posix.cStrToPrefixedFileW(&self.tmp_path_buf); - return renameW(&tmp_path_w, &dest_path_w); - } else { - @compileError("Unsupported OS"); + if (os.windows.is_the_target) { + const dest_path_w = try os.windows.sliceToPrefixedFileW(self.dest_path); + const tmp_path_w = try os.windows.cStrToPrefixedFileW(&self.tmp_path_buf); + return os.renameW(&tmp_path_w, &dest_path_w); } + const dest_path_c = try os.toPosixPath(self.dest_path); + return os.renameC(&self.tmp_path_buf, &dest_path_c); } }; @@ -207,17 +205,17 @@ const default_new_dir_mode = 0o755; /// Create a new directory. pub fn makeDir(dir_path: []const u8) !void { - return posix.mkdir(dir_path, default_new_dir_mode); + return os.mkdir(dir_path, default_new_dir_mode); } /// Same as `makeDir` except the parameter is a null-terminated UTF8-encoded string. pub fn makeDirC(dir_path: [*]const u8) !void { - return posix.mkdirC(dir_path, default_new_dir_mode); + return os.mkdirC(dir_path, default_new_dir_mode); } /// Same as `makeDir` except the parameter is a null-terminated UTF16LE-encoded string. pub fn makeDirW(dir_path: [*]const u16) !void { - return posix.mkdirW(dir_path, default_new_dir_mode); + return os.mkdirW(dir_path, default_new_dir_mode); } /// Calls makeDir recursively to make an entire path. Returns success if the path @@ -260,17 +258,17 @@ pub fn makePath(allocator: *Allocator, full_path: []const u8) !void { /// Returns `error.DirNotEmpty` if the directory is not empty. /// To delete a directory recursively, see `deleteTree`. pub fn deleteDir(dir_path: []const u8) DeleteDirError!void { - return posix.rmdir(dir_path); + return os.rmdir(dir_path); } /// Same as `deleteDir` except the parameter is a null-terminated UTF8-encoded string. pub fn deleteDirC(dir_path: [*]const u8) DeleteDirError!void { - return posix.rmdirC(dir_path); + return os.rmdirC(dir_path); } /// Same as `deleteDir` except the parameter is a null-terminated UTF16LE-encoded string. pub fn deleteDirW(dir_path: [*]const u16) DeleteDirError!void { - return posix.rmdirW(dir_path); + return os.rmdirW(dir_path); } /// Whether ::full_path describes a symlink, file, or directory, this function @@ -383,22 +381,22 @@ pub const Dir = struct { allocator: *Allocator, pub const Handle = switch (builtin.os) { - Os.macosx, Os.ios, Os.freebsd, Os.netbsd => struct { + .macosx, .ios, .freebsd, .netbsd => struct { fd: i32, seek: i64, buf: []u8, index: usize, end_index: usize, }, - Os.linux => struct { + .linux => struct { fd: i32, buf: []u8, index: usize, end_index: usize, }, - Os.windows => struct { - handle: windows.HANDLE, - find_file_data: windows.WIN32_FIND_DATAW, + .windows => struct { + handle: os.windows.HANDLE, + find_file_data: os.windows.WIN32_FIND_DATAW, first: bool, name_data: [256]u8, }, @@ -449,9 +447,9 @@ pub const Dir = struct { return Dir{ .allocator = allocator, .handle = switch (builtin.os) { - Os.windows => blk: { - var find_file_data: windows.WIN32_FIND_DATAW = undefined; - const handle = try windows_util.windowsFindFirstFile(dir_path, &find_file_data); + .windows => blk: { + var find_file_data: os.windows.WIN32_FIND_DATAW = undefined; + const handle = try os.windows.FindFirstFile(dir_path, &find_file_data); break :blk Handle{ .handle = handle, .find_file_data = find_file_data, // TODO guaranteed copy elision @@ -459,23 +457,15 @@ pub const Dir = struct { .name_data = undefined, }; }, - Os.macosx, Os.ios, Os.freebsd, Os.netbsd => Handle{ - .fd = try posixOpen( - dir_path, - posix.O_RDONLY | posix.O_NONBLOCK | posix.O_DIRECTORY | posix.O_CLOEXEC, - 0, - ), + .macosx, .ios, .freebsd, .netbsd => Handle{ + .fd = try os.open(dir_path, os.O_RDONLY | os.O_NONBLOCK | os.O_DIRECTORY | os.O_CLOEXEC, 0), .seek = 0, .index = 0, .end_index = 0, .buf = []u8{}, }, - Os.linux => Handle{ - .fd = try posixOpen( - dir_path, - posix.O_RDONLY | posix.O_DIRECTORY | posix.O_CLOEXEC, - 0, - ), + .linux => Handle{ + .fd = try os.open(dir_path, os.O_RDONLY | os.O_DIRECTORY | os.O_CLOEXEC, 0), .index = 0, .end_index = 0, .buf = []u8{}, @@ -486,27 +476,22 @@ pub const Dir = struct { } pub fn close(self: *Dir) void { - switch (builtin.os) { - Os.windows => { - _ = windows.FindClose(self.handle.handle); - }, - Os.macosx, Os.ios, Os.linux, Os.freebsd, Os.netbsd => { - self.allocator.free(self.handle.buf); - os.close(self.handle.fd); - }, - else => @compileError("unimplemented"), + if (os.windows.is_the_target) { + return os.windows.FindClose(self.handle.handle); } + self.allocator.free(self.handle.buf); + os.close(self.handle.fd); } /// Memory such as file names referenced in this returned entry becomes invalid /// with subsequent calls to next, as well as when this `Dir` is deinitialized. pub fn next(self: *Dir) !?Entry { switch (builtin.os) { - Os.linux => return self.nextLinux(), - Os.macosx, Os.ios => return self.nextDarwin(), - Os.windows => return self.nextWindows(), - Os.freebsd => return self.nextFreebsd(), - Os.netbsd => return self.nextFreebsd(), + .linux => return self.nextLinux(), + .macosx, .ios => return self.nextDarwin(), + .windows => return self.nextWindows(), + .freebsd => return self.nextBsd(), + .netbsd => return self.nextBsd(), else => @compileError("unimplemented"), } } @@ -519,18 +504,23 @@ pub const Dir = struct { } while (true) { - const result = system.__getdirentries64(self.handle.fd, self.handle.buf.ptr, self.handle.buf.len, &self.handle.seek); - if (result == 0) return null; - if (result < 0) { - switch (system.getErrno(result)) { - posix.EBADF => unreachable, - posix.EFAULT => unreachable, - posix.ENOTDIR => unreachable, - posix.EINVAL => { + const rc = os.system.__getdirentries64( + self.handle.fd, + self.handle.buf.ptr, + self.handle.buf.len, + &self.handle.seek, + ); + if (rc == 0) return null; + if (rc < 0) { + switch (os.errno(rc)) { + os.EBADF => unreachable, + os.EFAULT => unreachable, + os.ENOTDIR => unreachable, + os.EINVAL => { self.handle.buf = try self.allocator.realloc(self.handle.buf, self.handle.buf.len * 2); continue; }, - else => return unexpectedErrorPosix(err), + else => |err| return os.unexpectedErrno(err), } } self.handle.index = 0; @@ -538,7 +528,7 @@ pub const Dir = struct { break; } } - const darwin_entry = @ptrCast(*align(1) posix.dirent, &self.handle.buf[self.handle.index]); + const darwin_entry = @ptrCast(*align(1) os.dirent, &self.handle.buf[self.handle.index]); const next_index = self.handle.index + darwin_entry.d_reclen; self.handle.index = next_index; @@ -549,14 +539,14 @@ pub const Dir = struct { } const entry_kind = switch (darwin_entry.d_type) { - posix.DT_BLK => Entry.Kind.BlockDevice, - posix.DT_CHR => Entry.Kind.CharacterDevice, - posix.DT_DIR => Entry.Kind.Directory, - posix.DT_FIFO => Entry.Kind.NamedPipe, - posix.DT_LNK => Entry.Kind.SymLink, - posix.DT_REG => Entry.Kind.File, - posix.DT_SOCK => Entry.Kind.UnixDomainSocket, - posix.DT_WHT => Entry.Kind.Whiteout, + os.DT_BLK => Entry.Kind.BlockDevice, + os.DT_CHR => Entry.Kind.CharacterDevice, + os.DT_DIR => Entry.Kind.Directory, + os.DT_FIFO => Entry.Kind.NamedPipe, + os.DT_LNK => Entry.Kind.SymLink, + os.DT_REG => Entry.Kind.File, + os.DT_SOCK => Entry.Kind.UnixDomainSocket, + os.DT_WHT => Entry.Kind.Whiteout, else => Entry.Kind.Unknown, }; return Entry{ @@ -571,7 +561,7 @@ pub const Dir = struct { if (self.handle.first) { self.handle.first = false; } else { - if (!try posix.FindNextFile(self.handle.handle, &self.handle.find_file_data)) + if (!try os.windows.FindNextFile(self.handle.handle, &self.handle.find_file_data)) return null; } const name_utf16le = mem.toSlice(u16, self.handle.find_file_data.cFileName[0..].ptr); @@ -582,9 +572,9 @@ pub const Dir = struct { const name_utf8 = self.handle.name_data[0..name_utf8_len]; const kind = blk: { const attrs = self.handle.find_file_data.dwFileAttributes; - if (attrs & windows.FILE_ATTRIBUTE_DIRECTORY != 0) break :blk Entry.Kind.Directory; - if (attrs & windows.FILE_ATTRIBUTE_REPARSE_POINT != 0) break :blk Entry.Kind.SymLink; - if (attrs & windows.FILE_ATTRIBUTE_NORMAL != 0) break :blk Entry.Kind.File; + if (attrs & os.windows.FILE_ATTRIBUTE_DIRECTORY != 0) break :blk Entry.Kind.Directory; + if (attrs & os.windows.FILE_ATTRIBUTE_REPARSE_POINT != 0) break :blk Entry.Kind.SymLink; + if (attrs & os.windows.FILE_ATTRIBUTE_NORMAL != 0) break :blk Entry.Kind.File; break :blk Entry.Kind.Unknown; }; return Entry{ @@ -602,25 +592,25 @@ pub const Dir = struct { } while (true) { - const result = posix.getdents64(self.handle.fd, self.handle.buf.ptr, self.handle.buf.len); - const err = posix.getErrno(result); - if (err > 0) { - switch (err) { - posix.EBADF, posix.EFAULT, posix.ENOTDIR => unreachable, - posix.EINVAL => { - self.handle.buf = try self.allocator.realloc(self.handle.buf, self.handle.buf.len * 2); - continue; - }, - else => return unexpectedErrorPosix(err), - } + const rc = os.system.getdents64(self.handle.fd, self.handle.buf.ptr, self.handle.buf.len); + switch (os.errno(rc)) { + 0 => {}, + os.EBADF => unreachable, + os.EFAULT => unreachable, + os.ENOTDIR => unreachable, + os.EINVAL => { + self.handle.buf = try self.allocator.realloc(self.handle.buf, self.handle.buf.len * 2); + continue; + }, + else => |err| return os.unexpectedErrno(err), } - if (result == 0) return null; + if (rc == 0) return null; self.handle.index = 0; - self.handle.end_index = result; + self.handle.end_index = rc; break; } } - const linux_entry = @ptrCast(*align(1) posix.dirent64, &self.handle.buf[self.handle.index]); + const linux_entry = @ptrCast(*align(1) os.dirent64, &self.handle.buf[self.handle.index]); const next_index = self.handle.index + linux_entry.d_reclen; self.handle.index = next_index; @@ -632,13 +622,13 @@ pub const Dir = struct { } const entry_kind = switch (linux_entry.d_type) { - posix.DT_BLK => Entry.Kind.BlockDevice, - posix.DT_CHR => Entry.Kind.CharacterDevice, - posix.DT_DIR => Entry.Kind.Directory, - posix.DT_FIFO => Entry.Kind.NamedPipe, - posix.DT_LNK => Entry.Kind.SymLink, - posix.DT_REG => Entry.Kind.File, - posix.DT_SOCK => Entry.Kind.UnixDomainSocket, + os.DT_BLK => Entry.Kind.BlockDevice, + os.DT_CHR => Entry.Kind.CharacterDevice, + os.DT_DIR => Entry.Kind.Directory, + os.DT_FIFO => Entry.Kind.NamedPipe, + os.DT_LNK => Entry.Kind.SymLink, + os.DT_REG => Entry.Kind.File, + os.DT_SOCK => Entry.Kind.UnixDomainSocket, else => Entry.Kind.Unknown, }; return Entry{ @@ -648,7 +638,7 @@ pub const Dir = struct { } } - fn nextFreebsd(self: *Dir) !?Entry { + fn nextBsd(self: *Dir) !?Entry { start_over: while (true) { if (self.handle.index >= self.handle.end_index) { if (self.handle.buf.len == 0) { @@ -656,25 +646,30 @@ pub const Dir = struct { } while (true) { - const result = posix.getdirentries(self.handle.fd, self.handle.buf.ptr, self.handle.buf.len, &self.handle.seek); - const err = posix.getErrno(result); - if (err > 0) { - switch (err) { - posix.EBADF, posix.EFAULT, posix.ENOTDIR => unreachable, - posix.EINVAL => { - self.handle.buf = try self.allocator.realloc(self.handle.buf, self.handle.buf.len * 2); - continue; - }, - else => return unexpectedErrorPosix(err), - } + const rc = os.system.getdirentries( + self.handle.fd, + self.handle.buf.ptr, + self.handle.buf.len, + &self.handle.seek, + ); + switch (os.errno(rc)) { + 0 => {}, + os.EBADF => unreachable, + os.EFAULT => unreachable, + os.ENOTDIR => unreachable, + os.EINVAL => { + self.handle.buf = try self.allocator.realloc(self.handle.buf, self.handle.buf.len * 2); + continue; + }, + else => |err| return os.unexpectedErrno(err), } - if (result == 0) return null; + if (rc == 0) return null; self.handle.index = 0; - self.handle.end_index = result; + self.handle.end_index = @intCast(usize, rc); break; } } - const freebsd_entry = @ptrCast(*align(1) posix.dirent, &self.handle.buf[self.handle.index]); + const freebsd_entry = @ptrCast(*align(1) os.dirent, &self.handle.buf[self.handle.index]); const next_index = self.handle.index + freebsd_entry.d_reclen; self.handle.index = next_index; @@ -685,14 +680,14 @@ pub const Dir = struct { } const entry_kind = switch (freebsd_entry.d_type) { - posix.DT_BLK => Entry.Kind.BlockDevice, - posix.DT_CHR => Entry.Kind.CharacterDevice, - posix.DT_DIR => Entry.Kind.Directory, - posix.DT_FIFO => Entry.Kind.NamedPipe, - posix.DT_LNK => Entry.Kind.SymLink, - posix.DT_REG => Entry.Kind.File, - posix.DT_SOCK => Entry.Kind.UnixDomainSocket, - posix.DT_WHT => Entry.Kind.Whiteout, + os.DT_BLK => Entry.Kind.BlockDevice, + os.DT_CHR => Entry.Kind.CharacterDevice, + os.DT_DIR => Entry.Kind.Directory, + os.DT_FIFO => Entry.Kind.NamedPipe, + os.DT_LNK => Entry.Kind.SymLink, + os.DT_REG => Entry.Kind.File, + os.DT_SOCK => Entry.Kind.UnixDomainSocket, + os.DT_WHT => Entry.Kind.Whiteout, else => Entry.Kind.Unknown, }; return Entry{ @@ -705,52 +700,40 @@ pub const Dir = struct { /// Read value of a symbolic link. /// The return value is a slice of buffer, from index `0`. -pub fn readLink(buffer: *[posix.PATH_MAX]u8, pathname: []const u8) ![]u8 { - return posix.readlink(pathname, buffer); +pub fn readLink(pathname: []const u8, buffer: *[os.PATH_MAX]u8) ![]u8 { + return os.readlink(pathname, buffer); } /// Same as `readLink`, except the `pathname` parameter is null-terminated. -pub fn readLinkC(buffer: *[posix.PATH_MAX]u8, pathname: [*]const u8) ![]u8 { - return posix.readlinkC(pathname, buffer); +pub fn readLinkC(pathname: [*]const u8, buffer: *[os.PATH_MAX]u8) ![]u8 { + return os.readlinkC(pathname, buffer); } -pub fn openSelfExe() !os.File { - switch (builtin.os) { - Os.linux => return os.File.openReadC(c"/proc/self/exe"), - Os.macosx, Os.ios, Os.freebsd, Os.netbsd => { - var buf: [MAX_PATH_BYTES]u8 = undefined; - const self_exe_path = try selfExePath(&buf); - buf[self_exe_path.len] = 0; - return os.File.openReadC(self_exe_path.ptr); - }, - Os.windows => { - var buf: [posix.PATH_MAX_WIDE]u16 = undefined; - const wide_slice = try selfExePathW(&buf); - return os.File.openReadW(wide_slice.ptr); - }, - else => @compileError("Unsupported OS"), +pub const OpenSelfExeError = error{}; + +pub fn openSelfExe() OpenSelfExeError!File { + if (os.linux.is_the_target) { + return File.openReadC(c"/proc/self/exe"); + } + if (os.windows.is_the_target) { + var buf: [os.windows.PATH_MAX_WIDE]u16 = undefined; + const wide_slice = try selfExePathW(&buf); + return File.openReadW(wide_slice.ptr); } + var buf: [MAX_PATH_BYTES]u8 = undefined; + const self_exe_path = try selfExePath(&buf); + buf[self_exe_path.len] = 0; + return File.openReadC(self_exe_path.ptr); } test "openSelfExe" { switch (builtin.os) { - Os.linux, Os.macosx, Os.ios, Os.windows, Os.freebsd => (try openSelfExe()).close(), + .linux, .macosx, .ios, .windows, .freebsd => (try openSelfExe()).close(), else => return error.SkipZigTest, // Unsupported OS. } } -pub fn selfExePathW(out_buffer: *[posix.PATH_MAX_WIDE]u16) ![]u16 { - const casted_len = @intCast(windows.DWORD, out_buffer.len); // TODO shouldn't need this cast - const rc = windows.GetModuleFileNameW(null, out_buffer, casted_len); - assert(rc <= out_buffer.len); - if (rc == 0) { - const err = windows.GetLastError(); - switch (err) { - else => return windows.unexpectedError(err), - } - } - return out_buffer[0..rc]; -} +pub const SelfExePathError = os.ReadLinkError || os.SysCtlError; /// Get the path to the current executable. /// If you only need the directory, use selfExeDirPath. @@ -763,39 +746,44 @@ pub fn selfExePathW(out_buffer: *[posix.PATH_MAX_WIDE]u16) ![]u16 { /// been deleted, the file path looks something like `/a/b/c/exe (deleted)`. /// TODO make the return type of this a null terminated pointer pub fn selfExePath(out_buffer: *[MAX_PATH_BYTES]u8) ![]u8 { + if (os.darwin.is_the_target) { + var u32_len: u32 = out_buffer.len; + const rc = c._NSGetExecutablePath(out_buffer, &u32_len); + if (rc != 0) return error.NameTooLong; + return mem.toSlice(u8, out_buffer); + } switch (builtin.os) { - Os.linux => return readLink(out_buffer, "/proc/self/exe"), - Os.freebsd => { - var mib = [4]c_int{ posix.CTL_KERN, posix.KERN_PROC, posix.KERN_PROC_PATHNAME, -1 }; + .linux => return os.readlinkC(c"/proc/self/exe", out_buffer), + .freebsd => { + var mib = [4]c_int{ os.CTL_KERN, os.KERN_PROC, os.KERN_PROC_PATHNAME, -1 }; var out_len: usize = out_buffer.len; - try posix.sysctl(&mib, out_buffer, &out_len, null, 0); + try os.sysctl(&mib, out_buffer, &out_len, null, 0); // TODO could this slice from 0 to out_len instead? return mem.toSlice(u8, out_buffer); }, - Os.netbsd => { - var mib = [4]c_int{ posix.CTL_KERN, posix.KERN_PROC_ARGS, -1, posix.KERN_PROC_PATHNAME }; + .netbsd => { + var mib = [4]c_int{ os.CTL_KERN, os.KERN_PROC_ARGS, -1, os.KERN_PROC_PATHNAME }; var out_len: usize = out_buffer.len; - try posix.sysctl(&mib, out_buffer, &out_len, null, 0); + try os.sysctl(&mib, out_buffer, &out_len, null, 0); // TODO could this slice from 0 to out_len instead? return mem.toSlice(u8, out_buffer); }, - Os.windows => { - var utf16le_buf: [posix.PATH_MAX_WIDE]u16 = undefined; + .windows => { + var utf16le_buf: [os.windows.PATH_MAX_WIDE]u16 = undefined; const utf16le_slice = try selfExePathW(&utf16le_buf); // Trust that Windows gives us valid UTF-16LE. const end_index = std.unicode.utf16leToUtf8(out_buffer, utf16le_slice) catch unreachable; return out_buffer[0..end_index]; }, - Os.macosx, Os.ios => { - var u32_len: u32 = @intCast(u32, out_buffer.len); // TODO shouldn't need this cast - const rc = c._NSGetExecutablePath(out_buffer, &u32_len); - if (rc != 0) return error.NameTooLong; - return mem.toSlice(u8, out_buffer); - }, - else => @compileError("Unsupported OS"), + else => @compileError("std.fs.selfExePath not supported for this target"), } } +/// Same as `selfExePath` except the result is UTF16LE-encoded. +pub fn selfExePathW(out_buffer: *[os.windows.PATH_MAX_WIDE]u16) ![]u16 { + return os.windows.GetModuleFileNameW(null, out_buffer, out_buffer.len); +} + /// `selfExeDirPath` except allocates the result on the heap. /// Caller owns returned memory. pub fn selfExeDirPathAlloc(allocator: *Allocator) ![]u8 { @@ -806,31 +794,26 @@ pub fn selfExeDirPathAlloc(allocator: *Allocator) ![]u8 { /// Get the directory path that contains the current executable. /// Returned value is a slice of out_buffer. pub fn selfExeDirPath(out_buffer: *[MAX_PATH_BYTES]u8) ![]const u8 { - switch (builtin.os) { - Os.linux => { - // If the currently executing binary has been deleted, - // the file path looks something like `/a/b/c/exe (deleted)` - // This path cannot be opened, but it's valid for determining the directory - // the executable was in when it was run. - const full_exe_path = try readLinkC(out_buffer, c"/proc/self/exe"); - // Assume that /proc/self/exe has an absolute path, and therefore dirname - // will not return null. - return path.dirname(full_exe_path).?; - }, - Os.windows, Os.macosx, Os.ios, Os.freebsd, Os.netbsd => { - const self_exe_path = try selfExePath(out_buffer); - // Assume that the OS APIs return absolute paths, and therefore dirname - // will not return null. - return path.dirname(self_exe_path).?; - }, - else => @compileError("Unsupported OS"), + if (os.linux.is_the_target) { + // If the currently executing binary has been deleted, + // the file path looks something like `/a/b/c/exe (deleted)` + // This path cannot be opened, but it's valid for determining the directory + // the executable was in when it was run. + const full_exe_path = try os.readlinkC(c"/proc/self/exe", out_buffer); + // Assume that /proc/self/exe has an absolute path, and therefore dirname + // will not return null. + return path.dirname(full_exe_path).?; } + const self_exe_path = try selfExePath(out_buffer); + // Assume that the OS APIs return absolute paths, and therefore dirname + // will not return null. + return path.dirname(self_exe_path).?; } /// `realpath`, except caller must free the returned memory. -pub fn realAlloc(allocator: *Allocator, pathname: []const u8) ![]u8 { +pub fn realpathAlloc(allocator: *Allocator, pathname: []const u8) ![]u8 { var buf: [MAX_PATH_BYTES]u8 = undefined; - return mem.dupe(allocator, u8, try realpath(pathname, &buf)); + return mem.dupe(allocator, u8, try os.realpath(pathname, &buf)); } test "" { diff --git a/std/io.zig b/std/io.zig index 657352e295..5ca011cb4c 100644 --- a/std/io.zig +++ b/std/io.zig @@ -12,7 +12,7 @@ const meta = std.meta; const trait = meta.trait; const Buffer = std.Buffer; const fmt = std.fmt; -const File = std.os.File; +const File = std.fs.File; const testing = std.testing; const is_posix = builtin.os != builtin.Os.windows; @@ -963,8 +963,8 @@ pub fn BitOutStream(endian: builtin.Endian, comptime Error: type) type { pub const BufferedAtomicFile = struct { atomic_file: os.AtomicFile, - file_stream: os.File.OutStream, - buffered_stream: BufferedOutStream(os.File.WriteError), + file_stream: File.OutStream, + buffered_stream: BufferedOutStream(File.WriteError), allocator: *mem.Allocator, pub fn create(allocator: *mem.Allocator, dest_path: []const u8) !*BufferedAtomicFile { @@ -978,11 +978,11 @@ pub const BufferedAtomicFile = struct { }; errdefer allocator.destroy(self); - self.atomic_file = try os.AtomicFile.init(dest_path, os.File.default_mode); + self.atomic_file = try os.AtomicFile.init(dest_path, File.default_mode); errdefer self.atomic_file.deinit(); self.file_stream = self.atomic_file.file.outStream(); - self.buffered_stream = BufferedOutStream(os.File.WriteError).init(&self.file_stream.stream); + self.buffered_stream = BufferedOutStream(File.WriteError).init(&self.file_stream.stream); return self; } @@ -997,7 +997,7 @@ pub const BufferedAtomicFile = struct { try self.atomic_file.finish(); } - pub fn stream(self: *BufferedAtomicFile) *OutStream(os.File.WriteError) { + pub fn stream(self: *BufferedAtomicFile) *OutStream(File.WriteError) { return &self.buffered_stream.stream; } }; diff --git a/std/io/c_out_stream.zig b/std/io/c_out_stream.zig index c66b342f1e..cbc5254382 100644 --- a/std/io/c_out_stream.zig +++ b/std/io/c_out_stream.zig @@ -1,13 +1,13 @@ const std = @import("../std.zig"); +const os = std.os; const OutStream = std.io.OutStream; const builtin = @import("builtin"); -const posix = std.os.posix; -/// TODO make std.os.FILE use *FILE when linking libc and this just becomes -/// std.io.FileOutStream because std.os.File.write would do this when linking +/// TODO make a proposal to make `std.fs.File` use *FILE when linking libc and this just becomes +/// std.io.FileOutStream because std.fs.File.write would do this when linking /// libc. pub const COutStream = struct { - pub const Error = std.os.File.WriteError; + pub const Error = std.fs.File.WriteError; pub const Stream = OutStream(Error); stream: Stream, @@ -24,25 +24,20 @@ pub const COutStream = struct { const self = @fieldParentPtr(COutStream, "stream", out_stream); const amt_written = std.c.fwrite(bytes.ptr, 1, bytes.len, self.c_file); if (amt_written == bytes.len) return; - // TODO errno on windows. should we have a posix layer for windows? - if (builtin.os == .windows) { - return error.InputOutput; - } - const errno = std.c._errno().*; - switch (errno) { + switch (std.c._errno().*) { 0 => unreachable, - posix.EINVAL => unreachable, - posix.EFAULT => unreachable, - posix.EAGAIN => unreachable, // this is a blocking API - posix.EBADF => unreachable, // always a race condition - posix.EDESTADDRREQ => unreachable, // connect was never called - posix.EDQUOT => return error.DiskQuota, - posix.EFBIG => return error.FileTooBig, - posix.EIO => return error.InputOutput, - posix.ENOSPC => return error.NoSpaceLeft, - posix.EPERM => return error.AccessDenied, - posix.EPIPE => return error.BrokenPipe, - else => return std.os.unexpectedErrorPosix(@intCast(usize, errno)), + os.EINVAL => unreachable, + os.EFAULT => unreachable, + os.EAGAIN => unreachable, // this is a blocking API + os.EBADF => unreachable, // always a race condition + os.EDESTADDRREQ => unreachable, // connect was never called + os.EDQUOT => return error.DiskQuota, + os.EFBIG => return error.FileTooBig, + os.EIO => return error.InputOutput, + os.ENOSPC => return error.NoSpaceLeft, + os.EPERM => return error.AccessDenied, + os.EPIPE => return error.BrokenPipe, + else => return os.unexpectedErrno(@intCast(usize, errno)), } } }; diff --git a/std/io/test.zig b/std/io/test.zig index 07a3c0e8dd..7928baa597 100644 --- a/std/io/test.zig +++ b/std/io/test.zig @@ -1,3 +1,4 @@ +const builtin = @import("builtin"); const std = @import("../std.zig"); const io = std.io; const meta = std.meta; @@ -7,7 +8,7 @@ const expect = std.testing.expect; const expectError = std.testing.expectError; const mem = std.mem; const os = std.os; -const builtin = @import("builtin"); +const File = std.fs.File; test "write a file, read it, then delete it" { var raw_bytes: [200 * 1024]u8 = undefined; @@ -18,11 +19,11 @@ test "write a file, read it, then delete it" { prng.random.bytes(data[0..]); const tmp_file_name = "temp_test_file.txt"; { - var file = try os.File.openWrite(tmp_file_name); + var file = try File.openWrite(tmp_file_name); defer file.close(); var file_out_stream = file.outStream(); - var buf_stream = io.BufferedOutStream(os.File.WriteError).init(&file_out_stream.stream); + var buf_stream = io.BufferedOutStream(File.WriteError).init(&file_out_stream.stream); const st = &buf_stream.stream; try st.print("begin"); try st.write(data[0..]); @@ -32,15 +33,15 @@ test "write a file, read it, then delete it" { { // make sure openWriteNoClobber doesn't harm the file - if (os.File.openWriteNoClobber(tmp_file_name, os.File.default_mode)) |file| { + if (File.openWriteNoClobber(tmp_file_name, File.default_mode)) |file| { unreachable; } else |err| { - std.debug.assert(err == os.File.OpenError.PathAlreadyExists); + std.debug.assert(err == File.OpenError.PathAlreadyExists); } } { - var file = try os.File.openRead(tmp_file_name); + var file = try File.openRead(tmp_file_name); defer file.close(); const file_size = try file.getEndPos(); @@ -48,7 +49,7 @@ test "write a file, read it, then delete it" { expect(file_size == expected_file_size); var file_in_stream = file.inStream(); - var buf_stream = io.BufferedInStream(os.File.ReadError).init(&file_in_stream.stream); + var buf_stream = io.BufferedInStream(File.ReadError).init(&file_in_stream.stream); const st = &buf_stream.stream; const contents = try st.readAllAlloc(allocator, 2 * 1024); defer allocator.free(contents); @@ -273,12 +274,12 @@ test "BitOutStream" { test "BitStreams with File Stream" { const tmp_file_name = "temp_test_file.txt"; { - var file = try os.File.openWrite(tmp_file_name); + var file = try File.openWrite(tmp_file_name); defer file.close(); var file_out = file.outStream(); var file_out_stream = &file_out.stream; - const OutError = os.File.WriteError; + const OutError = File.WriteError; var bit_stream = io.BitOutStream(builtin.endian, OutError).init(file_out_stream); try bit_stream.writeBits(u2(1), 1); @@ -290,12 +291,12 @@ test "BitStreams with File Stream" { try bit_stream.flushBits(); } { - var file = try os.File.openRead(tmp_file_name); + var file = try File.openRead(tmp_file_name); defer file.close(); var file_in = file.inStream(); var file_in_stream = &file_in.stream; - const InError = os.File.ReadError; + const InError = File.ReadError; var bit_stream = io.BitInStream(builtin.endian, InError).init(file_in_stream); var out_bits: usize = undefined; diff --git a/std/os.zig b/std/os.zig index 5f6716e017..4cd847cf8f 100644 --- a/std/os.zig +++ b/std/os.zig @@ -16,6 +16,7 @@ const std = @import("std.zig"); const builtin = @import("builtin"); +const math = std.math; const MAX_PATH_BYTES = std.fs.MAX_PATH_BYTES; comptime { @@ -114,7 +115,7 @@ fn getRandomBytesDevURandom(buf: []u8) !void { const fd = try openC(c"/dev/urandom", O_RDONLY | O_CLOEXEC, 0); defer close(fd); - const stream = &os.File.openHandle(fd).inStream().stream; + const stream = &std.fs.File.openHandle(fd).inStream().stream; stream.readNoEof(buf) catch return error.Unexpected; } @@ -177,6 +178,21 @@ pub fn raise(sig: u8) RaiseError!void { } } +pub const KillError = error{ + PermissionDenied, + Unexpected, +}; + +pub fn kill(pid: pid_t, sig: u8) KillError!void { + switch (errno(system.kill(pid, sig))) { + 0 => return, + EINVAL => unreachable, // invalid signal + EPERM => return error.PermissionDenied, + ESRCH => unreachable, // always a race condition + else => |err| return unexpectedErrno(err), + } +} + /// Exits the program cleanly with the specified status code. pub fn exit(status: u8) noreturn { if (builtin.link_libc) { @@ -885,8 +901,7 @@ pub fn rename(old_path: []const u8, new_path: []const u8) RenameError!void { if (windows.is_the_target and !builtin.link_libc) { const old_path_w = try windows.sliceToPrefixedFileW(old_path); const new_path_w = try windows.sliceToPrefixedFileW(new_path); - const flags = windows.MOVEFILE_REPLACE_EXISTING | windows.MOVEFILE_WRITE_THROUGH; - return windows.MoveFileExW(&old_path_w, &new_path_w, flags); + return renameW(&old_path_w, &new_path_w); } else { const old_path_c = try toPosixPath(old_path); const new_path_c = try toPosixPath(new_path); @@ -899,8 +914,7 @@ pub fn renameC(old_path: [*]const u8, new_path: [*]const u8) RenameError!void { if (windows.is_the_target and !builtin.link_libc) { const old_path_w = try windows.cStrToPrefixedFileW(old_path); const new_path_w = try windows.cStrToPrefixedFileW(new_path); - const flags = windows.MOVEFILE_REPLACE_EXISTING | windows.MOVEFILE_WRITE_THROUGH; - return windows.MoveFileExW(&old_path_w, &new_path_w, flags); + return renameW(&old_path_w, &new_path_w); } switch (errno(system.rename(old_path, new_path))) { 0 => return, @@ -926,6 +940,13 @@ pub fn renameC(old_path: [*]const u8, new_path: [*]const u8) RenameError!void { } } +/// Same as `rename` except the parameters are null-terminated UTF16LE encoded byte arrays. +/// Assumes target is Windows. +pub fn renameW(old_path: [*]const u16, new_path: [*]const u16) RenameError!void { + const flags = windows.MOVEFILE_REPLACE_EXISTING | windows.MOVEFILE_WRITE_THROUGH; + return windows.MoveFileExW(old_path_w, new_path_w, flags); +} + pub const MakeDirError = error{ AccessDenied, DiskQuota, @@ -1684,10 +1705,10 @@ pub fn getsockoptError(sockfd: i32) ConnectError!void { } } -pub fn waitpid(pid: i32) i32 { +pub fn waitpid(pid: i32, flags: u32) i32 { var status: i32 = undefined; while (true) { - switch (errno(system.waitpid(pid, &status, 0))) { + switch (errno(system.waitpid(pid, &status, flags))) { 0 => return status, EINTR => continue, ECHILD => unreachable, // The process specified does not exist. It would be a race condition to handle this error. @@ -1988,9 +2009,10 @@ pub const PipeError = error{ }; /// Creates a unidirectional data channel that can be used for interprocess communication. -pub fn pipe(fds: *[2]fd_t) PipeError!void { - switch (errno(system.pipe(fds))) { - 0 => return, +pub fn pipe() PipeError![2]fd_t { + var fds: [2]i32 = undefined; + switch (errno(system.pipe(&fds))) { + 0 => return fds, EINVAL => unreachable, // Invalid parameters to pipe() EFAULT => unreachable, // Invalid fds pointer ENFILE => return error.SystemFdQuotaExceeded, @@ -1999,9 +2021,10 @@ pub fn pipe(fds: *[2]fd_t) PipeError!void { } } -pub fn pipe2(fds: *[2]fd_t, flags: u32) PipeError!void { - switch (errno(system.pipe2(fds, flags))) { - 0 => return, +pub fn pipe2(flags: u32) PipeError![2]fd_t { + var fds: [2]i32 = undefined; + switch (errno(system.pipe2(&fds, flags))) { + 0 => return fds, EINVAL => unreachable, // Invalid flags EFAULT => unreachable, // Invalid fds pointer ENFILE => return error.SystemFdQuotaExceeded, @@ -2281,6 +2304,67 @@ pub fn realpathW(pathname: [*]const u16, out_buffer: *[MAX_PATH_BYTES]u8) RealPa return out_buffer[0..end_index]; } +/// Spurious wakeups are possible and no precision of timing is guaranteed. +pub fn nanosleep(seconds: u64, nanoseconds: u64) void { + if (windows.is_the_target and !builtin.link_libc) { + // TODO https://github.com/ziglang/zig/issues/1284 + const small_s = math.cast(windows.DWORD, seconds) catch math.maxInt(windows.DWORD); + const ms_from_s = math.mul(small_s, std.time.ms_per_s) catch math.maxInt(windows.DWORD); + + const ns_per_ms = std.time.ns_per_s / std.time.ms_per_s; + const big_ms_from_ns = nanoseconds / ns_per_ms; + const ms_from_ns = math.cast(windows.DWORD, big_ms_from_ns) catch math.maxInt(windows.DWORD); + + const ms = math.add(ms_from_s, ms_from_ns) catch math.maxInt(windows.DWORD); + windows.kernel32.Sleep(ms); + return; + } + var req = timespec{ + .tv_sec = math.cast(isize, seconds) catch math.maxInt(isize), + .tv_nsec = math.cast(isize, nanoseconds) catch math.maxInt(isize), + }; + var rem: timespec = undefined; + while (true) { + switch (errno(system.nanosleep(&req, &rem))) { + EFAULT => unreachable, + EINVAL => { + // Sometimes Darwin returns EINVAL for no reason. + // We treat it as a spurious wakeup. + return; + }, + EINTR => { + req = rem; + continue; + }, + // This prong handles success as well as unexpected errors. + else => return, + } + } +} + +pub const ClockGetTimeError = error{ + UnsupportedClock, + Unexpected, +}; + +pub fn clock_gettime(clk_id: i32, tp: *timespec) ClockGetTimeError!void { + switch (errno(system.clock_gettime(clk_id, tp))) { + 0 => return, + EFAULT => unreachable, + EINVAL => return error.UnsupportedClock, + else => |err| return unexpectedErrno(err), + } +} + +pub fn clock_getres(clk_id: i32, res: *timespec) ClockGetTimeError!void { + switch (errno(system.clock_getres(clk_id, tp))) { + 0 => return, + EFAULT => unreachable, + EINVAL => return error.UnsupportedClock, + else => |err| return unexpectedErrno(err), + } +} + /// Used to convert a slice to a null terminated slice on the stack. /// TODO https://github.com/ziglang/zig/issues/287 pub fn toPosixPath(file_path: []const u8) ![PATH_MAX]u8 { diff --git a/std/os/bits/linux.zig b/std/os/bits/linux.zig index 40b554b27f..7957187896 100644 --- a/std/os/bits/linux.zig +++ b/std/os/bits/linux.zig @@ -1,4 +1,4 @@ -pub use @import("errno.zig"); +pub use @import("linux/errno.zig"); pub use switch (builtin.arch) { .x86_64 => @import("linux/x86_64.zig"), .aarch64 => @import("linux/arm64.zig"), @@ -744,16 +744,6 @@ pub const sockaddr_un = extern struct { path: [108]u8, }; -pub const iovec = extern struct { - iov_base: [*]u8, - iov_len: usize, -}; - -pub const iovec_const = extern struct { - iov_base: [*]const u8, - iov_len: usize, -}; - pub const mmsghdr = extern struct { msg_hdr: msghdr, msg_len: u32, diff --git a/std/os/test.zig b/std/os/test.zig index 35824001a5..54371e94ab 100644 --- a/std/os/test.zig +++ b/std/os/test.zig @@ -4,6 +4,7 @@ const testing = std.testing; const expect = std.testing.expect; const io = std.io; const mem = std.mem; +const File = std.fs.File; const a = std.debug.global_allocator; @@ -25,14 +26,14 @@ test "makePath, put some files in it, deleteTree" { test "access file" { try os.makePath(a, "os_test_tmp"); - if (os.File.access("os_test_tmp" ++ os.path.sep_str ++ "file.txt")) |ok| { + if (File.access("os_test_tmp" ++ os.path.sep_str ++ "file.txt")) |ok| { @panic("expected error"); } else |err| { expect(err == error.FileNotFound); } try io.writeFile("os_test_tmp" ++ os.path.sep_str ++ "file.txt", ""); - try os.File.access("os_test_tmp" ++ os.path.sep_str ++ "file.txt"); + try File.access("os_test_tmp" ++ os.path.sep_str ++ "file.txt"); try os.deleteTree(a, "os_test_tmp"); } @@ -102,7 +103,7 @@ test "AtomicFile" { \\ this is a test file ; { - var af = try os.AtomicFile.init(test_out_file, os.File.default_mode); + var af = try os.AtomicFile.init(test_out_file, File.default_mode); defer af.deinit(); try af.file.write(test_content); try af.finish(); diff --git a/std/os/windows.zig b/std/os/windows.zig index 70772ee807..e35d8bbbd2 100644 --- a/std/os/windows.zig +++ b/std/os/windows.zig @@ -753,6 +753,10 @@ pub fn CloseHandle(hObject: HANDLE) void { assert(kernel32.CloseHandle(hObject) != 0); } +pub fn FindClose(hFindFile: HANDLE) void { + assert(kernel32.FindClose(hFindFile) != 0); +} + pub const ReadFileError = error{Unexpected}; pub fn ReadFile(in_hFile: HANDLE, buffer: []u8) ReadFileError!usize { @@ -1063,6 +1067,28 @@ pub fn GetFileAttributesW(lpFileName: [*]const u16) GetFileAttributesError!DWORD return rc; } +const GetModuleFileNameError = error{Unexpected}; + +pub fn GetModuleFileNameW(hModule: ?HMODULE, buf_ptr: [*]u16, buf_len: DWORD) GetModuleFileNameError![]u16 { + const rc = kernel32.GetModuleFileNameW(hModule, buf_ptr, buf_len); + if (rc == 0) { + switch (kernel32.GetLastError()) { + else => |err| return unexpectedError(err), + } + } + return buf_ptr[0..rc]; +} + +pub const TerminateProcessError = error{Unexpected}; + +pub fn TerminateProcess(hProcess: HANDLE, uExitCode: UINT) TerminateProcessError!void { + if (kernel32.TerminateProcess(hProcess, uExitCode) == 0) { + switch (kernel32.GetLastError()) { + else => |err| return unexpectedError(err), + } + } +} + pub fn cStrToPrefixedFileW(s: [*]const u8) ![PATH_MAX_WIDE + 1]u16 { return sliceToPrefixedFileW(mem.toSliceConst(u8, s)); } diff --git a/std/pdb.zig b/std/pdb.zig index 0f3a6d00b5..2822d2546e 100644 --- a/std/pdb.zig +++ b/std/pdb.zig @@ -6,6 +6,7 @@ const mem = std.mem; const os = std.os; const warn = std.debug.warn; const coff = std.coff; +const File = std.fs.File; const ArrayList = std.ArrayList; @@ -459,7 +460,7 @@ pub const PDBStringTableHeader = packed struct { }; pub const Pdb = struct { - in_file: os.File, + in_file: File, allocator: *mem.Allocator, coff: *coff.Coff, string_table: *MsfStream, @@ -468,7 +469,7 @@ pub const Pdb = struct { msf: Msf, pub fn openFile(self: *Pdb, coff_ptr: *coff.Coff, file_name: []u8) !void { - self.in_file = try os.File.openRead(file_name); + self.in_file = try File.openRead(file_name); self.allocator = coff_ptr.allocator; self.coff = coff_ptr; @@ -492,7 +493,7 @@ const Msf = struct { directory: MsfStream, streams: []MsfStream, - fn openFile(self: *Msf, allocator: *mem.Allocator, file: os.File) !void { + fn openFile(self: *Msf, allocator: *mem.Allocator, file: File) !void { var file_stream = file.inStream(); const in = &file_stream.stream; @@ -587,7 +588,7 @@ const SuperBlock = packed struct { }; const MsfStream = struct { - in_file: os.File, + in_file: File, pos: u64, blocks: []u32, block_size: u32, @@ -598,7 +599,7 @@ const MsfStream = struct { pub const Error = @typeOf(read).ReturnType.ErrorSet; pub const Stream = io.InStream(Error); - fn init(block_size: u32, block_count: u32, pos: u64, file: os.File, allocator: *mem.Allocator) !MsfStream { + fn init(block_size: u32, block_count: u32, pos: u64, file: File, allocator: *mem.Allocator) !MsfStream { var stream = MsfStream{ .in_file = file, .pos = 0, diff --git a/std/special/build_runner.zig b/std/special/build_runner.zig index dfc3838577..f9a322cbeb 100644 --- a/std/special/build_runner.zig +++ b/std/special/build_runner.zig @@ -8,6 +8,7 @@ const Builder = std.build.Builder; const mem = std.mem; const ArrayList = std.ArrayList; const warn = std.debug.warn; +const File = std.fs.File; pub fn main() !void { var arg_it = os.args(); @@ -48,14 +49,14 @@ pub fn main() !void { var prefix: ?[]const u8 = null; var stderr_file = io.getStdErr(); - var stderr_file_stream: os.File.OutStream = undefined; + var stderr_file_stream: File.OutStream = undefined; var stderr_stream = if (stderr_file) |f| x: { stderr_file_stream = f.outStream(); break :x &stderr_file_stream.stream; } else |err| err; var stdout_file = io.getStdOut(); - var stdout_file_stream: os.File.OutStream = undefined; + var stdout_file_stream: File.OutStream = undefined; var stdout_stream = if (stdout_file) |f| x: { stdout_file_stream = f.outStream(); break :x &stdout_file_stream.stream; diff --git a/std/time.zig b/std/time.zig index abb6412843..1483a0a132 100644 --- a/std/time.zig +++ b/std/time.zig @@ -1,116 +1,61 @@ -const std = @import("../std.zig"); const builtin = @import("builtin"); -const Os = builtin.Os; -const debug = std.debug; +const std = @import("std.zig"); +const assert = std.debug.assert; const testing = std.testing; -const math = std.math; - -const windows = std.os.windows; -const linux = std.os.linux; -const darwin = std.os.darwin; -const wasi = std.os.wasi; -const posix = std.os.posix; +const os = std.os; pub const epoch = @import("epoch.zig"); /// Spurious wakeups are possible and no precision of timing is guaranteed. pub fn sleep(nanoseconds: u64) void { - switch (builtin.os) { - Os.linux, Os.macosx, Os.ios, Os.freebsd, Os.netbsd => { - const s = nanoseconds / ns_per_s; - const ns = nanoseconds % ns_per_s; - posixSleep(s, ns); - }, - Os.windows => { - const ns_per_ms = ns_per_s / ms_per_s; - const milliseconds = nanoseconds / ns_per_ms; - const ms_that_will_fit = std.math.cast(windows.DWORD, milliseconds) catch std.math.maxInt(windows.DWORD); - windows.Sleep(ms_that_will_fit); - }, - else => @compileError("Unsupported OS"), - } -} - -/// Spurious wakeups are possible and no precision of timing is guaranteed. -pub fn posixSleep(seconds: u64, nanoseconds: u64) void { - var req = posix.timespec{ - .tv_sec = std.math.cast(isize, seconds) catch std.math.maxInt(isize), - .tv_nsec = std.math.cast(isize, nanoseconds) catch std.math.maxInt(isize), - }; - var rem: posix.timespec = undefined; - while (true) { - const ret_val = posix.nanosleep(&req, &rem); - const err = posix.getErrno(ret_val); - switch (err) { - posix.EFAULT => unreachable, - posix.EINVAL => { - // Sometimes Darwin returns EINVAL for no reason. - // We treat it as a spurious wakeup. - return; - }, - posix.EINTR => { - req = rem; - continue; - }, - // This prong handles success as well as unexpected errors. - else => return, - } - } + const s = nanoseconds / ns_per_s; + const ns = nanoseconds % ns_per_s; + std.os.nanosleep(s, ns); } /// Get the posix timestamp, UTC, in seconds +/// TODO audit this function. is it possible to return an error? pub fn timestamp() u64 { return @divFloor(milliTimestamp(), ms_per_s); } /// Get the posix timestamp, UTC, in milliseconds -pub const milliTimestamp = switch (builtin.os) { - Os.windows => milliTimestampWindows, - Os.linux, Os.freebsd, Os.netbsd => milliTimestampPosix, - Os.macosx, Os.ios => milliTimestampDarwin, - Os.wasi => milliTimestampWasi, - else => @compileError("Unsupported OS"), -}; - -fn milliTimestampWasi() u64 { - var ns: wasi.timestamp_t = undefined; - - // TODO: Verify that precision is ignored - const err = wasi.clock_time_get(wasi.CLOCK_REALTIME, 1, &ns); - debug.assert(err == wasi.ESUCCESS); - - const ns_per_ms = 1000; - return @divFloor(ns, ns_per_ms); -} - -fn milliTimestampWindows() u64 { - //FileTime has a granularity of 100 nanoseconds - // and uses the NTFS/Windows epoch - var ft: windows.FILETIME = undefined; - windows.GetSystemTimeAsFileTime(&ft); - const hns_per_ms = (ns_per_s / 100) / ms_per_s; - const epoch_adj = epoch.windows * ms_per_s; - - const ft64 = (u64(ft.dwHighDateTime) << 32) | ft.dwLowDateTime; - return @divFloor(ft64, hns_per_ms) - -epoch_adj; -} +/// TODO audit this function. is it possible to return an error? +pub fn milliTimestamp() u64 { + if (os.windows.is_the_target and !builtin.link_libc) { + //FileTime has a granularity of 100 nanoseconds + // and uses the NTFS/Windows epoch + var ft: os.windows.FILETIME = undefined; + os.windows.kernel32.GetSystemTimeAsFileTime(&ft); + const hns_per_ms = (ns_per_s / 100) / ms_per_s; + const epoch_adj = epoch.windows * ms_per_s; + + const ft64 = (u64(ft.dwHighDateTime) << 32) | ft.dwLowDateTime; + return @divFloor(ft64, hns_per_ms) - -epoch_adj; + } + if (os.wasi.is_the_target and !builtin.link_libc) { + var ns: os.wasi.timestamp_t = undefined; -fn milliTimestampDarwin() u64 { - var tv: darwin.timeval = undefined; - var err = darwin.gettimeofday(&tv, null); - debug.assert(err == 0); - const sec_ms = tv.tv_sec * ms_per_s; - const usec_ms = @divFloor(tv.tv_usec, us_per_s / ms_per_s); - return @intCast(u64, sec_ms + usec_ms); -} + // TODO: Verify that precision is ignored + const err = os.wasi.clock_time_get(os.wasi.CLOCK_REALTIME, 1, &ns); + assert(err == os.wasi.ESUCCESS); -fn milliTimestampPosix() u64 { + const ns_per_ms = 1000; + return @divFloor(ns, ns_per_ms); + } + if (os.darwin.is_the_target) { + var tv: os.darwin.timeval = undefined; + var err = os.darwin.gettimeofday(&tv, null); + assert(err == 0); + const sec_ms = tv.tv_sec * ms_per_s; + const usec_ms = @divFloor(tv.tv_usec, us_per_s / ms_per_s); + return @intCast(u64, sec_ms + usec_ms); + } + var ts: os.timespec = undefined; //From what I can tell there's no reason clock_gettime // should ever fail for us with CLOCK_REALTIME, // seccomp aside. - var ts: posix.timespec = undefined; - const err = posix.clock_gettime(posix.CLOCK_REALTIME, &ts); - debug.assert(err == 0); + os.clock_gettime(os.CLOCK_REALTIME, &ts) catch unreachable; const sec_ms = @intCast(u64, ts.tv_sec) * ms_per_s; const nsec_ms = @divFloor(@intCast(u64, ts.tv_nsec), ns_per_s / ms_per_s); return sec_ms + nsec_ms; @@ -145,27 +90,23 @@ pub const s_per_week = s_per_day * 7; /// depends on the OS. On Windows and Darwin it is a hardware counter /// value that requires calculation to convert to a meaninful unit. pub const Timer = struct { - - //if we used resolution's value when performing the - // performance counter calc on windows/darwin, it would - // be less precise + ///if we used resolution's value when performing the + /// performance counter calc on windows/darwin, it would + /// be less precise frequency: switch (builtin.os) { - Os.windows => u64, - Os.macosx, Os.ios => darwin.mach_timebase_info_data, + .windows => u64, + .macosx, .ios, .tvos, .watchos => darwin.mach_timebase_info_data, else => void, }, resolution: u64, start_time: u64, - //At some point we may change our minds on RAW, but for now we're - // sticking with posix standard MONOTONIC. For more information, see: - // https://github.com/ziglang/zig/pull/933 - // - //const monotonic_clock_id = switch(builtin.os) { - // Os.linux => linux.CLOCK_MONOTONIC_RAW, - // else => posix.CLOCK_MONOTONIC, - //}; - const monotonic_clock_id = posix.CLOCK_MONOTONIC; + const Error = error{TimerUnsupported}; + + ///At some point we may change our minds on RAW, but for now we're + /// sticking with posix standard MONOTONIC. For more information, see: + /// https://github.com/ziglang/zig/pull/933 + const monotonic_clock_id = os.CLOCK_MONOTONIC; /// Initialize the timer structure. //This gives us an opportunity to grab the counter frequency in windows. //On Windows: QueryPerformanceCounter will succeed on anything >= XP/2000. @@ -174,66 +115,51 @@ pub const Timer = struct { // impossible here barring cosmic rays or other such occurrences of // incredibly bad luck. //On Darwin: This cannot fail, as far as I am able to tell. - const TimerError = error{ - TimerUnsupported, - Unexpected, - }; - pub fn start() TimerError!Timer { + pub fn start() Error!Timer { var self: Timer = undefined; - switch (builtin.os) { - Os.windows => { - var freq: i64 = undefined; - var err = windows.QueryPerformanceFrequency(&freq); - if (err == windows.FALSE) return error.TimerUnsupported; - self.frequency = @intCast(u64, freq); - self.resolution = @divFloor(ns_per_s, self.frequency); - - var start_time: i64 = undefined; - err = windows.QueryPerformanceCounter(&start_time); - debug.assert(err != windows.FALSE); - self.start_time = @intCast(u64, start_time); - }, - Os.linux, Os.freebsd, Os.netbsd => { - //On Linux, seccomp can do arbitrary things to our ability to call - // syscalls, including return any errno value it wants and - // inconsistently throwing errors. Since we can't account for - // abuses of seccomp in a reasonable way, we'll assume that if - // seccomp is going to block us it will at least do so consistently - var ts: posix.timespec = undefined; - var result = posix.clock_getres(monotonic_clock_id, &ts); - var errno = posix.getErrno(result); - switch (errno) { - 0 => {}, - posix.EINVAL => return error.TimerUnsupported, - else => return std.os.unexpectedErrorPosix(errno), - } - self.resolution = @intCast(u64, ts.tv_sec) * u64(ns_per_s) + @intCast(u64, ts.tv_nsec); - - result = posix.clock_gettime(monotonic_clock_id, &ts); - errno = posix.getErrno(result); - if (errno != 0) return std.os.unexpectedErrorPosix(errno); - self.start_time = @intCast(u64, ts.tv_sec) * u64(ns_per_s) + @intCast(u64, ts.tv_nsec); - }, - Os.macosx, Os.ios => { - darwin.mach_timebase_info(&self.frequency); - self.resolution = @divFloor(self.frequency.numer, self.frequency.denom); - self.start_time = darwin.mach_absolute_time(); - }, - else => @compileError("Unsupported OS"), + if (os.windows.is_the_target) { + var freq: i64 = undefined; + var err = windows.QueryPerformanceFrequency(&freq); + if (err == windows.FALSE) return error.TimerUnsupported; + self.frequency = @intCast(u64, freq); + self.resolution = @divFloor(ns_per_s, self.frequency); + + var start_time: i64 = undefined; + err = windows.QueryPerformanceCounter(&start_time); + assert(err != windows.FALSE); + self.start_time = @intCast(u64, start_time); + } else if (os.darwin.is_the_target) { + darwin.mach_timebase_info(&self.frequency); + self.resolution = @divFloor(self.frequency.numer, self.frequency.denom); + self.start_time = darwin.mach_absolute_time(); + } else { + //On Linux, seccomp can do arbitrary things to our ability to call + // syscalls, including return any errno value it wants and + // inconsistently throwing errors. Since we can't account for + // abuses of seccomp in a reasonable way, we'll assume that if + // seccomp is going to block us it will at least do so consistently + var ts: os.timespec = undefined; + os.clock_getres(monotonic_clock_id, &ts) catch return error.TimerUnsupported; + self.resolution = @intCast(u64, ts.tv_sec) * u64(ns_per_s) + @intCast(u64, ts.tv_nsec); + + os.clock_gettime(monotonic_clock_id, &ts) catch return error.TimerUnsupported; + self.start_time = @intCast(u64, ts.tv_sec) * u64(ns_per_s) + @intCast(u64, ts.tv_nsec); } + return self; } /// Reads the timer value since start or the last reset in nanoseconds pub fn read(self: *Timer) u64 { var clock = clockNative() - self.start_time; - return switch (builtin.os) { - Os.windows => @divFloor(clock * ns_per_s, self.frequency), - Os.linux, Os.freebsd, Os.netbsd => clock, - Os.macosx, Os.ios => @divFloor(clock * self.frequency.numer, self.frequency.denom), - else => @compileError("Unsupported OS"), - }; + if (os.windows.is_the_target) { + return @divFloor(clock * ns_per_s, self.frequency); + } + if (os.darwin.is_the_target) { + return @divFloor(clock * self.frequency.numer, self.frequency.denom); + } + return clock; } /// Resets the timer value to 0/now. @@ -249,37 +175,27 @@ pub const Timer = struct { return lap_time; } - const clockNative = switch (builtin.os) { - Os.windows => clockWindows, - Os.linux, Os.freebsd, Os.netbsd => clockLinux, - Os.macosx, Os.ios => clockDarwin, - else => @compileError("Unsupported OS"), - }; - - fn clockWindows() u64 { - var result: i64 = undefined; - var err = windows.QueryPerformanceCounter(&result); - debug.assert(err != windows.FALSE); - return @intCast(u64, result); - } - - fn clockDarwin() u64 { - return darwin.mach_absolute_time(); - } - - fn clockLinux() u64 { - var ts: posix.timespec = undefined; - var result = posix.clock_gettime(monotonic_clock_id, &ts); - debug.assert(posix.getErrno(result) == 0); + fn clockNative() u64 { + if (os.windows.is_the_target) { + var result: i64 = undefined; + var err = windows.QueryPerformanceCounter(&result); + assert(err != windows.FALSE); + return @intCast(u64, result); + } + if (os.darwin.is_the_target) { + return darwin.mach_absolute_time(); + } + var ts: os.timespec = undefined; + os.clock_gettime(monotonic_clock_id, &ts) catch unreachable; return @intCast(u64, ts.tv_sec) * u64(ns_per_s) + @intCast(u64, ts.tv_nsec); } }; -test "os.time.sleep" { +test "sleep" { sleep(1); } -test "os.time.timestamp" { +test "timestamp" { const ns_per_ms = (ns_per_s / ms_per_s); const margin = 50; @@ -290,7 +206,7 @@ test "os.time.timestamp" { testing.expect(interval > 0 and interval < margin); } -test "os.time.Timer" { +test "Timer" { const ns_per_ms = (ns_per_s / ms_per_s); const margin = ns_per_ms * 150; diff --git a/std/zig/bench.zig b/std/zig/bench.zig index ed6ae9a128..b43c491c97 100644 --- a/std/zig/bench.zig +++ b/std/zig/bench.zig @@ -10,7 +10,7 @@ var fixed_buffer_mem: [10 * 1024 * 1024]u8 = undefined; pub fn main() !void { var i: usize = 0; - var timer = try std.os.time.Timer.start(); + var timer = try std.time.Timer.start(); const start = timer.lap(); const iterations = 100; var memory_used: usize = 0; @@ -19,7 +19,7 @@ pub fn main() !void { } const end = timer.read(); memory_used /= iterations; - const elapsed_s = @intToFloat(f64, end - start) / std.os.time.ns_per_s; + const elapsed_s = @intToFloat(f64, end - start) / std.time.ns_per_s; const bytes_per_sec = @intToFloat(f64, source.len * iterations) / elapsed_s; const mb_per_sec = bytes_per_sec / (1024 * 1024); -- cgit v1.2.3 From 7cb6279ac0cec065234347bda5944be64fe8b3da Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sat, 25 May 2019 13:07:44 -0400 Subject: clean up references to posix --- CMakeLists.txt | 1 - std/atomic/queue.zig | 8 +- std/atomic/stack.zig | 8 +- std/c/darwin.zig | 2 +- std/c/linux.zig | 1 + std/child_process.zig | 18 +- std/crypto.zig | 2 +- std/debug.zig | 9 +- std/event/fs.zig | 38 +- std/event/loop.zig | 142 +++--- std/event/net.zig | 113 +++-- std/fs/path.zig | 37 +- std/heap.zig | 324 +++++++------ std/mem.zig | 6 +- std/mutex.zig | 4 +- std/net.zig | 24 +- std/os.zig | 35 +- std/os/bits/linux.zig | 25 + std/os/linux.zig | 874 ++++++++++++++++++++++++++++++++++- std/os/linux/sys.zig | 859 ---------------------------------- std/os/linux/tls.zig | 13 +- std/os/linux/x86_64.zig | 2 +- std/os/test.zig | 29 +- std/os/windows.zig | 16 +- std/process.zig | 34 +- std/special/bootstrap.zig | 4 +- std/statically_initialized_mutex.zig | 4 +- std/thread.zig | 150 +++--- 28 files changed, 1390 insertions(+), 1392 deletions(-) delete mode 100644 std/os/linux/sys.zig (limited to 'std/debug.zig') diff --git a/CMakeLists.txt b/CMakeLists.txt index 1c7ceb88b9..9f6af9a9de 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -620,7 +620,6 @@ set(ZIG_STD_FILES "os/freebsd.zig" "os/linux.zig" "os/linux/arm64.zig" - "os/linux/sys.zig" "os/linux/tls.zig" "os/linux/vdso.zig" "os/linux/x86_64.zig" diff --git a/std/atomic/queue.zig b/std/atomic/queue.zig index 34cd0d22f8..bf5700c51e 100644 --- a/std/atomic/queue.zig +++ b/std/atomic/queue.zig @@ -186,13 +186,13 @@ test "std.atomic.Queue" { } } } else { - var putters: [put_thread_count]*std.os.Thread = undefined; + var putters: [put_thread_count]*std.Thread = undefined; for (putters) |*t| { - t.* = try std.os.spawnThread(&context, startPuts); + t.* = try std.Thread.spawn(&context, startPuts); } - var getters: [put_thread_count]*std.os.Thread = undefined; + var getters: [put_thread_count]*std.Thread = undefined; for (getters) |*t| { - t.* = try std.os.spawnThread(&context, startGets); + t.* = try std.Thread.spawn(&context, startGets); } for (putters) |t| diff --git a/std/atomic/stack.zig b/std/atomic/stack.zig index 773bdf6f1b..e9d070ac7b 100644 --- a/std/atomic/stack.zig +++ b/std/atomic/stack.zig @@ -120,13 +120,13 @@ test "std.atomic.stack" { } } } else { - var putters: [put_thread_count]*std.os.Thread = undefined; + var putters: [put_thread_count]*std.Thread = undefined; for (putters) |*t| { - t.* = try std.os.spawnThread(&context, startPuts); + t.* = try std.Thread.spawn(&context, startPuts); } - var getters: [put_thread_count]*std.os.Thread = undefined; + var getters: [put_thread_count]*std.Thread = undefined; for (getters) |*t| { - t.* = try std.os.spawnThread(&context, startGets); + t.* = try std.Thread.spawn(&context, startGets); } for (putters) |t| diff --git a/std/c/darwin.zig b/std/c/darwin.zig index 3d0c7f4dd2..e45a158f68 100644 --- a/std/c/darwin.zig +++ b/std/c/darwin.zig @@ -3,7 +3,7 @@ const assert = std.debug.assert; const builtin = @import("builtin"); const macho = std.macho; -use @import("posix/darwin.zig"); +use @import("../os/bits.zig"); extern "c" fn __error() *c_int; pub extern "c" fn _NSGetExecutablePath(buf: [*]u8, bufsize: *u32) c_int; diff --git a/std/c/linux.zig b/std/c/linux.zig index 388d8edab8..00fc600dab 100644 --- a/std/c/linux.zig +++ b/std/c/linux.zig @@ -2,6 +2,7 @@ const std = @import("../std.zig"); use std.c; pub extern "c" fn getrandom(buf_ptr: [*]u8, buf_len: usize, flags: c_uint) c_int; +pub extern "c" fn sched_getaffinity(pid: c_int, size: usize, set: *cpu_set_t) c_int; extern "c" fn __errno_location() *c_int; pub const _errno = __errno_location; diff --git a/std/child_process.zig b/std/child_process.zig index fa20a5e814..95d11d46a3 100644 --- a/std/child_process.zig +++ b/std/child_process.zig @@ -351,22 +351,22 @@ pub const ChildProcess = struct { const err_pipe = try os.pipe(); errdefer destroyPipe(err_pipe); - const pid_result = try posix.fork(); + const pid_result = try os.fork(); if (pid_result == 0) { // we are the child - setUpChildIo(self.stdin_behavior, stdin_pipe[0], posix.STDIN_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err); - setUpChildIo(self.stdout_behavior, stdout_pipe[1], posix.STDOUT_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err); - setUpChildIo(self.stderr_behavior, stderr_pipe[1], posix.STDERR_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err); + setUpChildIo(self.stdin_behavior, stdin_pipe[0], os.STDIN_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err); + setUpChildIo(self.stdout_behavior, stdout_pipe[1], os.STDOUT_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err); + setUpChildIo(self.stderr_behavior, stderr_pipe[1], os.STDERR_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err); - if (self.stdin_behavior == StdIo.Pipe) { + if (self.stdin_behavior == .Pipe) { os.close(stdin_pipe[0]); os.close(stdin_pipe[1]); } - if (self.stdout_behavior == StdIo.Pipe) { + if (self.stdout_behavior == .Pipe) { os.close(stdout_pipe[0]); os.close(stdout_pipe[1]); } - if (self.stderr_behavior == StdIo.Pipe) { + if (self.stderr_behavior == .Pipe) { os.close(stderr_pipe[0]); os.close(stderr_pipe[1]); } @@ -383,7 +383,7 @@ pub const ChildProcess = struct { os.posix_setreuid(uid, uid) catch |err| forkChildErrReport(err_pipe[1], err); } - os.posix.execve(self.allocator, self.argv, env_map) catch |err| forkChildErrReport(err_pipe[1], err); + os.execve(self.allocator, self.argv, env_map) catch |err| forkChildErrReport(err_pipe[1], err); } // we are the parent @@ -736,7 +736,7 @@ fn destroyPipe(pipe: [2]i32) void { // Then the child exits. fn forkChildErrReport(fd: i32, err: ChildProcess.SpawnError) noreturn { writeIntFd(fd, ErrInt(@errorToInt(err))) catch {}; - posix.exit(1); + os.exit(1); } const ErrInt = @IntType(false, @sizeOf(anyerror) * 8); diff --git a/std/crypto.zig b/std/crypto.zig index 2c3d8eba9e..2b57de9e60 100644 --- a/std/crypto.zig +++ b/std/crypto.zig @@ -33,7 +33,7 @@ pub const Poly1305 = @import("crypto/poly1305.zig").Poly1305; pub const X25519 = @import("crypto/x25519.zig").X25519; const std = @import("std.zig"); -pub const randomBytes = std.posix.getrandom; +pub const randomBytes = std.os.getrandom; test "crypto" { _ = @import("crypto/blake2.zig"); diff --git a/std/debug.zig b/std/debug.zig index 83daad6dc5..a309c31bbe 100644 --- a/std/debug.zig +++ b/std/debug.zig @@ -1012,16 +1012,15 @@ fn openSelfDebugInfoLinux(allocator: *mem.Allocator) !DwarfInfo { errdefer S.self_exe_file.close(); const self_exe_mmap_len = try S.self_exe_file.getEndPos(); - const self_exe_mmap = os.posix.mmap( + const self_exe_mmap = try os.mmap( null, self_exe_mmap_len, - os.posix.PROT_READ, - os.posix.MAP_SHARED, + os.PROT_READ, + os.MAP_SHARED, S.self_exe_file.handle, 0, ); - if (self_exe_mmap == os.posix.MAP_FAILED) return error.OutOfMemory; - errdefer assert(os.posix.munmap(self_exe_mmap, self_exe_mmap_len) == 0); + errdefer os.munmap(self_exe_mmap, self_exe_mmap_len); const file_mmap_slice = @intToPtr([*]const u8, self_exe_mmap)[0..self_exe_mmap_len]; S.self_exe_mmap_seekable = io.SliceSeekableInStream.init(file_mmap_slice); diff --git a/std/event/fs.zig b/std/event/fs.zig index 346a6c6b69..fbdac65889 100644 --- a/std/event/fs.zig +++ b/std/event/fs.zig @@ -5,10 +5,9 @@ const assert = std.debug.assert; const testing = std.testing; const os = std.os; const mem = std.mem; -const posix = os.posix; const windows = os.windows; const Loop = event.Loop; -const fd_t = posix.fd_t; +const fd_t = os.fd_t; const File = std.fs.File; pub const RequestNode = std.atomic.Queue(Request).Node; @@ -33,7 +32,7 @@ pub const Request = struct { pub const PWriteV = struct { fd: fd_t, - iov: []const os.posix.iovec_const, + iov: []const os.iovec_const, offset: usize, result: Error!void, @@ -42,7 +41,7 @@ pub const Request = struct { pub const PReadV = struct { fd: fd_t, - iov: []const os.posix.iovec, + iov: []const os.iovec, offset: usize, result: Error!usize, @@ -89,11 +88,11 @@ pub async fn pwritev(loop: *Loop, fd: fd_t, data: []const []const u8, offset: us builtin.Os.freebsd, builtin.Os.netbsd, => { - const iovecs = try loop.allocator.alloc(os.posix.iovec_const, data.len); + const iovecs = try loop.allocator.alloc(os.iovec_const, data.len); defer loop.allocator.free(iovecs); for (data) |buf, i| { - iovecs[i] = os.posix.iovec_const{ + iovecs[i] = os.iovec_const{ .iov_base = buf.ptr, .iov_len = buf.len, }; @@ -171,7 +170,7 @@ pub async fn pwriteWindows(loop: *Loop, fd: fd_t, data: []const u8, offset: u64) pub async fn pwritevPosix( loop: *Loop, fd: fd_t, - iovecs: []const posix.iovec_const, + iovecs: []const os.iovec_const, offset: usize, ) os.PosixWriteError!void { // workaround for https://github.com/ziglang/zig/issues/1194 @@ -226,11 +225,11 @@ pub async fn preadv(loop: *Loop, fd: fd_t, data: []const []u8, offset: usize) PR builtin.Os.freebsd, builtin.Os.netbsd, => { - const iovecs = try loop.allocator.alloc(os.posix.iovec, data.len); + const iovecs = try loop.allocator.alloc(os.iovec, data.len); defer loop.allocator.free(iovecs); for (data) |buf, i| { - iovecs[i] = os.posix.iovec{ + iovecs[i] = os.iovec{ .iov_base = buf.ptr, .iov_len = buf.len, }; @@ -319,7 +318,7 @@ pub async fn preadWindows(loop: *Loop, fd: fd_t, data: []u8, offset: u64) !usize pub async fn preadvPosix( loop: *Loop, fd: fd_t, - iovecs: []const posix.iovec, + iovecs: []const os.iovec, offset: usize, ) os.PosixReadError!usize { // workaround for https://github.com/ziglang/zig/issues/1194 @@ -405,7 +404,7 @@ pub async fn openPosix( pub async fn openRead(loop: *Loop, path: []const u8) File.OpenError!fd_t { switch (builtin.os) { builtin.Os.macosx, builtin.Os.linux, builtin.Os.freebsd, builtin.Os.netbsd => { - const flags = posix.O_LARGEFILE | posix.O_RDONLY | posix.O_CLOEXEC; + const flags = os.O_LARGEFILE | os.O_RDONLY | os.O_CLOEXEC; return await (async openPosix(loop, path, flags, File.default_mode) catch unreachable); }, @@ -435,7 +434,7 @@ pub async fn openWriteMode(loop: *Loop, path: []const u8, mode: File.Mode) File. builtin.Os.freebsd, builtin.Os.netbsd, => { - const flags = posix.O_LARGEFILE | posix.O_WRONLY | posix.O_CREAT | posix.O_CLOEXEC | posix.O_TRUNC; + const flags = os.O_LARGEFILE | os.O_WRONLY | os.O_CREAT | os.O_CLOEXEC | os.O_TRUNC; return await (async openPosix(loop, path, flags, File.default_mode) catch unreachable); }, builtin.Os.windows => return os.windowsOpen( @@ -457,7 +456,7 @@ pub async fn openReadWrite( ) File.OpenError!fd_t { switch (builtin.os) { builtin.Os.macosx, builtin.Os.linux, builtin.Os.freebsd, builtin.Os.netbsd => { - const flags = posix.O_LARGEFILE | posix.O_RDWR | posix.O_CREAT | posix.O_CLOEXEC; + const flags = os.O_LARGEFILE | os.O_RDWR | os.O_CREAT | os.O_CLOEXEC; return await (async openPosix(loop, path, flags, mode) catch unreachable); }, @@ -888,10 +887,7 @@ pub fn Watch(comptime V: type) type { var close_op_consumed = false; defer if (!close_op_consumed) close_op.finish(); - const flags = switch (builtin.os) { - builtin.Os.macosx => posix.O_SYMLINK | posix.O_EVTONLY, - else => 0, - }; + const flags = if (os.darwin.is_the_target) os.O_SYMLINK | os.O_EVTONLY else 0; const mode = 0; const fd = try await (async openPosix(self.channel.loop, resolved_path, flags, mode) catch unreachable); close_op.setHandle(fd); @@ -943,16 +939,16 @@ pub fn Watch(comptime V: type) type { while (true) { if (await (async self.channel.loop.bsdWaitKev( @intCast(usize, close_op.getHandle()), - posix.EVFILT_VNODE, - posix.NOTE_WRITE | posix.NOTE_DELETE, + os.EVFILT_VNODE, + os.NOTE_WRITE | os.NOTE_DELETE, ) catch unreachable)) |kev| { // TODO handle EV_ERROR - if (kev.fflags & posix.NOTE_DELETE != 0) { + if (kev.fflags & os.NOTE_DELETE != 0) { await (async self.channel.put(Self.Event{ .id = Event.Id.Delete, .data = value_copy, }) catch unreachable); - } else if (kev.fflags & posix.NOTE_WRITE != 0) { + } else if (kev.fflags & os.NOTE_WRITE != 0) { await (async self.channel.put(Self.Event{ .id = Event.Id.CloseWrite, .data = value_copy, diff --git a/std/event/loop.zig b/std/event/loop.zig index 71bc45d48f..e1848684c7 100644 --- a/std/event/loop.zig +++ b/std/event/loop.zig @@ -7,9 +7,9 @@ const AtomicRmwOp = builtin.AtomicRmwOp; const AtomicOrder = builtin.AtomicOrder; const fs = std.event.fs; const os = std.os; -const posix = os.posix; const windows = os.windows; const maxInt = std.math.maxInt; +const Thread = std.Thread; pub const Loop = struct { allocator: *mem.Allocator, @@ -17,7 +17,7 @@ pub const Loop = struct { os_data: OsData, final_resume_node: ResumeNode, pending_event_count: usize, - extra_threads: []*os.Thread, + extra_threads: []*Thread, // pre-allocated eventfds. all permanently active. // this is how we send promises to be resumed on other threads. @@ -65,7 +65,7 @@ pub const Loop = struct { const KEventFd = struct { base: ResumeNode, - kevent: posix.Kevent, + kevent: os.Kevent, }; pub const Basic = switch (builtin.os) { @@ -81,7 +81,7 @@ pub const Loop = struct { const KEventBasic = struct { base: ResumeNode, - kev: posix.Kevent, + kev: os.Kevent, }; }; @@ -127,7 +127,7 @@ pub const Loop = struct { ); errdefer self.allocator.free(self.eventfd_resume_nodes); - self.extra_threads = try self.allocator.alloc(*os.Thread, extra_thread_count); + self.extra_threads = try self.allocator.alloc(*Thread, extra_thread_count); errdefer self.allocator.free(self.extra_threads); try self.initOsData(extra_thread_count); @@ -172,32 +172,32 @@ pub const Loop = struct { .handle = undefined, .overlapped = ResumeNode.overlapped_init, }, - .eventfd = try os.linuxEventFd(1, posix.EFD_CLOEXEC | posix.EFD_NONBLOCK), - .epoll_op = posix.EPOLL_CTL_ADD, + .eventfd = try os.linuxEventFd(1, os.EFD_CLOEXEC | os.EFD_NONBLOCK), + .epoll_op = os.EPOLL_CTL_ADD, }, .next = undefined, }; self.available_eventfd_resume_nodes.push(eventfd_node); } - self.os_data.epollfd = try os.linuxEpollCreate(posix.EPOLL_CLOEXEC); + self.os_data.epollfd = try os.linuxEpollCreate(os.EPOLL_CLOEXEC); errdefer os.close(self.os_data.epollfd); - self.os_data.final_eventfd = try os.linuxEventFd(0, posix.EFD_CLOEXEC | posix.EFD_NONBLOCK); + self.os_data.final_eventfd = try os.linuxEventFd(0, os.EFD_CLOEXEC | os.EFD_NONBLOCK); errdefer os.close(self.os_data.final_eventfd); - self.os_data.final_eventfd_event = posix.epoll_event{ - .events = posix.EPOLLIN, - .data = posix.epoll_data{ .ptr = @ptrToInt(&self.final_resume_node) }, + self.os_data.final_eventfd_event = os.epoll_event{ + .events = os.EPOLLIN, + .data = os.epoll_data{ .ptr = @ptrToInt(&self.final_resume_node) }, }; try os.linuxEpollCtl( self.os_data.epollfd, - posix.EPOLL_CTL_ADD, + os.EPOLL_CTL_ADD, self.os_data.final_eventfd, &self.os_data.final_eventfd_event, ); - self.os_data.fs_thread = try os.spawnThread(self, posixFsRun); + self.os_data.fs_thread = try Thread.spawn(self, posixFsRun); errdefer { self.posixFsRequest(&self.os_data.fs_end_request); self.os_data.fs_thread.wait(); @@ -218,7 +218,7 @@ pub const Loop = struct { } } while (extra_thread_index < extra_thread_count) : (extra_thread_index += 1) { - self.extra_threads[extra_thread_index] = try os.spawnThread(self, workerRun); + self.extra_threads[extra_thread_index] = try Thread.spawn(self, workerRun); } }, builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => { @@ -240,7 +240,7 @@ pub const Loop = struct { }, }; - const empty_kevs = ([*]posix.Kevent)(undefined)[0..0]; + const empty_kevs = ([*]os.Kevent)(undefined)[0..0]; for (self.eventfd_resume_nodes) |*eventfd_node, i| { eventfd_node.* = std.atomic.Stack(ResumeNode.EventFd).Node{ @@ -251,10 +251,10 @@ pub const Loop = struct { .overlapped = ResumeNode.overlapped_init, }, // this one is for sending events - .kevent = posix.Kevent{ + .kevent = os.Kevent{ .ident = i, - .filter = posix.EVFILT_USER, - .flags = posix.EV_CLEAR | posix.EV_ADD | posix.EV_DISABLE, + .filter = os.EVFILT_USER, + .flags = os.EV_CLEAR | os.EV_ADD | os.EV_DISABLE, .fflags = 0, .data = 0, .udata = @ptrToInt(&eventfd_node.data.base), @@ -263,46 +263,46 @@ pub const Loop = struct { .next = undefined, }; self.available_eventfd_resume_nodes.push(eventfd_node); - const kevent_array = (*const [1]posix.Kevent)(&eventfd_node.data.kevent); + const kevent_array = (*const [1]os.Kevent)(&eventfd_node.data.kevent); _ = try os.bsdKEvent(self.os_data.kqfd, kevent_array, empty_kevs, null); - eventfd_node.data.kevent.flags = posix.EV_CLEAR | posix.EV_ENABLE; - eventfd_node.data.kevent.fflags = posix.NOTE_TRIGGER; + eventfd_node.data.kevent.flags = os.EV_CLEAR | os.EV_ENABLE; + eventfd_node.data.kevent.fflags = os.NOTE_TRIGGER; } // Pre-add so that we cannot get error.SystemResources // later when we try to activate it. - self.os_data.final_kevent = posix.Kevent{ + self.os_data.final_kevent = os.Kevent{ .ident = extra_thread_count, - .filter = posix.EVFILT_USER, - .flags = posix.EV_ADD | posix.EV_DISABLE, + .filter = os.EVFILT_USER, + .flags = os.EV_ADD | os.EV_DISABLE, .fflags = 0, .data = 0, .udata = @ptrToInt(&self.final_resume_node), }; - const final_kev_arr = (*const [1]posix.Kevent)(&self.os_data.final_kevent); + const final_kev_arr = (*const [1]os.Kevent)(&self.os_data.final_kevent); _ = try os.bsdKEvent(self.os_data.kqfd, final_kev_arr, empty_kevs, null); - self.os_data.final_kevent.flags = posix.EV_ENABLE; - self.os_data.final_kevent.fflags = posix.NOTE_TRIGGER; + self.os_data.final_kevent.flags = os.EV_ENABLE; + self.os_data.final_kevent.fflags = os.NOTE_TRIGGER; - self.os_data.fs_kevent_wake = posix.Kevent{ + self.os_data.fs_kevent_wake = os.Kevent{ .ident = 0, - .filter = posix.EVFILT_USER, - .flags = posix.EV_ADD | posix.EV_ENABLE, - .fflags = posix.NOTE_TRIGGER, + .filter = os.EVFILT_USER, + .flags = os.EV_ADD | os.EV_ENABLE, + .fflags = os.NOTE_TRIGGER, .data = 0, .udata = undefined, }; - self.os_data.fs_kevent_wait = posix.Kevent{ + self.os_data.fs_kevent_wait = os.Kevent{ .ident = 0, - .filter = posix.EVFILT_USER, - .flags = posix.EV_ADD | posix.EV_CLEAR, + .filter = os.EVFILT_USER, + .flags = os.EV_ADD | os.EV_CLEAR, .fflags = 0, .data = 0, .udata = undefined, }; - self.os_data.fs_thread = try os.spawnThread(self, posixFsRun); + self.os_data.fs_thread = try Thread.spawn(self, posixFsRun); errdefer { self.posixFsRequest(&self.os_data.fs_end_request); self.os_data.fs_thread.wait(); @@ -322,7 +322,7 @@ pub const Loop = struct { } } while (extra_thread_index < extra_thread_count) : (extra_thread_index += 1) { - self.extra_threads[extra_thread_index] = try os.spawnThread(self, workerRun); + self.extra_threads[extra_thread_index] = try Thread.spawn(self, workerRun); } }, builtin.Os.windows => { @@ -371,7 +371,7 @@ pub const Loop = struct { } } while (extra_thread_index < extra_thread_count) : (extra_thread_index += 1) { - self.extra_threads[extra_thread_index] = try os.spawnThread(self, workerRun); + self.extra_threads[extra_thread_index] = try Thread.spawn(self, workerRun); } }, else => {}, @@ -400,19 +400,19 @@ pub const Loop = struct { /// resume_node must live longer than the promise that it holds a reference to. /// flags must contain EPOLLET pub fn linuxAddFd(self: *Loop, fd: i32, resume_node: *ResumeNode, flags: u32) !void { - assert(flags & posix.EPOLLET == posix.EPOLLET); + assert(flags & os.EPOLLET == os.EPOLLET); self.beginOneEvent(); errdefer self.finishOneEvent(); try self.linuxModFd( fd, - posix.EPOLL_CTL_ADD, + os.EPOLL_CTL_ADD, flags, resume_node, ); } pub fn linuxModFd(self: *Loop, fd: i32, op: u32, flags: u32, resume_node: *ResumeNode) !void { - assert(flags & posix.EPOLLET == posix.EPOLLET); + assert(flags & os.EPOLLET == os.EPOLLET); var ev = os.linux.epoll_event{ .events = flags, .data = os.linux.epoll_data{ .ptr = @ptrToInt(resume_node) }, @@ -440,7 +440,7 @@ pub const Loop = struct { } } - pub async fn bsdWaitKev(self: *Loop, ident: usize, filter: i16, fflags: u32) !posix.Kevent { + pub async fn bsdWaitKev(self: *Loop, ident: usize, filter: i16, fflags: u32) !os.Kevent { // TODO #1194 suspend { resume @handle(); @@ -464,30 +464,30 @@ pub const Loop = struct { pub fn bsdAddKev(self: *Loop, resume_node: *ResumeNode.Basic, ident: usize, filter: i16, fflags: u32) !void { self.beginOneEvent(); errdefer self.finishOneEvent(); - var kev = posix.Kevent{ + var kev = os.Kevent{ .ident = ident, .filter = filter, - .flags = posix.EV_ADD | posix.EV_ENABLE | posix.EV_CLEAR, + .flags = os.EV_ADD | os.EV_ENABLE | os.EV_CLEAR, .fflags = fflags, .data = 0, .udata = @ptrToInt(&resume_node.base), }; - const kevent_array = (*const [1]posix.Kevent)(&kev); - const empty_kevs = ([*]posix.Kevent)(undefined)[0..0]; + const kevent_array = (*const [1]os.Kevent)(&kev); + const empty_kevs = ([*]os.Kevent)(undefined)[0..0]; _ = try os.bsdKEvent(self.os_data.kqfd, kevent_array, empty_kevs, null); } pub fn bsdRemoveKev(self: *Loop, ident: usize, filter: i16) void { - var kev = posix.Kevent{ + var kev = os.Kevent{ .ident = ident, .filter = filter, - .flags = posix.EV_DELETE, + .flags = os.EV_DELETE, .fflags = 0, .data = 0, .udata = 0, }; - const kevent_array = (*const [1]posix.Kevent)(&kev); - const empty_kevs = ([*]posix.Kevent)(undefined)[0..0]; + const kevent_array = (*const [1]os.Kevent)(&kev); + const empty_kevs = ([*]os.Kevent)(undefined)[0..0]; _ = os.bsdKEvent(self.os_data.kqfd, kevent_array, empty_kevs, null) catch undefined; self.finishOneEvent(); } @@ -502,8 +502,8 @@ pub const Loop = struct { eventfd_node.base.handle = next_tick_node.data; switch (builtin.os) { builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => { - const kevent_array = (*const [1]posix.Kevent)(&eventfd_node.kevent); - const empty_kevs = ([*]posix.Kevent)(undefined)[0..0]; + const kevent_array = (*const [1]os.Kevent)(&eventfd_node.kevent); + const empty_kevs = ([*]os.Kevent)(undefined)[0..0]; _ = os.bsdKEvent(self.os_data.kqfd, kevent_array, empty_kevs, null) catch { self.next_tick_queue.unget(next_tick_node); self.available_eventfd_resume_nodes.push(resume_stack_node); @@ -512,7 +512,7 @@ pub const Loop = struct { }, builtin.Os.linux => { // the pending count is already accounted for - const epoll_events = posix.EPOLLONESHOT | os.linux.EPOLLIN | os.linux.EPOLLOUT | + const epoll_events = os.EPOLLONESHOT | os.linux.EPOLLIN | os.linux.EPOLLOUT | os.linux.EPOLLET; self.linuxModFd( eventfd_node.eventfd, @@ -631,8 +631,8 @@ pub const Loop = struct { }, builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => { self.posixFsRequest(&self.os_data.fs_end_request); - const final_kevent = (*const [1]posix.Kevent)(&self.os_data.final_kevent); - const empty_kevs = ([*]posix.Kevent)(undefined)[0..0]; + const final_kevent = (*const [1]os.Kevent)(&self.os_data.final_kevent); + const empty_kevs = ([*]os.Kevent)(undefined)[0..0]; // cannot fail because we already added it and this just enables it _ = os.bsdKEvent(self.os_data.kqfd, final_kevent, empty_kevs, null) catch unreachable; return; @@ -676,7 +676,7 @@ pub const Loop = struct { ResumeNode.Id.Stop => return, ResumeNode.Id.EventFd => { const event_fd_node = @fieldParentPtr(ResumeNode.EventFd, "base", resume_node); - event_fd_node.epoll_op = posix.EPOLL_CTL_MOD; + event_fd_node.epoll_op = os.EPOLL_CTL_MOD; const stack_node = @fieldParentPtr(std.atomic.Stack(ResumeNode.EventFd).Node, "data", event_fd_node); self.available_eventfd_resume_nodes.push(stack_node); }, @@ -688,8 +688,8 @@ pub const Loop = struct { } }, builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => { - var eventlist: [1]posix.Kevent = undefined; - const empty_kevs = ([*]posix.Kevent)(undefined)[0..0]; + var eventlist: [1]os.Kevent = undefined; + const empty_kevs = ([*]os.Kevent)(undefined)[0..0]; const count = os.bsdKEvent(self.os_data.kqfd, empty_kevs, eventlist[0..], null) catch unreachable; for (eventlist[0..count]) |ev| { const resume_node = @intToPtr(*ResumeNode, ev.udata); @@ -751,8 +751,8 @@ pub const Loop = struct { self.os_data.fs_queue.put(request_node); switch (builtin.os) { builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => { - const fs_kevs = (*const [1]posix.Kevent)(&self.os_data.fs_kevent_wake); - const empty_kevs = ([*]posix.Kevent)(undefined)[0..0]; + const fs_kevs = (*const [1]os.Kevent)(&self.os_data.fs_kevent_wake); + const empty_kevs = ([*]os.Kevent)(undefined)[0..0]; _ = os.bsdKEvent(self.os_data.fs_kqfd, fs_kevs, empty_kevs, null) catch unreachable; }, builtin.Os.linux => { @@ -760,7 +760,7 @@ pub const Loop = struct { const rc = os.linux.futex_wake(&self.os_data.fs_queue_item, os.linux.FUTEX_WAKE, 1); switch (os.linux.getErrno(rc)) { 0 => {}, - posix.EINVAL => unreachable, + os.EINVAL => unreachable, else => unreachable, } }, @@ -793,8 +793,8 @@ pub const Loop = struct { }, @TagType(fs.Request.Msg).Close => |*msg| os.close(msg.fd), @TagType(fs.Request.Msg).WriteFile => |*msg| blk: { - const flags = posix.O_LARGEFILE | posix.O_WRONLY | posix.O_CREAT | - posix.O_CLOEXEC | posix.O_TRUNC; + const flags = os.O_LARGEFILE | os.O_WRONLY | os.O_CREAT | + os.O_CLOEXEC | os.O_TRUNC; const fd = os.openC(msg.path.ptr, flags, msg.mode) catch |err| { msg.result = err; break :blk; @@ -816,13 +816,13 @@ pub const Loop = struct { builtin.Os.linux => { const rc = os.linux.futex_wait(&self.os_data.fs_queue_item, os.linux.FUTEX_WAIT, 0, null); switch (os.linux.getErrno(rc)) { - 0, posix.EINTR, posix.EAGAIN => continue, + 0, os.EINTR, os.EAGAIN => continue, else => unreachable, } }, builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => { - const fs_kevs = (*const [1]posix.Kevent)(&self.os_data.fs_kevent_wait); - var out_kevs: [1]posix.Kevent = undefined; + const fs_kevs = (*const [1]os.Kevent)(&self.os_data.fs_kevent_wait); + var out_kevs: [1]os.Kevent = undefined; _ = os.bsdKEvent(self.os_data.fs_kqfd, fs_kevs, out_kevs[0..], null) catch unreachable; }, else => @compileError("Unsupported OS"), @@ -842,10 +842,10 @@ pub const Loop = struct { const KEventData = struct { kqfd: i32, - final_kevent: posix.Kevent, - fs_kevent_wake: posix.Kevent, - fs_kevent_wait: posix.Kevent, - fs_thread: *os.Thread, + final_kevent: os.Kevent, + fs_kevent_wake: os.Kevent, + fs_kevent_wait: os.Kevent, + fs_thread: *Thread, fs_kqfd: i32, fs_queue: std.atomic.Queue(fs.Request), fs_end_request: fs.RequestNode, @@ -855,7 +855,7 @@ pub const Loop = struct { epollfd: i32, final_eventfd: i32, final_eventfd_event: os.linux.epoll_event, - fs_thread: *os.Thread, + fs_thread: *Thread, fs_queue_item: i32, fs_queue: std.atomic.Queue(fs.Request), fs_end_request: fs.RequestNode, diff --git a/std/event/net.zig b/std/event/net.zig index bb492f1715..2346b1eb23 100644 --- a/std/event/net.zig +++ b/std/event/net.zig @@ -4,11 +4,9 @@ const testing = std.testing; const event = std.event; const mem = std.mem; const os = std.os; -const posix = os.posix; const Loop = std.event.Loop; const File = std.fs.File; - -const fd_t = posix.fd_t; +const fd_t = os.fd_t; pub const Server = struct { handleRequestFn: async<*mem.Allocator> fn (*Server, *const std.net.Address, File) void, @@ -47,19 +45,19 @@ pub const Server = struct { ) !void { self.handleRequestFn = handleRequestFn; - const sockfd = try os.posixSocket(posix.AF_INET, posix.SOCK_STREAM | posix.SOCK_CLOEXEC | posix.SOCK_NONBLOCK, posix.PROTO_tcp); + const sockfd = try os.posixSocket(os.AF_INET, os.SOCK_STREAM | os.SOCK_CLOEXEC | os.SOCK_NONBLOCK, os.PROTO_tcp); errdefer os.close(sockfd); self.sockfd = sockfd; try os.posixBind(sockfd, &address.os_addr); - try os.posixListen(sockfd, posix.SOMAXCONN); + try os.posixListen(sockfd, os.SOMAXCONN); self.listen_address = std.net.Address.initPosix(try os.posixGetSockName(sockfd)); self.accept_coro = try async Server.handler(self); errdefer cancel self.accept_coro.?; self.listen_resume_node.handle = self.accept_coro.?; - try self.loop.linuxAddFd(sockfd, &self.listen_resume_node, posix.EPOLLIN | posix.EPOLLOUT | posix.EPOLLET); + try self.loop.linuxAddFd(sockfd, &self.listen_resume_node, os.EPOLLIN | os.EPOLLOUT | os.EPOLLET); errdefer self.loop.removeFd(sockfd); } @@ -78,7 +76,7 @@ pub const Server = struct { while (true) { var accepted_addr: std.net.Address = undefined; // TODO just inline the following function here and don't expose it as posixAsyncAccept - if (os.posixAsyncAccept(self.sockfd.?, &accepted_addr.os_addr, posix.SOCK_NONBLOCK | posix.SOCK_CLOEXEC)) |accepted_fd| { + if (os.posixAsyncAccept(self.sockfd.?, &accepted_addr.os_addr, os.SOCK_NONBLOCK | os.SOCK_CLOEXEC)) |accepted_fd| { if (accepted_fd == -1) { // would block suspend; // we will get resumed by epoll_wait in the event loop @@ -108,22 +106,22 @@ pub const Server = struct { pub async fn connectUnixSocket(loop: *Loop, path: []const u8) !i32 { const sockfd = try os.posixSocket( - posix.AF_UNIX, - posix.SOCK_STREAM | posix.SOCK_CLOEXEC | posix.SOCK_NONBLOCK, + os.AF_UNIX, + os.SOCK_STREAM | os.SOCK_CLOEXEC | os.SOCK_NONBLOCK, 0, ); errdefer os.close(sockfd); - var sock_addr = posix.sockaddr_un{ - .family = posix.AF_UNIX, + var sock_addr = os.sockaddr_un{ + .family = os.AF_UNIX, .path = undefined, }; if (path.len > @typeOf(sock_addr.path).len) return error.NameTooLong; mem.copy(u8, sock_addr.path[0..], path); - const size = @intCast(u32, @sizeOf(posix.sa_family_t) + path.len); + const size = @intCast(u32, @sizeOf(os.sa_family_t) + path.len); try os.posixConnectAsync(sockfd, &sock_addr, size); - try await try async loop.linuxWaitFd(sockfd, posix.EPOLLIN | posix.EPOLLOUT | posix.EPOLLET); + try await try async loop.linuxWaitFd(sockfd, os.EPOLLIN | os.EPOLLOUT | os.EPOLLET); try os.posixGetSockOptConnectError(sockfd); return sockfd; @@ -143,50 +141,48 @@ pub const ReadError = error{ /// returns number of bytes read. 0 means EOF. pub async fn read(loop: *std.event.Loop, fd: fd_t, buffer: []u8) ReadError!usize { - const iov = posix.iovec{ + const iov = os.iovec{ .iov_base = buffer.ptr, .iov_len = buffer.len, }; - const iovs: *const [1]posix.iovec = &iov; + const iovs: *const [1]os.iovec = &iov; return await (async readvPosix(loop, fd, iovs, 1) catch unreachable); } pub const WriteError = error{}; pub async fn write(loop: *std.event.Loop, fd: fd_t, buffer: []const u8) WriteError!void { - const iov = posix.iovec_const{ + const iov = os.iovec_const{ .iov_base = buffer.ptr, .iov_len = buffer.len, }; - const iovs: *const [1]posix.iovec_const = &iov; + const iovs: *const [1]os.iovec_const = &iov; return await (async writevPosix(loop, fd, iovs, 1) catch unreachable); } -pub async fn writevPosix(loop: *Loop, fd: i32, iov: [*]const posix.iovec_const, count: usize) !void { +pub async fn writevPosix(loop: *Loop, fd: i32, iov: [*]const os.iovec_const, count: usize) !void { while (true) { switch (builtin.os) { - builtin.Os.macosx, builtin.Os.linux => { - const rc = posix.writev(fd, iov, count); - const err = posix.getErrno(rc); - switch (err) { + .macosx, .linux => { + switch (os.errno(os.system.writev(fd, iov, count))) { 0 => return, - posix.EINTR => continue, - posix.ESPIPE => unreachable, - posix.EINVAL => unreachable, - posix.EFAULT => unreachable, - posix.EAGAIN => { - try await (async loop.linuxWaitFd(fd, posix.EPOLLET | posix.EPOLLOUT) catch unreachable); + os.EINTR => continue, + os.ESPIPE => unreachable, + os.EINVAL => unreachable, + os.EFAULT => unreachable, + os.EAGAIN => { + try await (async loop.linuxWaitFd(fd, os.EPOLLET | os.EPOLLOUT) catch unreachable); continue; }, - posix.EBADF => unreachable, // always a race condition - posix.EDESTADDRREQ => unreachable, // connect was never called - posix.EDQUOT => unreachable, - posix.EFBIG => unreachable, - posix.EIO => return error.InputOutput, - posix.ENOSPC => unreachable, - posix.EPERM => return error.AccessDenied, - posix.EPIPE => unreachable, - else => return os.unexpectedErrorPosix(err), + os.EBADF => unreachable, // always a race condition + os.EDESTADDRREQ => unreachable, // connect was never called + os.EDQUOT => unreachable, + os.EFBIG => unreachable, + os.EIO => return error.InputOutput, + os.ENOSPC => unreachable, + os.EPERM => return error.AccessDenied, + os.EPIPE => unreachable, + else => |err| return os.unexpectedErrno(err), } }, else => @compileError("Unsupported OS"), @@ -195,27 +191,26 @@ pub async fn writevPosix(loop: *Loop, fd: i32, iov: [*]const posix.iovec_const, } /// returns number of bytes read. 0 means EOF. -pub async fn readvPosix(loop: *std.event.Loop, fd: i32, iov: [*]posix.iovec, count: usize) !usize { +pub async fn readvPosix(loop: *std.event.Loop, fd: i32, iov: [*]os.iovec, count: usize) !usize { while (true) { switch (builtin.os) { builtin.Os.linux, builtin.Os.freebsd, builtin.Os.macosx => { - const rc = posix.readv(fd, iov, count); - const err = posix.getErrno(rc); - switch (err) { + const rc = os.system.readv(fd, iov, count); + switch (os.errno(rc)) { 0 => return rc, - posix.EINTR => continue, - posix.EINVAL => unreachable, - posix.EFAULT => unreachable, - posix.EAGAIN => { - try await (async loop.linuxWaitFd(fd, posix.EPOLLET | posix.EPOLLIN) catch unreachable); + os.EINTR => continue, + os.EINVAL => unreachable, + os.EFAULT => unreachable, + os.EAGAIN => { + try await (async loop.linuxWaitFd(fd, os.EPOLLET | os.EPOLLIN) catch unreachable); continue; }, - posix.EBADF => unreachable, // always a race condition - posix.EIO => return error.InputOutput, - posix.EISDIR => unreachable, - posix.ENOBUFS => return error.SystemResources, - posix.ENOMEM => return error.SystemResources, - else => return os.unexpectedErrorPosix(err), + os.EBADF => unreachable, // always a race condition + os.EIO => return error.InputOutput, + os.EISDIR => unreachable, + os.ENOBUFS => return error.SystemResources, + os.ENOMEM => return error.SystemResources, + else => |err| return os.unexpectedErrno(err), } }, else => @compileError("Unsupported OS"), @@ -224,11 +219,11 @@ pub async fn readvPosix(loop: *std.event.Loop, fd: i32, iov: [*]posix.iovec, cou } pub async fn writev(loop: *Loop, fd: fd_t, data: []const []const u8) !void { - const iovecs = try loop.allocator.alloc(os.posix.iovec_const, data.len); + const iovecs = try loop.allocator.alloc(os.iovec_const, data.len); defer loop.allocator.free(iovecs); for (data) |buf, i| { - iovecs[i] = os.posix.iovec_const{ + iovecs[i] = os.iovec_const{ .iov_base = buf.ptr, .iov_len = buf.len, }; @@ -238,11 +233,11 @@ pub async fn writev(loop: *Loop, fd: fd_t, data: []const []const u8) !void { } pub async fn readv(loop: *Loop, fd: fd_t, data: []const []u8) !usize { - const iovecs = try loop.allocator.alloc(os.posix.iovec, data.len); + const iovecs = try loop.allocator.alloc(os.iovec, data.len); defer loop.allocator.free(iovecs); for (data) |buf, i| { - iovecs[i] = os.posix.iovec{ + iovecs[i] = os.iovec{ .iov_base = buf.ptr, .iov_len = buf.len, }; @@ -254,11 +249,11 @@ pub async fn readv(loop: *Loop, fd: fd_t, data: []const []u8) !usize { pub async fn connect(loop: *Loop, _address: *const std.net.Address) !File { var address = _address.*; // TODO https://github.com/ziglang/zig/issues/1592 - const sockfd = try os.posixSocket(posix.AF_INET, posix.SOCK_STREAM | posix.SOCK_CLOEXEC | posix.SOCK_NONBLOCK, posix.PROTO_tcp); + const sockfd = try os.posixSocket(os.AF_INET, os.SOCK_STREAM | os.SOCK_CLOEXEC | os.SOCK_NONBLOCK, os.PROTO_tcp); errdefer os.close(sockfd); - try os.posixConnectAsync(sockfd, &address.os_addr, @sizeOf(posix.sockaddr_in)); - try await try async loop.linuxWaitFd(sockfd, posix.EPOLLIN | posix.EPOLLOUT | posix.EPOLLET); + try os.posixConnectAsync(sockfd, &address.os_addr, @sizeOf(os.sockaddr_in)); + try await try async loop.linuxWaitFd(sockfd, os.EPOLLIN | os.EPOLLOUT | os.EPOLLET); try os.posixGetSockOptConnectError(sockfd); return File.openHandle(sockfd); diff --git a/std/fs/path.zig b/std/fs/path.zig index 4c73834609..b82ddc8117 100644 --- a/std/fs/path.zig +++ b/std/fs/path.zig @@ -9,24 +9,21 @@ const fmt = std.fmt; const Allocator = mem.Allocator; const os = std.os; const math = std.math; -const posix = os.posix; const windows = os.windows; const cstr = std.cstr; pub const sep_windows = '\\'; pub const sep_posix = '/'; -pub const sep = if (is_windows) sep_windows else sep_posix; +pub const sep = if (windows.is_the_target) sep_windows else sep_posix; pub const sep_str = [1]u8{sep}; pub const delimiter_windows = ';'; pub const delimiter_posix = ':'; -pub const delimiter = if (is_windows) delimiter_windows else delimiter_posix; - -const is_windows = builtin.os == builtin.Os.windows; +pub const delimiter = if (windows.is_the_target) delimiter_windows else delimiter_posix; pub fn isSep(byte: u8) bool { - if (is_windows) { + if (windows.is_the_target) { return byte == '/' or byte == '\\'; } else { return byte == '/'; @@ -76,7 +73,7 @@ fn joinSep(allocator: *Allocator, separator: u8, paths: []const []const u8) ![]u return buf; } -pub const join = if (is_windows) joinWindows else joinPosix; +pub const join = if (windows.is_the_target) joinWindows else joinPosix; /// Naively combines a series of paths with the native path seperator. /// Allocates memory for the result, which must be freed by the caller. @@ -133,7 +130,7 @@ test "join" { } pub fn isAbsolute(path: []const u8) bool { - if (is_windows) { + if (windows.is_the_target) { return isAbsoluteWindows(path); } else { return isAbsolutePosix(path); @@ -312,7 +309,7 @@ test "windowsParsePath" { } pub fn diskDesignator(path: []const u8) []const u8 { - if (is_windows) { + if (windows.is_the_target) { return diskDesignatorWindows(path); } else { return ""; @@ -377,7 +374,7 @@ fn asciiEqlIgnoreCase(s1: []const u8, s2: []const u8) bool { /// On Windows, this calls `resolveWindows` and on POSIX it calls `resolvePosix`. pub fn resolve(allocator: *Allocator, paths: []const []const u8) ![]u8 { - if (is_windows) { + if (windows.is_the_target) { return resolveWindows(allocator, paths); } else { return resolvePosix(allocator, paths); @@ -394,7 +391,7 @@ pub fn resolve(allocator: *Allocator, paths: []const []const u8) ![]u8 { /// Without performing actual syscalls, resolving `..` could be incorrect. pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 { if (paths.len == 0) { - assert(is_windows); // resolveWindows called on non windows can't use getCwd + assert(windows.is_the_target); // resolveWindows called on non windows can't use getCwd return os.getCwdAlloc(allocator); } @@ -489,7 +486,7 @@ pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 { result_disk_designator = result[0..result_index]; }, WindowsPath.Kind.None => { - assert(is_windows); // resolveWindows called on non windows can't use getCwd + assert(windows.is_the_target); // resolveWindows called on non windows can't use getCwd const cwd = try os.getCwdAlloc(allocator); defer allocator.free(cwd); const parsed_cwd = windowsParsePath(cwd); @@ -504,7 +501,7 @@ pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 { }, } } else { - assert(is_windows); // resolveWindows called on non windows can't use getCwd + assert(windows.is_the_target); // resolveWindows called on non windows can't use getCwd // TODO call get cwd for the result_disk_designator instead of the global one const cwd = try os.getCwdAlloc(allocator); defer allocator.free(cwd); @@ -575,7 +572,7 @@ pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 { /// Without performing actual syscalls, resolving `..` could be incorrect. pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 { if (paths.len == 0) { - assert(!is_windows); // resolvePosix called on windows can't use getCwd + assert(!windows.is_the_target); // resolvePosix called on windows can't use getCwd return os.getCwdAlloc(allocator); } @@ -597,7 +594,7 @@ pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 { if (have_abs) { result = try allocator.alloc(u8, max_size); } else { - assert(!is_windows); // resolvePosix called on windows can't use getCwd + assert(!windows.is_the_target); // resolvePosix called on windows can't use getCwd const cwd = try os.getCwdAlloc(allocator); defer allocator.free(cwd); result = try allocator.alloc(u8, max_size + cwd.len + 1); @@ -638,7 +635,7 @@ pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 { test "resolve" { const cwd = try os.getCwdAlloc(debug.global_allocator); - if (is_windows) { + if (windows.is_the_target) { if (windowsParsePath(cwd).kind == WindowsPath.Kind.Drive) { cwd[0] = asciiUpper(cwd[0]); } @@ -650,7 +647,7 @@ test "resolve" { } test "resolveWindows" { - if (is_windows) { + if (windows.is_the_target) { const cwd = try os.getCwdAlloc(debug.global_allocator); const parsed_cwd = windowsParsePath(cwd); { @@ -716,7 +713,7 @@ fn testResolvePosix(paths: []const []const u8) []u8 { /// If the path is a file in the current directory (no directory component) /// then returns null pub fn dirname(path: []const u8) ?[]const u8 { - if (is_windows) { + if (windows.is_the_target) { return dirnameWindows(path); } else { return dirnamePosix(path); @@ -848,7 +845,7 @@ fn testDirnameWindows(input: []const u8, expected_output: ?[]const u8) void { } pub fn basename(path: []const u8) []const u8 { - if (is_windows) { + if (windows.is_the_target) { return basenameWindows(path); } else { return basenamePosix(path); @@ -964,7 +961,7 @@ fn testBasenameWindows(input: []const u8, expected_output: []const u8) void { /// string is returned. /// On Windows this canonicalizes the drive to a capital letter and paths to `\\`. pub fn relative(allocator: *Allocator, from: []const u8, to: []const u8) ![]u8 { - if (is_windows) { + if (windows.is_the_target) { return relativeWindows(allocator, from, to); } else { return relativePosix(allocator, from, to); diff --git a/std/heap.zig b/std/heap.zig index 3bbb35e65d..4ff36f3f5f 100644 --- a/std/heap.zig +++ b/std/heap.zig @@ -5,7 +5,6 @@ const testing = std.testing; const mem = std.mem; const os = std.os; const builtin = @import("builtin"); -const Os = builtin.Os; const c = std.c; const maxInt = std.math.maxInt; @@ -51,201 +50,190 @@ pub const DirectAllocator = struct { if (n == 0) return (([*]u8)(undefined))[0..0]; - switch (builtin.os) { - Os.linux, Os.macosx, Os.ios, Os.freebsd, Os.netbsd => { - const p = os.posix; - const alloc_size = if (alignment <= os.page_size) n else n + alignment; - const addr = p.mmap(null, alloc_size, p.PROT_READ | p.PROT_WRITE, p.MAP_PRIVATE | p.MAP_ANONYMOUS, -1, 0); - if (addr == p.MAP_FAILED) return error.OutOfMemory; - if (alloc_size == n) return @intToPtr([*]u8, addr)[0..n]; - - const aligned_addr = mem.alignForward(addr, alignment); - - // Unmap the extra bytes that were only requested in order to guarantee - // that the range of memory we were provided had a proper alignment in - // it somewhere. The extra bytes could be at the beginning, or end, or both. - const unused_start_len = aligned_addr - addr; - if (unused_start_len != 0) { - const err = p.munmap(addr, unused_start_len); - assert(p.getErrno(err) == 0); - } - const aligned_end_addr = std.mem.alignForward(aligned_addr + n, os.page_size); - const unused_end_len = addr + alloc_size - aligned_end_addr; - if (unused_end_len != 0) { - const err = p.munmap(aligned_end_addr, unused_end_len); - assert(p.getErrno(err) == 0); - } + if (os.windows.is_the_target) { + const w = os.windows; + + // Although officially it's at least aligned to page boundary, + // Windows is known to reserve pages on a 64K boundary. It's + // even more likely that the requested alignment is <= 64K than + // 4K, so we're just allocating blindly and hoping for the best. + // see https://devblogs.microsoft.com/oldnewthing/?p=42223 + const addr = w.VirtualAlloc( + null, + n, + w.MEM_COMMIT | w.MEM_RESERVE, + w.PAGE_READWRITE, + ) catch return error.OutOfMemory; + + // If the allocation is sufficiently aligned, use it. + if (@ptrToInt(addr) & (alignment - 1) == 0) { + return @ptrCast([*]u8, addr)[0..n]; + } - return @intToPtr([*]u8, aligned_addr)[0..n]; - }, - .windows => { - const w = os.windows; - - // Although officially it's at least aligned to page boundary, - // Windows is known to reserve pages on a 64K boundary. It's - // even more likely that the requested alignment is <= 64K than - // 4K, so we're just allocating blindly and hoping for the best. - // see https://devblogs.microsoft.com/oldnewthing/?p=42223 - const addr = w.VirtualAlloc( + // If it wasn't, actually do an explicitely aligned allocation. + w.VirtualFree(addr, 0, w.MEM_RELEASE); + const alloc_size = n + alignment; + + const final_addr = while (true) { + // Reserve a range of memory large enough to find a sufficiently + // aligned address. + const reserved_addr = w.VirtualAlloc( null, + alloc_size, + w.MEM_RESERVE, + w.PAGE_NOACCESS, + ) catch return error.OutOfMemory; + const aligned_addr = mem.alignForward(@ptrToInt(reserved_addr), alignment); + + // Release the reserved pages (not actually used). + w.VirtualFree(reserved_addr, 0, w.MEM_RELEASE); + + // At this point, it is possible that another thread has + // obtained some memory space that will cause the next + // VirtualAlloc call to fail. To handle this, we will retry + // until it succeeds. + return w.VirtualAlloc( + @intToPtr(*c_void, aligned_addr), n, w.MEM_COMMIT | w.MEM_RESERVE, w.PAGE_READWRITE, - ) orelse return error.OutOfMemory; + ) catch continue; + }; - // If the allocation is sufficiently aligned, use it. - if (@ptrToInt(addr) & (alignment - 1) == 0) { - return @ptrCast([*]u8, addr)[0..n]; - } + return @ptrCast([*]u8, final_addr)[0..n]; + } - // If it wasn't, actually do an explicitely aligned allocation. - if (w.VirtualFree(addr, 0, w.MEM_RELEASE) == 0) unreachable; - const alloc_size = n + alignment; - - const final_addr = while (true) { - // Reserve a range of memory large enough to find a sufficiently - // aligned address. - const reserved_addr = w.VirtualAlloc( - null, - alloc_size, - w.MEM_RESERVE, - w.PAGE_NOACCESS, - ) orelse return error.OutOfMemory; - const aligned_addr = mem.alignForward(@ptrToInt(reserved_addr), alignment); - - // Release the reserved pages (not actually used). - if (w.VirtualFree(reserved_addr, 0, w.MEM_RELEASE) == 0) unreachable; - - // At this point, it is possible that another thread has - // obtained some memory space that will cause the next - // VirtualAlloc call to fail. To handle this, we will retry - // until it succeeds. - if (w.VirtualAlloc( - @intToPtr(*c_void, aligned_addr), - n, - w.MEM_COMMIT | w.MEM_RESERVE, - w.PAGE_READWRITE, - )) |ptr| break ptr; - } else unreachable; // TODO else unreachable should not be necessary - - return @ptrCast([*]u8, final_addr)[0..n]; - }, - else => @compileError("Unsupported OS"), + const alloc_size = if (alignment <= os.page_size) n else n + alignment; + const addr = os.mmap( + null, + alloc_size, + os.PROT_READ | os.PROT_WRITE, + os.MAP_PRIVATE | os.MAP_ANONYMOUS, + -1, + 0, + ) catch return error.OutOfMemory; + if (alloc_size == n) return @intToPtr([*]u8, addr)[0..n]; + + const aligned_addr = mem.alignForward(addr, alignment); + + // Unmap the extra bytes that were only requested in order to guarantee + // that the range of memory we were provided had a proper alignment in + // it somewhere. The extra bytes could be at the beginning, or end, or both. + const unused_start_len = aligned_addr - addr; + if (unused_start_len != 0) { + os.munmap(addr, unused_start_len); + } + const aligned_end_addr = std.mem.alignForward(aligned_addr + n, os.page_size); + const unused_end_len = addr + alloc_size - aligned_end_addr; + if (unused_end_len != 0) { + os.munmap(aligned_end_addr, unused_end_len); } + + return @intToPtr([*]u8, aligned_addr)[0..n]; } fn shrink(allocator: *Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) []u8 { - switch (builtin.os) { - Os.linux, Os.macosx, Os.ios, Os.freebsd, Os.netbsd => { + if (os.windows.is_the_target) { + const w = os.windows; + if (new_size == 0) { + // From the docs: + // "If the dwFreeType parameter is MEM_RELEASE, this parameter + // must be 0 (zero). The function frees the entire region that + // is reserved in the initial allocation call to VirtualAlloc." + // So we can only use MEM_RELEASE when actually releasing the + // whole allocation. + w.VirtualFree(old_mem.ptr, 0, w.MEM_RELEASE); + } else { const base_addr = @ptrToInt(old_mem.ptr); const old_addr_end = base_addr + old_mem.len; const new_addr_end = base_addr + new_size; const new_addr_end_rounded = mem.alignForward(new_addr_end, os.page_size); if (old_addr_end > new_addr_end_rounded) { - _ = os.posix.munmap(new_addr_end_rounded, old_addr_end - new_addr_end_rounded); - } - return old_mem[0..new_size]; - }, - .windows => { - const w = os.windows; - if (new_size == 0) { - // From the docs: - // "If the dwFreeType parameter is MEM_RELEASE, this parameter - // must be 0 (zero). The function frees the entire region that - // is reserved in the initial allocation call to VirtualAlloc." - // So we can only use MEM_RELEASE when actually releasing the - // whole allocation. - if (w.VirtualFree(old_mem.ptr, 0, w.MEM_RELEASE) == 0) unreachable; - } else { - const base_addr = @ptrToInt(old_mem.ptr); - const old_addr_end = base_addr + old_mem.len; - const new_addr_end = base_addr + new_size; - const new_addr_end_rounded = mem.alignForward(new_addr_end, os.page_size); - if (old_addr_end > new_addr_end_rounded) { - // For shrinking that is not releasing, we will only - // decommit the pages not needed anymore. - if (w.VirtualFree( - @intToPtr(*c_void, new_addr_end_rounded), - old_addr_end - new_addr_end_rounded, - w.MEM_DECOMMIT, - ) == 0) unreachable; - } + // For shrinking that is not releasing, we will only + // decommit the pages not needed anymore. + w.VirtualFree( + @intToPtr(*c_void, new_addr_end_rounded), + old_addr_end - new_addr_end_rounded, + w.MEM_DECOMMIT, + ); } - return old_mem[0..new_size]; - }, - else => @compileError("Unsupported OS"), + } + return old_mem[0..new_size]; } + const base_addr = @ptrToInt(old_mem.ptr); + const old_addr_end = base_addr + old_mem.len; + const new_addr_end = base_addr + new_size; + const new_addr_end_rounded = mem.alignForward(new_addr_end, os.page_size); + if (old_addr_end > new_addr_end_rounded) { + os.munmap(new_addr_end_rounded, old_addr_end - new_addr_end_rounded); + } + return old_mem[0..new_size]; } fn realloc(allocator: *Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) ![]u8 { - switch (builtin.os) { - Os.linux, Os.macosx, Os.ios, Os.freebsd, Os.netbsd => { - if (new_size <= old_mem.len and new_align <= old_align) { - return shrink(allocator, old_mem, old_align, new_size, new_align); - } - const result = try alloc(allocator, new_size, new_align); - if (old_mem.len != 0) { - @memcpy(result.ptr, old_mem.ptr, std.math.min(old_mem.len, result.len)); - _ = os.posix.munmap(@ptrToInt(old_mem.ptr), old_mem.len); - } - return result; - }, - .windows => { - if (old_mem.len == 0) { - return alloc(allocator, new_size, new_align); - } - - if (new_size <= old_mem.len and new_align <= old_align) { - return shrink(allocator, old_mem, old_align, new_size, new_align); - } - - const w = os.windows; - const base_addr = @ptrToInt(old_mem.ptr); + if (os.windows.is_the_target) { + if (old_mem.len == 0) { + return alloc(allocator, new_size, new_align); + } - if (new_align > old_align and base_addr & (new_align - 1) != 0) { - // Current allocation doesn't satisfy the new alignment. - // For now we'll do a new one no matter what, but maybe - // there is something smarter to do instead. - const result = try alloc(allocator, new_size, new_align); - assert(old_mem.len != 0); - @memcpy(result.ptr, old_mem.ptr, std.math.min(old_mem.len, result.len)); - if (w.VirtualFree(old_mem.ptr, 0, w.MEM_RELEASE) == 0) unreachable; + if (new_size <= old_mem.len and new_align <= old_align) { + return shrink(allocator, old_mem, old_align, new_size, new_align); + } - return result; - } + const w = os.windows; + const base_addr = @ptrToInt(old_mem.ptr); - const old_addr_end = base_addr + old_mem.len; - const old_addr_end_rounded = mem.alignForward(old_addr_end, os.page_size); - const new_addr_end = base_addr + new_size; - const new_addr_end_rounded = mem.alignForward(new_addr_end, os.page_size); - if (new_addr_end_rounded == old_addr_end_rounded) { - // The reallocation fits in the already allocated pages. - return @ptrCast([*]u8, old_mem.ptr)[0..new_size]; - } - assert(new_addr_end_rounded > old_addr_end_rounded); + if (new_align > old_align and base_addr & (new_align - 1) != 0) { + // Current allocation doesn't satisfy the new alignment. + // For now we'll do a new one no matter what, but maybe + // there is something smarter to do instead. + const result = try alloc(allocator, new_size, new_align); + assert(old_mem.len != 0); + @memcpy(result.ptr, old_mem.ptr, std.math.min(old_mem.len, result.len)); + w.VirtualFree(old_mem.ptr, 0, w.MEM_RELEASE); - // We need to commit new pages. - const additional_size = new_addr_end - old_addr_end_rounded; - const realloc_addr = w.VirtualAlloc( - @intToPtr(*c_void, old_addr_end_rounded), - additional_size, - w.MEM_COMMIT | w.MEM_RESERVE, - w.PAGE_READWRITE, - ) orelse { - // Committing new pages at the end of the existing allocation - // failed, we need to try a new one. - const new_alloc_mem = try alloc(allocator, new_size, new_align); - @memcpy(new_alloc_mem.ptr, old_mem.ptr, old_mem.len); - if (w.VirtualFree(old_mem.ptr, 0, w.MEM_RELEASE) == 0) unreachable; - - return new_alloc_mem; - }; + return result; + } - assert(@ptrToInt(realloc_addr) == old_addr_end_rounded); + const old_addr_end = base_addr + old_mem.len; + const old_addr_end_rounded = mem.alignForward(old_addr_end, os.page_size); + const new_addr_end = base_addr + new_size; + const new_addr_end_rounded = mem.alignForward(new_addr_end, os.page_size); + if (new_addr_end_rounded == old_addr_end_rounded) { + // The reallocation fits in the already allocated pages. return @ptrCast([*]u8, old_mem.ptr)[0..new_size]; - }, - else => @compileError("Unsupported OS"), + } + assert(new_addr_end_rounded > old_addr_end_rounded); + + // We need to commit new pages. + const additional_size = new_addr_end - old_addr_end_rounded; + const realloc_addr = w.kernel32.VirtualAlloc( + @intToPtr(*c_void, old_addr_end_rounded), + additional_size, + w.MEM_COMMIT | w.MEM_RESERVE, + w.PAGE_READWRITE, + ) orelse { + // Committing new pages at the end of the existing allocation + // failed, we need to try a new one. + const new_alloc_mem = try alloc(allocator, new_size, new_align); + @memcpy(new_alloc_mem.ptr, old_mem.ptr, old_mem.len); + w.VirtualFree(old_mem.ptr, 0, w.MEM_RELEASE); + + return new_alloc_mem; + }; + + assert(@ptrToInt(realloc_addr) == old_addr_end_rounded); + return @ptrCast([*]u8, old_mem.ptr)[0..new_size]; + } + if (new_size <= old_mem.len and new_align <= old_align) { + return shrink(allocator, old_mem, old_align, new_size, new_align); } + const result = try alloc(allocator, new_size, new_align); + if (old_mem.len != 0) { + @memcpy(result.ptr, old_mem.ptr, std.math.min(old_mem.len, result.len)); + os.munmap(@ptrToInt(old_mem.ptr), old_mem.len); + } + return result; } }; diff --git a/std/mem.zig b/std/mem.zig index 50ed2771ec..4f7ea768d7 100644 --- a/std/mem.zig +++ b/std/mem.zig @@ -1466,17 +1466,17 @@ test "std.mem.alignForward" { pub fn getBaseAddress() usize { switch (builtin.os) { .linux => { - const base = std.os.posix.getauxval(std.elf.AT_BASE); + const base = std.os.system.getauxval(std.elf.AT_BASE); if (base != 0) { return base; } - const phdr = std.os.posix.getauxval(std.elf.AT_PHDR); + const phdr = std.os.system.getauxval(std.elf.AT_PHDR); return phdr - @sizeOf(std.elf.Ehdr); }, .macosx, .freebsd, .netbsd => { return @ptrToInt(&std.c._mh_execute_header); }, - .windows => return @ptrToInt(windows.GetModuleHandleW(null)), + .windows => return @ptrToInt(windows.kernel32.GetModuleHandleW(null)), else => @compileError("Unsupported OS"), } } diff --git a/std/mutex.zig b/std/mutex.zig index 2b3ac4e366..6b8e586ea8 100644 --- a/std/mutex.zig +++ b/std/mutex.zig @@ -152,9 +152,9 @@ test "std.Mutex" { testing.expect(context.data == TestContext.incr_count); } else { const thread_count = 10; - var threads: [thread_count]*std.os.Thread = undefined; + var threads: [thread_count]*std.Thread = undefined; for (threads) |*t| { - t.* = try std.os.spawnThread(&context, worker); + t.* = try std.Thread.spawn(&context, worker); } for (threads) |t| t.wait(); diff --git a/std/net.zig b/std/net.zig index bb292efd83..b426759701 100644 --- a/std/net.zig +++ b/std/net.zig @@ -2,8 +2,8 @@ const std = @import("std.zig"); const builtin = @import("builtin"); const assert = std.debug.assert; const net = @This(); -const posix = std.os.posix; const mem = std.mem; +const os = std.os; pub const TmpWinAddr = struct { family: u8, @@ -12,7 +12,7 @@ pub const TmpWinAddr = struct { pub const OsAddress = switch (builtin.os) { builtin.Os.windows => TmpWinAddr, - else => posix.sockaddr, + else => os.sockaddr, }; pub const Address = struct { @@ -20,9 +20,9 @@ pub const Address = struct { pub fn initIp4(ip4: u32, _port: u16) Address { return Address{ - .os_addr = posix.sockaddr{ - .in = posix.sockaddr_in{ - .family = posix.AF_INET, + .os_addr = os.sockaddr{ + .in = os.sockaddr_in{ + .family = os.AF_INET, .port = mem.nativeToBig(u16, _port), .addr = ip4, .zero = []u8{0} ** 8, @@ -33,10 +33,10 @@ pub const Address = struct { pub fn initIp6(ip6: *const Ip6Addr, _port: u16) Address { return Address{ - .family = posix.AF_INET6, - .os_addr = posix.sockaddr{ - .in6 = posix.sockaddr_in6{ - .family = posix.AF_INET6, + .family = os.AF_INET6, + .os_addr = os.sockaddr{ + .in6 = os.sockaddr_in6{ + .family = os.AF_INET6, .port = mem.nativeToBig(u16, _port), .flowinfo = 0, .addr = ip6.addr, @@ -50,18 +50,18 @@ pub const Address = struct { return mem.bigToNative(u16, self.os_addr.in.port); } - pub fn initPosix(addr: posix.sockaddr) Address { + pub fn initPosix(addr: os.sockaddr) Address { return Address{ .os_addr = addr }; } pub fn format(self: *const Address, out_stream: var) !void { switch (self.os_addr.in.family) { - posix.AF_INET => { + os.AF_INET => { const native_endian_port = mem.bigToNative(u16, self.os_addr.in.port); const bytes = ([]const u8)((*self.os_addr.in.addr)[0..1]); try out_stream.print("{}.{}.{}.{}:{}", bytes[0], bytes[1], bytes[2], bytes[3], native_endian_port); }, - posix.AF_INET6 => { + os.AF_INET6 => { const native_endian_port = mem.bigToNative(u16, self.os_addr.in6.port); try out_stream.print("[TODO render ip6 address]:{}", native_endian_port); }, diff --git a/std/os.zig b/std/os.zig index 4cd847cf8f..3b29570fcd 100644 --- a/std/os.zig +++ b/std/os.zig @@ -164,7 +164,7 @@ pub fn raise(sig: u8) RaiseError!void { } if (windows.is_the_target) { - @compileError("TODO implement std.posix.raise for Windows"); + @compileError("TODO implement std.os.raise for Windows"); } var set: system.sigset_t = undefined; @@ -690,20 +690,6 @@ pub fn getenvC(key: [*]const u8) ?[]const u8 { return getenv(mem.toSliceConst(u8, key)); } -/// See std.elf for the constants. -pub fn getauxval(index: usize) usize { - if (builtin.link_libc) { - return usize(system.getauxval(index)); - } else if (linux.elf_aux_maybe) |auxv| { - var i: usize = 0; - while (auxv[i].a_type != std.elf.AT_NULL) : (i += 1) { - if (auxv[i].a_type == index) - return auxv[i].a_un.a_val; - } - } - return 0; -} - pub const GetCwdError = error{ NameTooLong, CurrentWorkingDirectoryUnlinked, @@ -1198,7 +1184,7 @@ pub fn isatty(handle: fd_t) bool { return windows.kernel32.GetConsoleMode(handle, &out) != 0; } if (wasi.is_the_target) { - @compileError("TODO implement std.os.posix.isatty for WASI"); + @compileError("TODO implement std.os.isatty for WASI"); } if (linux.is_the_target) { var wsz: system.winsize = undefined; @@ -2365,6 +2351,23 @@ pub fn clock_getres(clk_id: i32, res: *timespec) ClockGetTimeError!void { } } +pub const SchedGetAffinityError = error{ + PermissionDenied, + Unexpected, +}; + +pub fn sched_getaffinity(pid: pid_t) SchedGetAffinityError!cpu_set_t { + var set: cpu_set_t = undefined; + switch (errno(system.sched_getaffinity(pid, &set))) { + 0 => return set, + EFAULT => unreachable, + EINVAL => unreachable, + ESRCH => unreachable, + EPERM => return error.PermissionDenied, + else => |err| return unexpectedErrno(err), + } +} + /// Used to convert a slice to a null terminated slice on the stack. /// TODO https://github.com/ziglang/zig/issues/287 pub fn toPosixPath(file_path: []const u8) ![PATH_MAX]u8 { diff --git a/std/os/bits/linux.zig b/std/os/bits/linux.zig index 7957187896..f40beeaf68 100644 --- a/std/os/bits/linux.zig +++ b/std/os/bits/linux.zig @@ -1,3 +1,5 @@ +const std = @import("../../std.zig"); + pub use @import("linux/errno.zig"); pub use switch (builtin.arch) { .x86_64 => @import("linux/x86_64.zig"), @@ -907,3 +909,26 @@ pub const pthread_attr_t = extern struct { __size: [56]u8, __align: c_long, }; + +pub const CPU_SETSIZE = 128; +pub const cpu_set_t = [CPU_SETSIZE / @sizeOf(usize)]usize; +pub const cpu_count_t = @IntType(false, std.math.log2(CPU_SETSIZE * 8)); + +pub fn CPU_COUNT(set: cpu_set_t) cpu_count_t { + var sum: cpu_count_t = 0; + for (set) |x| { + sum += @popCount(usize, x); + } + return sum; +} + +// TODO port these over +//#define CPU_SET(i, set) CPU_SET_S(i,sizeof(cpu_set_t),set) +//#define CPU_CLR(i, set) CPU_CLR_S(i,sizeof(cpu_set_t),set) +//#define CPU_ISSET(i, set) CPU_ISSET_S(i,sizeof(cpu_set_t),set) +//#define CPU_AND(d,s1,s2) CPU_AND_S(sizeof(cpu_set_t),d,s1,s2) +//#define CPU_OR(d,s1,s2) CPU_OR_S(sizeof(cpu_set_t),d,s1,s2) +//#define CPU_XOR(d,s1,s2) CPU_XOR_S(sizeof(cpu_set_t),d,s1,s2) +//#define CPU_COUNT(set) CPU_COUNT_S(sizeof(cpu_set_t),set) +//#define CPU_ZERO(set) CPU_ZERO_S(sizeof(cpu_set_t),set) +//#define CPU_EQUAL(s1,s2) CPU_EQUAL_S(sizeof(cpu_set_t),s1,s2) diff --git a/std/os/linux.zig b/std/os/linux.zig index 40e9fa5108..88a2980129 100644 --- a/std/os/linux.zig +++ b/std/os/linux.zig @@ -1,8 +1,878 @@ +// This file provides the system interface functions for Linux matching those +// that are provided by libc, whether or not libc is linked. The following +// abstractions are made: +// * Work around kernel bugs and limitations. For example, see sendmmsg. +// * Implement all the syscalls in the same way that libc functions will +// provide `rename` when only the `renameat` syscall exists. +// * Does not support POSIX thread cancellation. const std = @import("../std.zig"); const builtin = @import("builtin"); +const assert = std.debug.assert; +const maxInt = std.math.maxInt; +const elf = std.elf; +const vdso = @import("linux/vdso.zig"); +const dl = @import("../dynamic_library.zig"); + pub const is_the_target = builtin.os == .linux; -pub const sys = @import("linux/sys.zig"); -pub use if (builtin.link_libc) std.c else sys; +pub use switch (builtin.arch) { + .x86_64 => @import("linux/x86_64.zig"), + .aarch64 => @import("linux/arm64.zig"), + else => struct {}, +}; +pub use @import("bits.zig"); + +/// Set by startup code, used by `getauxval`. +pub var elf_aux_maybe: ?[*]std.elf.Auxv = null; + +/// See `std.elf` for the constants. +pub fn getauxval(index: usize) usize { + const auxv = elf_aux_maybe orelse return 0; + var i: usize = 0; + while (auxv[i].a_type != std.elf.AT_NULL) : (i += 1) { + if (auxv[i].a_type == index) + return auxv[i].a_un.a_val; + } + return 0; +} + +/// Get the errno from a syscall return value, or 0 for no error. +pub fn getErrno(r: usize) u12 { + const signed_r = @bitCast(isize, r); + return if (signed_r > -4096 and signed_r < 0) @intCast(u12, -signed_r) else 0; +} + +pub fn dup2(old: i32, new: i32) usize { + if (@hasDecl(@This(), "SYS_dup2")) { + return syscall2(SYS_dup2, @bitCast(usize, isize(old)), @bitCast(usize, isize(new))); + } else { + if (old == new) { + if (std.debug.runtime_safety) { + const rc = syscall2(SYS_fcntl, @bitCast(usize, isize(old)), F_GETFD); + if (@bitCast(isize, rc) < 0) return rc; + } + return @intCast(usize, old); + } else { + return syscall3(SYS_dup3, @bitCast(usize, isize(old)), @bitCast(usize, isize(new)), 0); + } + } +} + +pub fn dup3(old: i32, new: i32, flags: u32) usize { + return syscall3(SYS_dup3, @bitCast(usize, isize(old)), @bitCast(usize, isize(new)), flags); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn chdir(path: [*]const u8) usize { + return syscall1(SYS_chdir, @ptrToInt(path)); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn chroot(path: [*]const u8) usize { + return syscall1(SYS_chroot, @ptrToInt(path)); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn execve(path: [*]const u8, argv: [*]const ?[*]const u8, envp: [*]const ?[*]const u8) usize { + return syscall3(SYS_execve, @ptrToInt(path), @ptrToInt(argv), @ptrToInt(envp)); +} + +pub fn fork() usize { + if (@hasDecl(@This(), "SYS_fork")) { + return syscall0(SYS_fork); + } else { + return syscall2(SYS_clone, SIGCHLD, 0); + } +} + +/// This must be inline, and inline call the syscall function, because if the +/// child does a return it will clobber the parent's stack. +/// It is advised to avoid this function and use clone instead, because +/// the compiler is not aware of how vfork affects control flow and you may +/// see different results in optimized builds. +pub inline fn vfork() usize { + return @inlineCall(syscall0, SYS_vfork); +} + +pub fn futex_wait(uaddr: *const i32, futex_op: u32, val: i32, timeout: ?*timespec) usize { + return syscall4(SYS_futex, @ptrToInt(uaddr), futex_op, @bitCast(u32, val), @ptrToInt(timeout)); +} + +pub fn futex_wake(uaddr: *const i32, futex_op: u32, val: i32) usize { + return syscall3(SYS_futex, @ptrToInt(uaddr), futex_op, @bitCast(u32, val)); +} + +pub fn getcwd(buf: [*]u8, size: usize) usize { + return syscall2(SYS_getcwd, @ptrToInt(buf), size); +} + +pub fn getdents(fd: i32, dirp: [*]u8, count: usize) usize { + return syscall3(SYS_getdents, @bitCast(usize, isize(fd)), @ptrToInt(dirp), count); +} + +pub fn getdents64(fd: i32, dirp: [*]u8, count: usize) usize { + return syscall3(SYS_getdents64, @bitCast(usize, isize(fd)), @ptrToInt(dirp), count); +} + +pub fn inotify_init1(flags: u32) usize { + return syscall1(SYS_inotify_init1, flags); +} + +pub fn inotify_add_watch(fd: i32, pathname: [*]const u8, mask: u32) usize { + return syscall3(SYS_inotify_add_watch, @bitCast(usize, isize(fd)), @ptrToInt(pathname), mask); +} + +pub fn inotify_rm_watch(fd: i32, wd: i32) usize { + return syscall2(SYS_inotify_rm_watch, @bitCast(usize, isize(fd)), @bitCast(usize, isize(wd))); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn readlink(noalias path: [*]const u8, noalias buf_ptr: [*]u8, buf_len: usize) usize { + if (@hasDecl(@This(), "SYS_readlink")) { + return syscall3(SYS_readlink, @ptrToInt(path), @ptrToInt(buf_ptr), buf_len); + } else { + return syscall4(SYS_readlinkat, AT_FDCWD, @ptrToInt(path), @ptrToInt(buf_ptr), buf_len); + } +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn readlinkat(dirfd: i32, noalias path: [*]const u8, noalias buf_ptr: [*]u8, buf_len: usize) usize { + return syscall4(SYS_readlinkat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), @ptrToInt(buf_ptr), buf_len); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn mkdir(path: [*]const u8, mode: u32) usize { + if (@hasDecl(@This(), "SYS_mkdir")) { + return syscall2(SYS_mkdir, @ptrToInt(path), mode); + } else { + return syscall3(SYS_mkdirat, AT_FDCWD, @ptrToInt(path), mode); + } +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn mkdirat(dirfd: i32, path: [*]const u8, mode: u32) usize { + return syscall3(SYS_mkdirat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), mode); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn mount(special: [*]const u8, dir: [*]const u8, fstype: [*]const u8, flags: u32, data: usize) usize { + return syscall5(SYS_mount, @ptrToInt(special), @ptrToInt(dir), @ptrToInt(fstype), flags, data); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn umount(special: [*]const u8) usize { + return syscall2(SYS_umount2, @ptrToInt(special), 0); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn umount2(special: [*]const u8, flags: u32) usize { + return syscall2(SYS_umount2, @ptrToInt(special), flags); +} + +pub fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: u32, fd: i32, offset: isize) usize { + return syscall6(SYS_mmap, @ptrToInt(address), length, prot, flags, @bitCast(usize, isize(fd)), @bitCast(usize, offset)); +} + +pub fn mprotect(address: usize, length: usize, protection: usize) usize { + return syscall3(SYS_mprotect, address, length, protection); +} + +pub fn munmap(address: usize, length: usize) usize { + return syscall2(SYS_munmap, address, length); +} + +pub fn read(fd: i32, buf: [*]u8, count: usize) usize { + return syscall3(SYS_read, @bitCast(usize, isize(fd)), @ptrToInt(buf), count); +} + +pub fn preadv(fd: i32, iov: [*]const iovec, count: usize, offset: u64) usize { + return syscall4(SYS_preadv, @bitCast(usize, isize(fd)), @ptrToInt(iov), count, offset); +} + +pub fn readv(fd: i32, iov: [*]const iovec, count: usize) usize { + return syscall3(SYS_readv, @bitCast(usize, isize(fd)), @ptrToInt(iov), count); +} + +pub fn writev(fd: i32, iov: [*]const iovec_const, count: usize) usize { + return syscall3(SYS_writev, @bitCast(usize, isize(fd)), @ptrToInt(iov), count); +} + +pub fn pwritev(fd: i32, iov: [*]const iovec_const, count: usize, offset: u64) usize { + return syscall4(SYS_pwritev, @bitCast(usize, isize(fd)), @ptrToInt(iov), count, offset); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn rmdir(path: [*]const u8) usize { + if (@hasDecl(@This(), "SYS_rmdir")) { + return syscall1(SYS_rmdir, @ptrToInt(path)); + } else { + return syscall3(SYS_unlinkat, AT_FDCWD, @ptrToInt(path), AT_REMOVEDIR); + } +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn symlink(existing: [*]const u8, new: [*]const u8) usize { + if (@hasDecl(@This(), "SYS_symlink")) { + return syscall2(SYS_symlink, @ptrToInt(existing), @ptrToInt(new)); + } else { + return syscall3(SYS_symlinkat, @ptrToInt(existing), AT_FDCWD, @ptrToInt(new)); + } +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn symlinkat(existing: [*]const u8, newfd: i32, newpath: [*]const u8) usize { + return syscall3(SYS_symlinkat, @ptrToInt(existing), @bitCast(usize, isize(newfd)), @ptrToInt(newpath)); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn pread(fd: i32, buf: [*]u8, count: usize, offset: usize) usize { + return syscall4(SYS_pread, @bitCast(usize, isize(fd)), @ptrToInt(buf), count, offset); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn access(path: [*]const u8, mode: u32) usize { + return syscall2(SYS_access, @ptrToInt(path), mode); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn faccessat(dirfd: i32, path: [*]const u8, mode: u32) usize { + return syscall3(SYS_faccessat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), mode); +} + +pub fn pipe(fd: *[2]i32) usize { + if (@hasDecl(@This(), "SYS_pipe")) { + return syscall1(SYS_pipe, @ptrToInt(fd)); + } else { + return syscall2(SYS_pipe2, @ptrToInt(fd), 0); + } +} + +pub fn pipe2(fd: *[2]i32, flags: u32) usize { + return syscall2(SYS_pipe2, @ptrToInt(fd), flags); +} + +pub fn write(fd: i32, buf: [*]const u8, count: usize) usize { + return syscall3(SYS_write, @bitCast(usize, isize(fd)), @ptrToInt(buf), count); +} + +pub fn pwrite(fd: i32, buf: [*]const u8, count: usize, offset: usize) usize { + return syscall4(SYS_pwrite, @bitCast(usize, isize(fd)), @ptrToInt(buf), count, offset); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn rename(old: [*]const u8, new: [*]const u8) usize { + if (@hasDecl(@This(), "SYS_rename")) { + return syscall2(SYS_rename, @ptrToInt(old), @ptrToInt(new)); + } else if (@hasDecl(@This(), "SYS_renameat")) { + return syscall4(SYS_renameat, AT_FDCWD, @ptrToInt(old), AT_FDCWD, @ptrToInt(new)); + } else { + return syscall5(SYS_renameat2, AT_FDCWD, @ptrToInt(old), AT_FDCWD, @ptrToInt(new), 0); + } +} + +pub fn renameat(oldfd: i32, oldpath: [*]const u8, newfd: i32, newpath: [*]const u8) usize { + if (@hasDecl(@This(), "SYS_renameat")) { + return syscall4( + SYS_renameat, + @bitCast(usize, isize(oldfd)), + @ptrToInt(old), + @bitCast(usize, isize(newfd)), + @ptrToInt(new), + ); + } else { + return syscall5( + SYS_renameat2, + @bitCast(usize, isize(oldfd)), + @ptrToInt(old), + @bitCast(usize, isize(newfd)), + @ptrToInt(new), + 0, + ); + } +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn renameat2(oldfd: i32, oldpath: [*]const u8, newfd: i32, newpath: [*]const u8, flags: u32) usize { + return syscall5( + SYS_renameat2, + @bitCast(usize, isize(oldfd)), + @ptrToInt(oldpath), + @bitCast(usize, isize(newfd)), + @ptrToInt(newpath), + flags, + ); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn open(path: [*]const u8, flags: u32, perm: usize) usize { + return syscall3(SYS_open, @ptrToInt(path), flags, perm); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn create(path: [*]const u8, perm: usize) usize { + return syscall2(SYS_creat, @ptrToInt(path), perm); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn openat(dirfd: i32, path: [*]const u8, flags: u32, mode: usize) usize { + // dirfd could be negative, for example AT_FDCWD is -100 + return syscall4(SYS_openat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), flags, mode); +} + +/// See also `clone` (from the arch-specific include) +pub fn clone5(flags: usize, child_stack_ptr: usize, parent_tid: *i32, child_tid: *i32, newtls: usize) usize { + return syscall5(SYS_clone, flags, child_stack_ptr, @ptrToInt(parent_tid), @ptrToInt(child_tid), newtls); +} + +/// See also `clone` (from the arch-specific include) +pub fn clone2(flags: u32, child_stack_ptr: usize) usize { + return syscall2(SYS_clone, flags, child_stack_ptr); +} + +pub fn close(fd: i32) usize { + return syscall1(SYS_close, @bitCast(usize, isize(fd))); +} + +/// Can only be called on 32 bit systems. For 64 bit see `lseek`. +pub fn llseek(fd: i32, offset: u64, result: ?*u64, whence: usize) usize { + return syscall5( + SYS__llseek, + @bitCast(usize, isize(fd)), + @truncate(usize, offset >> 32), + @truncate(usize, offset), + @ptrToInt(result), + whence, + ); +} + +/// Can only be called on 64 bit systems. For 32 bit see `llseek`. +pub fn lseek(fd: i32, offset: i64, whence: usize) usize { + return syscall3(SYS_lseek, @bitCast(usize, isize(fd)), @bitCast(usize, offset), whence); +} + +pub fn exit(status: i32) noreturn { + _ = syscall1(SYS_exit, @bitCast(usize, isize(status))); + unreachable; +} + +pub fn exit_group(status: i32) noreturn { + _ = syscall1(SYS_exit_group, @bitCast(usize, isize(status))); + unreachable; +} + +pub fn getrandom(buf: [*]u8, count: usize, flags: u32) usize { + return syscall3(SYS_getrandom, @ptrToInt(buf), count, flags); +} + +pub fn kill(pid: i32, sig: i32) usize { + return syscall2(SYS_kill, @bitCast(usize, isize(pid)), @bitCast(usize, isize(sig))); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn unlink(path: [*]const u8) usize { + if (@hasDecl(@This(), "SYS_unlink")) { + return syscall1(SYS_unlink, @ptrToInt(path)); + } else { + return syscall3(SYS_unlinkat, AT_FDCWD, @ptrToInt(path), 0); + } +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn unlinkat(dirfd: i32, path: [*]const u8, flags: u32) usize { + return syscall3(SYS_unlinkat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), flags); +} + +pub fn waitpid(pid: i32, status: *i32, options: i32) usize { + return syscall4(SYS_wait4, @bitCast(usize, isize(pid)), @ptrToInt(status), @bitCast(usize, isize(options)), 0); +} + +var vdso_clock_gettime = @ptrCast(?*const c_void, init_vdso_clock_gettime); + +// We must follow the C calling convention when we call into the VDSO +const vdso_clock_gettime_ty = extern fn (i32, *timespec) usize; + +pub fn clock_gettime(clk_id: i32, tp: *timespec) usize { + if (VDSO_CGT_SYM.len != 0) { + const ptr = @atomicLoad(?*const c_void, &vdso_clock_gettime, .Unordered); + if (ptr) |fn_ptr| { + const f = @ptrCast(vdso_clock_gettime_ty, fn_ptr); + const rc = f(clk_id, tp); + switch (rc) { + 0, @bitCast(usize, isize(-EINVAL)) => return rc, + else => {}, + } + } + } + return syscall2(SYS_clock_gettime, @bitCast(usize, isize(clk_id)), @ptrToInt(tp)); +} + +extern fn init_vdso_clock_gettime(clk: i32, ts: *timespec) usize { + const ptr = @intToPtr(?*const c_void, vdso.lookup(VDSO_CGT_VER, VDSO_CGT_SYM)); + // Note that we may not have a VDSO at all, update the stub address anyway + // so that clock_gettime will fall back on the good old (and slow) syscall + _ = @cmpxchgStrong(?*const c_void, &vdso_clock_gettime, &init_vdso_clock_gettime, ptr, .Monotonic, .Monotonic); + // Call into the VDSO if available + if (ptr) |fn_ptr| { + const f = @ptrCast(vdso_clock_gettime_ty, fn_ptr); + return f(clk, ts); + } + return @bitCast(usize, isize(-ENOSYS)); +} + +pub fn clock_getres(clk_id: i32, tp: *timespec) usize { + return syscall2(SYS_clock_getres, @bitCast(usize, isize(clk_id)), @ptrToInt(tp)); +} + +pub fn clock_settime(clk_id: i32, tp: *const timespec) usize { + return syscall2(SYS_clock_settime, @bitCast(usize, isize(clk_id)), @ptrToInt(tp)); +} + +pub fn gettimeofday(tv: *timeval, tz: *timezone) usize { + return syscall2(SYS_gettimeofday, @ptrToInt(tv), @ptrToInt(tz)); +} + +pub fn settimeofday(tv: *const timeval, tz: *const timezone) usize { + return syscall2(SYS_settimeofday, @ptrToInt(tv), @ptrToInt(tz)); +} + +pub fn nanosleep(req: *const timespec, rem: ?*timespec) usize { + return syscall2(SYS_nanosleep, @ptrToInt(req), @ptrToInt(rem)); +} + +pub fn setuid(uid: u32) usize { + return syscall1(SYS_setuid, uid); +} + +pub fn setgid(gid: u32) usize { + return syscall1(SYS_setgid, gid); +} + +pub fn setreuid(ruid: u32, euid: u32) usize { + return syscall2(SYS_setreuid, ruid, euid); +} + +pub fn setregid(rgid: u32, egid: u32) usize { + return syscall2(SYS_setregid, rgid, egid); +} + +pub fn getuid() u32 { + return u32(syscall0(SYS_getuid)); +} + +pub fn getgid() u32 { + return u32(syscall0(SYS_getgid)); +} + +pub fn geteuid() u32 { + return u32(syscall0(SYS_geteuid)); +} + +pub fn getegid() u32 { + return u32(syscall0(SYS_getegid)); +} + +pub fn seteuid(euid: u32) usize { + return syscall1(SYS_seteuid, euid); +} + +pub fn setegid(egid: u32) usize { + return syscall1(SYS_setegid, egid); +} + +pub fn getresuid(ruid: *u32, euid: *u32, suid: *u32) usize { + return syscall3(SYS_getresuid, @ptrToInt(ruid), @ptrToInt(euid), @ptrToInt(suid)); +} + +pub fn getresgid(rgid: *u32, egid: *u32, sgid: *u32) usize { + return syscall3(SYS_getresgid, @ptrToInt(rgid), @ptrToInt(egid), @ptrToInt(sgid)); +} + +pub fn setresuid(ruid: u32, euid: u32, suid: u32) usize { + return syscall3(SYS_setresuid, ruid, euid, suid); +} + +pub fn setresgid(rgid: u32, egid: u32, sgid: u32) usize { + return syscall3(SYS_setresgid, rgid, egid, sgid); +} + +pub fn getgroups(size: usize, list: *u32) usize { + return syscall2(SYS_getgroups, size, @ptrToInt(list)); +} + +pub fn setgroups(size: usize, list: *const u32) usize { + return syscall2(SYS_setgroups, size, @ptrToInt(list)); +} + +pub fn getpid() i32 { + return @bitCast(i32, @truncate(u32, syscall0(SYS_getpid))); +} + +pub fn gettid() i32 { + return @bitCast(i32, @truncate(u32, syscall0(SYS_gettid))); +} + +pub fn sigprocmask(flags: u32, noalias set: *const sigset_t, noalias oldset: ?*sigset_t) usize { + return syscall4(SYS_rt_sigprocmask, flags, @ptrToInt(set), @ptrToInt(oldset), NSIG / 8); +} + +pub fn sigaction(sig: u6, noalias act: *const Sigaction, noalias oact: ?*Sigaction) usize { + assert(sig >= 1); + assert(sig != SIGKILL); + assert(sig != SIGSTOP); + var ksa = k_sigaction{ + .handler = act.handler, + .flags = act.flags | SA_RESTORER, + .mask = undefined, + .restorer = @ptrCast(extern fn () void, restore_rt), + }; + var ksa_old: k_sigaction = undefined; + @memcpy(@ptrCast([*]u8, &ksa.mask), @ptrCast([*]const u8, &act.mask), 8); + const result = syscall4(SYS_rt_sigaction, sig, @ptrToInt(&ksa), @ptrToInt(&ksa_old), @sizeOf(@typeOf(ksa.mask))); + const err = getErrno(result); + if (err != 0) { + return result; + } + if (oact) |old| { + old.handler = ksa_old.handler; + old.flags = @truncate(u32, ksa_old.flags); + @memcpy(@ptrCast([*]u8, &old.mask), @ptrCast([*]const u8, &ksa_old.mask), @sizeOf(@typeOf(ksa_old.mask))); + } + return 0; +} + +fn blockAllSignals(set: *sigset_t) void { + _ = syscall4(SYS_rt_sigprocmask, SIG_BLOCK, @ptrToInt(&all_mask), @ptrToInt(set), NSIG / 8); +} + +fn blockAppSignals(set: *sigset_t) void { + _ = syscall4(SYS_rt_sigprocmask, SIG_BLOCK, @ptrToInt(&app_mask), @ptrToInt(set), NSIG / 8); +} + +fn restoreSignals(set: *sigset_t) void { + _ = syscall4(SYS_rt_sigprocmask, SIG_SETMASK, @ptrToInt(set), 0, NSIG / 8); +} + +pub fn sigaddset(set: *sigset_t, sig: u6) void { + const s = sig - 1; + (set.*)[@intCast(usize, s) / usize.bit_count] |= @intCast(usize, 1) << (s & (usize.bit_count - 1)); +} + +pub fn sigismember(set: *const sigset_t, sig: u6) bool { + const s = sig - 1; + return ((set.*)[@intCast(usize, s) / usize.bit_count] & (@intCast(usize, 1) << (s & (usize.bit_count - 1)))) != 0; +} + +pub fn getsockname(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize { + return syscall3(SYS_getsockname, @bitCast(usize, isize(fd)), @ptrToInt(addr), @ptrToInt(len)); +} + +pub fn getpeername(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize { + return syscall3(SYS_getpeername, @bitCast(usize, isize(fd)), @ptrToInt(addr), @ptrToInt(len)); +} + +pub fn socket(domain: u32, socket_type: u32, protocol: u32) usize { + return syscall3(SYS_socket, domain, socket_type, protocol); +} + +pub fn setsockopt(fd: i32, level: u32, optname: u32, optval: [*]const u8, optlen: socklen_t) usize { + return syscall5(SYS_setsockopt, @bitCast(usize, isize(fd)), level, optname, @ptrToInt(optval), @intCast(usize, optlen)); +} + +pub fn getsockopt(fd: i32, level: u32, optname: u32, noalias optval: [*]u8, noalias optlen: *socklen_t) usize { + return syscall5(SYS_getsockopt, @bitCast(usize, isize(fd)), level, optname, @ptrToInt(optval), @ptrToInt(optlen)); +} + +pub fn sendmsg(fd: i32, msg: *msghdr_const, flags: u32) usize { + return syscall3(SYS_sendmsg, @bitCast(usize, isize(fd)), @ptrToInt(msg), flags); +} + +pub fn sendmmsg(fd: i32, msgvec: [*]mmsghdr_const, vlen: u32, flags: u32) usize { + if (@typeInfo(usize).Int.bits > @typeInfo(@typeOf(mmsghdr(undefined).msg_len)).Int.bits) { + // workaround kernel brokenness: + // if adding up all iov_len overflows a i32 then split into multiple calls + // see https://www.openwall.com/lists/musl/2014/06/07/5 + const kvlen = if (vlen > IOV_MAX) IOV_MAX else vlen; // matches kernel + var next_unsent: usize = 0; + for (msgvec[0..kvlen]) |*msg, i| { + var size: i32 = 0; + const msg_iovlen = @intCast(usize, msg.msg_hdr.msg_iovlen); // kernel side this is treated as unsigned + for (msg.msg_hdr.msg_iov[0..msg_iovlen]) |iov, j| { + if (iov.iov_len > std.math.maxInt(i32) or @addWithOverflow(i32, size, @intCast(i32, iov.iov_len), &size)) { + // batch-send all messages up to the current message + if (next_unsent < i) { + const batch_size = i - next_unsent; + const r = syscall4(SYS_sendmmsg, @bitCast(usize, isize(fd)), @ptrToInt(&msgvec[next_unsent]), batch_size, flags); + if (getErrno(r) != 0) return next_unsent; + if (r < batch_size) return next_unsent + r; + } + // send current message as own packet + const r = sendmsg(fd, &msg.msg_hdr, flags); + if (getErrno(r) != 0) return r; + // Linux limits the total bytes sent by sendmsg to INT_MAX, so this cast is safe. + msg.msg_len = @intCast(u32, r); + next_unsent = i + 1; + break; + } + } + } + if (next_unsent < kvlen or next_unsent == 0) { // want to make sure at least one syscall occurs (e.g. to trigger MSG_EOR) + const batch_size = kvlen - next_unsent; + const r = syscall4(SYS_sendmmsg, @bitCast(usize, isize(fd)), @ptrToInt(&msgvec[next_unsent]), batch_size, flags); + if (getErrno(r) != 0) return r; + return next_unsent + r; + } + return kvlen; + } + return syscall4(SYS_sendmmsg, @bitCast(usize, isize(fd)), @ptrToInt(msgvec), vlen, flags); +} + +pub fn connect(fd: i32, addr: *const c_void, len: socklen_t) usize { + return syscall3(SYS_connect, @bitCast(usize, isize(fd)), @ptrToInt(addr), len); +} + +pub fn recvmsg(fd: i32, msg: *msghdr, flags: u32) usize { + return syscall3(SYS_recvmsg, @bitCast(usize, isize(fd)), @ptrToInt(msg), flags); +} + +pub fn recvfrom(fd: i32, noalias buf: [*]u8, len: usize, flags: u32, noalias addr: ?*sockaddr, noalias alen: ?*socklen_t) usize { + return syscall6(SYS_recvfrom, @bitCast(usize, isize(fd)), @ptrToInt(buf), len, flags, @ptrToInt(addr), @ptrToInt(alen)); +} + +pub fn shutdown(fd: i32, how: i32) usize { + return syscall2(SYS_shutdown, @bitCast(usize, isize(fd)), @bitCast(usize, isize(how))); +} + +pub fn bind(fd: i32, addr: *const sockaddr, len: socklen_t) usize { + return syscall3(SYS_bind, @bitCast(usize, isize(fd)), @ptrToInt(addr), @intCast(usize, len)); +} + +pub fn listen(fd: i32, backlog: u32) usize { + return syscall2(SYS_listen, @bitCast(usize, isize(fd)), backlog); +} + +pub fn sendto(fd: i32, buf: [*]const u8, len: usize, flags: u32, addr: ?*const sockaddr, alen: socklen_t) usize { + return syscall6(SYS_sendto, @bitCast(usize, isize(fd)), @ptrToInt(buf), len, flags, @ptrToInt(addr), @intCast(usize, alen)); +} + +pub fn socketpair(domain: i32, socket_type: i32, protocol: i32, fd: [2]i32) usize { + return syscall4(SYS_socketpair, @intCast(usize, domain), @intCast(usize, socket_type), @intCast(usize, protocol), @ptrToInt(&fd[0])); +} + +pub fn accept(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize { + return accept4(fd, addr, len, 0); +} + +pub fn accept4(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t, flags: u32) usize { + return syscall4(SYS_accept4, @bitCast(usize, isize(fd)), @ptrToInt(addr), @ptrToInt(len), flags); +} + +pub fn fstat(fd: i32, stat_buf: *Stat) usize { + return syscall2(SYS_fstat, @bitCast(usize, isize(fd)), @ptrToInt(stat_buf)); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn stat(pathname: [*]const u8, statbuf: *Stat) usize { + return syscall2(SYS_stat, @ptrToInt(pathname), @ptrToInt(statbuf)); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn lstat(pathname: [*]const u8, statbuf: *Stat) usize { + return syscall2(SYS_lstat, @ptrToInt(pathname), @ptrToInt(statbuf)); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn fstatat(dirfd: i32, path: [*]const u8, stat_buf: *Stat, flags: u32) usize { + return syscall4(SYS_fstatat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), @ptrToInt(stat_buf), flags); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn listxattr(path: [*]const u8, list: [*]u8, size: usize) usize { + return syscall3(SYS_listxattr, @ptrToInt(path), @ptrToInt(list), size); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn llistxattr(path: [*]const u8, list: [*]u8, size: usize) usize { + return syscall3(SYS_llistxattr, @ptrToInt(path), @ptrToInt(list), size); +} + +pub fn flistxattr(fd: usize, list: [*]u8, size: usize) usize { + return syscall3(SYS_flistxattr, fd, @ptrToInt(list), size); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn getxattr(path: [*]const u8, name: [*]const u8, value: [*]u8, size: usize) usize { + return syscall4(SYS_getxattr, @ptrToInt(path), @ptrToInt(name), @ptrToInt(value), size); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn lgetxattr(path: [*]const u8, name: [*]const u8, value: [*]u8, size: usize) usize { + return syscall4(SYS_lgetxattr, @ptrToInt(path), @ptrToInt(name), @ptrToInt(value), size); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn fgetxattr(fd: usize, name: [*]const u8, value: [*]u8, size: usize) usize { + return syscall4(SYS_lgetxattr, fd, @ptrToInt(name), @ptrToInt(value), size); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn setxattr(path: [*]const u8, name: [*]const u8, value: *const void, size: usize, flags: usize) usize { + return syscall5(SYS_setxattr, @ptrToInt(path), @ptrToInt(name), @ptrToInt(value), size, flags); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn lsetxattr(path: [*]const u8, name: [*]const u8, value: *const void, size: usize, flags: usize) usize { + return syscall5(SYS_lsetxattr, @ptrToInt(path), @ptrToInt(name), @ptrToInt(value), size, flags); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn fsetxattr(fd: usize, name: [*]const u8, value: *const void, size: usize, flags: usize) usize { + return syscall5(SYS_fsetxattr, fd, @ptrToInt(name), @ptrToInt(value), size, flags); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn removexattr(path: [*]const u8, name: [*]const u8) usize { + return syscall2(SYS_removexattr, @ptrToInt(path), @ptrToInt(name)); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn lremovexattr(path: [*]const u8, name: [*]const u8) usize { + return syscall2(SYS_lremovexattr, @ptrToInt(path), @ptrToInt(name)); +} + +// TODO https://github.com/ziglang/zig/issues/265 +pub fn fremovexattr(fd: usize, name: [*]const u8) usize { + return syscall2(SYS_fremovexattr, fd, @ptrToInt(name)); +} + +pub fn sched_getaffinity(pid: i32, size: usize, set: *cpu_set_t) usize { + const rc = syscall3(SYS_sched_getaffinity, @bitCast(usize, isize(pid)), size, @ptrToInt(set)); + if (@bitCast(isize, rc) < 0) return rc; + if (rc < size) @memset(@ptrCast([*]u8, set) + rc, 0, size - rc); + return 0; +} + +pub fn epoll_create() usize { + return epoll_create1(0); +} + +pub fn epoll_create1(flags: usize) usize { + return syscall1(SYS_epoll_create1, flags); +} + +pub fn epoll_ctl(epoll_fd: i32, op: u32, fd: i32, ev: *epoll_event) usize { + return syscall4(SYS_epoll_ctl, @bitCast(usize, isize(epoll_fd)), @intCast(usize, op), @bitCast(usize, isize(fd)), @ptrToInt(ev)); +} + +pub fn epoll_wait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout: i32) usize { + return syscall4( + SYS_epoll_wait, + @bitCast(usize, isize(epoll_fd)), + @ptrToInt(events), + maxevents, + @bitCast(usize, isize(timeout)), + ); +} + +pub fn epoll_pwait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout: i32, sigmask: ?*sigset_t) usize { + return syscall6( + SYS_epoll_pwait, + @bitCast(usize, isize(epoll_fd)), + @ptrToInt(events), + @intCast(usize, maxevents), + @bitCast(usize, isize(timeout)), + @ptrToInt(sigmask), + @sizeOf(sigset_t), + ); +} + +pub fn eventfd(count: u32, flags: u32) usize { + return syscall2(SYS_eventfd2, count, flags); +} + +pub fn timerfd_create(clockid: i32, flags: u32) usize { + return syscall2(SYS_timerfd_create, @bitCast(usize, isize(clockid)), flags); +} + +pub const itimerspec = extern struct { + it_interval: timespec, + it_value: timespec, +}; + +pub fn timerfd_gettime(fd: i32, curr_value: *itimerspec) usize { + return syscall2(SYS_timerfd_gettime, @bitCast(usize, isize(fd)), @ptrToInt(curr_value)); +} + +pub fn timerfd_settime(fd: i32, flags: u32, new_value: *const itimerspec, old_value: ?*itimerspec) usize { + return syscall4(SYS_timerfd_settime, @bitCast(usize, isize(fd)), flags, @ptrToInt(new_value), @ptrToInt(old_value)); +} + +pub fn unshare(flags: usize) usize { + return syscall1(SYS_unshare, flags); +} + +pub fn capget(hdrp: *cap_user_header_t, datap: *cap_user_data_t) usize { + return syscall2(SYS_capget, @ptrToInt(hdrp), @ptrToInt(datap)); +} + +pub fn capset(hdrp: *cap_user_header_t, datap: *const cap_user_data_t) usize { + return syscall2(SYS_capset, @ptrToInt(hdrp), @ptrToInt(datap)); +} + +// XXX: This should be weak +extern const __ehdr_start: elf.Ehdr = undefined; + +pub fn dl_iterate_phdr(comptime T: type, callback: extern fn (info: *dl_phdr_info, size: usize, data: ?*T) i32, data: ?*T) isize { + if (builtin.link_libc) { + return std.c.dl_iterate_phdr(@ptrCast(std.c.dl_iterate_phdr_callback, callback), @ptrCast(?*c_void, data)); + } + + const elf_base = @ptrToInt(&__ehdr_start); + const n_phdr = __ehdr_start.e_phnum; + const phdrs = (@intToPtr([*]elf.Phdr, elf_base + __ehdr_start.e_phoff))[0..n_phdr]; + + var it = dl.linkmap_iterator(phdrs) catch return 0; + + // The executable has no dynamic link segment, create a single entry for + // the whole ELF image + if (it.end()) { + var info = dl_phdr_info{ + .dlpi_addr = elf_base, + .dlpi_name = c"/proc/self/exe", + .dlpi_phdr = @intToPtr([*]elf.Phdr, elf_base + __ehdr_start.e_phoff), + .dlpi_phnum = __ehdr_start.e_phnum, + }; + + return callback(&info, @sizeOf(dl_phdr_info), data); + } + + // Last return value from the callback function + var last_r: isize = 0; + while (it.next()) |entry| { + var dlpi_phdr: usize = undefined; + var dlpi_phnum: u16 = undefined; + + if (entry.l_addr != 0) { + const elf_header = @intToPtr(*elf.Ehdr, entry.l_addr); + dlpi_phdr = entry.l_addr + elf_header.e_phoff; + dlpi_phnum = elf_header.e_phnum; + } else { + // This is the running ELF image + dlpi_phdr = elf_base + __ehdr_start.e_phoff; + dlpi_phnum = __ehdr_start.e_phnum; + } + + var info = dl_phdr_info{ + .dlpi_addr = entry.l_addr, + .dlpi_name = entry.l_name, + .dlpi_phdr = @intToPtr([*]elf.Phdr, dlpi_phdr), + .dlpi_phnum = dlpi_phnum, + }; + + last_r = callback(&info, @sizeOf(dl_phdr_info), data); + if (last_r != 0) break; + } + + return last_r; +} test "" { if (is_the_target) { diff --git a/std/os/linux/sys.zig b/std/os/linux/sys.zig deleted file mode 100644 index 272124f06c..0000000000 --- a/std/os/linux/sys.zig +++ /dev/null @@ -1,859 +0,0 @@ -// This file provides the system interface functions for Linux matching those -// that are provided by libc, whether or not libc is linked. The following -// abstractions are made: -// * Work around kernel bugs and limitations. For example, see sendmmsg. -// * Implement all the syscalls in the same way that libc functions will -// provide `rename` when only the `renameat` syscall exists. -// * Does not support POSIX thread cancellation. -const std = @import("../../std.zig"); -const builtin = @import("builtin"); -const assert = std.debug.assert; -const maxInt = std.math.maxInt; -const elf = std.elf; -const vdso = @import("vdso.zig"); -const dl = @import("../../dynamic_library.zig"); -pub use switch (builtin.arch) { - .x86_64 => @import("x86_64.zig"), - .aarch64 => @import("arm64.zig"), - else => struct {}, -}; -pub use @import("../bits.zig"); - -/// See `std.os.posix.getauxval`. -pub var elf_aux_maybe: ?[*]std.elf.Auxv = null; - -/// Get the errno from a syscall return value, or 0 for no error. -pub fn getErrno(r: usize) u12 { - const signed_r = @bitCast(isize, r); - return if (signed_r > -4096 and signed_r < 0) @intCast(u12, -signed_r) else 0; -} - -pub fn dup2(old: i32, new: i32) usize { - if (@hasDecl(@This(), "SYS_dup2")) { - return syscall2(SYS_dup2, @bitCast(usize, isize(old)), @bitCast(usize, isize(new))); - } else { - if (old == new) { - if (std.debug.runtime_safety) { - const rc = syscall2(SYS_fcntl, @bitCast(usize, isize(old)), F_GETFD); - if (@bitCast(isize, rc) < 0) return rc; - } - return @intCast(usize, old); - } else { - return syscall3(SYS_dup3, @bitCast(usize, isize(old)), @bitCast(usize, isize(new)), 0); - } - } -} - -pub fn dup3(old: i32, new: i32, flags: u32) usize { - return syscall3(SYS_dup3, @bitCast(usize, isize(old)), @bitCast(usize, isize(new)), flags); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn chdir(path: [*]const u8) usize { - return syscall1(SYS_chdir, @ptrToInt(path)); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn chroot(path: [*]const u8) usize { - return syscall1(SYS_chroot, @ptrToInt(path)); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn execve(path: [*]const u8, argv: [*]const ?[*]const u8, envp: [*]const ?[*]const u8) usize { - return syscall3(SYS_execve, @ptrToInt(path), @ptrToInt(argv), @ptrToInt(envp)); -} - -pub fn fork() usize { - if (@hasDecl(@This(), "SYS_fork")) { - return syscall0(SYS_fork); - } else { - return syscall2(SYS_clone, SIGCHLD, 0); - } -} - -/// This must be inline, and inline call the syscall function, because if the -/// child does a return it will clobber the parent's stack. -/// It is advised to avoid this function and use clone instead, because -/// the compiler is not aware of how vfork affects control flow and you may -/// see different results in optimized builds. -pub inline fn vfork() usize { - return @inlineCall(syscall0, SYS_vfork); -} - -pub fn futex_wait(uaddr: *const i32, futex_op: u32, val: i32, timeout: ?*timespec) usize { - return syscall4(SYS_futex, @ptrToInt(uaddr), futex_op, @bitCast(u32, val), @ptrToInt(timeout)); -} - -pub fn futex_wake(uaddr: *const i32, futex_op: u32, val: i32) usize { - return syscall3(SYS_futex, @ptrToInt(uaddr), futex_op, @bitCast(u32, val)); -} - -pub fn getcwd(buf: [*]u8, size: usize) usize { - return syscall2(SYS_getcwd, @ptrToInt(buf), size); -} - -pub fn getdents(fd: i32, dirp: [*]u8, count: usize) usize { - return syscall3(SYS_getdents, @bitCast(usize, isize(fd)), @ptrToInt(dirp), count); -} - -pub fn getdents64(fd: i32, dirp: [*]u8, count: usize) usize { - return syscall3(SYS_getdents64, @bitCast(usize, isize(fd)), @ptrToInt(dirp), count); -} - -pub fn inotify_init1(flags: u32) usize { - return syscall1(SYS_inotify_init1, flags); -} - -pub fn inotify_add_watch(fd: i32, pathname: [*]const u8, mask: u32) usize { - return syscall3(SYS_inotify_add_watch, @bitCast(usize, isize(fd)), @ptrToInt(pathname), mask); -} - -pub fn inotify_rm_watch(fd: i32, wd: i32) usize { - return syscall2(SYS_inotify_rm_watch, @bitCast(usize, isize(fd)), @bitCast(usize, isize(wd))); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn readlink(noalias path: [*]const u8, noalias buf_ptr: [*]u8, buf_len: usize) usize { - if (@hasDecl(@This(), "SYS_readlink")) { - return syscall3(SYS_readlink, @ptrToInt(path), @ptrToInt(buf_ptr), buf_len); - } else { - return syscall4(SYS_readlinkat, AT_FDCWD, @ptrToInt(path), @ptrToInt(buf_ptr), buf_len); - } -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn readlinkat(dirfd: i32, noalias path: [*]const u8, noalias buf_ptr: [*]u8, buf_len: usize) usize { - return syscall4(SYS_readlinkat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), @ptrToInt(buf_ptr), buf_len); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn mkdir(path: [*]const u8, mode: u32) usize { - if (@hasDecl(@This(), "SYS_mkdir")) { - return syscall2(SYS_mkdir, @ptrToInt(path), mode); - } else { - return syscall3(SYS_mkdirat, AT_FDCWD, @ptrToInt(path), mode); - } -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn mkdirat(dirfd: i32, path: [*]const u8, mode: u32) usize { - return syscall3(SYS_mkdirat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), mode); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn mount(special: [*]const u8, dir: [*]const u8, fstype: [*]const u8, flags: u32, data: usize) usize { - return syscall5(SYS_mount, @ptrToInt(special), @ptrToInt(dir), @ptrToInt(fstype), flags, data); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn umount(special: [*]const u8) usize { - return syscall2(SYS_umount2, @ptrToInt(special), 0); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn umount2(special: [*]const u8, flags: u32) usize { - return syscall2(SYS_umount2, @ptrToInt(special), flags); -} - -pub fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: u32, fd: i32, offset: isize) usize { - return syscall6(SYS_mmap, @ptrToInt(address), length, prot, flags, @bitCast(usize, isize(fd)), @bitCast(usize, offset)); -} - -pub fn mprotect(address: usize, length: usize, protection: usize) usize { - return syscall3(SYS_mprotect, address, length, protection); -} - -pub fn munmap(address: usize, length: usize) usize { - return syscall2(SYS_munmap, address, length); -} - -pub fn read(fd: i32, buf: [*]u8, count: usize) usize { - return syscall3(SYS_read, @bitCast(usize, isize(fd)), @ptrToInt(buf), count); -} - -pub fn preadv(fd: i32, iov: [*]const iovec, count: usize, offset: u64) usize { - return syscall4(SYS_preadv, @bitCast(usize, isize(fd)), @ptrToInt(iov), count, offset); -} - -pub fn readv(fd: i32, iov: [*]const iovec, count: usize) usize { - return syscall3(SYS_readv, @bitCast(usize, isize(fd)), @ptrToInt(iov), count); -} - -pub fn writev(fd: i32, iov: [*]const iovec_const, count: usize) usize { - return syscall3(SYS_writev, @bitCast(usize, isize(fd)), @ptrToInt(iov), count); -} - -pub fn pwritev(fd: i32, iov: [*]const iovec_const, count: usize, offset: u64) usize { - return syscall4(SYS_pwritev, @bitCast(usize, isize(fd)), @ptrToInt(iov), count, offset); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn rmdir(path: [*]const u8) usize { - if (@hasDecl(@This(), "SYS_rmdir")) { - return syscall1(SYS_rmdir, @ptrToInt(path)); - } else { - return syscall3(SYS_unlinkat, AT_FDCWD, @ptrToInt(path), AT_REMOVEDIR); - } -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn symlink(existing: [*]const u8, new: [*]const u8) usize { - if (@hasDecl(@This(), "SYS_symlink")) { - return syscall2(SYS_symlink, @ptrToInt(existing), @ptrToInt(new)); - } else { - return syscall3(SYS_symlinkat, @ptrToInt(existing), AT_FDCWD, @ptrToInt(new)); - } -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn symlinkat(existing: [*]const u8, newfd: i32, newpath: [*]const u8) usize { - return syscall3(SYS_symlinkat, @ptrToInt(existing), @bitCast(usize, isize(newfd)), @ptrToInt(newpath)); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn pread(fd: i32, buf: [*]u8, count: usize, offset: usize) usize { - return syscall4(SYS_pread, @bitCast(usize, isize(fd)), @ptrToInt(buf), count, offset); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn access(path: [*]const u8, mode: u32) usize { - return syscall2(SYS_access, @ptrToInt(path), mode); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn faccessat(dirfd: i32, path: [*]const u8, mode: u32) usize { - return syscall3(SYS_faccessat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), mode); -} - -pub fn pipe(fd: *[2]i32) usize { - if (@hasDecl(@This(), "SYS_pipe")) { - return syscall1(SYS_pipe, @ptrToInt(fd)); - } else { - return syscall2(SYS_pipe2, @ptrToInt(fd), 0); - } -} - -pub fn pipe2(fd: *[2]i32, flags: u32) usize { - return syscall2(SYS_pipe2, @ptrToInt(fd), flags); -} - -pub fn write(fd: i32, buf: [*]const u8, count: usize) usize { - return syscall3(SYS_write, @bitCast(usize, isize(fd)), @ptrToInt(buf), count); -} - -pub fn pwrite(fd: i32, buf: [*]const u8, count: usize, offset: usize) usize { - return syscall4(SYS_pwrite, @bitCast(usize, isize(fd)), @ptrToInt(buf), count, offset); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn rename(old: [*]const u8, new: [*]const u8) usize { - if (@hasDecl(@This(), "SYS_rename")) { - return syscall2(SYS_rename, @ptrToInt(old), @ptrToInt(new)); - } else if (@hasDecl(@This(), "SYS_renameat")) { - return syscall4(SYS_renameat, AT_FDCWD, @ptrToInt(old), AT_FDCWD, @ptrToInt(new)); - } else { - return syscall5(SYS_renameat2, AT_FDCWD, @ptrToInt(old), AT_FDCWD, @ptrToInt(new), 0); - } -} - -pub fn renameat(oldfd: i32, oldpath: [*]const u8, newfd: i32, newpath: [*]const u8) usize { - if (@hasDecl(@This(), "SYS_renameat")) { - return syscall4( - SYS_renameat, - @bitCast(usize, isize(oldfd)), - @ptrToInt(old), - @bitCast(usize, isize(newfd)), - @ptrToInt(new), - ); - } else { - return syscall5( - SYS_renameat2, - @bitCast(usize, isize(oldfd)), - @ptrToInt(old), - @bitCast(usize, isize(newfd)), - @ptrToInt(new), - 0, - ); - } -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn renameat2(oldfd: i32, oldpath: [*]const u8, newfd: i32, newpath: [*]const u8, flags: u32) usize { - return syscall5( - SYS_renameat2, - @bitCast(usize, isize(oldfd)), - @ptrToInt(oldpath), - @bitCast(usize, isize(newfd)), - @ptrToInt(newpath), - flags, - ); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn open(path: [*]const u8, flags: u32, perm: usize) usize { - return syscall3(SYS_open, @ptrToInt(path), flags, perm); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn create(path: [*]const u8, perm: usize) usize { - return syscall2(SYS_creat, @ptrToInt(path), perm); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn openat(dirfd: i32, path: [*]const u8, flags: u32, mode: usize) usize { - // dirfd could be negative, for example AT_FDCWD is -100 - return syscall4(SYS_openat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), flags, mode); -} - -/// See also `clone` (from the arch-specific include) -pub fn clone5(flags: usize, child_stack_ptr: usize, parent_tid: *i32, child_tid: *i32, newtls: usize) usize { - return syscall5(SYS_clone, flags, child_stack_ptr, @ptrToInt(parent_tid), @ptrToInt(child_tid), newtls); -} - -/// See also `clone` (from the arch-specific include) -pub fn clone2(flags: u32, child_stack_ptr: usize) usize { - return syscall2(SYS_clone, flags, child_stack_ptr); -} - -pub fn close(fd: i32) usize { - return syscall1(SYS_close, @bitCast(usize, isize(fd))); -} - -/// Can only be called on 32 bit systems. For 64 bit see `lseek`. -pub fn llseek(fd: i32, offset: u64, result: ?*u64, whence: usize) usize { - return syscall5( - SYS__llseek, - @bitCast(usize, isize(fd)), - @truncate(usize, offset >> 32), - @truncate(usize, offset), - @ptrToInt(result), - whence, - ); -} - -/// Can only be called on 64 bit systems. For 32 bit see `llseek`. -pub fn lseek(fd: i32, offset: i64, whence: usize) usize { - return syscall3(SYS_lseek, @bitCast(usize, isize(fd)), @bitCast(usize, offset), whence); -} - -pub fn exit(status: i32) noreturn { - _ = syscall1(SYS_exit, @bitCast(usize, isize(status))); - unreachable; -} - -pub fn exit_group(status: i32) noreturn { - _ = syscall1(SYS_exit_group, @bitCast(usize, isize(status))); - unreachable; -} - -pub fn getrandom(buf: [*]u8, count: usize, flags: u32) usize { - return syscall3(SYS_getrandom, @ptrToInt(buf), count, flags); -} - -pub fn kill(pid: i32, sig: i32) usize { - return syscall2(SYS_kill, @bitCast(usize, isize(pid)), @bitCast(usize, isize(sig))); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn unlink(path: [*]const u8) usize { - if (@hasDecl(@This(), "SYS_unlink")) { - return syscall1(SYS_unlink, @ptrToInt(path)); - } else { - return syscall3(SYS_unlinkat, AT_FDCWD, @ptrToInt(path), 0); - } -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn unlinkat(dirfd: i32, path: [*]const u8, flags: u32) usize { - return syscall3(SYS_unlinkat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), flags); -} - -pub fn waitpid(pid: i32, status: *i32, options: i32) usize { - return syscall4(SYS_wait4, @bitCast(usize, isize(pid)), @ptrToInt(status), @bitCast(usize, isize(options)), 0); -} - -var vdso_clock_gettime = @ptrCast(?*const c_void, init_vdso_clock_gettime); - -// We must follow the C calling convention when we call into the VDSO -const vdso_clock_gettime_ty = extern fn (i32, *timespec) usize; - -pub fn clock_gettime(clk_id: i32, tp: *timespec) usize { - if (VDSO_CGT_SYM.len != 0) { - const ptr = @atomicLoad(?*const c_void, &vdso_clock_gettime, .Unordered); - if (ptr) |fn_ptr| { - const f = @ptrCast(vdso_clock_gettime_ty, fn_ptr); - const rc = f(clk_id, tp); - switch (rc) { - 0, @bitCast(usize, isize(-EINVAL)) => return rc, - else => {}, - } - } - } - return syscall2(SYS_clock_gettime, @bitCast(usize, isize(clk_id)), @ptrToInt(tp)); -} - -extern fn init_vdso_clock_gettime(clk: i32, ts: *timespec) usize { - const ptr = @intToPtr(?*const c_void, vdso.lookup(VDSO_CGT_VER, VDSO_CGT_SYM)); - // Note that we may not have a VDSO at all, update the stub address anyway - // so that clock_gettime will fall back on the good old (and slow) syscall - _ = @cmpxchgStrong(?*const c_void, &vdso_clock_gettime, &init_vdso_clock_gettime, ptr, .Monotonic, .Monotonic); - // Call into the VDSO if available - if (ptr) |fn_ptr| { - const f = @ptrCast(vdso_clock_gettime_ty, fn_ptr); - return f(clk, ts); - } - return @bitCast(usize, isize(-ENOSYS)); -} - -pub fn clock_getres(clk_id: i32, tp: *timespec) usize { - return syscall2(SYS_clock_getres, @bitCast(usize, isize(clk_id)), @ptrToInt(tp)); -} - -pub fn clock_settime(clk_id: i32, tp: *const timespec) usize { - return syscall2(SYS_clock_settime, @bitCast(usize, isize(clk_id)), @ptrToInt(tp)); -} - -pub fn gettimeofday(tv: *timeval, tz: *timezone) usize { - return syscall2(SYS_gettimeofday, @ptrToInt(tv), @ptrToInt(tz)); -} - -pub fn settimeofday(tv: *const timeval, tz: *const timezone) usize { - return syscall2(SYS_settimeofday, @ptrToInt(tv), @ptrToInt(tz)); -} - -pub fn nanosleep(req: *const timespec, rem: ?*timespec) usize { - return syscall2(SYS_nanosleep, @ptrToInt(req), @ptrToInt(rem)); -} - -pub fn setuid(uid: u32) usize { - return syscall1(SYS_setuid, uid); -} - -pub fn setgid(gid: u32) usize { - return syscall1(SYS_setgid, gid); -} - -pub fn setreuid(ruid: u32, euid: u32) usize { - return syscall2(SYS_setreuid, ruid, euid); -} - -pub fn setregid(rgid: u32, egid: u32) usize { - return syscall2(SYS_setregid, rgid, egid); -} - -pub fn getuid() u32 { - return u32(syscall0(SYS_getuid)); -} - -pub fn getgid() u32 { - return u32(syscall0(SYS_getgid)); -} - -pub fn geteuid() u32 { - return u32(syscall0(SYS_geteuid)); -} - -pub fn getegid() u32 { - return u32(syscall0(SYS_getegid)); -} - -pub fn seteuid(euid: u32) usize { - return syscall1(SYS_seteuid, euid); -} - -pub fn setegid(egid: u32) usize { - return syscall1(SYS_setegid, egid); -} - -pub fn getresuid(ruid: *u32, euid: *u32, suid: *u32) usize { - return syscall3(SYS_getresuid, @ptrToInt(ruid), @ptrToInt(euid), @ptrToInt(suid)); -} - -pub fn getresgid(rgid: *u32, egid: *u32, sgid: *u32) usize { - return syscall3(SYS_getresgid, @ptrToInt(rgid), @ptrToInt(egid), @ptrToInt(sgid)); -} - -pub fn setresuid(ruid: u32, euid: u32, suid: u32) usize { - return syscall3(SYS_setresuid, ruid, euid, suid); -} - -pub fn setresgid(rgid: u32, egid: u32, sgid: u32) usize { - return syscall3(SYS_setresgid, rgid, egid, sgid); -} - -pub fn getgroups(size: usize, list: *u32) usize { - return syscall2(SYS_getgroups, size, @ptrToInt(list)); -} - -pub fn setgroups(size: usize, list: *const u32) usize { - return syscall2(SYS_setgroups, size, @ptrToInt(list)); -} - -pub fn getpid() i32 { - return @bitCast(i32, @truncate(u32, syscall0(SYS_getpid))); -} - -pub fn gettid() i32 { - return @bitCast(i32, @truncate(u32, syscall0(SYS_gettid))); -} - -pub fn sigprocmask(flags: u32, noalias set: *const sigset_t, noalias oldset: ?*sigset_t) usize { - return syscall4(SYS_rt_sigprocmask, flags, @ptrToInt(set), @ptrToInt(oldset), NSIG / 8); -} - -pub fn sigaction(sig: u6, noalias act: *const Sigaction, noalias oact: ?*Sigaction) usize { - assert(sig >= 1); - assert(sig != SIGKILL); - assert(sig != SIGSTOP); - var ksa = k_sigaction{ - .handler = act.handler, - .flags = act.flags | SA_RESTORER, - .mask = undefined, - .restorer = @ptrCast(extern fn () void, restore_rt), - }; - var ksa_old: k_sigaction = undefined; - @memcpy(@ptrCast([*]u8, &ksa.mask), @ptrCast([*]const u8, &act.mask), 8); - const result = syscall4(SYS_rt_sigaction, sig, @ptrToInt(&ksa), @ptrToInt(&ksa_old), @sizeOf(@typeOf(ksa.mask))); - const err = getErrno(result); - if (err != 0) { - return result; - } - if (oact) |old| { - old.handler = ksa_old.handler; - old.flags = @truncate(u32, ksa_old.flags); - @memcpy(@ptrCast([*]u8, &old.mask), @ptrCast([*]const u8, &ksa_old.mask), @sizeOf(@typeOf(ksa_old.mask))); - } - return 0; -} - -fn blockAllSignals(set: *sigset_t) void { - _ = syscall4(SYS_rt_sigprocmask, SIG_BLOCK, @ptrToInt(&all_mask), @ptrToInt(set), NSIG / 8); -} - -fn blockAppSignals(set: *sigset_t) void { - _ = syscall4(SYS_rt_sigprocmask, SIG_BLOCK, @ptrToInt(&app_mask), @ptrToInt(set), NSIG / 8); -} - -fn restoreSignals(set: *sigset_t) void { - _ = syscall4(SYS_rt_sigprocmask, SIG_SETMASK, @ptrToInt(set), 0, NSIG / 8); -} - -pub fn sigaddset(set: *sigset_t, sig: u6) void { - const s = sig - 1; - (set.*)[@intCast(usize, s) / usize.bit_count] |= @intCast(usize, 1) << (s & (usize.bit_count - 1)); -} - -pub fn sigismember(set: *const sigset_t, sig: u6) bool { - const s = sig - 1; - return ((set.*)[@intCast(usize, s) / usize.bit_count] & (@intCast(usize, 1) << (s & (usize.bit_count - 1)))) != 0; -} - -pub fn getsockname(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize { - return syscall3(SYS_getsockname, @bitCast(usize, isize(fd)), @ptrToInt(addr), @ptrToInt(len)); -} - -pub fn getpeername(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize { - return syscall3(SYS_getpeername, @bitCast(usize, isize(fd)), @ptrToInt(addr), @ptrToInt(len)); -} - -pub fn socket(domain: u32, socket_type: u32, protocol: u32) usize { - return syscall3(SYS_socket, domain, socket_type, protocol); -} - -pub fn setsockopt(fd: i32, level: u32, optname: u32, optval: [*]const u8, optlen: socklen_t) usize { - return syscall5(SYS_setsockopt, @bitCast(usize, isize(fd)), level, optname, @ptrToInt(optval), @intCast(usize, optlen)); -} - -pub fn getsockopt(fd: i32, level: u32, optname: u32, noalias optval: [*]u8, noalias optlen: *socklen_t) usize { - return syscall5(SYS_getsockopt, @bitCast(usize, isize(fd)), level, optname, @ptrToInt(optval), @ptrToInt(optlen)); -} - -pub fn sendmsg(fd: i32, msg: *msghdr_const, flags: u32) usize { - return syscall3(SYS_sendmsg, @bitCast(usize, isize(fd)), @ptrToInt(msg), flags); -} - -pub fn sendmmsg(fd: i32, msgvec: [*]mmsghdr_const, vlen: u32, flags: u32) usize { - if (@typeInfo(usize).Int.bits > @typeInfo(@typeOf(mmsghdr(undefined).msg_len)).Int.bits) { - // workaround kernel brokenness: - // if adding up all iov_len overflows a i32 then split into multiple calls - // see https://www.openwall.com/lists/musl/2014/06/07/5 - const kvlen = if (vlen > IOV_MAX) IOV_MAX else vlen; // matches kernel - var next_unsent: usize = 0; - for (msgvec[0..kvlen]) |*msg, i| { - var size: i32 = 0; - const msg_iovlen = @intCast(usize, msg.msg_hdr.msg_iovlen); // kernel side this is treated as unsigned - for (msg.msg_hdr.msg_iov[0..msg_iovlen]) |iov, j| { - if (iov.iov_len > std.math.maxInt(i32) or @addWithOverflow(i32, size, @intCast(i32, iov.iov_len), &size)) { - // batch-send all messages up to the current message - if (next_unsent < i) { - const batch_size = i - next_unsent; - const r = syscall4(SYS_sendmmsg, @bitCast(usize, isize(fd)), @ptrToInt(&msgvec[next_unsent]), batch_size, flags); - if (getErrno(r) != 0) return next_unsent; - if (r < batch_size) return next_unsent + r; - } - // send current message as own packet - const r = sendmsg(fd, &msg.msg_hdr, flags); - if (getErrno(r) != 0) return r; - // Linux limits the total bytes sent by sendmsg to INT_MAX, so this cast is safe. - msg.msg_len = @intCast(u32, r); - next_unsent = i + 1; - break; - } - } - } - if (next_unsent < kvlen or next_unsent == 0) { // want to make sure at least one syscall occurs (e.g. to trigger MSG_EOR) - const batch_size = kvlen - next_unsent; - const r = syscall4(SYS_sendmmsg, @bitCast(usize, isize(fd)), @ptrToInt(&msgvec[next_unsent]), batch_size, flags); - if (getErrno(r) != 0) return r; - return next_unsent + r; - } - return kvlen; - } - return syscall4(SYS_sendmmsg, @bitCast(usize, isize(fd)), @ptrToInt(msgvec), vlen, flags); -} - -pub fn connect(fd: i32, addr: *const c_void, len: socklen_t) usize { - return syscall3(SYS_connect, @bitCast(usize, isize(fd)), @ptrToInt(addr), len); -} - -pub fn recvmsg(fd: i32, msg: *msghdr, flags: u32) usize { - return syscall3(SYS_recvmsg, @bitCast(usize, isize(fd)), @ptrToInt(msg), flags); -} - -pub fn recvfrom(fd: i32, noalias buf: [*]u8, len: usize, flags: u32, noalias addr: ?*sockaddr, noalias alen: ?*socklen_t) usize { - return syscall6(SYS_recvfrom, @bitCast(usize, isize(fd)), @ptrToInt(buf), len, flags, @ptrToInt(addr), @ptrToInt(alen)); -} - -pub fn shutdown(fd: i32, how: i32) usize { - return syscall2(SYS_shutdown, @bitCast(usize, isize(fd)), @bitCast(usize, isize(how))); -} - -pub fn bind(fd: i32, addr: *const sockaddr, len: socklen_t) usize { - return syscall3(SYS_bind, @bitCast(usize, isize(fd)), @ptrToInt(addr), @intCast(usize, len)); -} - -pub fn listen(fd: i32, backlog: u32) usize { - return syscall2(SYS_listen, @bitCast(usize, isize(fd)), backlog); -} - -pub fn sendto(fd: i32, buf: [*]const u8, len: usize, flags: u32, addr: ?*const sockaddr, alen: socklen_t) usize { - return syscall6(SYS_sendto, @bitCast(usize, isize(fd)), @ptrToInt(buf), len, flags, @ptrToInt(addr), @intCast(usize, alen)); -} - -pub fn socketpair(domain: i32, socket_type: i32, protocol: i32, fd: [2]i32) usize { - return syscall4(SYS_socketpair, @intCast(usize, domain), @intCast(usize, socket_type), @intCast(usize, protocol), @ptrToInt(&fd[0])); -} - -pub fn accept(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t) usize { - return accept4(fd, addr, len, 0); -} - -pub fn accept4(fd: i32, noalias addr: *sockaddr, noalias len: *socklen_t, flags: u32) usize { - return syscall4(SYS_accept4, @bitCast(usize, isize(fd)), @ptrToInt(addr), @ptrToInt(len), flags); -} - -pub fn fstat(fd: i32, stat_buf: *Stat) usize { - return syscall2(SYS_fstat, @bitCast(usize, isize(fd)), @ptrToInt(stat_buf)); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn stat(pathname: [*]const u8, statbuf: *Stat) usize { - return syscall2(SYS_stat, @ptrToInt(pathname), @ptrToInt(statbuf)); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn lstat(pathname: [*]const u8, statbuf: *Stat) usize { - return syscall2(SYS_lstat, @ptrToInt(pathname), @ptrToInt(statbuf)); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn fstatat(dirfd: i32, path: [*]const u8, stat_buf: *Stat, flags: u32) usize { - return syscall4(SYS_fstatat, @bitCast(usize, isize(dirfd)), @ptrToInt(path), @ptrToInt(stat_buf), flags); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn listxattr(path: [*]const u8, list: [*]u8, size: usize) usize { - return syscall3(SYS_listxattr, @ptrToInt(path), @ptrToInt(list), size); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn llistxattr(path: [*]const u8, list: [*]u8, size: usize) usize { - return syscall3(SYS_llistxattr, @ptrToInt(path), @ptrToInt(list), size); -} - -pub fn flistxattr(fd: usize, list: [*]u8, size: usize) usize { - return syscall3(SYS_flistxattr, fd, @ptrToInt(list), size); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn getxattr(path: [*]const u8, name: [*]const u8, value: [*]u8, size: usize) usize { - return syscall4(SYS_getxattr, @ptrToInt(path), @ptrToInt(name), @ptrToInt(value), size); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn lgetxattr(path: [*]const u8, name: [*]const u8, value: [*]u8, size: usize) usize { - return syscall4(SYS_lgetxattr, @ptrToInt(path), @ptrToInt(name), @ptrToInt(value), size); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn fgetxattr(fd: usize, name: [*]const u8, value: [*]u8, size: usize) usize { - return syscall4(SYS_lgetxattr, fd, @ptrToInt(name), @ptrToInt(value), size); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn setxattr(path: [*]const u8, name: [*]const u8, value: *const void, size: usize, flags: usize) usize { - return syscall5(SYS_setxattr, @ptrToInt(path), @ptrToInt(name), @ptrToInt(value), size, flags); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn lsetxattr(path: [*]const u8, name: [*]const u8, value: *const void, size: usize, flags: usize) usize { - return syscall5(SYS_lsetxattr, @ptrToInt(path), @ptrToInt(name), @ptrToInt(value), size, flags); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn fsetxattr(fd: usize, name: [*]const u8, value: *const void, size: usize, flags: usize) usize { - return syscall5(SYS_fsetxattr, fd, @ptrToInt(name), @ptrToInt(value), size, flags); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn removexattr(path: [*]const u8, name: [*]const u8) usize { - return syscall2(SYS_removexattr, @ptrToInt(path), @ptrToInt(name)); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn lremovexattr(path: [*]const u8, name: [*]const u8) usize { - return syscall2(SYS_lremovexattr, @ptrToInt(path), @ptrToInt(name)); -} - -// TODO https://github.com/ziglang/zig/issues/265 -pub fn fremovexattr(fd: usize, name: [*]const u8) usize { - return syscall2(SYS_fremovexattr, fd, @ptrToInt(name)); -} - -pub fn sched_getaffinity(pid: i32, set: []usize) usize { - return syscall3(SYS_sched_getaffinity, @bitCast(usize, isize(pid)), set.len * @sizeOf(usize), @ptrToInt(set.ptr)); -} - -pub fn epoll_create() usize { - return epoll_create1(0); -} - -pub fn epoll_create1(flags: usize) usize { - return syscall1(SYS_epoll_create1, flags); -} - -pub fn epoll_ctl(epoll_fd: i32, op: u32, fd: i32, ev: *epoll_event) usize { - return syscall4(SYS_epoll_ctl, @bitCast(usize, isize(epoll_fd)), @intCast(usize, op), @bitCast(usize, isize(fd)), @ptrToInt(ev)); -} - -pub fn epoll_wait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout: i32) usize { - return syscall4( - SYS_epoll_wait, - @bitCast(usize, isize(epoll_fd)), - @ptrToInt(events), - maxevents, - @bitCast(usize, isize(timeout)), - ); -} - -pub fn epoll_pwait(epoll_fd: i32, events: [*]epoll_event, maxevents: u32, timeout: i32, sigmask: ?*sigset_t) usize { - return syscall6( - SYS_epoll_pwait, - @bitCast(usize, isize(epoll_fd)), - @ptrToInt(events), - @intCast(usize, maxevents), - @bitCast(usize, isize(timeout)), - @ptrToInt(sigmask), - @sizeOf(sigset_t), - ); -} - -pub fn eventfd(count: u32, flags: u32) usize { - return syscall2(SYS_eventfd2, count, flags); -} - -pub fn timerfd_create(clockid: i32, flags: u32) usize { - return syscall2(SYS_timerfd_create, @bitCast(usize, isize(clockid)), flags); -} - -pub const itimerspec = extern struct { - it_interval: timespec, - it_value: timespec, -}; - -pub fn timerfd_gettime(fd: i32, curr_value: *itimerspec) usize { - return syscall2(SYS_timerfd_gettime, @bitCast(usize, isize(fd)), @ptrToInt(curr_value)); -} - -pub fn timerfd_settime(fd: i32, flags: u32, new_value: *const itimerspec, old_value: ?*itimerspec) usize { - return syscall4(SYS_timerfd_settime, @bitCast(usize, isize(fd)), flags, @ptrToInt(new_value), @ptrToInt(old_value)); -} - -pub fn unshare(flags: usize) usize { - return syscall1(SYS_unshare, flags); -} - -pub fn capget(hdrp: *cap_user_header_t, datap: *cap_user_data_t) usize { - return syscall2(SYS_capget, @ptrToInt(hdrp), @ptrToInt(datap)); -} - -pub fn capset(hdrp: *cap_user_header_t, datap: *const cap_user_data_t) usize { - return syscall2(SYS_capset, @ptrToInt(hdrp), @ptrToInt(datap)); -} - -// XXX: This should be weak -extern const __ehdr_start: elf.Ehdr = undefined; - -pub fn dl_iterate_phdr(comptime T: type, callback: extern fn (info: *dl_phdr_info, size: usize, data: ?*T) i32, data: ?*T) isize { - if (builtin.link_libc) { - return std.c.dl_iterate_phdr(@ptrCast(std.c.dl_iterate_phdr_callback, callback), @ptrCast(?*c_void, data)); - } - - const elf_base = @ptrToInt(&__ehdr_start); - const n_phdr = __ehdr_start.e_phnum; - const phdrs = (@intToPtr([*]elf.Phdr, elf_base + __ehdr_start.e_phoff))[0..n_phdr]; - - var it = dl.linkmap_iterator(phdrs) catch return 0; - - // The executable has no dynamic link segment, create a single entry for - // the whole ELF image - if (it.end()) { - var info = dl_phdr_info{ - .dlpi_addr = elf_base, - .dlpi_name = c"/proc/self/exe", - .dlpi_phdr = @intToPtr([*]elf.Phdr, elf_base + __ehdr_start.e_phoff), - .dlpi_phnum = __ehdr_start.e_phnum, - }; - - return callback(&info, @sizeOf(dl_phdr_info), data); - } - - // Last return value from the callback function - var last_r: isize = 0; - while (it.next()) |entry| { - var dlpi_phdr: usize = undefined; - var dlpi_phnum: u16 = undefined; - - if (entry.l_addr != 0) { - const elf_header = @intToPtr(*elf.Ehdr, entry.l_addr); - dlpi_phdr = entry.l_addr + elf_header.e_phoff; - dlpi_phnum = elf_header.e_phnum; - } else { - // This is the running ELF image - dlpi_phdr = elf_base + __ehdr_start.e_phoff; - dlpi_phnum = __ehdr_start.e_phnum; - } - - var info = dl_phdr_info{ - .dlpi_addr = entry.l_addr, - .dlpi_name = entry.l_name, - .dlpi_phdr = @intToPtr([*]elf.Phdr, dlpi_phdr), - .dlpi_phnum = dlpi_phnum, - }; - - last_r = callback(&info, @sizeOf(dl_phdr_info), data); - if (last_r != 0) break; - } - - return last_r; -} diff --git a/std/os/linux/tls.zig b/std/os/linux/tls.zig index e040a45bc0..f06ed144b9 100644 --- a/std/os/linux/tls.zig +++ b/std/os/linux/tls.zig @@ -1,6 +1,6 @@ const std = @import("std"); +const os = std.os; const mem = std.mem; -const posix = std.os.posix; const elf = std.elf; const builtin = @import("builtin"); const assert = std.debug.assert; @@ -237,9 +237,14 @@ pub fn allocateTLS(size: usize) usize { return @ptrToInt(&main_thread_tls_buffer); } - const addr = posix.mmap(null, size, posix.PROT_READ | posix.PROT_WRITE, posix.MAP_PRIVATE | posix.MAP_ANONYMOUS, -1, 0); - - if (posix.getErrno(addr) != 0) @panic("out of memory"); + const addr = os.mmap( + null, + size, + os.PROT_READ | os.PROT_WRITE, + os.MAP_PRIVATE | os.MAP_ANONYMOUS, + -1, + 0, + ) catch @panic("out of memory"); return addr; } diff --git a/std/os/linux/x86_64.zig b/std/os/linux/x86_64.zig index 9909b6ee3f..fa866cff4c 100644 --- a/std/os/linux/x86_64.zig +++ b/std/os/linux/x86_64.zig @@ -1,4 +1,4 @@ -use @import("posix/x86_64.zig"); +use @import("../bits.zig"); pub fn syscall0(number: usize) usize { return asm volatile ("syscall" diff --git a/std/os/test.zig b/std/os/test.zig index 54371e94ab..d4bdb06278 100644 --- a/std/os/test.zig +++ b/std/os/test.zig @@ -5,6 +5,7 @@ const expect = std.testing.expect; const io = std.io; const mem = std.mem; const File = std.fs.File; +const Thread = std.Thread; const a = std.debug.global_allocator; @@ -37,25 +38,25 @@ test "access file" { try os.deleteTree(a, "os_test_tmp"); } -fn testThreadIdFn(thread_id: *os.Thread.Id) void { - thread_id.* = os.Thread.getCurrentId(); +fn testThreadIdFn(thread_id: *Thread.Id) void { + thread_id.* = Thread.getCurrentId(); } -test "std.os.Thread.getCurrentId" { +test "std.Thread.getCurrentId" { if (builtin.single_threaded) return error.SkipZigTest; - var thread_current_id: os.Thread.Id = undefined; - const thread = try os.spawnThread(&thread_current_id, testThreadIdFn); + var thread_current_id: Thread.Id = undefined; + const thread = try Thread.spawn(&thread_current_id, testThreadIdFn); const thread_id = thread.handle(); thread.wait(); - if (os.Thread.use_pthreads) { + if (Thread.use_pthreads) { expect(thread_current_id == thread_id); } else { switch (builtin.os) { - builtin.Os.windows => expect(os.Thread.getCurrentId() != thread_current_id), + builtin.Os.windows => expect(Thread.getCurrentId() != thread_current_id), else => { // If the thread completes very quickly, then thread_id can be 0. See the - // documentation comments for `std.os.Thread.handle`. + // documentation comments for `std.Thread.handle`. expect(thread_id == 0 or thread_current_id == thread_id); }, } @@ -67,10 +68,10 @@ test "spawn threads" { var shared_ctx: i32 = 1; - const thread1 = try std.os.spawnThread({}, start1); - const thread2 = try std.os.spawnThread(&shared_ctx, start2); - const thread3 = try std.os.spawnThread(&shared_ctx, start2); - const thread4 = try std.os.spawnThread(&shared_ctx, start2); + const thread1 = try Thread.spawn({}, start1); + const thread2 = try Thread.spawn(&shared_ctx, start2); + const thread3 = try Thread.spawn(&shared_ctx, start2); + const thread4 = try Thread.spawn(&shared_ctx, start2); thread1.wait(); thread2.wait(); @@ -116,8 +117,8 @@ test "AtomicFile" { test "thread local storage" { if (builtin.single_threaded) return error.SkipZigTest; - const thread1 = try std.os.spawnThread({}, testTls); - const thread2 = try std.os.spawnThread({}, testTls); + const thread1 = try Thread.spawn({}, testTls); + const thread2 = try Thread.spawn({}, testTls); testTls({}); thread1.wait(); thread2.wait(); diff --git a/std/os/windows.zig b/std/os/windows.zig index e35d8bbbd2..c18adc66fa 100644 --- a/std/os/windows.zig +++ b/std/os/windows.zig @@ -591,7 +591,7 @@ pub fn CreateFileW( ERROR.ACCESS_DENIED => return error.AccessDenied, ERROR.PIPE_BUSY => return error.PipeBusy, ERROR.FILENAME_EXCED_RANGE => return error.NameTooLong, - else => |err| return unexpectedErrorWindows(err), + else => |err| return unexpectedError(err), } } @@ -1089,6 +1089,20 @@ pub fn TerminateProcess(hProcess: HANDLE, uExitCode: UINT) TerminateProcessError } } +pub const VirtualAllocError = error{Unexpected}; + +pub fn VirtualAlloc(addr: ?LPVOID, size: usize, alloc_type: DWORD, flProtect: DWORD) VirtualAllocError!LPVOID { + return kernel32.VirtualAlloc(addr, size, alloc_type, flProtect) orelse { + switch (kernel32.GetLastError()) { + else => |err| return unexpectedError(err), + } + }; +} + +pub fn VirtualFree(lpAddress: ?LPVOID, dwSize: usize, dwFreeType: DWORD) void { + assert(kernel32.VirtualFree(lpAddress, dwSize, dwFreeType) != 0); +} + pub fn cStrToPrefixedFileW(s: [*]const u8) ![PATH_MAX_WIDE + 1]u16 { return sliceToPrefixedFileW(mem.toSliceConst(u8, s)); } diff --git a/std/process.zig b/std/process.zig index f2f1dbf4fd..1e29170e88 100644 --- a/std/process.zig +++ b/std/process.zig @@ -1,13 +1,13 @@ const std = @import("std.zig"); -const posix = std.os.posix; +const os = std.os; const BufMap = std.BufMap; const mem = std.mem; const Allocator = mem.Allocator; const assert = std.debug.assert; const testing = std.testing; -pub const abort = posix.abort; -pub const exit = posix.exit; +pub const abort = os.abort; +pub const exit = os.exit; /// Caller must free result when done. /// TODO make this go through libc when we have it @@ -42,13 +42,13 @@ pub fn getEnvMap(allocator: *Allocator) !BufMap { try result.setMove(key, value); } - } else if (builtin.os == Os.wasi) { + } else if (builtin.os == .wasi) { var environ_count: usize = undefined; var environ_buf_size: usize = undefined; - const environ_sizes_get_ret = std.os.wasi.environ_sizes_get(&environ_count, &environ_buf_size); + const environ_sizes_get_ret = os.wasi.environ_sizes_get(&environ_count, &environ_buf_size); if (environ_sizes_get_ret != os.wasi.ESUCCESS) { - return unexpectedErrorPosix(environ_sizes_get_ret); + return os.unexpectedErrno(environ_sizes_get_ret); } // TODO: Verify that the documentation is incorrect @@ -58,9 +58,9 @@ pub fn getEnvMap(allocator: *Allocator) !BufMap { var environ_buf = try std.heap.wasm_allocator.alloc(u8, environ_buf_size); defer allocator.free(environ_buf); - const environ_get_ret = std.os.wasi.environ_get(environ.ptr, environ_buf.ptr); + const environ_get_ret = os.wasi.environ_get(environ.ptr, environ_buf.ptr); if (environ_get_ret != os.wasi.ESUCCESS) { - return unexpectedErrorPosix(environ_get_ret); + return os.unexpectedErrno(environ_get_ret); } for (environ) |env| { @@ -74,7 +74,7 @@ pub fn getEnvMap(allocator: *Allocator) !BufMap { } return result; } else { - for (posix.environ) |ptr| { + for (os.environ) |ptr| { var line_i: usize = 0; while (ptr[line_i] != 0 and ptr[line_i] != '=') : (line_i += 1) {} const key = ptr[0..line_i]; @@ -331,12 +331,12 @@ pub const ArgIteratorWindows = struct { }; pub const ArgIterator = struct { - const InnerType = if (builtin.os == Os.windows) ArgIteratorWindows else ArgIteratorPosix; + const InnerType = if (builtin.os == .windows) ArgIteratorWindows else ArgIteratorPosix; inner: InnerType, pub fn init() ArgIterator { - if (builtin.os == Os.wasi) { + if (builtin.os == .wasi) { // TODO: Figure out a compatible interface accomodating WASI @compileError("ArgIterator is not yet supported in WASI. Use argsAlloc and argsFree instead."); } @@ -348,7 +348,7 @@ pub const ArgIterator = struct { /// You must free the returned memory when done. pub fn next(self: *ArgIterator, allocator: *Allocator) ?(NextError![]u8) { - if (builtin.os == Os.windows) { + if (builtin.os == .windows) { return self.inner.next(allocator); } else { return mem.dupe(allocator, u8, self.inner.next() orelse return null); @@ -373,13 +373,13 @@ pub fn args() ArgIterator { /// Caller must call argsFree on result. pub fn argsAlloc(allocator: *mem.Allocator) ![]const []u8 { - if (builtin.os == Os.wasi) { + if (builtin.os == .wasi) { var count: usize = undefined; var buf_size: usize = undefined; const args_sizes_get_ret = os.wasi.args_sizes_get(&count, &buf_size); if (args_sizes_get_ret != os.wasi.ESUCCESS) { - return unexpectedErrorPosix(args_sizes_get_ret); + return os.unexpectedErrno(args_sizes_get_ret); } var argv = try allocator.alloc([*]u8, count); @@ -388,7 +388,7 @@ pub fn argsAlloc(allocator: *mem.Allocator) ![]const []u8 { var argv_buf = try allocator.alloc(u8, buf_size); const args_get_ret = os.wasi.args_get(argv.ptr, argv_buf.ptr); if (args_get_ret != os.wasi.ESUCCESS) { - return unexpectedErrorPosix(args_get_ret); + return os.unexpectedErrno(args_get_ret); } var result_slice = try allocator.alloc([]u8, count); @@ -438,7 +438,7 @@ pub fn argsAlloc(allocator: *mem.Allocator) ![]const []u8 { } pub fn argsFree(allocator: *mem.Allocator, args_alloc: []const []u8) void { - if (builtin.os == Os.wasi) { + if (builtin.os == .wasi) { const last_item = args_alloc[args_alloc.len - 1]; const last_byte_addr = @ptrToInt(last_item.ptr) + last_item.len + 1; // null terminated const first_item_ptr = args_alloc[0].ptr; @@ -491,7 +491,7 @@ pub const UserInfo = struct { /// POSIX function which gets a uid from username. pub fn getUserInfo(name: []const u8) !UserInfo { return switch (builtin.os) { - .linux, .macosx, .ios, .freebsd, .netbsd => posixGetUserInfo(name), + .linux, .macosx, .watchos, .tvos, .ios, .freebsd, .netbsd => posixGetUserInfo(name), else => @compileError("Unsupported OS"), }; } diff --git a/std/special/bootstrap.zig b/std/special/bootstrap.zig index c6630a594c..86228acb9f 100644 --- a/std/special/bootstrap.zig +++ b/std/special/bootstrap.zig @@ -92,14 +92,14 @@ fn posixCallMainAndExit() noreturn { } } - std.os.posix.exit(callMainWithArgs(argc, argv, envp)); + std.os.exit(callMainWithArgs(argc, argv, envp)); } // This is marked inline because for some reason LLVM in release mode fails to inline it, // and we want fewer call frames in stack traces. inline fn callMainWithArgs(argc: usize, argv: [*][*]u8, envp: [][*]u8) u8 { std.os.ArgIteratorPosix.raw = argv[0..argc]; - std.os.posix.environ = envp; + std.os.environ = envp; return callMain(); } diff --git a/std/statically_initialized_mutex.zig b/std/statically_initialized_mutex.zig index cfcaf036d3..9c59fccefe 100644 --- a/std/statically_initialized_mutex.zig +++ b/std/statically_initialized_mutex.zig @@ -96,9 +96,9 @@ test "std.StaticallyInitializedMutex" { expect(context.data == TestContext.incr_count); } else { const thread_count = 10; - var threads: [thread_count]*std.os.Thread = undefined; + var threads: [thread_count]*std.Thread = undefined; for (threads) |*t| { - t.* = try std.os.spawnThread(&context, TestContext.worker); + t.* = try std.Thread.spawn(&context, TestContext.worker); } for (threads) |t| t.wait(); diff --git a/std/thread.zig b/std/thread.zig index 729d4aff60..eac6b73f7d 100644 --- a/std/thread.zig +++ b/std/thread.zig @@ -1,6 +1,8 @@ const builtin = @import("builtin"); const std = @import("std.zig"); +const os = std.os; const windows = std.os.windows; +const c = std.c; pub const Thread = struct { data: Data, @@ -13,8 +15,8 @@ pub const Thread = struct { pub const Handle = if (use_pthreads) c.pthread_t else switch (builtin.os) { - builtin.Os.linux => i32, - builtin.Os.windows => windows.HANDLE, + .linux => i32, + .windows => windows.HANDLE, else => @compileError("Unsupported OS"), }; @@ -22,7 +24,7 @@ pub const Thread = struct { /// May be an integer or pointer depending on the platform. /// On Linux and POSIX, this is the same as Handle. pub const Id = switch (builtin.os) { - builtin.Os.windows => windows.DWORD, + .windows => windows.DWORD, else => Handle, }; @@ -33,12 +35,12 @@ pub const Thread = struct { mmap_len: usize, } else switch (builtin.os) { - builtin.Os.linux => struct { + .linux => struct { handle: Thread.Handle, mmap_addr: usize, mmap_len: usize, }, - builtin.Os.windows => struct { + .windows => struct { handle: Thread.Handle, alloc_start: *c_void, heap_handle: windows.HANDLE, @@ -54,8 +56,8 @@ pub const Thread = struct { return c.pthread_self(); } else return switch (builtin.os) { - builtin.Os.linux => linux.gettid(), - builtin.Os.windows => windows.GetCurrentThreadId(), + .linux => linux.gettid(), + .windows => windows.GetCurrentThreadId(), else => @compileError("Unsupported OS"), }; } @@ -75,28 +77,28 @@ pub const Thread = struct { const err = c.pthread_join(self.data.handle, null); switch (err) { 0 => {}, - posix.EINVAL => unreachable, - posix.ESRCH => unreachable, - posix.EDEADLK => unreachable, + os.EINVAL => unreachable, + os.ESRCH => unreachable, + os.EDEADLK => unreachable, else => unreachable, } - assert(posix.munmap(self.data.mmap_addr, self.data.mmap_len) == 0); + os.munmap(self.data.mmap_addr, self.data.mmap_len); } else switch (builtin.os) { - builtin.Os.linux => { + .linux => { while (true) { const pid_value = @atomicLoad(i32, &self.data.handle, .SeqCst); if (pid_value == 0) break; const rc = linux.futex_wait(&self.data.handle, linux.FUTEX_WAIT, pid_value, null); switch (linux.getErrno(rc)) { 0 => continue, - posix.EINTR => continue, - posix.EAGAIN => continue, + os.EINTR => continue, + os.EAGAIN => continue, else => unreachable, } } - assert(posix.munmap(self.data.mmap_addr, self.data.mmap_len) == 0); + os.munmap(self.data.mmap_addr, self.data.mmap_len); }, - builtin.Os.windows => { + .windows => { assert(windows.WaitForSingleObject(self.data.handle, windows.INFINITE) == windows.WAIT_OBJECT_0); assert(windows.CloseHandle(self.data.handle) != 0); assert(windows.HeapFree(self.data.heap_handle, 0, self.data.alloc_start) != 0); @@ -153,10 +155,10 @@ pub const Thread = struct { extern fn threadMain(raw_arg: windows.LPVOID) windows.DWORD { const arg = if (@sizeOf(Context) == 0) {} else @ptrCast(*Context, @alignCast(@alignOf(Context), raw_arg)).*; switch (@typeId(@typeOf(startFn).ReturnType)) { - builtin.TypeId.Int => { + .Int => { return startFn(arg); }, - builtin.TypeId.Void => { + .Void => { startFn(arg); return 0; }, @@ -196,10 +198,10 @@ pub const Thread = struct { const arg = if (@sizeOf(Context) == 0) {} else @intToPtr(*const Context, ctx_addr).*; switch (@typeId(@typeOf(startFn).ReturnType)) { - builtin.TypeId.Int => { + .Int => { return startFn(arg); }, - builtin.TypeId.Void => { + .Void => { startFn(arg); return 0; }, @@ -217,7 +219,7 @@ pub const Thread = struct { } }; - const MAP_GROWSDOWN = if (builtin.os == builtin.Os.linux) linux.MAP_GROWSDOWN else 0; + const MAP_GROWSDOWN = if (builtin.os == .linux) linux.MAP_GROWSDOWN else 0; var stack_end_offset: usize = undefined; var thread_start_offset: usize = undefined; @@ -247,9 +249,8 @@ pub const Thread = struct { } break :blk l; }; - const mmap_addr = posix.mmap(null, mmap_len, posix.PROT_READ | posix.PROT_WRITE, posix.MAP_PRIVATE | posix.MAP_ANONYMOUS | MAP_GROWSDOWN, -1, 0); - if (mmap_addr == posix.MAP_FAILED) return error.OutOfMemory; - errdefer assert(posix.munmap(mmap_addr, mmap_len) == 0); + const mmap_addr = try os.mmap(null, mmap_len, os.PROT_READ | os.PROT_WRITE, os.MAP_PRIVATE | os.MAP_ANONYMOUS | MAP_GROWSDOWN, -1, 0); + errdefer os.munmap(mmap_addr, mmap_len); const thread_ptr = @alignCast(@alignOf(Thread), @intToPtr(*Thread, mmap_addr + thread_start_offset)); thread_ptr.data.mmap_addr = mmap_addr; @@ -273,31 +274,30 @@ pub const Thread = struct { const err = c.pthread_create(&thread_ptr.data.handle, &attr, MainFuncs.posixThreadMain, @intToPtr(*c_void, arg)); switch (err) { 0 => return thread_ptr, - posix.EAGAIN => return error.SystemResources, - posix.EPERM => unreachable, - posix.EINVAL => unreachable, - else => return unexpectedErrorPosix(@intCast(usize, err)), + os.EAGAIN => return error.SystemResources, + os.EPERM => unreachable, + os.EINVAL => unreachable, + else => return os.unexpectedErrno(@intCast(usize, err)), } - } else if (builtin.os == builtin.Os.linux) { - var flags: u32 = posix.CLONE_VM | posix.CLONE_FS | posix.CLONE_FILES | posix.CLONE_SIGHAND | - posix.CLONE_THREAD | posix.CLONE_SYSVSEM | posix.CLONE_PARENT_SETTID | posix.CLONE_CHILD_CLEARTID | - posix.CLONE_DETACHED; + } else if (builtin.os == .linux) { + var flags: u32 = os.CLONE_VM | os.CLONE_FS | os.CLONE_FILES | os.CLONE_SIGHAND | + os.CLONE_THREAD | os.CLONE_SYSVSEM | os.CLONE_PARENT_SETTID | os.CLONE_CHILD_CLEARTID | + os.CLONE_DETACHED; var newtls: usize = undefined; if (linux.tls.tls_image) |tls_img| { newtls = linux.tls.copyTLS(mmap_addr + tls_start_offset); - flags |= posix.CLONE_SETTLS; + flags |= os.CLONE_SETTLS; } - const rc = posix.clone(MainFuncs.linuxThreadMain, mmap_addr + stack_end_offset, flags, arg, &thread_ptr.data.handle, newtls, &thread_ptr.data.handle); - const err = posix.getErrno(rc); - switch (err) { + const rc = os.linux.clone(MainFuncs.linuxThreadMain, mmap_addr + stack_end_offset, flags, arg, &thread_ptr.data.handle, newtls, &thread_ptr.data.handle); + switch (os.errno(rc)) { 0 => return thread_ptr, - posix.EAGAIN => return error.ThreadQuotaExceeded, - posix.EINVAL => unreachable, - posix.ENOMEM => return error.SystemResources, - posix.ENOSPC => unreachable, - posix.EPERM => unreachable, - posix.EUSERS => unreachable, - else => return unexpectedErrorPosix(err), + os.EAGAIN => return error.ThreadQuotaExceeded, + os.EINVAL => unreachable, + os.ENOMEM => return error.SystemResources, + os.ENOSPC => unreachable, + os.EPERM => unreachable, + os.EUSERS => unreachable, + else => |err| return os.unexpectedErrno(err), } } else { @compileError("Unsupported OS"); @@ -310,56 +310,20 @@ pub const Thread = struct { Unexpected, }; - pub fn cpuCount(fallback_allocator: *mem.Allocator) CpuCountError!usize { - switch (builtin.os) { - .macosx, .freebsd, .netbsd => { - var count: c_int = undefined; - var count_len: usize = @sizeOf(c_int); - const name = switch (builtin.os) { - builtin.Os.macosx => c"hw.logicalcpu", - else => c"hw.ncpu", - }; - try posix.sysctlbyname(name, @ptrCast(*c_void, &count), &count_len, null, 0); - return @intCast(usize, count); - }, - .linux => { - const usize_count = 16; - const allocator = std.heap.stackFallback(usize_count * @sizeOf(usize), fallback_allocator).get(); - - var set = try allocator.alloc(usize, usize_count); - defer allocator.free(set); - - while (true) { - const rc = posix.sched_getaffinity(0, set); - const err = posix.getErrno(rc); - switch (err) { - 0 => { - if (rc < set.len * @sizeOf(usize)) { - const result = set[0 .. rc / @sizeOf(usize)]; - var sum: usize = 0; - for (result) |x| { - sum += @popCount(usize, x); - } - return sum; - } else { - set = try allocator.realloc(set, set.len * 2); - continue; - } - }, - posix.EFAULT => unreachable, - posix.EINVAL => unreachable, - posix.EPERM => return CpuCountError.PermissionDenied, - posix.ESRCH => unreachable, - else => return os.unexpectedErrorPosix(err), - } - } - }, - .windows => { - var system_info: windows.SYSTEM_INFO = undefined; - windows.GetSystemInfo(&system_info); - return @intCast(usize, system_info.dwNumberOfProcessors); - }, - else => @compileError("unsupported OS"), + pub fn cpuCount() CpuCountError!usize { + if (os.linux.is_the_target) { + const cpu_set = try os.sched_getaffinity(0); + return os.CPU_COUNT(cpu_set); + } + if (os.windows.is_the_target) { + var system_info: windows.SYSTEM_INFO = undefined; + windows.GetSystemInfo(&system_info); + return @intCast(usize, system_info.dwNumberOfProcessors); } + var count: c_int = undefined; + var count_len: usize = @sizeOf(c_int); + const name = if (os.darwin.is_the_target) c"hw.logicalcpu" else c"hw.ncpu"; + try os.sysctlbyname(name, @ptrCast(*c_void, &count), &count_len, null, 0); + return @intCast(usize, count); } }; -- cgit v1.2.3 From 2f040a23c8b968db56ab4c4725d6651f5ea3418e Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 26 May 2019 13:17:34 -0400 Subject: clean up references to os --- build.zig | 28 +++--- doc/docgen.zig | 58 ++++++------ doc/langref.html.in | 2 +- src-self-hosted/compilation.zig | 23 +++-- src-self-hosted/errmsg.zig | 9 +- src-self-hosted/introspect.zig | 12 +-- src-self-hosted/libc_installation.zig | 35 ++++---- src-self-hosted/link.zig | 6 +- src-self-hosted/main.zig | 80 +++++++++-------- src-self-hosted/stage1.zig | 59 ++++++------ src-self-hosted/test.zig | 22 ++--- std/buffer.zig | 4 +- std/build.zig | 165 +++++++++++++++++----------------- std/child_process.zig | 48 +++++----- std/cstr.zig | 15 ---- std/debug.zig | 140 ++++++++++++++--------------- std/dynamic_library.zig | 5 +- std/event/fs.zig | 12 +-- std/fmt.zig | 2 +- std/fs.zig | 20 ++--- std/fs/file.zig | 24 ----- std/fs/get_app_data_dir.zig | 13 +-- std/fs/path.zig | 22 +++-- std/heap.zig | 36 ++++---- std/io.zig | 27 +++++- std/mem.zig | 18 ---- std/os.zig | 107 +++++++++++----------- std/os/bits/linux.zig | 10 +-- std/os/bits/wasi.zig | 10 +-- std/os/linux.zig | 7 +- std/os/linux/vdso.zig | 5 +- std/os/test.zig | 13 +-- std/os/windows.zig | 39 +++++++- std/process.zig | 68 ++++++++------ std/special/bootstrap.zig | 4 +- std/special/build_runner.zig | 8 +- std/thread.zig | 2 +- test/cli.zig | 40 +++++---- test/tests.zig | 90 +++++++++---------- 39 files changed, 648 insertions(+), 640 deletions(-) (limited to 'std/debug.zig') diff --git a/build.zig b/build.zig index 2bc0229475..ec0bd9c02c 100644 --- a/build.zig +++ b/build.zig @@ -2,28 +2,28 @@ const builtin = @import("builtin"); const std = @import("std"); const Builder = std.build.Builder; const tests = @import("test/tests.zig"); -const os = std.os; const BufMap = std.BufMap; const warn = std.debug.warn; const mem = std.mem; const ArrayList = std.ArrayList; const Buffer = std.Buffer; const io = std.io; +const fs = std.fs; pub fn build(b: *Builder) !void { const mode = b.standardReleaseOptions(); var docgen_exe = b.addExecutable("docgen", "doc/docgen.zig"); - const rel_zig_exe = try os.path.relative(b.allocator, b.build_root, b.zig_exe); - const langref_out_path = os.path.join( + const rel_zig_exe = try fs.path.relative(b.allocator, b.build_root, b.zig_exe); + const langref_out_path = fs.path.join( b.allocator, [][]const u8{ b.cache_root, "langref.html" }, ) catch unreachable; var docgen_cmd = docgen_exe.run(); docgen_cmd.addArgs([][]const u8{ rel_zig_exe, - "doc" ++ os.path.sep_str ++ "langref.html.in", + "doc" ++ fs.path.sep_str ++ "langref.html.in", langref_out_path, }); docgen_cmd.step.dependOn(&docgen_exe.step); @@ -137,7 +137,7 @@ fn dependOnLib(b: *Builder, lib_exe_obj: var, dep: LibraryDep) void { for (dep.libdirs.toSliceConst()) |lib_dir| { lib_exe_obj.addLibPath(lib_dir); } - const lib_dir = os.path.join( + const lib_dir = fs.path.join( b.allocator, [][]const u8{ dep.prefix, "lib" }, ) catch unreachable; @@ -146,7 +146,7 @@ fn dependOnLib(b: *Builder, lib_exe_obj: var, dep: LibraryDep) void { ([]const u8)("libncurses.a") else b.fmt("lib{}.a", lib); - const static_lib_name = os.path.join( + const static_lib_name = fs.path.join( b.allocator, [][]const u8{ lib_dir, static_bare_name }, ) catch unreachable; @@ -166,7 +166,7 @@ fn dependOnLib(b: *Builder, lib_exe_obj: var, dep: LibraryDep) void { } fn fileExists(filename: []const u8) !bool { - os.File.access(filename) catch |err| switch (err) { + fs.File.access(filename) catch |err| switch (err) { error.PermissionDenied, error.FileNotFound, => return false, @@ -177,7 +177,7 @@ fn fileExists(filename: []const u8) !bool { fn addCppLib(b: *Builder, lib_exe_obj: var, cmake_binary_dir: []const u8, lib_name: []const u8) void { const lib_prefix = if (lib_exe_obj.target.isWindows()) "" else "lib"; - lib_exe_obj.addObjectFile(os.path.join(b.allocator, [][]const u8{ + lib_exe_obj.addObjectFile(fs.path.join(b.allocator, [][]const u8{ cmake_binary_dir, "zig_cpp", b.fmt("{}{}{}", lib_prefix, lib_name, lib_exe_obj.target.libFileExt()), @@ -223,7 +223,7 @@ fn findLLVM(b: *Builder, llvm_config_exe: []const u8) !LibraryDep { if (mem.startsWith(u8, lib_arg, "-l")) { try result.system_libs.append(lib_arg[2..]); } else { - if (os.path.isAbsolute(lib_arg)) { + if (fs.path.isAbsolute(lib_arg)) { try result.libs.append(lib_arg); } else { try result.system_libs.append(lib_arg); @@ -257,8 +257,8 @@ fn findLLVM(b: *Builder, llvm_config_exe: []const u8) !LibraryDep { pub fn installStdLib(b: *Builder, stdlib_files: []const u8) void { var it = mem.tokenize(stdlib_files, ";"); while (it.next()) |stdlib_file| { - const src_path = os.path.join(b.allocator, [][]const u8{ "std", stdlib_file }) catch unreachable; - const dest_path = os.path.join( + const src_path = fs.path.join(b.allocator, [][]const u8{ "std", stdlib_file }) catch unreachable; + const dest_path = fs.path.join( b.allocator, [][]const u8{ "lib", "zig", "std", stdlib_file }, ) catch unreachable; @@ -269,8 +269,8 @@ pub fn installStdLib(b: *Builder, stdlib_files: []const u8) void { pub fn installCHeaders(b: *Builder, c_header_files: []const u8) void { var it = mem.tokenize(c_header_files, ";"); while (it.next()) |c_header_file| { - const src_path = os.path.join(b.allocator, [][]const u8{ "c_headers", c_header_file }) catch unreachable; - const dest_path = os.path.join( + const src_path = fs.path.join(b.allocator, [][]const u8{ "c_headers", c_header_file }) catch unreachable; + const dest_path = fs.path.join( b.allocator, [][]const u8{ "lib", "zig", "include", c_header_file }, ) catch unreachable; @@ -315,7 +315,7 @@ fn configureStage2(b: *Builder, exe: var, ctx: Context) !void { } dependOnLib(b, exe, ctx.llvm); - if (exe.target.getOs() == builtin.Os.linux) { + if (exe.target.getOs() == .linux) { try addCxxKnownPath(b, ctx, exe, "libstdc++.a", \\Unable to determine path to libstdc++.a \\On Fedora, install libstdc++-static and try again. diff --git a/doc/docgen.zig b/doc/docgen.zig index a0e1b39bf6..998db54bbc 100644 --- a/doc/docgen.zig +++ b/doc/docgen.zig @@ -1,7 +1,9 @@ const builtin = @import("builtin"); const std = @import("std"); const io = std.io; -const os = std.os; +const fs = std.fs; +const process = std.process; +const ChildProcess = std.ChildProcess; const warn = std.debug.warn; const mem = std.mem; const testing = std.testing; @@ -11,7 +13,7 @@ const max_doc_file_size = 10 * 1024 * 1024; const exe_ext = std.build.Target(std.build.Target.Native).exeFileExt(); const obj_ext = std.build.Target(std.build.Target.Native).oFileExt(); const tmp_dir_name = "docgen_tmp"; -const test_out_path = tmp_dir_name ++ os.path.sep_str ++ "test" ++ exe_ext; +const test_out_path = tmp_dir_name ++ fs.path.sep_str ++ "test" ++ exe_ext; pub fn main() !void { var direct_allocator = std.heap.DirectAllocator.init(); @@ -22,7 +24,7 @@ pub fn main() !void { const allocator = &arena.allocator; - var args_it = os.args(); + var args_it = process.args(); if (!args_it.skip()) @panic("expected self arg"); @@ -35,10 +37,10 @@ pub fn main() !void { const out_file_name = try (args_it.next(allocator) orelse @panic("expected output arg")); defer allocator.free(out_file_name); - var in_file = try os.File.openRead(in_file_name); + var in_file = try fs.File.openRead(in_file_name); defer in_file.close(); - var out_file = try os.File.openWrite(out_file_name); + var out_file = try fs.File.openWrite(out_file_name); defer out_file.close(); var file_in_stream = in_file.inStream(); @@ -46,13 +48,13 @@ pub fn main() !void { const input_file_bytes = try file_in_stream.stream.readAllAlloc(allocator, max_doc_file_size); var file_out_stream = out_file.outStream(); - var buffered_out_stream = io.BufferedOutStream(os.File.WriteError).init(&file_out_stream.stream); + var buffered_out_stream = io.BufferedOutStream(fs.File.WriteError).init(&file_out_stream.stream); var tokenizer = Tokenizer.init(in_file_name, input_file_bytes); var toc = try genToc(allocator, &tokenizer); - try os.makePath(allocator, tmp_dir_name); - defer os.deleteTree(allocator, tmp_dir_name) catch {}; + try fs.makePath(allocator, tmp_dir_name); + defer fs.deleteTree(allocator, tmp_dir_name) catch {}; try genHtml(allocator, &tokenizer, &toc, &buffered_out_stream.stream, zig_exe); try buffered_out_stream.flush(); @@ -950,7 +952,7 @@ fn tokenizeAndPrint(docgen_tokenizer: *Tokenizer, out: var, source_token: Token) fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var, zig_exe: []const u8) !void { var code_progress_index: usize = 0; - var env_map = try os.getEnvMap(allocator); + var env_map = try process.getEnvMap(allocator); try env_map.set("ZIG_DEBUG_COLOR", "1"); const builtin_code = try getBuiltinCode(allocator, &env_map, zig_exe); @@ -1012,7 +1014,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var try tokenizeAndPrint(tokenizer, out, code.source_token); try out.write(""); const name_plus_ext = try std.fmt.allocPrint(allocator, "{}.zig", code.name); - const tmp_source_file_name = try os.path.join( + const tmp_source_file_name = try fs.path.join( allocator, [][]const u8{ tmp_dir_name, name_plus_ext }, ); @@ -1021,7 +1023,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var switch (code.id) { Code.Id.Exe => |expected_outcome| code_block: { const name_plus_bin_ext = try std.fmt.allocPrint(allocator, "{}{}", code.name, exe_ext); - const tmp_bin_file_name = try os.path.join( + const tmp_bin_file_name = try fs.path.join( allocator, [][]const u8{ tmp_dir_name, name_plus_bin_ext }, ); @@ -1056,7 +1058,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var } for (code.link_objects) |link_object| { const name_with_ext = try std.fmt.allocPrint(allocator, "{}{}", link_object, obj_ext); - const full_path_object = try os.path.join( + const full_path_object = try fs.path.join( allocator, [][]const u8{ tmp_dir_name, name_with_ext }, ); @@ -1076,7 +1078,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var } } if (expected_outcome == .BuildFail) { - const result = try os.ChildProcess.exec( + const result = try ChildProcess.exec( allocator, build_args.toSliceConst(), null, @@ -1084,7 +1086,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var max_doc_file_size, ); switch (result.term) { - os.ChildProcess.Term.Exited => |exit_code| { + .Exited => |exit_code| { if (exit_code == 0) { warn("{}\nThe following command incorrectly succeeded:\n", result.stderr); for (build_args.toSliceConst()) |arg| @@ -1113,7 +1115,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var if (code.target_str) |triple| { if (mem.startsWith(u8, triple, "wasm32") or mem.startsWith(u8, triple, "x86_64-linux") and - (builtin.os != builtin.Os.linux or builtin.arch != builtin.Arch.x86_64)) + (builtin.os != .linux or builtin.arch != .x86_64)) { // skip execution try out.print("\n"); @@ -1124,9 +1126,9 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var const run_args = [][]const u8{tmp_bin_file_name}; const result = if (expected_outcome == ExpectedOutcome.Fail) blk: { - const result = try os.ChildProcess.exec(allocator, run_args, null, &env_map, max_doc_file_size); + const result = try ChildProcess.exec(allocator, run_args, null, &env_map, max_doc_file_size); switch (result.term) { - os.ChildProcess.Term.Exited => |exit_code| { + .Exited => |exit_code| { if (exit_code == 0) { warn("{}\nThe following command incorrectly succeeded:\n", result.stderr); for (run_args) |arg| @@ -1216,9 +1218,9 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var try out.print(" --release-small"); }, } - const result = try os.ChildProcess.exec(allocator, test_args.toSliceConst(), null, &env_map, max_doc_file_size); + const result = try ChildProcess.exec(allocator, test_args.toSliceConst(), null, &env_map, max_doc_file_size); switch (result.term) { - os.ChildProcess.Term.Exited => |exit_code| { + .Exited => |exit_code| { if (exit_code == 0) { warn("{}\nThe following command incorrectly succeeded:\n", result.stderr); for (test_args.toSliceConst()) |arg| @@ -1274,9 +1276,9 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var }, } - const result = try os.ChildProcess.exec(allocator, test_args.toSliceConst(), null, &env_map, max_doc_file_size); + const result = try ChildProcess.exec(allocator, test_args.toSliceConst(), null, &env_map, max_doc_file_size); switch (result.term) { - os.ChildProcess.Term.Exited => |exit_code| { + .Exited => |exit_code| { if (exit_code == 0) { warn("{}\nThe following command incorrectly succeeded:\n", result.stderr); for (test_args.toSliceConst()) |arg| @@ -1310,7 +1312,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var }, Code.Id.Obj => |maybe_error_match| { const name_plus_obj_ext = try std.fmt.allocPrint(allocator, "{}{}", code.name, obj_ext); - const tmp_obj_file_name = try os.path.join( + const tmp_obj_file_name = try fs.path.join( allocator, [][]const u8{ tmp_dir_name, name_plus_obj_ext }, ); @@ -1318,7 +1320,7 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var defer build_args.deinit(); const name_plus_h_ext = try std.fmt.allocPrint(allocator, "{}.h", code.name); - const output_h_file_name = try os.path.join( + const output_h_file_name = try fs.path.join( allocator, [][]const u8{ tmp_dir_name, name_plus_h_ext }, ); @@ -1367,9 +1369,9 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var } if (maybe_error_match) |error_match| { - const result = try os.ChildProcess.exec(allocator, build_args.toSliceConst(), null, &env_map, max_doc_file_size); + const result = try ChildProcess.exec(allocator, build_args.toSliceConst(), null, &env_map, max_doc_file_size); switch (result.term) { - os.ChildProcess.Term.Exited => |exit_code| { + .Exited => |exit_code| { if (exit_code == 0) { warn("{}\nThe following command incorrectly succeeded:\n", result.stderr); for (build_args.toSliceConst()) |arg| @@ -1448,10 +1450,10 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var } } -fn exec(allocator: *mem.Allocator, env_map: *std.BufMap, args: []const []const u8) !os.ChildProcess.ExecResult { - const result = try os.ChildProcess.exec(allocator, args, null, env_map, max_doc_file_size); +fn exec(allocator: *mem.Allocator, env_map: *std.BufMap, args: []const []const u8) !ChildProcess.ExecResult { + const result = try ChildProcess.exec(allocator, args, null, env_map, max_doc_file_size); switch (result.term) { - os.ChildProcess.Term.Exited => |exit_code| { + .Exited => |exit_code| { if (exit_code != 0) { warn("{}\nThe following command exited with code {}:\n", result.stderr, exit_code); for (args) |arg| diff --git a/doc/langref.html.in b/doc/langref.html.in index 8310f2e2ab..5b9beba644 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -195,7 +195,7 @@ const std = @import("std"); pub fn main() !void { // If this program is run without stdout attached, exit with an error. - const stdout_file = try std.os.File.stdout(); + const stdout_file = try std.io.getStdOut(); // If this program encounters pipe failure when printing to stdout, exit // with an error. try stdout_file.write("Hello, world!\n"); diff --git a/src-self-hosted/compilation.zig b/src-self-hosted/compilation.zig index 7a30bbad98..720c26945d 100644 --- a/src-self-hosted/compilation.zig +++ b/src-self-hosted/compilation.zig @@ -1,5 +1,4 @@ const std = @import("std"); -const os = std.os; const io = std.io; const mem = std.mem; const Allocator = mem.Allocator; @@ -54,7 +53,7 @@ pub const ZigCompiler = struct { }; var seed_bytes: [@sizeOf(u64)]u8 = undefined; - try std.os.getRandomBytes(seed_bytes[0..]); + try std.crypto.randomBytes(seed_bytes[0..]); const seed = mem.readIntNative(u64, &seed_bytes); return ZigCompiler{ @@ -487,7 +486,7 @@ pub const Compilation = struct { comp.name = try Buffer.init(comp.arena(), name); comp.llvm_triple = try target.getTriple(comp.arena()); comp.llvm_target = try Target.llvmTargetFromTriple(comp.llvm_triple); - comp.zig_std_dir = try std.os.path.join(comp.arena(), [][]const u8{ zig_lib_dir, "std" }); + comp.zig_std_dir = try std.fs.path.join(comp.arena(), [][]const u8{ zig_lib_dir, "std" }); const opt_level = switch (build_mode) { builtin.Mode.Debug => llvm.CodeGenLevelNone, @@ -529,8 +528,8 @@ pub const Compilation = struct { defer comp.events.destroy(); if (root_src_path) |root_src| { - const dirname = std.os.path.dirname(root_src) orelse "."; - const basename = std.os.path.basename(root_src); + const dirname = std.fs.path.dirname(root_src) orelse "."; + const basename = std.fs.path.basename(root_src); comp.root_package = try Package.create(comp.arena(), dirname, basename); comp.std_package = try Package.create(comp.arena(), comp.zig_std_dir, "std.zig"); @@ -558,7 +557,7 @@ pub const Compilation = struct { if (comp.tmp_dir.getOrNull()) |tmp_dir_result| if (tmp_dir_result.*) |tmp_dir| { // TODO evented I/O? - os.deleteTree(comp.arena(), tmp_dir) catch {}; + std.fs.deleteTree(comp.arena(), tmp_dir) catch {}; } else |_| {}; } @@ -970,8 +969,8 @@ pub const Compilation = struct { async fn initialCompile(self: *Compilation) !void { if (self.root_src_path) |root_src_path| { const root_scope = blk: { - // TODO async/await os.path.real - const root_src_real_path = os.path.realAlloc(self.gpa(), root_src_path) catch |err| { + // TODO async/await std.fs.realpath + const root_src_real_path = std.fs.realpathAlloc(self.gpa(), root_src_path) catch |err| { try self.addCompileErrorCli(root_src_path, "unable to open: {}", @errorName(err)); return; }; @@ -1196,7 +1195,7 @@ pub const Compilation = struct { const file_name = try std.fmt.allocPrint(self.gpa(), "{}{}", file_prefix[0..], suffix); defer self.gpa().free(file_name); - const full_path = try os.path.join(self.gpa(), [][]const u8{ tmp_dir, file_name[0..] }); + const full_path = try std.fs.path.join(self.gpa(), [][]const u8{ tmp_dir, file_name[0..] }); errdefer self.gpa().free(full_path); return Buffer.fromOwnedSlice(self.gpa(), full_path); @@ -1217,8 +1216,8 @@ pub const Compilation = struct { const zig_dir_path = try getZigDir(self.gpa()); defer self.gpa().free(zig_dir_path); - const tmp_dir = try os.path.join(self.arena(), [][]const u8{ zig_dir_path, comp_dir_name[0..] }); - try os.makePath(self.gpa(), tmp_dir); + const tmp_dir = try std.fs.path.join(self.arena(), [][]const u8{ zig_dir_path, comp_dir_name[0..] }); + try std.fs.makePath(self.gpa(), tmp_dir); return tmp_dir; } @@ -1384,7 +1383,7 @@ async fn addFnToLinkSet(comp: *Compilation, fn_val: *Value.Fn) void { } fn getZigDir(allocator: *mem.Allocator) ![]u8 { - return os.getAppDataDir(allocator, "zig"); + return std.fs.getAppDataDir(allocator, "zig"); } async fn analyzeFnType( diff --git a/src-self-hosted/errmsg.zig b/src-self-hosted/errmsg.zig index fc49fad410..eef5817d58 100644 --- a/src-self-hosted/errmsg.zig +++ b/src-self-hosted/errmsg.zig @@ -1,6 +1,7 @@ const std = @import("std"); const mem = std.mem; -const os = std.os; +const fs = std.fs; +const process = std.process; const Token = std.zig.Token; const ast = std.zig.ast; const TokenIndex = std.zig.ast.TokenIndex; @@ -239,10 +240,10 @@ pub const Msg = struct { const allocator = msg.getAllocator(); const tree = msg.getTree(); - const cwd = try os.getCwdAlloc(allocator); + const cwd = try process.getCwdAlloc(allocator); defer allocator.free(cwd); - const relpath = try os.path.relative(allocator, cwd, msg.realpath); + const relpath = try fs.path.relative(allocator, cwd, msg.realpath); defer allocator.free(relpath); const path = if (relpath.len < msg.realpath.len) relpath else msg.realpath; @@ -276,7 +277,7 @@ pub const Msg = struct { try stream.write("\n"); } - pub fn printToFile(msg: *const Msg, file: os.File, color: Color) !void { + pub fn printToFile(msg: *const Msg, file: fs.File, color: Color) !void { const color_on = switch (color) { Color.Auto => file.isTty(), Color.On => true, diff --git a/src-self-hosted/introspect.zig b/src-self-hosted/introspect.zig index 8f859a82ce..538232e620 100644 --- a/src-self-hosted/introspect.zig +++ b/src-self-hosted/introspect.zig @@ -2,19 +2,19 @@ const std = @import("std"); const mem = std.mem; -const os = std.os; +const fs = std.fs; const warn = std.debug.warn; /// Caller must free result pub fn testZigInstallPrefix(allocator: *mem.Allocator, test_path: []const u8) ![]u8 { - const test_zig_dir = try os.path.join(allocator, [][]const u8{ test_path, "lib", "zig" }); + const test_zig_dir = try fs.path.join(allocator, [][]const u8{ test_path, "lib", "zig" }); errdefer allocator.free(test_zig_dir); - const test_index_file = try os.path.join(allocator, [][]const u8{ test_zig_dir, "std", "std.zig" }); + const test_index_file = try fs.path.join(allocator, [][]const u8{ test_zig_dir, "std", "std.zig" }); defer allocator.free(test_index_file); - var file = try os.File.openRead(test_index_file); + var file = try fs.File.openRead(test_index_file); file.close(); return test_zig_dir; @@ -22,12 +22,12 @@ pub fn testZigInstallPrefix(allocator: *mem.Allocator, test_path: []const u8) ![ /// Caller must free result pub fn findZigLibDir(allocator: *mem.Allocator) ![]u8 { - const self_exe_path = try os.selfExeDirPathAlloc(allocator); + const self_exe_path = try fs.selfExeDirPathAlloc(allocator); defer allocator.free(self_exe_path); var cur_path: []const u8 = self_exe_path; while (true) { - const test_dir = os.path.dirname(cur_path) orelse "."; + const test_dir = fs.path.dirname(cur_path) orelse "."; if (mem.eql(u8, test_dir, cur_path)) { break; diff --git a/src-self-hosted/libc_installation.zig b/src-self-hosted/libc_installation.zig index 6a530da1f0..53790ec8f4 100644 --- a/src-self-hosted/libc_installation.zig +++ b/src-self-hosted/libc_installation.zig @@ -3,6 +3,7 @@ const builtin = @import("builtin"); const event = std.event; const Target = @import("target.zig").Target; const c = @import("c.zig"); +const fs = std.fs; /// See the render function implementation for documentation of the fields. pub const LibCInstallation = struct { @@ -30,7 +31,7 @@ pub const LibCInstallation = struct { self: *LibCInstallation, allocator: *std.mem.Allocator, libc_file: []const u8, - stderr: *std.io.OutStream(std.os.File.WriteError), + stderr: *std.io.OutStream(fs.File.WriteError), ) !void { self.initEmpty(); @@ -100,7 +101,7 @@ pub const LibCInstallation = struct { } } - pub fn render(self: *const LibCInstallation, out: *std.io.OutStream(std.os.File.WriteError)) !void { + pub fn render(self: *const LibCInstallation, out: *std.io.OutStream(fs.File.WriteError)) !void { @setEvalBranchQuota(4000); try out.print( \\# The directory that contains `stdlib.h`. @@ -148,7 +149,7 @@ pub const LibCInstallation = struct { errdefer if (windows_sdk) |sdk| c.zig_free_windows_sdk(@ptrCast(?[*]c.ZigWindowsSDK, sdk)); switch (builtin.os) { - builtin.Os.windows => { + .windows => { var sdk: *c.ZigWindowsSDK = undefined; switch (c.zig_find_windows_sdk(@ptrCast(?[*]?[*]c.ZigWindowsSDK, &sdk))) { c.ZigFindWindowsSdkError.None => { @@ -166,13 +167,13 @@ pub const LibCInstallation = struct { c.ZigFindWindowsSdkError.PathTooLong => return error.NotFound, } }, - builtin.Os.linux => { + .linux => { try group.call(findNativeIncludeDirLinux, self, loop); try group.call(findNativeLibDirLinux, self, loop); try group.call(findNativeStaticLibDir, self, loop); try group.call(findNativeDynamicLinker, self, loop); }, - builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => { + .macosx, .freebsd, .netbsd => { self.include_dir = try std.mem.dupe(loop.allocator, u8, "/usr/include"); }, else => @compileError("unimplemented: find libc for this OS"), @@ -181,7 +182,7 @@ pub const LibCInstallation = struct { } async fn findNativeIncludeDirLinux(self: *LibCInstallation, loop: *event.Loop) !void { - const cc_exe = std.os.getEnvPosix("CC") orelse "cc"; + const cc_exe = std.process.getEnvPosix("CC") orelse "cc"; const argv = []const []const u8{ cc_exe, "-E", @@ -190,7 +191,7 @@ pub const LibCInstallation = struct { "/dev/null", }; // TODO make this use event loop - const errorable_result = std.os.ChildProcess.exec(loop.allocator, argv, null, null, 1024 * 1024); + const errorable_result = std.ChildProcess.exec(loop.allocator, argv, null, null, 1024 * 1024); const exec_result = if (std.debug.runtime_safety) blk: { break :blk errorable_result catch unreachable; } else blk: { @@ -205,7 +206,7 @@ pub const LibCInstallation = struct { } switch (exec_result.term) { - std.os.ChildProcess.Term.Exited => |code| { + std.ChildProcess.Term.Exited => |code| { if (code != 0) return error.CCompilerExitCode; }, else => { @@ -230,7 +231,7 @@ pub const LibCInstallation = struct { while (path_i < search_paths.len) : (path_i += 1) { const search_path_untrimmed = search_paths.at(search_paths.len - path_i - 1); const search_path = std.mem.trimLeft(u8, search_path_untrimmed, " "); - const stdlib_path = try std.os.path.join(loop.allocator, [][]const u8{ search_path, "stdlib.h" }); + const stdlib_path = try fs.path.join(loop.allocator, [][]const u8{ search_path, "stdlib.h" }); defer loop.allocator.free(stdlib_path); if (try fileExists(stdlib_path)) { @@ -254,7 +255,7 @@ pub const LibCInstallation = struct { const stream = &std.io.BufferOutStream.init(&result_buf).stream; try stream.print("{}\\Include\\{}\\ucrt", search.path, search.version); - const stdlib_path = try std.os.path.join( + const stdlib_path = try fs.path.join( loop.allocator, [][]const u8{ result_buf.toSliceConst(), "stdlib.h" }, ); @@ -286,7 +287,7 @@ pub const LibCInstallation = struct { builtin.Arch.aarch64 => try stream.write("arm"), else => return error.UnsupportedArchitecture, } - const ucrt_lib_path = try std.os.path.join( + const ucrt_lib_path = try fs.path.join( loop.allocator, [][]const u8{ result_buf.toSliceConst(), "ucrt.lib" }, ); @@ -364,7 +365,7 @@ pub const LibCInstallation = struct { builtin.Arch.aarch64 => try stream.write("arm\\"), else => return error.UnsupportedArchitecture, } - const kernel32_path = try std.os.path.join( + const kernel32_path = try fs.path.join( loop.allocator, [][]const u8{ result_buf.toSliceConst(), "kernel32.lib" }, ); @@ -391,14 +392,14 @@ pub const LibCInstallation = struct { /// caller owns returned memory async fn ccPrintFileName(loop: *event.Loop, o_file: []const u8, want_dirname: bool) ![]u8 { - const cc_exe = std.os.getEnvPosix("CC") orelse "cc"; + const cc_exe = std.process.getEnvPosix("CC") orelse "cc"; const arg1 = try std.fmt.allocPrint(loop.allocator, "-print-file-name={}", o_file); defer loop.allocator.free(arg1); const argv = []const []const u8{ cc_exe, arg1 }; // TODO This simulates evented I/O for the child process exec await (async loop.yield() catch unreachable); - const errorable_result = std.os.ChildProcess.exec(loop.allocator, argv, null, null, 1024 * 1024); + const errorable_result = std.ChildProcess.exec(loop.allocator, argv, null, null, 1024 * 1024); const exec_result = if (std.debug.runtime_safety) blk: { break :blk errorable_result catch unreachable; } else blk: { @@ -412,7 +413,7 @@ async fn ccPrintFileName(loop: *event.Loop, o_file: []const u8, want_dirname: bo loop.allocator.free(exec_result.stderr); } switch (exec_result.term) { - std.os.ChildProcess.Term.Exited => |code| { + .Exited => |code| { if (code != 0) return error.CCompilerExitCode; }, else => { @@ -421,7 +422,7 @@ async fn ccPrintFileName(loop: *event.Loop, o_file: []const u8, want_dirname: bo } var it = std.mem.tokenize(exec_result.stdout, "\n\r"); const line = it.next() orelse return error.LibCRuntimeNotFound; - const dirname = std.os.path.dirname(line) orelse return error.LibCRuntimeNotFound; + const dirname = fs.path.dirname(line) orelse return error.LibCRuntimeNotFound; if (want_dirname) { return std.mem.dupe(loop.allocator, u8, dirname); @@ -459,7 +460,7 @@ fn fillSearch(search_buf: *[2]Search, sdk: *c.ZigWindowsSDK) []Search { } fn fileExists(path: []const u8) !bool { - if (std.os.File.access(path)) |_| { + if (fs.File.access(path)) |_| { return true; } else |err| switch (err) { error.FileNotFound, error.PermissionDenied => return false, diff --git a/src-self-hosted/link.zig b/src-self-hosted/link.zig index 5689ee7925..a47dbbbe92 100644 --- a/src-self-hosted/link.zig +++ b/src-self-hosted/link.zig @@ -302,7 +302,7 @@ fn constructLinkerArgsElf(ctx: *Context) !void { try ctx.args.append(c"--allow-shlib-undefined"); } - if (ctx.comp.target.getOs() == builtin.Os.zen) { + if (ctx.comp.target.getOs() == .zen) { try ctx.args.append(c"-e"); try ctx.args.append(c"_start"); @@ -311,7 +311,7 @@ fn constructLinkerArgsElf(ctx: *Context) !void { } fn addPathJoin(ctx: *Context, dirname: []const u8, basename: []const u8) !void { - const full_path = try std.os.path.join(&ctx.arena.allocator, [][]const u8{ dirname, basename }); + const full_path = try std.fs.path.join(&ctx.arena.allocator, [][]const u8{ dirname, basename }); const full_path_with_null = try std.cstr.addNullByte(&ctx.arena.allocator, full_path); try ctx.args.append(full_path_with_null.ptr); } @@ -668,7 +668,7 @@ const DarwinPlatform = struct { break :blk ver; }, Compilation.DarwinVersionMin.None => blk: { - assert(comp.target.getOs() == builtin.Os.macosx); + assert(comp.target.getOs() == .macosx); result.kind = Kind.MacOS; break :blk "10.10"; }, diff --git a/src-self-hosted/main.zig b/src-self-hosted/main.zig index cbbf73f3f5..1d2ac4917b 100644 --- a/src-self-hosted/main.zig +++ b/src-self-hosted/main.zig @@ -4,7 +4,9 @@ const builtin = @import("builtin"); const event = std.event; const os = std.os; const io = std.io; +const fs = std.fs; const mem = std.mem; +const process = std.process; const Allocator = mem.Allocator; const ArrayList = std.ArrayList; const Buffer = std.Buffer; @@ -20,9 +22,9 @@ const Target = @import("target.zig").Target; const errmsg = @import("errmsg.zig"); const LibCInstallation = @import("libc_installation.zig").LibCInstallation; -var stderr_file: os.File = undefined; -var stderr: *io.OutStream(os.File.WriteError) = undefined; -var stdout: *io.OutStream(os.File.WriteError) = undefined; +var stderr_file: fs.File = undefined; +var stderr: *io.OutStream(fs.File.WriteError) = undefined; +var stdout: *io.OutStream(fs.File.WriteError) = undefined; pub const max_src_size = 2 * 1024 * 1024 * 1024; // 2 GiB @@ -62,14 +64,14 @@ pub fn main() !void { var stderr_out_stream = stderr_file.outStream(); stderr = &stderr_out_stream.stream; - const args = try os.argsAlloc(allocator); + const args = try process.argsAlloc(allocator); // TODO I'm getting unreachable code here, which shouldn't happen - //defer os.argsFree(allocator, args); + //defer process.argsFree(allocator, args); if (args.len <= 1) { try stderr.write("expected command argument\n\n"); try stderr.write(usage); - os.exit(1); + process.exit(1); } const commands = []Command{ @@ -125,7 +127,7 @@ pub fn main() !void { try stderr.print("unknown command: {}\n\n", args[1]); try stderr.write(usage); - os.exit(1); + process.exit(1); } const usage_build_generic = @@ -256,7 +258,7 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co if (flags.present("help")) { try stdout.write(usage_build_generic); - os.exit(0); + process.exit(0); } const build_mode = blk: { @@ -324,14 +326,14 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co cur_pkg = parent; } else { try stderr.print("encountered --pkg-end with no matching --pkg-begin\n"); - os.exit(1); + process.exit(1); } } } if (cur_pkg.parent != null) { try stderr.print("unmatched --pkg-begin\n"); - os.exit(1); + process.exit(1); } const provided_name = flags.single("name"); @@ -340,18 +342,18 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co 1 => flags.positionals.at(0), else => { try stderr.print("unexpected extra parameter: {}\n", flags.positionals.at(1)); - os.exit(1); + process.exit(1); }, }; const root_name = if (provided_name) |n| n else blk: { if (root_source_file) |file| { - const basename = os.path.basename(file); + const basename = fs.path.basename(file); var it = mem.separate(basename, "."); break :blk it.next() orelse basename; } else { try stderr.write("--name [name] not provided and unable to infer\n"); - os.exit(1); + process.exit(1); } }; @@ -361,12 +363,12 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co const link_objects = flags.many("object"); if (root_source_file == null and link_objects.len == 0 and assembly_files.len == 0) { try stderr.write("Expected source file argument or at least one --object or --assembly argument\n"); - os.exit(1); + process.exit(1); } if (out_type == Compilation.Kind.Obj and link_objects.len != 0) { try stderr.write("When building an object file, --object arguments are invalid\n"); - os.exit(1); + process.exit(1); } var clang_argv_buf = ArrayList([]const u8).init(allocator); @@ -379,7 +381,7 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co } try ZigCompiler.setLlvmArgv(allocator, mllvm_flags); - const zig_lib_dir = introspect.resolveZigLibDir(allocator) catch os.exit(1); + const zig_lib_dir = introspect.resolveZigLibDir(allocator) catch process.exit(1); defer allocator.free(zig_lib_dir); var override_libc: LibCInstallation = undefined; @@ -448,7 +450,7 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co if (flags.single("mmacosx-version-min") != null and flags.single("mios-version-min") != null) { try stderr.write("-mmacosx-version-min and -mios-version-min options not allowed together\n"); - os.exit(1); + process.exit(1); } if (flags.single("mmacosx-version-min")) |ver| { @@ -478,16 +480,16 @@ async fn processBuildEvents(comp: *Compilation, color: errmsg.Color) void { switch (build_event) { Compilation.Event.Ok => { - stderr.print("Build {} succeeded\n", count) catch os.exit(1); + stderr.print("Build {} succeeded\n", count) catch process.exit(1); }, Compilation.Event.Error => |err| { - stderr.print("Build {} failed: {}\n", count, @errorName(err)) catch os.exit(1); + stderr.print("Build {} failed: {}\n", count, @errorName(err)) catch process.exit(1); }, Compilation.Event.Fail => |msgs| { - stderr.print("Build {} compile errors:\n", count) catch os.exit(1); + stderr.print("Build {} compile errors:\n", count) catch process.exit(1); for (msgs) |msg| { defer msg.destroy(); - msg.printToFile(stderr_file, color) catch os.exit(1); + msg.printToFile(stderr_file, color) catch process.exit(1); } }, } @@ -550,8 +552,8 @@ fn parseLibcPaths(allocator: *Allocator, libc: *LibCInstallation, libc_paths_fil "Try running `zig libc` to see an example for the native target.\n", libc_paths_file, @errorName(err), - ) catch os.exit(1); - os.exit(1); + ) catch process.exit(1); + process.exit(1); }; } @@ -565,7 +567,7 @@ fn cmdLibC(allocator: *Allocator, args: []const []const u8) !void { }, else => { try stderr.print("unexpected extra parameter: {}\n", args[1]); - os.exit(1); + process.exit(1); }, } @@ -584,10 +586,10 @@ fn cmdLibC(allocator: *Allocator, args: []const []const u8) !void { async fn findLibCAsync(zig_compiler: *ZigCompiler) void { const libc = (await (async zig_compiler.getNativeLibC() catch unreachable)) catch |err| { - stderr.print("unable to find libc: {}\n", @errorName(err)) catch os.exit(1); - os.exit(1); + stderr.print("unable to find libc: {}\n", @errorName(err)) catch process.exit(1); + process.exit(1); }; - libc.render(stdout) catch os.exit(1); + libc.render(stdout) catch process.exit(1); } fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void { @@ -596,7 +598,7 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void { if (flags.present("help")) { try stdout.write(usage_fmt); - os.exit(0); + process.exit(0); } const color = blk: { @@ -616,7 +618,7 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void { if (flags.present("stdin")) { if (flags.positionals.len != 0) { try stderr.write("cannot use --stdin with positional arguments\n"); - os.exit(1); + process.exit(1); } var stdin_file = try io.getStdIn(); @@ -627,7 +629,7 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void { const tree = std.zig.parse(allocator, source_code) catch |err| { try stderr.print("error parsing stdin: {}\n", err); - os.exit(1); + process.exit(1); }; defer tree.deinit(); @@ -639,12 +641,12 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void { try msg.printToFile(stderr_file, color); } if (tree.errors.len != 0) { - os.exit(1); + process.exit(1); } if (flags.present("check")) { const anything_changed = try std.zig.render(allocator, io.null_out_stream, tree); const code = if (anything_changed) u8(1) else u8(0); - os.exit(code); + process.exit(code); } _ = try std.zig.render(allocator, stdout, tree); @@ -653,7 +655,7 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void { if (flags.positionals.len == 0) { try stderr.write("expected at least one source file argument\n"); - os.exit(1); + process.exit(1); } var loop: event.Loop = undefined; @@ -700,7 +702,7 @@ const FmtError = error{ ReadOnlyFileSystem, LinkQuotaExceeded, FileBusy, -} || os.File.OpenError; +} || fs.File.OpenError; async fn asyncFmtMain( loop: *event.Loop, @@ -725,7 +727,7 @@ async fn asyncFmtMain( } try await (async group.wait() catch unreachable); if (fmt.any_error) { - os.exit(1); + process.exit(1); } } @@ -747,13 +749,13 @@ async fn fmtPath(fmt: *Fmt, file_path_ref: []const u8, check_mode: bool) FmtErro )) catch |err| switch (err) { error.IsDir, error.AccessDenied => { // TODO make event based (and dir.next()) - var dir = try std.os.Dir.open(fmt.loop.allocator, file_path); + var dir = try fs.Dir.open(fmt.loop.allocator, file_path); defer dir.close(); var group = event.Group(FmtError!void).init(fmt.loop); while (try dir.next()) |entry| { - if (entry.kind == std.os.Dir.Entry.Kind.Directory or mem.endsWith(u8, entry.name, ".zig")) { - const full_path = try os.path.join(fmt.loop.allocator, [][]const u8{ file_path, entry.name }); + if (entry.kind == fs.Dir.Entry.Kind.Directory or mem.endsWith(u8, entry.name, ".zig")) { + const full_path = try fs.path.join(fmt.loop.allocator, [][]const u8{ file_path, entry.name }); try group.call(fmtPath, fmt, full_path, check_mode); } } @@ -891,7 +893,7 @@ const usage_internal = fn cmdInternal(allocator: *Allocator, args: []const []const u8) !void { if (args.len == 0) { try stderr.write(usage_internal); - os.exit(1); + process.exit(1); } const sub_commands = []Command{Command{ diff --git a/src-self-hosted/stage1.zig b/src-self-hosted/stage1.zig index 7a44e8f3a0..dd1c137c74 100644 --- a/src-self-hosted/stage1.zig +++ b/src-self-hosted/stage1.zig @@ -1,8 +1,24 @@ // This is Zig code that is used by both stage1 and stage2. // The prototypes in src/userland.h must match these definitions. -const std = @import("std"); const builtin = @import("builtin"); +const std = @import("std"); +const io = std.io; +const mem = std.mem; +const fs = std.fs; +const process = std.process; +const Allocator = mem.Allocator; +const ArrayList = std.ArrayList; +const Buffer = std.Buffer; +const arg = @import("arg.zig"); +const self_hosted_main = @import("main.zig"); +const Args = arg.Args; +const Flag = arg.Flag; +const errmsg = @import("errmsg.zig"); + +var stderr_file: fs.File = undefined; +var stderr: *io.OutStream(fs.File.WriteError) = undefined; +var stdout: *io.OutStream(fs.File.WriteError) = undefined; // ABI warning export fn stage2_zen(ptr: *[*]const u8, len: *usize) void { @@ -157,7 +173,7 @@ fn fmtMain(argc: c_int, argv: [*]const [*]const u8) !void { if (flags.present("help")) { try stdout.write(self_hosted_main.usage_fmt); - os.exit(0); + process.exit(0); } const color = blk: { @@ -177,7 +193,7 @@ fn fmtMain(argc: c_int, argv: [*]const [*]const u8) !void { if (flags.present("stdin")) { if (flags.positionals.len != 0) { try stderr.write("cannot use --stdin with positional arguments\n"); - os.exit(1); + process.exit(1); } var stdin_file = try io.getStdIn(); @@ -188,7 +204,7 @@ fn fmtMain(argc: c_int, argv: [*]const [*]const u8) !void { const tree = std.zig.parse(allocator, source_code) catch |err| { try stderr.print("error parsing stdin: {}\n", err); - os.exit(1); + process.exit(1); }; defer tree.deinit(); @@ -197,12 +213,12 @@ fn fmtMain(argc: c_int, argv: [*]const [*]const u8) !void { try printErrMsgToFile(allocator, parse_error, tree, "", stderr_file, color); } if (tree.errors.len != 0) { - os.exit(1); + process.exit(1); } if (flags.present("check")) { const anything_changed = try std.zig.render(allocator, io.null_out_stream, tree); const code = if (anything_changed) u8(1) else u8(0); - os.exit(code); + process.exit(code); } _ = try std.zig.render(allocator, stdout, tree); @@ -211,7 +227,7 @@ fn fmtMain(argc: c_int, argv: [*]const [*]const u8) !void { if (flags.positionals.len == 0) { try stderr.write("expected at least one source file argument\n"); - os.exit(1); + process.exit(1); } var fmt = Fmt{ @@ -227,7 +243,7 @@ fn fmtMain(argc: c_int, argv: [*]const [*]const u8) !void { try fmtPath(&fmt, file_path, check_mode); } if (fmt.any_error) { - os.exit(1); + process.exit(1); } } @@ -250,7 +266,7 @@ const FmtError = error{ ReadOnlyFileSystem, LinkQuotaExceeded, FileBusy, -} || os.File.OpenError; +} || fs.File.OpenError; fn fmtPath(fmt: *Fmt, file_path_ref: []const u8, check_mode: bool) FmtError!void { const file_path = try std.mem.dupe(fmt.allocator, u8, file_path_ref); @@ -261,12 +277,12 @@ fn fmtPath(fmt: *Fmt, file_path_ref: []const u8, check_mode: bool) FmtError!void const source_code = io.readFileAlloc(fmt.allocator, file_path) catch |err| switch (err) { error.IsDir, error.AccessDenied => { // TODO make event based (and dir.next()) - var dir = try std.os.Dir.open(fmt.allocator, file_path); + var dir = try fs.Dir.open(fmt.allocator, file_path); defer dir.close(); while (try dir.next()) |entry| { - if (entry.kind == std.os.Dir.Entry.Kind.Directory or mem.endsWith(u8, entry.name, ".zig")) { - const full_path = try os.path.join(fmt.allocator, [][]const u8{ file_path, entry.name }); + if (entry.kind == fs.Dir.Entry.Kind.Directory or mem.endsWith(u8, entry.name, ".zig")) { + const full_path = try fs.path.join(fmt.allocator, [][]const u8{ file_path, entry.name }); try fmtPath(fmt, full_path, check_mode); } } @@ -329,7 +345,7 @@ fn printErrMsgToFile( parse_error: *const ast.Error, tree: *ast.Tree, path: []const u8, - file: os.File, + file: fs.File, color: errmsg.Color, ) !void { const color_on = switch (color) { @@ -377,20 +393,3 @@ fn printErrMsgToFile( try stream.writeByteNTimes('~', last_token.end - first_token.start); try stream.write("\n"); } - -const os = std.os; -const io = std.io; -const mem = std.mem; -const Allocator = mem.Allocator; -const ArrayList = std.ArrayList; -const Buffer = std.Buffer; - -const arg = @import("arg.zig"); -const self_hosted_main = @import("main.zig"); -const Args = arg.Args; -const Flag = arg.Flag; -const errmsg = @import("errmsg.zig"); - -var stderr_file: os.File = undefined; -var stderr: *io.OutStream(os.File.WriteError) = undefined; -var stdout: *io.OutStream(os.File.WriteError) = undefined; diff --git a/src-self-hosted/test.zig b/src-self-hosted/test.zig index 4be6d53932..526518ca47 100644 --- a/src-self-hosted/test.zig +++ b/src-self-hosted/test.zig @@ -55,12 +55,12 @@ pub const TestContext = struct { self.zig_lib_dir = try introspect.resolveZigLibDir(allocator); errdefer allocator.free(self.zig_lib_dir); - try std.os.makePath(allocator, tmp_dir_name); - errdefer std.os.deleteTree(allocator, tmp_dir_name) catch {}; + try std.fs.makePath(allocator, tmp_dir_name); + errdefer std.fs.deleteTree(allocator, tmp_dir_name) catch {}; } fn deinit(self: *TestContext) void { - std.os.deleteTree(allocator, tmp_dir_name) catch {}; + std.fs.deleteTree(allocator, tmp_dir_name) catch {}; allocator.free(self.zig_lib_dir); self.zig_compiler.deinit(); self.loop.deinit(); @@ -87,10 +87,10 @@ pub const TestContext = struct { ) !void { var file_index_buf: [20]u8 = undefined; const file_index = try std.fmt.bufPrint(file_index_buf[0..], "{}", self.file_index.incr()); - const file1_path = try std.os.path.join(allocator, [][]const u8{ tmp_dir_name, file_index, file1 }); + const file1_path = try std.fs.path.join(allocator, [][]const u8{ tmp_dir_name, file_index, file1 }); - if (std.os.path.dirname(file1_path)) |dirname| { - try std.os.makePath(allocator, dirname); + if (std.fs.path.dirname(file1_path)) |dirname| { + try std.fs.makePath(allocator, dirname); } // TODO async I/O @@ -120,11 +120,11 @@ pub const TestContext = struct { ) !void { var file_index_buf: [20]u8 = undefined; const file_index = try std.fmt.bufPrint(file_index_buf[0..], "{}", self.file_index.incr()); - const file1_path = try std.os.path.join(allocator, [][]const u8{ tmp_dir_name, file_index, file1 }); + const file1_path = try std.fs.path.join(allocator, [][]const u8{ tmp_dir_name, file_index, file1 }); const output_file = try std.fmt.allocPrint(allocator, "{}-out{}", file1_path, Target(Target.Native).exeFileExt()); - if (std.os.path.dirname(file1_path)) |dirname| { - try std.os.makePath(allocator, dirname); + if (std.fs.path.dirname(file1_path)) |dirname| { + try std.fs.makePath(allocator, dirname); } // TODO async I/O @@ -164,9 +164,9 @@ pub const TestContext = struct { Compilation.Event.Ok => { const argv = []const []const u8{exe_file_2}; // TODO use event loop - const child = try std.os.ChildProcess.exec(allocator, argv, null, null, 1024 * 1024); + const child = try std.ChildProcess.exec(allocator, argv, null, null, 1024 * 1024); switch (child.term) { - std.os.ChildProcess.Term.Exited => |code| { + .Exited => |code| { if (code != 0) { return error.BadReturnCode; } diff --git a/std/buffer.zig b/std/buffer.zig index 32228af558..bc6aa254da 100644 --- a/std/buffer.zig +++ b/std/buffer.zig @@ -139,15 +139,13 @@ pub const Buffer = struct { }; test "simple Buffer" { - const cstr = @import("cstr.zig"); - var buf = try Buffer.init(debug.global_allocator, ""); testing.expect(buf.len() == 0); try buf.append("hello"); try buf.append(" "); try buf.append("world"); testing.expect(buf.eql("hello world")); - testing.expect(mem.eql(u8, cstr.toSliceConst(buf.toSliceConst().ptr), buf.toSliceConst())); + testing.expect(mem.eql(u8, mem.toSliceConst(u8, buf.toSliceConst().ptr), buf.toSliceConst())); var buf2 = try Buffer.initFromBuffer(buf); testing.expect(buf.eql(buf2.toSliceConst())); diff --git a/std/build.zig b/std/build.zig index 0f2d6093c4..115cb2c999 100644 --- a/std/build.zig +++ b/std/build.zig @@ -1,6 +1,7 @@ const std = @import("std.zig"); const builtin = @import("builtin"); const io = std.io; +const fs = std.fs; const mem = std.mem; const debug = std.debug; const assert = debug.assert; @@ -8,9 +9,7 @@ const warn = std.debug.warn; const ArrayList = std.ArrayList; const HashMap = std.HashMap; const Allocator = mem.Allocator; -const os = std.os; -const StdIo = os.ChildProcess.StdIo; -const Term = os.ChildProcess.Term; +const process = std.process; const BufSet = std.BufSet; const BufMap = std.BufMap; const fmt_lib = std.fmt; @@ -96,11 +95,11 @@ pub const Builder = struct { pub fn init(allocator: *Allocator, zig_exe: []const u8, build_root: []const u8, cache_root: []const u8) Builder { const env_map = allocator.create(BufMap) catch unreachable; - env_map.* = os.getEnvMap(allocator) catch unreachable; + env_map.* = process.getEnvMap(allocator) catch unreachable; var self = Builder{ .zig_exe = zig_exe, .build_root = build_root, - .cache_root = os.path.relative(allocator, build_root, cache_root) catch unreachable, + .cache_root = fs.path.relative(allocator, build_root, cache_root) catch unreachable, .verbose = false, .verbose_tokenize = false, .verbose_ast = false, @@ -154,8 +153,8 @@ pub const Builder = struct { pub fn setInstallPrefix(self: *Builder, maybe_prefix: ?[]const u8) void { self.prefix = maybe_prefix orelse "/usr/local"; // TODO better default - self.lib_dir = os.path.join(self.allocator, [][]const u8{ self.prefix, "lib" }) catch unreachable; - self.exe_dir = os.path.join(self.allocator, [][]const u8{ self.prefix, "bin" }) catch unreachable; + self.lib_dir = fs.path.join(self.allocator, [][]const u8{ self.prefix, "lib" }) catch unreachable; + self.exe_dir = fs.path.join(self.allocator, [][]const u8{ self.prefix, "bin" }) catch unreachable; } pub fn addExecutable(self: *Builder, name: []const u8, root_src: ?[]const u8) *LibExeObjStep { @@ -287,7 +286,7 @@ pub const Builder = struct { if (self.verbose) { warn("rm {}\n", installed_file); } - os.deleteFile(installed_file) catch {}; + fs.deleteFile(installed_file) catch {}; } // TODO remove empty directories @@ -326,7 +325,7 @@ pub const Builder = struct { fn detectNativeSystemPaths(self: *Builder) void { var is_nixos = false; - if (os.getEnvVarOwned(self.allocator, "NIX_CFLAGS_COMPILE")) |nix_cflags_compile| { + if (process.getEnvVarOwned(self.allocator, "NIX_CFLAGS_COMPILE")) |nix_cflags_compile| { is_nixos = true; var it = mem.tokenize(nix_cflags_compile, " "); while (true) { @@ -345,7 +344,7 @@ pub const Builder = struct { } else |err| { assert(err == error.EnvironmentVariableNotFound); } - if (os.getEnvVarOwned(self.allocator, "NIX_LDFLAGS")) |nix_ldflags| { + if (process.getEnvVarOwned(self.allocator, "NIX_LDFLAGS")) |nix_ldflags| { is_nixos = true; var it = mem.tokenize(nix_ldflags, " "); while (true) { @@ -369,7 +368,7 @@ pub const Builder = struct { } if (is_nixos) return; switch (builtin.os) { - builtin.Os.windows => {}, + .windows => {}, else => { const triple = (CrossTarget{ .arch = builtin.arch, @@ -602,7 +601,7 @@ pub const Builder = struct { printCmd(cwd, argv); } - const child = os.ChildProcess.init(argv, self.allocator) catch unreachable; + const child = std.ChildProcess.init(argv, self.allocator) catch unreachable; defer child.deinit(); child.cwd = cwd; @@ -614,7 +613,7 @@ pub const Builder = struct { }; switch (term) { - Term.Exited => |code| { + .Exited => |code| { if (code != 0) { warn("The following command exited with error code {}:\n", code); printCmd(cwd, argv); @@ -631,7 +630,7 @@ pub const Builder = struct { } pub fn makePath(self: *Builder, path: []const u8) !void { - os.makePath(self.allocator, self.pathFromRoot(path)) catch |err| { + fs.makePath(self.allocator, self.pathFromRoot(path)) catch |err| { warn("Unable to create path {}: {}\n", path, @errorName(err)); return err; }; @@ -652,7 +651,7 @@ pub const Builder = struct { ///::dest_rel_path is relative to prefix path or it can be an absolute path pub fn addInstallFile(self: *Builder, src_path: []const u8, dest_rel_path: []const u8) *InstallFileStep { - const full_dest_path = os.path.resolve( + const full_dest_path = fs.path.resolve( self.allocator, [][]const u8{ self.prefix, dest_rel_path }, ) catch unreachable; @@ -677,20 +676,20 @@ pub const Builder = struct { warn("cp {} {}\n", source_path, dest_path); } - const dirname = os.path.dirname(dest_path) orelse "."; + const dirname = fs.path.dirname(dest_path) orelse "."; const abs_source_path = self.pathFromRoot(source_path); - os.makePath(self.allocator, dirname) catch |err| { + fs.makePath(self.allocator, dirname) catch |err| { warn("Unable to create path {}: {}\n", dirname, @errorName(err)); return err; }; - os.copyFileMode(abs_source_path, dest_path, mode) catch |err| { + fs.copyFileMode(abs_source_path, dest_path, mode) catch |err| { warn("Unable to copy {} to {}: {}\n", abs_source_path, dest_path, @errorName(err)); return err; }; } fn pathFromRoot(self: *Builder, rel_path: []const u8) []u8 { - return os.path.resolve(self.allocator, [][]const u8{ self.build_root, rel_path }) catch unreachable; + return fs.path.resolve(self.allocator, [][]const u8{ self.build_root, rel_path }) catch unreachable; } pub fn fmt(self: *Builder, comptime format: []const u8, args: ...) []u8 { @@ -702,11 +701,11 @@ pub const Builder = struct { const exe_extension = (Target{ .Native = {} }).exeFileExt(); for (self.search_prefixes.toSliceConst()) |search_prefix| { for (names) |name| { - if (os.path.isAbsolute(name)) { + if (fs.path.isAbsolute(name)) { return name; } - const full_path = try os.path.join(self.allocator, [][]const u8{ search_prefix, "bin", self.fmt("{}{}", name, exe_extension) }); - if (os.path.real(self.allocator, full_path)) |real_path| { + const full_path = try fs.path.join(self.allocator, [][]const u8{ search_prefix, "bin", self.fmt("{}{}", name, exe_extension) }); + if (fs.path.real(self.allocator, full_path)) |real_path| { return real_path; } else |_| { continue; @@ -715,13 +714,13 @@ pub const Builder = struct { } if (self.env_map.get("PATH")) |PATH| { for (names) |name| { - if (os.path.isAbsolute(name)) { + if (fs.path.isAbsolute(name)) { return name; } - var it = mem.tokenize(PATH, []u8{os.path.delimiter}); + var it = mem.tokenize(PATH, []u8{fs.path.delimiter}); while (it.next()) |path| { - const full_path = try os.path.join(self.allocator, [][]const u8{ path, self.fmt("{}{}", name, exe_extension) }); - if (os.path.real(self.allocator, full_path)) |real_path| { + const full_path = try fs.path.join(self.allocator, [][]const u8{ path, self.fmt("{}{}", name, exe_extension) }); + if (fs.path.real(self.allocator, full_path)) |real_path| { return real_path; } else |_| { continue; @@ -730,12 +729,12 @@ pub const Builder = struct { } } for (names) |name| { - if (os.path.isAbsolute(name)) { + if (fs.path.isAbsolute(name)) { return name; } for (paths) |path| { - const full_path = try os.path.join(self.allocator, [][]const u8{ path, self.fmt("{}{}", name, exe_extension) }); - if (os.path.real(self.allocator, full_path)) |real_path| { + const full_path = try fs.path.join(self.allocator, [][]const u8{ path, self.fmt("{}{}", name, exe_extension) }); + if (fs.path.real(self.allocator, full_path)) |real_path| { return real_path; } else |_| { continue; @@ -749,12 +748,12 @@ pub const Builder = struct { assert(argv.len != 0); const max_output_size = 100 * 1024; - const child = try os.ChildProcess.init(argv, self.allocator); + const child = try std.ChildProcess.init(argv, self.allocator); defer child.deinit(); - child.stdin_behavior = os.ChildProcess.StdIo.Ignore; - child.stdout_behavior = os.ChildProcess.StdIo.Pipe; - child.stderr_behavior = os.ChildProcess.StdIo.Inherit; + child.stdin_behavior = .Ignore; + child.stdout_behavior = .Pipe; + child.stderr_behavior = .Inherit; try child.spawn(); @@ -766,7 +765,7 @@ pub const Builder = struct { const term = child.wait() catch |err| std.debug.panic("unable to spawn {}: {}", argv[0], err); switch (term) { - os.ChildProcess.Term.Exited => |code| { + .Exited => |code| { if (code != 0) { warn("The following command exited with error code {}:\n", code); printCmd(null, argv); @@ -846,7 +845,7 @@ pub const Target = union(enum) { } } - pub fn oFileExt(self: *const Target) []const u8 { + pub fn oFileExt(self: Target) []const u8 { const abi = switch (self.*) { Target.Native => builtin.abi, Target.Cross => |t| t.abi, @@ -857,49 +856,49 @@ pub const Target = union(enum) { }; } - pub fn exeFileExt(self: *const Target) []const u8 { + pub fn exeFileExt(self: Target) []const u8 { return switch (self.getOs()) { - builtin.Os.windows => ".exe", + .windows => ".exe", else => "", }; } - pub fn libFileExt(self: *const Target) []const u8 { + pub fn libFileExt(self: Target) []const u8 { return switch (self.getOs()) { - builtin.Os.windows => ".lib", + .windows => ".lib", else => ".a", }; } - pub fn getOs(self: *const Target) builtin.Os { - return switch (self.*) { + pub fn getOs(self: Target) builtin.Os { + return switch (self) { Target.Native => builtin.os, Target.Cross => |t| t.os, }; } - pub fn isDarwin(self: *const Target) bool { + pub fn isDarwin(self: Target) bool { return switch (self.getOs()) { - builtin.Os.ios, builtin.Os.macosx => true, + .ios, .macosx, .watchos, .tvos => true, else => false, }; } - pub fn isWindows(self: *const Target) bool { + pub fn isWindows(self: Target) bool { return switch (self.getOs()) { - builtin.Os.windows => true, + .windows => true, else => false, }; } - pub fn isFreeBSD(self: *const Target) bool { + pub fn isFreeBSD(self: Target) bool { return switch (self.getOs()) { - builtin.Os.freebsd => true, + .freebsd => true, else => false, }; } - pub fn wantSharedLibSymLinks(self: *const Target) bool { + pub fn wantSharedLibSymLinks(self: Target) bool { return !self.isWindows(); } }; @@ -1065,19 +1064,19 @@ pub const LibExeObjStep = struct { fn computeOutFileNames(self: *LibExeObjStep) void { switch (self.kind) { - Kind.Obj => { + .Obj => { self.out_filename = self.builder.fmt("{}{}", self.name, self.target.oFileExt()); }, - Kind.Exe => { + .Exe => { self.out_filename = self.builder.fmt("{}{}", self.name, self.target.exeFileExt()); }, - Kind.Test => { + .Test => { self.out_filename = self.builder.fmt("test{}", self.target.exeFileExt()); }, - Kind.Lib => { + .Lib => { if (!self.is_dynamic) { switch (self.target.getOs()) { - builtin.Os.windows => { + .windows => { self.out_filename = self.builder.fmt("{}.lib", self.name); }, else => { @@ -1087,13 +1086,13 @@ pub const LibExeObjStep = struct { self.out_lib_filename = self.out_filename; } else { switch (self.target.getOs()) { - builtin.Os.ios, builtin.Os.macosx => { + .ios, .macosx => { self.out_filename = self.builder.fmt("lib{}.{d}.{d}.{d}.dylib", self.name, self.version.major, self.version.minor, self.version.patch); self.major_only_filename = self.builder.fmt("lib{}.{d}.dylib", self.name, self.version.major); self.name_only_filename = self.builder.fmt("lib{}.dylib", self.name); self.out_lib_filename = self.out_filename; }, - builtin.Os.windows => { + .windows => { self.out_filename = self.builder.fmt("{}.dll", self.name); self.out_lib_filename = self.builder.fmt("{}.lib", self.name); }, @@ -1228,7 +1227,7 @@ pub const LibExeObjStep = struct { /// the make step, from a step that has declared a dependency on this one. /// To run an executable built with zig build, use `run`, or create an install step and invoke it. pub fn getOutputPath(self: *LibExeObjStep) []const u8 { - return os.path.join( + return fs.path.join( self.builder.allocator, [][]const u8{ self.output_dir.?, self.out_filename }, ) catch unreachable; @@ -1238,7 +1237,7 @@ pub const LibExeObjStep = struct { /// the make step, from a step that has declared a dependency on this one. pub fn getOutputLibPath(self: *LibExeObjStep) []const u8 { assert(self.kind == Kind.Lib); - return os.path.join( + return fs.path.join( self.builder.allocator, [][]const u8{ self.output_dir.?, self.out_lib_filename }, ) catch unreachable; @@ -1249,7 +1248,7 @@ pub const LibExeObjStep = struct { pub fn getOutputHPath(self: *LibExeObjStep) []const u8 { assert(self.kind != Kind.Exe); assert(!self.disable_gen_h); - return os.path.join( + return fs.path.join( self.builder.allocator, [][]const u8{ self.output_dir.?, self.out_h_filename }, ) catch unreachable; @@ -1365,7 +1364,7 @@ pub const LibExeObjStep = struct { try zig_args.append("--library"); try zig_args.append(full_path_lib); - if (os.path.dirname(full_path_lib)) |dirname| { + if (fs.path.dirname(full_path_lib)) |dirname| { try zig_args.append("-rpath"); try zig_args.append(dirname); } @@ -1391,7 +1390,7 @@ pub const LibExeObjStep = struct { } if (self.build_options_contents.len() > 0) { - const build_options_file = try os.path.join( + const build_options_file = try fs.path.join( builder.allocator, [][]const u8{ builder.cache_root, builder.fmt("{}_build_options.zig", self.name) }, ); @@ -1503,7 +1502,7 @@ pub const LibExeObjStep = struct { IncludeDir.OtherStep => |other| { const h_path = other.getOutputHPath(); try zig_args.append("-isystem"); - try zig_args.append(os.path.dirname(h_path).?); + try zig_args.append(fs.path.dirname(h_path).?); }, } } @@ -1576,7 +1575,7 @@ pub const LibExeObjStep = struct { const output_path_nl = try builder.exec(zig_args.toSliceConst()); const output_path = mem.trimRight(u8, output_path_nl, "\r\n"); - self.output_dir = os.path.dirname(output_path).?; + self.output_dir = fs.path.dirname(output_path).?; } if (self.kind == Kind.Lib and self.is_dynamic and self.target.wantSharedLibSymLinks()) { @@ -1637,20 +1636,20 @@ pub const RunStep = struct { } pub fn addPathDir(self: *RunStep, search_path: []const u8) void { - const PATH = if (builtin.os == builtin.Os.windows) "Path" else "PATH"; + const PATH = if (std.os.windows.is_the_target) "Path" else "PATH"; const env_map = self.getEnvMap(); const prev_path = env_map.get(PATH) orelse { env_map.set(PATH, search_path) catch unreachable; return; }; - const new_path = self.builder.fmt("{}" ++ [1]u8{os.path.delimiter} ++ "{}", prev_path, search_path); + const new_path = self.builder.fmt("{}" ++ [1]u8{fs.path.delimiter} ++ "{}", prev_path, search_path); env_map.set(PATH, new_path) catch unreachable; } pub fn getEnvMap(self: *RunStep) *BufMap { return self.env_map orelse { const env_map = self.builder.allocator.create(BufMap) catch unreachable; - env_map.* = os.getEnvMap(self.builder.allocator) catch unreachable; + env_map.* = process.getEnvMap(self.builder.allocator) catch unreachable; self.env_map = env_map; return env_map; }; @@ -1688,7 +1687,7 @@ pub const RunStep = struct { switch (link_object) { LibExeObjStep.LinkObject.OtherStep => |other| { if (other.target.isWindows() and other.isDynamicLibrary()) { - self.addPathDir(os.path.dirname(other.getOutputPath()).?); + self.addPathDir(fs.path.dirname(other.getOutputPath()).?); self.addPathForDynLibs(other); } }, @@ -1718,7 +1717,7 @@ const InstallArtifactStep = struct { .builder = builder, .step = Step.init(builder.fmt("install {}", artifact.step.name), builder.allocator, make), .artifact = artifact, - .dest_file = os.path.join( + .dest_file = fs.path.join( builder.allocator, [][]const u8{ dest_dir, artifact.out_filename }, ) catch unreachable, @@ -1726,11 +1725,11 @@ const InstallArtifactStep = struct { self.step.dependOn(&artifact.step); builder.pushInstalledFile(self.dest_file); if (self.artifact.kind == LibExeObjStep.Kind.Lib and self.artifact.is_dynamic) { - builder.pushInstalledFile(os.path.join( + builder.pushInstalledFile(fs.path.join( builder.allocator, [][]const u8{ builder.lib_dir, artifact.major_only_filename }, ) catch unreachable); - builder.pushInstalledFile(os.path.join( + builder.pushInstalledFile(fs.path.join( builder.allocator, [][]const u8{ builder.lib_dir, artifact.name_only_filename }, ) catch unreachable); @@ -1743,12 +1742,12 @@ const InstallArtifactStep = struct { const builder = self.builder; const mode = switch (builtin.os) { - builtin.Os.windows => {}, + .windows => {}, else => switch (self.artifact.kind) { - LibExeObjStep.Kind.Obj => unreachable, - LibExeObjStep.Kind.Test => unreachable, - LibExeObjStep.Kind.Exe => u32(0o755), - LibExeObjStep.Kind.Lib => if (!self.artifact.is_dynamic) u32(0o666) else u32(0o755), + .Obj => unreachable, + .Test => unreachable, + .Exe => u32(0o755), + .Lib => if (!self.artifact.is_dynamic) u32(0o666) else u32(0o755), }, }; try builder.copyFileMode(self.artifact.getOutputPath(), self.dest_file, mode); @@ -1797,8 +1796,8 @@ pub const WriteFileStep = struct { fn make(step: *Step) !void { const self = @fieldParentPtr(WriteFileStep, "step", step); const full_path = self.builder.pathFromRoot(self.file_path); - const full_path_dir = os.path.dirname(full_path) orelse "."; - os.makePath(self.builder.allocator, full_path_dir) catch |err| { + const full_path_dir = fs.path.dirname(full_path) orelse "."; + fs.makePath(self.builder.allocator, full_path_dir) catch |err| { warn("unable to make path {}: {}\n", full_path_dir, @errorName(err)); return err; }; @@ -1845,7 +1844,7 @@ pub const RemoveDirStep = struct { const self = @fieldParentPtr(RemoveDirStep, "step", step); const full_path = self.builder.pathFromRoot(self.dir_path); - os.deleteTree(self.builder.allocator, full_path) catch |err| { + fs.deleteTree(self.builder.allocator, full_path) catch |err| { warn("Unable to remove {}: {}\n", full_path, @errorName(err)); return err; }; @@ -1887,23 +1886,23 @@ pub const Step = struct { }; fn doAtomicSymLinks(allocator: *Allocator, output_path: []const u8, filename_major_only: []const u8, filename_name_only: []const u8) !void { - const out_dir = os.path.dirname(output_path) orelse "."; - const out_basename = os.path.basename(output_path); + const out_dir = fs.path.dirname(output_path) orelse "."; + const out_basename = fs.path.basename(output_path); // sym link for libfoo.so.1 to libfoo.so.1.2.3 - const major_only_path = os.path.join( + const major_only_path = fs.path.join( allocator, [][]const u8{ out_dir, filename_major_only }, ) catch unreachable; - os.atomicSymLink(allocator, out_basename, major_only_path) catch |err| { + fs.atomicSymLink(allocator, out_basename, major_only_path) catch |err| { warn("Unable to symlink {} -> {}\n", major_only_path, out_basename); return err; }; // sym link for libfoo.so to libfoo.so.1 - const name_only_path = os.path.join( + const name_only_path = fs.path.join( allocator, [][]const u8{ out_dir, filename_name_only }, ) catch unreachable; - os.atomicSymLink(allocator, filename_major_only, name_only_path) catch |err| { + fs.atomicSymLink(allocator, filename_major_only, name_only_path) catch |err| { warn("Unable to symlink {} -> {}\n", name_only_path, filename_major_only); return err; }; diff --git a/std/child_process.zig b/std/child_process.zig index 95d11d46a3..6305f10344 100644 --- a/std/child_process.zig +++ b/std/child_process.zig @@ -2,7 +2,9 @@ const std = @import("std.zig"); const cstr = std.cstr; const unicode = std.unicode; const io = std.io; +const fs = std.fs; const os = std.os; +const process = std.process; const File = std.fs.File; const windows = os.windows; const mem = std.mem; @@ -14,12 +16,10 @@ const Os = builtin.Os; const LinkedList = std.LinkedList; const maxInt = std.math.maxInt; -const is_windows = builtin.os == .windows; - pub const ChildProcess = struct { - pub pid: if (is_windows) void else i32, - pub handle: if (is_windows) windows.HANDLE else void, - pub thread_handle: if (is_windows) windows.HANDLE else void, + pub pid: if (os.windows.is_the_target) void else i32, + pub handle: if (os.windows.is_the_target) windows.HANDLE else void, + pub thread_handle: if (os.windows.is_the_target) windows.HANDLE else void, pub allocator: *mem.Allocator, @@ -39,16 +39,16 @@ pub const ChildProcess = struct { pub stderr_behavior: StdIo, /// Set to change the user id when spawning the child process. - pub uid: if (is_windows) void else ?u32, + pub uid: if (os.windows.is_the_target) void else ?u32, /// Set to change the group id when spawning the child process. - pub gid: if (is_windows) void else ?u32, + pub gid: if (os.windows.is_the_target) void else ?u32, /// Set to change the current working directory when spawning the child process. pub cwd: ?[]const u8, - err_pipe: if (is_windows) void else [2]i32, - llnode: if (is_windows) void else LinkedList(*ChildProcess).Node, + err_pipe: if (os.windows.is_the_target) void else [2]i32, + llnode: if (os.windows.is_the_target) void else LinkedList(*ChildProcess).Node, pub const SpawnError = error{ ProcessFdQuotaExceeded, @@ -98,10 +98,8 @@ pub const ChildProcess = struct { .term = null, .env_map = null, .cwd = null, - .uid = if (is_windows) {} else - null, - .gid = if (is_windows) {} else - null, + .uid = if (os.windows.is_the_target) {} else null, + .gid = if (os.windows.is_the_target) {} else null, .stdin = null, .stdout = null, .stderr = null, @@ -121,7 +119,7 @@ pub const ChildProcess = struct { /// On success must call `kill` or `wait`. pub fn spawn(self: *ChildProcess) !void { - if (is_windows) { + if (os.windows.is_the_target) { return self.spawnWindows(); } else { return self.spawnPosix(); @@ -135,7 +133,7 @@ pub const ChildProcess = struct { /// Forcibly terminates child process and then cleans up all resources. pub fn kill(self: *ChildProcess) !Term { - if (is_windows) { + if (os.windows.is_the_target) { return self.killWindows(1); } else { return self.killPosix(); @@ -165,7 +163,7 @@ pub const ChildProcess = struct { /// Blocks until child process terminates and then cleans up all resources. pub fn wait(self: *ChildProcess) !Term { - if (is_windows) { + if (os.windows.is_the_target) { return self.waitWindows(); } else { return self.waitPosix(); @@ -339,7 +337,7 @@ pub const ChildProcess = struct { break :x env_map; } else x: { we_own_env_map = true; - env_map_owned = try os.getEnvMap(self.allocator); + env_map_owned = try process.getEnvMap(self.allocator); break :x &env_map_owned; }; defer { @@ -372,15 +370,15 @@ pub const ChildProcess = struct { } if (self.cwd) |cwd| { - os.changeCurDir(cwd) catch |err| forkChildErrReport(err_pipe[1], err); + os.chdir(cwd) catch |err| forkChildErrReport(err_pipe[1], err); } if (self.gid) |gid| { - os.posix_setregid(gid, gid) catch |err| forkChildErrReport(err_pipe[1], err); + os.setregid(gid, gid) catch |err| forkChildErrReport(err_pipe[1], err); } if (self.uid) |uid| { - os.posix_setreuid(uid, uid) catch |err| forkChildErrReport(err_pipe[1], err); + os.setreuid(uid, uid) catch |err| forkChildErrReport(err_pipe[1], err); } os.execve(self.allocator, self.argv, env_map) catch |err| forkChildErrReport(err_pipe[1], err); @@ -541,7 +539,7 @@ pub const ChildProcess = struct { // to match posix semantics const app_name = x: { if (self.cwd) |cwd| { - const resolved = try os.path.resolve(self.allocator, [][]const u8{ cwd, self.argv[0] }); + const resolved = try fs.path.resolve(self.allocator, [][]const u8{ cwd, self.argv[0] }); defer self.allocator.free(resolved); break :x try cstr.addNullByte(self.allocator, resolved); } else { @@ -559,12 +557,12 @@ pub const ChildProcess = struct { windowsCreateProcess(app_name_w.ptr, cmd_line_w.ptr, envp_ptr, cwd_w_ptr, &siStartInfo, &piProcInfo) catch |no_path_err| { if (no_path_err != error.FileNotFound) return no_path_err; - const PATH = try os.getEnvVarOwned(self.allocator, "PATH"); + const PATH = try process.getEnvVarOwned(self.allocator, "PATH"); defer self.allocator.free(PATH); var it = mem.tokenize(PATH, ";"); while (it.next()) |search_path| { - const joined_path = try os.path.join(self.allocator, [][]const u8{ search_path, app_name }); + const joined_path = try fs.path.join(self.allocator, [][]const u8{ search_path, app_name }); defer self.allocator.free(joined_path); const joined_path_w = try unicode.utf8ToUtf16LeWithNull(self.allocator, joined_path); @@ -616,10 +614,10 @@ pub const ChildProcess = struct { fn setUpChildIo(stdio: StdIo, pipe_fd: i32, std_fileno: i32, dev_null_fd: i32) !void { switch (stdio) { - StdIo.Pipe => try os.posixDup2(pipe_fd, std_fileno), + StdIo.Pipe => try os.dup2(pipe_fd, std_fileno), StdIo.Close => os.close(std_fileno), StdIo.Inherit => {}, - StdIo.Ignore => try os.posixDup2(dev_null_fd, std_fileno), + StdIo.Ignore => try os.dup2(dev_null_fd, std_fileno), } } }; diff --git a/std/cstr.zig b/std/cstr.zig index 49d6373732..c8c3447921 100644 --- a/std/cstr.zig +++ b/std/cstr.zig @@ -9,11 +9,6 @@ pub const line_sep = switch (builtin.os) { else => "\n", }; -/// Deprecated, use mem.len -pub fn len(ptr: [*]const u8) usize { - return mem.len(u8, ptr); -} - pub fn cmp(a: [*]const u8, b: [*]const u8) i8 { var index: usize = 0; while (a[index] == b[index] and a[index] != 0) : (index += 1) {} @@ -26,16 +21,6 @@ pub fn cmp(a: [*]const u8, b: [*]const u8) i8 { } } -/// Deprecated, use mem.toSliceConst -pub fn toSliceConst(str: [*]const u8) []const u8 { - return mem.toSliceConst(u8, str); -} - -/// Deprecated, use mem.toSlice -pub fn toSlice(str: [*]u8) []u8 { - return mem.toSlice(u8, str); -} - test "cstr fns" { comptime testCStrFnsImpl(); testCStrFnsImpl(); diff --git a/std/debug.zig b/std/debug.zig index a309c31bbe..2c623d42f9 100644 --- a/std/debug.zig +++ b/std/debug.zig @@ -3,16 +3,18 @@ const math = std.math; const mem = std.mem; const io = std.io; const os = std.os; +const fs = std.fs; +const process = std.process; const elf = std.elf; const DW = std.dwarf; const macho = std.macho; const coff = std.coff; const pdb = std.pdb; -const windows = os.windows; const ArrayList = std.ArrayList; const builtin = @import("builtin"); const maxInt = std.math.maxInt; const File = std.fs.File; +const windows = std.os.windows; const leb = @import("debug/leb128.zig"); @@ -20,8 +22,8 @@ pub const FailingAllocator = @import("debug/failing_allocator.zig").FailingAlloc pub const failing_allocator = &FailingAllocator.init(global_allocator, 0).allocator; pub const runtime_safety = switch (builtin.mode) { - builtin.Mode.Debug, builtin.Mode.ReleaseSafe => true, - builtin.Mode.ReleaseFast, builtin.Mode.ReleaseSmall => false, + .Debug, .ReleaseSafe => true, + .ReleaseFast, .ReleaseSmall => false, }; const Module = struct { @@ -76,7 +78,7 @@ pub fn getSelfDebugInfo() !*DebugInfo { fn wantTtyColor() bool { var bytes: [128]u8 = undefined; const allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator; - return if (std.os.getEnvVarOwned(allocator, "ZIG_DEBUG_COLOR")) |_| true else |_| stderr_file.isTty(); + return if (process.getEnvVarOwned(allocator, "ZIG_DEBUG_COLOR")) |_| true else |_| stderr_file.isTty(); } /// Tries to print the current stack trace to stderr, unbuffered, and ignores any error returned. @@ -100,47 +102,44 @@ pub fn dumpCurrentStackTrace(start_addr: ?usize) void { /// chopping off the irrelevant frames and shifting so that the returned addresses pointer /// equals the passed in addresses pointer. pub fn captureStackTrace(first_address: ?usize, stack_trace: *builtin.StackTrace) void { - switch (builtin.os) { - builtin.Os.windows => { - const addrs = stack_trace.instruction_addresses; - const u32_addrs_len = @intCast(u32, addrs.len); - const first_addr = first_address orelse { - stack_trace.index = windows.RtlCaptureStackBackTrace( - 0, - u32_addrs_len, - @ptrCast(**c_void, addrs.ptr), - null, - ); - return; - }; - var addr_buf_stack: [32]usize = undefined; - const addr_buf = if (addr_buf_stack.len > addrs.len) addr_buf_stack[0..] else addrs; - const n = windows.RtlCaptureStackBackTrace(0, u32_addrs_len, @ptrCast(**c_void, addr_buf.ptr), null); - const first_index = for (addr_buf[0..n]) |addr, i| { - if (addr == first_addr) { - break i; - } - } else { - stack_trace.index = 0; + if (windows.is_the_target) { + const addrs = stack_trace.instruction_addresses; + const u32_addrs_len = @intCast(u32, addrs.len); + const first_addr = first_address orelse { + stack_trace.index = windows.ntdll.RtlCaptureStackBackTrace( + 0, + u32_addrs_len, + @ptrCast(**c_void, addrs.ptr), + null, + ); + return; + }; + var addr_buf_stack: [32]usize = undefined; + const addr_buf = if (addr_buf_stack.len > addrs.len) addr_buf_stack[0..] else addrs; + const n = windows.ntdll.RtlCaptureStackBackTrace(0, u32_addrs_len, @ptrCast(**c_void, addr_buf.ptr), null); + const first_index = for (addr_buf[0..n]) |addr, i| { + if (addr == first_addr) { + break i; + } + } else { + stack_trace.index = 0; + return; + }; + const slice = addr_buf[first_index..n]; + // We use a for loop here because slice and addrs may alias. + for (slice) |addr, i| { + addrs[i] = addr; + } + stack_trace.index = slice.len; + } else { + var it = StackIterator.init(first_address); + for (stack_trace.instruction_addresses) |*addr, i| { + addr.* = it.next() orelse { + stack_trace.index = i; return; }; - const slice = addr_buf[first_index..n]; - // We use a for loop here because slice and addrs may alias. - for (slice) |addr, i| { - addrs[i] = addr; - } - stack_trace.index = slice.len; - }, - else => { - var it = StackIterator.init(first_address); - for (stack_trace.instruction_addresses) |*addr, i| { - addr.* = it.next() orelse { - stack_trace.index = i; - return; - }; - } - stack_trace.index = stack_trace.instruction_addresses.len; - }, + } + stack_trace.index = stack_trace.instruction_addresses.len; } } @@ -260,9 +259,8 @@ pub const StackIterator = struct { }; pub fn writeCurrentStackTrace(out_stream: var, debug_info: *DebugInfo, tty_color: bool, start_addr: ?usize) !void { - switch (builtin.os) { - builtin.Os.windows => return writeCurrentStackTraceWindows(out_stream, debug_info, tty_color, start_addr), - else => {}, + if (windows.is_the_target) { + return writeCurrentStackTraceWindows(out_stream, debug_info, tty_color, start_addr); } var it = StackIterator.init(start_addr); while (it.next()) |return_address| { @@ -277,7 +275,7 @@ pub fn writeCurrentStackTraceWindows( start_addr: ?usize, ) !void { var addr_buf: [1024]usize = undefined; - const n = windows.RtlCaptureStackBackTrace(0, addr_buf.len, @ptrCast(**c_void, &addr_buf), null); + const n = windows.ntdll.RtlCaptureStackBackTrace(0, addr_buf.len, @ptrCast(**c_void, &addr_buf), null); const addrs = addr_buf[0..n]; var start_i: usize = if (start_addr) |saddr| blk: { for (addrs) |addr, i| { @@ -291,17 +289,18 @@ pub fn writeCurrentStackTraceWindows( } pub fn printSourceAtAddress(debug_info: *DebugInfo, out_stream: var, address: usize, tty_color: bool) !void { - switch (builtin.os) { - builtin.Os.macosx => return printSourceAtAddressMacOs(debug_info, out_stream, address, tty_color), - builtin.Os.linux, builtin.Os.freebsd, builtin.Os.netbsd => return printSourceAtAddressLinux(debug_info, out_stream, address, tty_color), - builtin.Os.windows => return printSourceAtAddressWindows(debug_info, out_stream, address, tty_color), - else => return error.UnsupportedOperatingSystem, + if (windows.is_the_target) { + return printSourceAtAddressWindows(debug_info, out_stream, address, tty_color); } + if (os.darwin.is_the_target) { + return printSourceAtAddressMacOs(debug_info, out_stream, address, tty_color); + } + return printSourceAtAddressPosix(debug_info, out_stream, address, tty_color); } fn printSourceAtAddressWindows(di: *DebugInfo, out_stream: var, relocated_address: usize, tty_color: bool) !void { const allocator = getDebugInfoAllocator(); - const base_address = os.getBaseAddress(); + const base_address = process.getBaseAddress(); const relative_address = relocated_address - base_address; var coff_section: *coff.Section = undefined; @@ -331,7 +330,7 @@ fn printSourceAtAddressWindows(di: *DebugInfo, out_stream: var, relocated_addres const mod = &di.modules[mod_index]; try populateModule(di, mod); - const obj_basename = os.path.basename(mod.obj_file_name); + const obj_basename = fs.path.basename(mod.obj_file_name); var symbol_i: usize = 0; const symbol_name = while (symbol_i != mod.symbols.len) { @@ -634,7 +633,7 @@ fn machoSearchSymbols(symbols: []const MachoSymbol, address: usize) ?*const Mach } fn printSourceAtAddressMacOs(di: *DebugInfo, out_stream: var, address: usize, tty_color: bool) !void { - const base_addr = std.os.getBaseAddress(); + const base_addr = process.getBaseAddress(); const adjusted_addr = 0x100000000 + (address - base_addr); const symbol = machoSearchSymbols(di.symbols, adjusted_addr) orelse { @@ -649,7 +648,7 @@ fn printSourceAtAddressMacOs(di: *DebugInfo, out_stream: var, address: usize, tt const symbol_name = mem.toSliceConst(u8, di.strings.ptr + symbol.nlist.n_strx); const compile_unit_name = if (symbol.ofile) |ofile| blk: { const ofile_path = mem.toSliceConst(u8, di.strings.ptr + ofile.n_strx); - break :blk os.path.basename(ofile_path); + break :blk fs.path.basename(ofile_path); } else "???"; if (getLineNumberInfoMacOs(di, symbol.*, adjusted_addr)) |line_info| { defer line_info.deinit(); @@ -716,7 +715,7 @@ pub fn printSourceAtAddressDwarf( } } -pub fn printSourceAtAddressLinux(debug_info: *DebugInfo, out_stream: var, address: usize, tty_color: bool) !void { +pub fn printSourceAtAddressPosix(debug_info: *DebugInfo, out_stream: var, address: usize, tty_color: bool) !void { return printSourceAtAddressDwarf(debug_info, out_stream, address, tty_color, printLineFromFileAnyOs); } @@ -776,16 +775,17 @@ pub const OpenSelfDebugInfoError = error{ }; pub fn openSelfDebugInfo(allocator: *mem.Allocator) !DebugInfo { - switch (builtin.os) { - builtin.Os.linux, builtin.Os.freebsd, builtin.Os.netbsd => return openSelfDebugInfoLinux(allocator), - builtin.Os.macosx, builtin.Os.ios => return openSelfDebugInfoMacOs(allocator), - builtin.Os.windows => return openSelfDebugInfoWindows(allocator), - else => return error.UnsupportedOperatingSystem, + if (windows.is_the_target) { + return openSelfDebugInfoWindows(allocator); + } + if (os.darwin.is_the_target) { + return openSelfDebugInfoMacOs(allocator); } + return openSelfDebugInfoPosix(allocator); } fn openSelfDebugInfoWindows(allocator: *mem.Allocator) !DebugInfo { - const self_file = try os.openSelfExe(); + const self_file = try fs.openSelfExe(); defer self_file.close(); const coff_obj = try allocator.create(coff.Coff); @@ -812,7 +812,7 @@ fn openSelfDebugInfoWindows(allocator: *mem.Allocator) !DebugInfo { const len = try di.coff.getPdbPath(path_buf[0..]); const raw_path = path_buf[0..len]; - const path = try os.path.resolve(allocator, [][]const u8{raw_path}); + const path = try fs.path.resolve(allocator, [][]const u8{raw_path}); try di.pdb.openFile(di.coff, path); @@ -1002,13 +1002,13 @@ pub fn openElfDebugInfo( return di; } -fn openSelfDebugInfoLinux(allocator: *mem.Allocator) !DwarfInfo { +fn openSelfDebugInfoPosix(allocator: *mem.Allocator) !DwarfInfo { const S = struct { var self_exe_file: File = undefined; var self_exe_mmap_seekable: io.SliceSeekableInStream = undefined; }; - S.self_exe_file = try os.openSelfExe(); + S.self_exe_file = try fs.openSelfExe(); errdefer S.self_exe_file.close(); const self_exe_mmap_len = try S.self_exe_file.getEndPos(); @@ -1195,7 +1195,7 @@ pub const DwarfInfo = struct { }; pub const DebugInfo = switch (builtin.os) { - builtin.Os.macosx, builtin.Os.ios => struct { + .macosx, .ios, .watchos, .tvos => struct { symbols: []const MachoSymbol, strings: []const u8, ofiles: OFileTable, @@ -1211,13 +1211,13 @@ pub const DebugInfo = switch (builtin.os) { return self.ofiles.allocator; } }, - builtin.Os.uefi, builtin.Os.windows => struct { + .uefi, .windows => struct { pdb: pdb.Pdb, coff: *coff.Coff, sect_contribs: []pdb.SectionContribEntry, modules: []Module, }, - builtin.Os.linux, builtin.Os.freebsd, builtin.Os.netbsd => DwarfInfo, + .linux, .freebsd, .netbsd => DwarfInfo, else => @compileError("Unsupported OS"), }; @@ -1411,7 +1411,7 @@ const LineNumberProgram = struct { return error.InvalidDebugInfo; } else self.include_dirs[file_entry.dir_index]; - const file_name = try os.path.join(self.file_entries.allocator, [][]const u8{ dir_name, file_entry.file_name }); + const file_name = try fs.path.join(self.file_entries.allocator, [][]const u8{ dir_name, file_entry.file_name }); errdefer self.file_entries.allocator.free(file_name); return LineInfo{ .line = if (self.prev_line >= 0) @intCast(u64, self.prev_line) else 0, diff --git a/std/dynamic_library.zig b/std/dynamic_library.zig index 2832b54dbb..ce721350ea 100644 --- a/std/dynamic_library.zig +++ b/std/dynamic_library.zig @@ -2,7 +2,6 @@ const builtin = @import("builtin"); const std = @import("std.zig"); const mem = std.mem; -const cstr = std.cstr; const os = std.os; const assert = std.debug.assert; const testing = std.testing; @@ -223,7 +222,7 @@ pub const ElfLib = struct { if (0 == (u32(1) << @intCast(u5, self.syms[i].st_info & 0xf) & OK_TYPES)) continue; if (0 == (u32(1) << @intCast(u5, self.syms[i].st_info >> 4) & OK_BINDS)) continue; if (0 == self.syms[i].st_shndx) continue; - if (!mem.eql(u8, name, cstr.toSliceConst(self.strings + self.syms[i].st_name))) continue; + if (!mem.eql(u8, name, mem.toSliceConst(u8, self.strings + self.syms[i].st_name))) continue; if (maybe_versym) |versym| { if (!checkver(self.verdef.?, versym[i], vername, self.strings)) continue; @@ -246,7 +245,7 @@ fn checkver(def_arg: *elf.Verdef, vsym_arg: i32, vername: []const u8, strings: [ def = @intToPtr(*elf.Verdef, @ptrToInt(def) + def.vd_next); } const aux = @intToPtr(*elf.Verdaux, @ptrToInt(def) + def.vd_aux); - return mem.eql(u8, vername, cstr.toSliceConst(strings + aux.vda_name)); + return mem.eql(u8, vername, mem.toSliceConst(u8, strings + aux.vda_name)); } pub const WindowsDynLib = struct { diff --git a/std/event/fs.zig b/std/event/fs.zig index fbdac65889..221070a062 100644 --- a/std/event/fs.zig +++ b/std/event/fs.zig @@ -879,7 +879,7 @@ pub fn Watch(comptime V: type) type { } async fn addFileKEvent(self: *Self, file_path: []const u8, value: V) !?V { - const resolved_path = try os.path.resolve(self.channel.loop.allocator, [][]const u8{file_path}); + const resolved_path = try std.fs.path.resolve(self.channel.loop.allocator, [][]const u8{file_path}); var resolved_path_consumed = false; defer if (!resolved_path_consumed) self.channel.loop.allocator.free(resolved_path); @@ -967,12 +967,12 @@ pub fn Watch(comptime V: type) type { async fn addFileLinux(self: *Self, file_path: []const u8, value: V) !?V { const value_copy = value; - const dirname = os.path.dirname(file_path) orelse "."; + const dirname = std.fs.path.dirname(file_path) orelse "."; const dirname_with_null = try std.cstr.addNullByte(self.channel.loop.allocator, dirname); var dirname_with_null_consumed = false; defer if (!dirname_with_null_consumed) self.channel.loop.allocator.free(dirname_with_null); - const basename = os.path.basename(file_path); + const basename = std.fs.path.basename(file_path); const basename_with_null = try std.cstr.addNullByte(self.channel.loop.allocator, basename); var basename_with_null_consumed = false; defer if (!basename_with_null_consumed) self.channel.loop.allocator.free(basename_with_null); @@ -1013,7 +1013,7 @@ pub fn Watch(comptime V: type) type { const value_copy = value; // TODO we might need to convert dirname and basename to canonical file paths ("short"?) - const dirname = try std.mem.dupe(self.channel.loop.allocator, u8, os.path.dirname(file_path) orelse "."); + const dirname = try std.mem.dupe(self.channel.loop.allocator, u8, std.fs.path.dirname(file_path) orelse "."); var dirname_consumed = false; defer if (!dirname_consumed) self.channel.loop.allocator.free(dirname); @@ -1021,7 +1021,7 @@ pub fn Watch(comptime V: type) type { defer self.channel.loop.allocator.free(dirname_utf16le); // TODO https://github.com/ziglang/zig/issues/265 - const basename = os.path.basename(file_path); + const basename = std.fs.path.basename(file_path); const basename_utf16le_null = try std.unicode.utf8ToUtf16LeWithNull(self.channel.loop.allocator, basename); var basename_utf16le_null_consumed = false; defer if (!basename_utf16le_null_consumed) self.channel.loop.allocator.free(basename_utf16le_null); @@ -1334,7 +1334,7 @@ async fn testFsWatchCantFail(loop: *Loop, result: *(anyerror!void)) void { } async fn testFsWatch(loop: *Loop) !void { - const file_path = try os.path.join(loop.allocator, [][]const u8{ test_tmp_dir, "file.txt" }); + const file_path = try std.fs.path.join(loop.allocator, [][]const u8{ test_tmp_dir, "file.txt" }); defer loop.allocator.free(file_path); const contents = diff --git a/std/fmt.zig b/std/fmt.zig index 27a8abba6e..74c36f7086 100644 --- a/std/fmt.zig +++ b/std/fmt.zig @@ -226,7 +226,7 @@ pub fn formatType( builtin.TypeInfo.Pointer.Size.Many => { if (ptr_info.child == u8) { if (fmt.len > 0 and fmt[0] == 's') { - const len = std.cstr.len(value); + const len = mem.len(u8, value); return formatText(value[0..len], fmt, context, Errors, output); } } diff --git a/std/fs.zig b/std/fs.zig index 91e4aca824..b81bc38a75 100644 --- a/std/fs.zig +++ b/std/fs.zig @@ -1,6 +1,8 @@ +const builtin = @import("builtin"); const std = @import("std.zig"); const os = std.os; const mem = std.mem; +const Allocator = std.mem.Allocator; pub const path = @import("fs/path.zig"); pub const File = @import("fs/file.zig").File; @@ -12,8 +14,6 @@ pub const deleteFileC = os.unlinkC; pub const rename = os.rename; pub const renameC = os.renameC; pub const renameW = os.renameW; -pub const changeCurDir = os.chdir; -pub const changeCurDirC = os.chdirC; pub const realpath = os.realpath; pub const realpathC = os.realpathC; pub const realpathW = os.realpathW; @@ -65,13 +65,13 @@ pub fn atomicSymLink(allocator: *Allocator, existing_path: []const u8, new_path: else => return err, // TODO zig should know this set does not include PathAlreadyExists } - const dirname = os.path.dirname(new_path) orelse "."; + const dirname = path.dirname(new_path) orelse "."; var rand_buf: [12]u8 = undefined; const tmp_path = try allocator.alloc(u8, dirname.len + 1 + base64.Base64Encoder.calcSize(rand_buf.len)); defer allocator.free(tmp_path); mem.copy(u8, tmp_path[0..], dirname); - tmp_path[dirname.len] = os.path.sep; + tmp_path[dirname.len] = path.sep; while (true) { try getRandomBytes(rand_buf[0..]); b64_fs_encoder.encode(tmp_path[dirname.len + 1 ..], rand_buf); @@ -143,7 +143,7 @@ pub const AtomicFile = struct { /// TODO once we have null terminated pointers, use the /// openWriteNoClobberN function pub fn init(dest_path: []const u8, mode: File.Mode) InitError!AtomicFile { - const dirname = os.path.dirname(dest_path); + const dirname = path.dirname(dest_path); var rand_buf: [12]u8 = undefined; const dirname_component_len = if (dirname) |d| d.len + 1 else 0; const encoded_rand_len = comptime base64.Base64Encoder.calcSize(rand_buf.len); @@ -153,7 +153,7 @@ pub const AtomicFile = struct { if (dirname) |dir| { mem.copy(u8, tmp_path_buf[0..], dir); - tmp_path_buf[dir.len] = os.path.sep; + tmp_path_buf[dir.len] = path.sep; } tmp_path_buf[tmp_path_len] = 0; @@ -240,7 +240,7 @@ pub fn makePath(allocator: *Allocator, full_path: []const u8) !void { // march end_index backward until next path component while (true) { end_index -= 1; - if (os.path.isSep(resolved_path[end_index])) break; + if (path.isSep(resolved_path[end_index])) break; } continue; }, @@ -250,7 +250,7 @@ pub fn makePath(allocator: *Allocator, full_path: []const u8) !void { // march end_index forward until next path component while (true) { end_index += 1; - if (end_index == resolved_path.len or os.path.isSep(resolved_path[end_index])) break; + if (end_index == resolved_path.len or path.isSep(resolved_path[end_index])) break; } } } @@ -614,7 +614,7 @@ pub const Dir = struct { const next_index = self.handle.index + linux_entry.d_reclen; self.handle.index = next_index; - const name = cstr.toSlice(@ptrCast([*]u8, &linux_entry.d_name)); + const name = mem.toSlice(u8, @ptrCast([*]u8, &linux_entry.d_name)); // skip . and .. entries if (mem.eql(u8, name, ".") or mem.eql(u8, name, "..")) { @@ -709,7 +709,7 @@ pub fn readLinkC(pathname: [*]const u8, buffer: *[os.PATH_MAX]u8) ![]u8 { return os.readlinkC(pathname, buffer); } -pub const OpenSelfExeError = error{}; +pub const OpenSelfExeError = os.OpenError || os.windows.CreateFileError; pub fn openSelfExe() OpenSelfExeError!File { if (os.linux.is_the_target) { diff --git a/std/fs/file.zig b/std/fs/file.zig index 8de5d5eb3e..7cbbd4a4f0 100644 --- a/std/fs/file.zig +++ b/std/fs/file.zig @@ -307,28 +307,4 @@ pub const File = struct { return self.file.getPos(); } }; - - pub fn stdout() !File { - if (windows.is_the_target) { - const handle = try windows.GetStdHandle(windows.STD_OUTPUT_HANDLE); - return openHandle(handle); - } - return openHandle(os.STDOUT_FILENO); - } - - pub fn stderr() !File { - if (windows.is_the_target) { - const handle = try windows.GetStdHandle(windows.STD_ERROR_HANDLE); - return openHandle(handle); - } - return openHandle(os.STDERR_FILENO); - } - - pub fn stdin() !File { - if (windows.is_the_target) { - const handle = try windows.GetStdHandle(windows.STD_INPUT_HANDLE); - return openHandle(handle); - } - return openHandle(os.STDIN_FILENO); - } }; diff --git a/std/fs/get_app_data_dir.zig b/std/fs/get_app_data_dir.zig index 23dbaad86d..713b40b22b 100644 --- a/std/fs/get_app_data_dir.zig +++ b/std/fs/get_app_data_dir.zig @@ -2,6 +2,7 @@ const std = @import("../std.zig"); const builtin = @import("builtin"); const unicode = std.unicode; const mem = std.mem; +const fs = std.fs; const os = std.os; pub const GetAppDataDirError = error{ @@ -15,7 +16,7 @@ pub fn getAppDataDir(allocator: *mem.Allocator, appname: []const u8) GetAppDataD switch (builtin.os) { .windows => { var dir_path_ptr: [*]u16 = undefined; - switch (os.windows.SHGetKnownFolderPath( + switch (os.windows.shell32.SHGetKnownFolderPath( &os.windows.FOLDERID_LocalAppData, os.windows.KF_FLAG_CREATE, null, @@ -30,25 +31,25 @@ pub fn getAppDataDir(allocator: *mem.Allocator, appname: []const u8) GetAppDataD error.OutOfMemory => return error.OutOfMemory, }; defer allocator.free(global_dir); - return os.path.join(allocator, [][]const u8{ global_dir, appname }); + return fs.path.join(allocator, [][]const u8{ global_dir, appname }); }, os.windows.E_OUTOFMEMORY => return error.OutOfMemory, else => return error.AppDataDirUnavailable, } }, .macosx => { - const home_dir = os.getEnvPosix("HOME") orelse { + const home_dir = os.getenv("HOME") orelse { // TODO look in /etc/passwd return error.AppDataDirUnavailable; }; - return os.path.join(allocator, [][]const u8{ home_dir, "Library", "Application Support", appname }); + return fs.path.join(allocator, [][]const u8{ home_dir, "Library", "Application Support", appname }); }, .linux, .freebsd, .netbsd => { - const home_dir = os.getEnvPosix("HOME") orelse { + const home_dir = os.getenv("HOME") orelse { // TODO look in /etc/passwd return error.AppDataDirUnavailable; }; - return os.path.join(allocator, [][]const u8{ home_dir, ".local", "share", appname }); + return fs.path.join(allocator, [][]const u8{ home_dir, ".local", "share", appname }); }, else => @compileError("Unsupported OS"), } diff --git a/std/fs/path.zig b/std/fs/path.zig index b82ddc8117..217bb44327 100644 --- a/std/fs/path.zig +++ b/std/fs/path.zig @@ -1,16 +1,14 @@ -const std = @import("../std.zig"); const builtin = @import("builtin"); -const Os = builtin.Os; +const std = @import("../std.zig"); const debug = std.debug; const assert = debug.assert; const testing = std.testing; const mem = std.mem; const fmt = std.fmt; const Allocator = mem.Allocator; -const os = std.os; const math = std.math; -const windows = os.windows; -const cstr = std.cstr; +const windows = std.os.windows; +const fs = std.fs; pub const sep_windows = '\\'; pub const sep_posix = '/'; @@ -392,7 +390,7 @@ pub fn resolve(allocator: *Allocator, paths: []const []const u8) ![]u8 { pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 { if (paths.len == 0) { assert(windows.is_the_target); // resolveWindows called on non windows can't use getCwd - return os.getCwdAlloc(allocator); + return fs.getCwdAlloc(allocator); } // determine which disk designator we will result with, if any @@ -487,7 +485,7 @@ pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 { }, WindowsPath.Kind.None => { assert(windows.is_the_target); // resolveWindows called on non windows can't use getCwd - const cwd = try os.getCwdAlloc(allocator); + const cwd = try fs.getCwdAlloc(allocator); defer allocator.free(cwd); const parsed_cwd = windowsParsePath(cwd); result = try allocator.alloc(u8, max_size + parsed_cwd.disk_designator.len + 1); @@ -503,7 +501,7 @@ pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 { } else { assert(windows.is_the_target); // resolveWindows called on non windows can't use getCwd // TODO call get cwd for the result_disk_designator instead of the global one - const cwd = try os.getCwdAlloc(allocator); + const cwd = try fs.getCwdAlloc(allocator); defer allocator.free(cwd); result = try allocator.alloc(u8, max_size + cwd.len + 1); @@ -573,7 +571,7 @@ pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 { pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 { if (paths.len == 0) { assert(!windows.is_the_target); // resolvePosix called on windows can't use getCwd - return os.getCwdAlloc(allocator); + return fs.getCwdAlloc(allocator); } var first_index: usize = 0; @@ -595,7 +593,7 @@ pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 { result = try allocator.alloc(u8, max_size); } else { assert(!windows.is_the_target); // resolvePosix called on windows can't use getCwd - const cwd = try os.getCwdAlloc(allocator); + const cwd = try fs.getCwdAlloc(allocator); defer allocator.free(cwd); result = try allocator.alloc(u8, max_size + cwd.len + 1); mem.copy(u8, result, cwd); @@ -634,7 +632,7 @@ pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 { } test "resolve" { - const cwd = try os.getCwdAlloc(debug.global_allocator); + const cwd = try fs.getCwdAlloc(debug.global_allocator); if (windows.is_the_target) { if (windowsParsePath(cwd).kind == WindowsPath.Kind.Drive) { cwd[0] = asciiUpper(cwd[0]); @@ -648,7 +646,7 @@ test "resolve" { test "resolveWindows" { if (windows.is_the_target) { - const cwd = try os.getCwdAlloc(debug.global_allocator); + const cwd = try fs.getCwdAlloc(debug.global_allocator); const parsed_cwd = windowsParsePath(cwd); { const result = testResolveWindows([][]const u8{ "/usr/local", "lib\\zig\\std\\array_list.zig" }); diff --git a/std/heap.zig b/std/heap.zig index 4ff36f3f5f..1abe1da4fb 100644 --- a/std/heap.zig +++ b/std/heap.zig @@ -103,7 +103,7 @@ pub const DirectAllocator = struct { return @ptrCast([*]u8, final_addr)[0..n]; } - const alloc_size = if (alignment <= os.page_size) n else n + alignment; + const alloc_size = if (alignment <= mem.page_size) n else n + alignment; const addr = os.mmap( null, alloc_size, @@ -123,7 +123,7 @@ pub const DirectAllocator = struct { if (unused_start_len != 0) { os.munmap(addr, unused_start_len); } - const aligned_end_addr = std.mem.alignForward(aligned_addr + n, os.page_size); + const aligned_end_addr = std.mem.alignForward(aligned_addr + n, mem.page_size); const unused_end_len = addr + alloc_size - aligned_end_addr; if (unused_end_len != 0) { os.munmap(aligned_end_addr, unused_end_len); @@ -147,7 +147,7 @@ pub const DirectAllocator = struct { const base_addr = @ptrToInt(old_mem.ptr); const old_addr_end = base_addr + old_mem.len; const new_addr_end = base_addr + new_size; - const new_addr_end_rounded = mem.alignForward(new_addr_end, os.page_size); + const new_addr_end_rounded = mem.alignForward(new_addr_end, mem.page_size); if (old_addr_end > new_addr_end_rounded) { // For shrinking that is not releasing, we will only // decommit the pages not needed anymore. @@ -163,7 +163,7 @@ pub const DirectAllocator = struct { const base_addr = @ptrToInt(old_mem.ptr); const old_addr_end = base_addr + old_mem.len; const new_addr_end = base_addr + new_size; - const new_addr_end_rounded = mem.alignForward(new_addr_end, os.page_size); + const new_addr_end_rounded = mem.alignForward(new_addr_end, mem.page_size); if (old_addr_end > new_addr_end_rounded) { os.munmap(new_addr_end_rounded, old_addr_end - new_addr_end_rounded); } @@ -196,9 +196,9 @@ pub const DirectAllocator = struct { } const old_addr_end = base_addr + old_mem.len; - const old_addr_end_rounded = mem.alignForward(old_addr_end, os.page_size); + const old_addr_end_rounded = mem.alignForward(old_addr_end, mem.page_size); const new_addr_end = base_addr + new_size; - const new_addr_end_rounded = mem.alignForward(new_addr_end, os.page_size); + const new_addr_end_rounded = mem.alignForward(new_addr_end, mem.page_size); if (new_addr_end_rounded == old_addr_end_rounded) { // The reallocation fits in the already allocated pages. return @ptrCast([*]u8, old_mem.ptr)[0..new_size]; @@ -374,7 +374,7 @@ pub const ArenaAllocator = struct { var len = prev_len; while (true) { len += len / 2; - len += os.page_size - @rem(len, os.page_size); + len += mem.page_size - @rem(len, mem.page_size); if (len >= actual_min_size) break; } const buf = try self.child_allocator.alignedAlloc(u8, @alignOf(BufNode), len); @@ -520,11 +520,11 @@ const WasmAllocator = struct { const adjusted_index = self.end_index + (adjusted_addr - addr); const new_end_index = adjusted_index + size; - if (new_end_index > self.num_pages * os.page_size) { - const required_memory = new_end_index - (self.num_pages * os.page_size); + if (new_end_index > self.num_pages * mem.page_size) { + const required_memory = new_end_index - (self.num_pages * mem.page_size); - var num_pages: usize = required_memory / os.page_size; - if (required_memory % os.page_size != 0) { + var num_pages: usize = required_memory / mem.page_size; + if (required_memory % mem.page_size != 0) { num_pages += 1; } @@ -553,14 +553,14 @@ const WasmAllocator = struct { // Initialize start_ptr at the first realloc if (self.num_pages == 0) { - self.start_ptr = @intToPtr([*]u8, @intCast(usize, @"llvm.wasm.memory.size.i32"(0)) * os.page_size); + self.start_ptr = @intToPtr([*]u8, @intCast(usize, @"llvm.wasm.memory.size.i32"(0)) * mem.page_size); } if (is_last_item(allocator, old_mem, new_align)) { const start_index = self.end_index - old_mem.len; const new_end_index = start_index + new_size; - if (new_end_index > self.num_pages * os.page_size) { + if (new_end_index > self.num_pages * mem.page_size) { _ = try alloc(allocator, new_end_index - self.end_index, new_align); } const result = self.start_ptr[start_index..new_end_index]; @@ -876,10 +876,10 @@ fn testAllocatorAligned(allocator: *mem.Allocator, comptime alignment: u29) !voi fn testAllocatorLargeAlignment(allocator: *mem.Allocator) mem.Allocator.Error!void { //Maybe a platform's page_size is actually the same as or // very near usize? - if (os.page_size << 2 > maxInt(usize)) return; + if (mem.page_size << 2 > maxInt(usize)) return; const USizeShift = @IntType(false, std.math.log2(usize.bit_count)); - const large_align = u29(os.page_size << 2); + const large_align = u29(mem.page_size << 2); var align_mask: usize = undefined; _ = @shlWithOverflow(usize, ~usize(0), USizeShift(@ctz(u29, large_align)), &align_mask); @@ -906,7 +906,7 @@ fn testAllocatorAlignedShrink(allocator: *mem.Allocator) mem.Allocator.Error!voi var debug_buffer: [1000]u8 = undefined; const debug_allocator = &FixedBufferAllocator.init(&debug_buffer).allocator; - const alloc_size = os.page_size * 2 + 50; + const alloc_size = mem.page_size * 2 + 50; var slice = try allocator.alignedAlloc(u8, 16, alloc_size); defer allocator.free(slice); @@ -915,7 +915,7 @@ fn testAllocatorAlignedShrink(allocator: *mem.Allocator) mem.Allocator.Error!voi // which is 16 pages, hence the 32. This test may require to increase // the size of the allocations feeding the `allocator` parameter if they // fail, because of this high over-alignment we want to have. - while (@ptrToInt(slice.ptr) == mem.alignForward(@ptrToInt(slice.ptr), os.page_size * 32)) { + while (@ptrToInt(slice.ptr) == mem.alignForward(@ptrToInt(slice.ptr), mem.page_size * 32)) { try stuff_to_free.append(slice); slice = try allocator.alignedAlloc(u8, 16, alloc_size); } @@ -926,7 +926,7 @@ fn testAllocatorAlignedShrink(allocator: *mem.Allocator) mem.Allocator.Error!voi slice[60] = 0x34; // realloc to a smaller size but with a larger alignment - slice = try allocator.alignedRealloc(slice, os.page_size * 32, alloc_size / 2); + slice = try allocator.alignedRealloc(slice, mem.page_size * 32, alloc_size / 2); testing.expect(slice[0] == 0x12); testing.expect(slice[60] == 0x34); } diff --git a/std/io.zig b/std/io.zig index 5ca011cb4c..41dc38f245 100644 --- a/std/io.zig +++ b/std/io.zig @@ -15,8 +15,31 @@ const fmt = std.fmt; const File = std.fs.File; const testing = std.testing; -const is_posix = builtin.os != builtin.Os.windows; -const is_windows = builtin.os == builtin.Os.windows; +pub const GetStdIoError = os.windows.GetStdHandleError; + +pub fn getStdOut() GetStdIoError!File { + if (os.windows.is_the_target) { + const handle = try os.windows.GetStdHandle(os.windows.STD_OUTPUT_HANDLE); + return File.openHandle(handle); + } + return File.openHandle(os.STDOUT_FILENO); +} + +pub fn getStdErr() GetStdIoError!File { + if (os.windows.is_the_target) { + const handle = try os.windows.GetStdHandle(os.windows.STD_ERROR_HANDLE); + return File.openHandle(handle); + } + return File.openHandle(os.STDERR_FILENO); +} + +pub fn getStdIn() GetStdIoError!File { + if (os.windows.is_the_target) { + const handle = try os.windows.GetStdHandle(os.windows.STD_INPUT_HANDLE); + return File.openHandle(handle); + } + return File.openHandle(os.STDIN_FILENO); +} pub const SeekableStream = @import("io/seekable_stream.zig").SeekableStream; pub const SliceSeekableInStream = @import("io/seekable_stream.zig").SliceSeekableInStream; diff --git a/std/mem.zig b/std/mem.zig index 4f7ea768d7..643d297191 100644 --- a/std/mem.zig +++ b/std/mem.zig @@ -1462,21 +1462,3 @@ test "std.mem.alignForward" { testing.expect(alignForward(16, 8) == 16); testing.expect(alignForward(17, 8) == 24); } - -pub fn getBaseAddress() usize { - switch (builtin.os) { - .linux => { - const base = std.os.system.getauxval(std.elf.AT_BASE); - if (base != 0) { - return base; - } - const phdr = std.os.system.getauxval(std.elf.AT_PHDR); - return phdr - @sizeOf(std.elf.Ehdr); - }, - .macosx, .freebsd, .netbsd => { - return @ptrToInt(&std.c._mh_execute_header); - }, - .windows => return @ptrToInt(windows.kernel32.GetModuleHandleW(null)), - else => @compileError("Unsupported OS"), - } -} diff --git a/std/os.zig b/std/os.zig index 3b29570fcd..ef4d695aff 100644 --- a/std/os.zig +++ b/std/os.zig @@ -8,7 +8,7 @@ // cross platform abstracting. // * When there exists a corresponding libc function and linking libc, the libc // implementation is used. Exceptions are made for known buggy areas of libc. -// On Linux libc can be side-stepped by using `std.os.linux.sys`. +// On Linux libc can be side-stepped by using `std.os.linux` directly. // * For Windows, this file represents the API that libc would provide for // Windows. For thin wrappers around Windows-specific APIs, see `std.os.windows`. // Note: The Zig standard library does not support POSIX thread cancellation, and @@ -16,7 +16,9 @@ const std = @import("std.zig"); const builtin = @import("builtin"); +const assert = std.debug.assert; const math = std.math; +const mem = std.mem; const MAX_PATH_BYTES = std.fs.MAX_PATH_BYTES; comptime { @@ -46,9 +48,14 @@ pub const system = if (builtin.link_libc) std.c else switch (builtin.os) { pub use @import("os/bits.zig"); -/// See also `getenv`. +/// See also `getenv`. Populated by startup code before main(). pub var environ: [][*]u8 = undefined; +/// Populated by startup code before main(). +/// Not available on Windows. See `std.process.args` +/// for obtaining the process arguments. +pub var argv: [][*]u8 = undefined; + /// To obtain errno, call this function with the return value of the /// system function call. For some systems this will obtain the value directly /// from the return code; for others it will use a thread-local errno variable. @@ -103,7 +110,7 @@ pub fn getrandom(buf: []u8) GetRandomError!void { } } if (wasi.is_the_target) { - switch (os.wasi.random_get(buf.ptr, buf.len)) { + switch (wasi.random_get(buf.ptr, buf.len)) { 0 => return, else => |err| return unexpectedErrno(err), } @@ -138,15 +145,15 @@ pub fn abort() noreturn { while (true) {} } - raise(SIGABRT); + raise(SIGABRT) catch {}; // TODO the rest of the implementation of abort() from musl libc here - raise(SIGKILL); + raise(SIGKILL) catch {}; exit(127); } -pub const RaiseError = error{}; +pub const RaiseError = error{Unexpected}; pub fn raise(sig: u8) RaiseError!void { if (builtin.link_libc) { @@ -163,19 +170,19 @@ pub fn raise(sig: u8) RaiseError!void { } } - if (windows.is_the_target) { - @compileError("TODO implement std.os.raise for Windows"); + if (linux.is_the_target) { + var set: linux.sigset_t = undefined; + linux.blockAppSignals(&set); + const tid = linux.syscall0(linux.SYS_gettid); + const rc = linux.syscall2(linux.SYS_tkill, tid, sig); + linux.restoreSignals(&set); + switch (errno(rc)) { + 0 => return, + else => |err| return unexpectedErrno(err), + } } - var set: system.sigset_t = undefined; - system.blockAppSignals(&set); - const tid = system.syscall0(system.SYS_gettid); - const rc = system.syscall2(system.SYS_tkill, tid, sig); - system.restoreSignals(&set); - switch (errno(rc)) { - 0 => return, - else => |err| return unexpectedErrno(err), - } + @compileError("std.os.raise unimplemented for this target"); } pub const KillError = error{ @@ -229,13 +236,13 @@ pub fn read(fd: fd_t, buf: []u8) ReadError!usize { } if (wasi.is_the_target and !builtin.link_libc) { - const iovs = [1]was.iovec_t{wasi.iovec_t{ - .buf = buf.ptr, - .buf_len = buf.len, + const iovs = [1]iovec{iovec{ + .iov_base = buf.ptr, + .iov_len = buf.len, }}; var nread: usize = undefined; - switch (fd_read(fd, &iovs, iovs.len, &nread)) { + switch (wasi.fd_read(fd, &iovs, iovs.len, &nread)) { 0 => return nread, else => |err| return unexpectedErrno(err), } @@ -277,7 +284,7 @@ pub fn read(fd: fd_t, buf: []u8) ReadError!usize { /// This function is for blocking file descriptors only. For non-blocking, see /// `preadvAsync`. pub fn preadv(fd: fd_t, iov: [*]const iovec, count: usize, offset: u64) ReadError!usize { - if (os.darwin.is_the_target) { + if (darwin.is_the_target) { // Darwin does not have preadv but it does have pread. var off: usize = 0; var iov_i: usize = 0; @@ -353,12 +360,12 @@ pub fn write(fd: fd_t, bytes: []const u8) WriteError!void { } if (wasi.is_the_target and !builtin.link_libc) { - const ciovs = [1]wasi.ciovec_t{wasi.ciovec_t{ - .buf = bytes.ptr, - .buf_len = bytes.len, + const ciovs = [1]iovec_const{iovec_const{ + .iov_base = bytes.ptr, + .iov_len = bytes.len, }}; var nwritten: usize = undefined; - switch (fd_write(fd, &ciovs, ciovs.len, &nwritten)) { + switch (wasi.fd_write(fd, &ciovs, ciovs.len, &nwritten)) { 0 => return, else => |err| return unexpectedErrno(err), } @@ -538,29 +545,29 @@ pub fn dup2(old_fd: fd_t, new_fd: fd_t) !void { /// `argv[0]` is the executable path. /// This function also uses the PATH environment variable to get the full path to the executable. /// TODO provide execveC which does not take an allocator -pub fn execve(allocator: *Allocator, argv: []const []const u8, env_map: *const BufMap) !void { - const argv_buf = try allocator.alloc(?[*]u8, argv.len + 1); +pub fn execve(allocator: *mem.Allocator, argv_slice: []const []const u8, env_map: *const std.BufMap) !void { + const argv_buf = try allocator.alloc(?[*]u8, argv_slice.len + 1); mem.set(?[*]u8, argv_buf, null); defer { for (argv_buf) |arg| { - const arg_buf = if (arg) |ptr| cstr.toSlice(ptr) else break; + const arg_buf = if (arg) |ptr| mem.toSlice(u8, ptr) else break; allocator.free(arg_buf); } allocator.free(argv_buf); } - for (argv) |arg, i| { + for (argv_slice) |arg, i| { const arg_buf = try allocator.alloc(u8, arg.len + 1); @memcpy(arg_buf.ptr, arg.ptr, arg.len); arg_buf[arg.len] = 0; argv_buf[i] = arg_buf.ptr; } - argv_buf[argv.len] = null; + argv_buf[argv_slice.len] = null; const envp_buf = try createNullDelimitedEnvMap(allocator, env_map); defer freeNullDelimitedEnvMap(allocator, envp_buf); - const exe_path = argv[0]; + const exe_path = argv_slice[0]; if (mem.indexOfScalar(u8, exe_path, '/') != null) { return execveErrnoToErr(errno(system.execve(argv_buf[0].?, argv_buf.ptr, envp_buf.ptr))); } @@ -593,7 +600,7 @@ pub fn execve(allocator: *Allocator, argv: []const []const u8, env_map: *const B return execveErrnoToErr(err); } -pub fn createNullDelimitedEnvMap(allocator: *Allocator, env_map: *const BufMap) ![]?[*]u8 { +pub fn createNullDelimitedEnvMap(allocator: *mem.Allocator, env_map: *const std.BufMap) ![]?[*]u8 { const envp_count = env_map.count(); const envp_buf = try allocator.alloc(?[*]u8, envp_count + 1); mem.set(?[*]u8, envp_buf, null); @@ -616,9 +623,9 @@ pub fn createNullDelimitedEnvMap(allocator: *Allocator, env_map: *const BufMap) return envp_buf; } -pub fn freeNullDelimitedEnvMap(allocator: *Allocator, envp_buf: []?[*]u8) void { +pub fn freeNullDelimitedEnvMap(allocator: *mem.Allocator, envp_buf: []?[*]u8) void { for (envp_buf) |env| { - const env_buf = if (env) |ptr| ptr[0 .. cstr.len(ptr) + 1] else break; + const env_buf = if (env) |ptr| ptr[0 .. mem.len(u8, ptr) + 1] else break; allocator.free(env_buf); } allocator.free(envp_buf); @@ -633,6 +640,8 @@ pub const ExecveError = error{ FileNotFound, NotDir, FileBusy, + ProcessFdQuotaExceeded, + NameTooLong, Unexpected, }; @@ -703,17 +712,17 @@ pub fn getcwd(out_buffer: []u8) GetCwdError![]u8 { } const err = if (builtin.link_libc) blk: { - break :blk if (system.getcwd(out_buffer.ptr, out_buffer.len)) |_| 0 else system._errno().*; + break :blk if (std.c.getcwd(out_buffer.ptr, out_buffer.len)) |_| 0 else std.c._errno().*; } else blk: { - break :blk errno(system.getcwd(out_buffer, out_buffer.len)); + break :blk errno(system.getcwd(out_buffer.ptr, out_buffer.len)); }; switch (err) { - 0 => return mem.toSlice(u8, out_buffer), + 0 => return mem.toSlice(u8, out_buffer.ptr), EFAULT => unreachable, EINVAL => unreachable, ENOENT => return error.CurrentWorkingDirectoryUnlinked, ERANGE => return error.NameTooLong, - else => |err| return unexpectedErrno(err), + else => return unexpectedErrno(err), } } @@ -1711,8 +1720,8 @@ pub const FStatError = error{ pub fn fstat(fd: fd_t) FStatError!Stat { var stat: Stat = undefined; - if (os.darwin.is_the_target) { - switch (errno(system.@"fstat$INODE64"(fd, buf))) { + if (darwin.is_the_target) { + switch (errno(system.@"fstat$INODE64"(fd, &stat))) { 0 => return stat, EBADF => unreachable, // Always a race condition. ENOMEM => return error.SystemResources, @@ -1877,7 +1886,7 @@ pub const ForkError = error{ pub fn fork() ForkError!pid_t { const rc = system.fork(); switch (errno(rc)) { - 0 => return rc, + 0 => return @intCast(pid_t, rc), EAGAIN => return error.SystemResources, ENOMEM => return error.SystemResources, else => |err| return unexpectedErrno(err), @@ -1891,6 +1900,7 @@ pub const MMapError = error{ SystemFdQuotaExceeded, MemoryMappingNotSupported, OutOfMemory, + Unexpected, }; /// Map files or devices into memory. @@ -1992,6 +2002,7 @@ pub fn accessC(path: [*]const u8, mode: u32) AccessError!void { pub const PipeError = error{ SystemFdQuotaExceeded, ProcessFdQuotaExceeded, + Unexpected, }; /// Creates a unidirectional data channel that can be used for interprocess communication. @@ -2065,18 +2076,6 @@ pub fn gettimeofday(tv: ?*timeval, tz: ?*timezone) void { } } -pub fn nanosleep(req: timespec) void { - var rem = req; - while (true) { - switch (errno(system.nanosleep(&rem, &rem))) { - 0 => return, - EINVAL => unreachable, // Invalid parameters. - EFAULT => unreachable, - EINTR => continue, - } - } -} - pub const SeekError = error{ Unseekable, Unexpected, diff --git a/std/os/bits/linux.zig b/std/os/bits/linux.zig index f40beeaf68..b4f51f3518 100644 --- a/std/os/bits/linux.zig +++ b/std/os/bits/linux.zig @@ -692,12 +692,12 @@ pub const winsize = extern struct { ws_ypixel: u16, }; -const NSIG = 65; -const sigset_t = [128 / @sizeOf(usize)]usize; -const all_mask = []u32{ 0xffffffff, 0xffffffff }; -const app_mask = []u32{ 0xfffffffc, 0x7fffffff }; +pub const NSIG = 65; +pub const sigset_t = [128 / @sizeOf(usize)]usize; +pub const all_mask = []u32{ 0xffffffff, 0xffffffff }; +pub const app_mask = []u32{ 0xfffffffc, 0x7fffffff }; -const k_sigaction = extern struct { +pub const k_sigaction = extern struct { handler: extern fn (i32) void, flags: usize, restorer: extern fn () void, diff --git a/std/os/bits/wasi.zig b/std/os/bits/wasi.zig index 4a8be6890a..cf6542a159 100644 --- a/std/os/bits/wasi.zig +++ b/std/os/bits/wasi.zig @@ -13,10 +13,7 @@ pub const ADVICE_WILLNEED: advice_t = 3; pub const ADVICE_DONTNEED: advice_t = 4; pub const ADVICE_NOREUSE: advice_t = 5; -pub const ciovec_t = extern struct { - buf: [*]const u8, - buf_len: usize, -}; +pub const ciovec_t = iovec_const; pub const clockid_t = u32; pub const CLOCK_REALTIME: clockid_t = 0; @@ -186,10 +183,7 @@ pub const FILESTAT_SET_MTIM_NOW: fstflags_t = 0x0008; pub const inode_t = u64; -pub const iovec_t = extern struct { - buf: [*]u8, - buf_len: usize, -}; +pub const iovec_t = iovec; pub const linkcount_t = u32; diff --git a/std/os/linux.zig b/std/os/linux.zig index 88a2980129..958c699dc5 100644 --- a/std/os/linux.zig +++ b/std/os/linux.zig @@ -20,6 +20,7 @@ pub use switch (builtin.arch) { else => struct {}, }; pub use @import("bits.zig"); +pub const tls = @import("linux/tls.zig"); /// Set by startup code, used by `getauxval`. pub var elf_aux_maybe: ?[*]std.elf.Auxv = null; @@ -539,15 +540,15 @@ pub fn sigaction(sig: u6, noalias act: *const Sigaction, noalias oact: ?*Sigacti return 0; } -fn blockAllSignals(set: *sigset_t) void { +pub fn blockAllSignals(set: *sigset_t) void { _ = syscall4(SYS_rt_sigprocmask, SIG_BLOCK, @ptrToInt(&all_mask), @ptrToInt(set), NSIG / 8); } -fn blockAppSignals(set: *sigset_t) void { +pub fn blockAppSignals(set: *sigset_t) void { _ = syscall4(SYS_rt_sigprocmask, SIG_BLOCK, @ptrToInt(&app_mask), @ptrToInt(set), NSIG / 8); } -fn restoreSignals(set: *sigset_t) void { +pub fn restoreSignals(set: *sigset_t) void { _ = syscall4(SYS_rt_sigprocmask, SIG_SETMASK, @ptrToInt(set), 0, NSIG / 8); } diff --git a/std/os/linux/vdso.zig b/std/os/linux/vdso.zig index efd69d8542..44cd0271cd 100644 --- a/std/os/linux/vdso.zig +++ b/std/os/linux/vdso.zig @@ -1,7 +1,6 @@ const std = @import("../../std.zig"); const elf = std.elf; const linux = std.os.linux; -const cstr = std.cstr; const mem = std.mem; const maxInt = std.math.maxInt; @@ -66,7 +65,7 @@ pub fn lookup(vername: []const u8, name: []const u8) usize { if (0 == (u32(1) << @intCast(u5, syms[i].st_info & 0xf) & OK_TYPES)) continue; if (0 == (u32(1) << @intCast(u5, syms[i].st_info >> 4) & OK_BINDS)) continue; if (0 == syms[i].st_shndx) continue; - if (!mem.eql(u8, name, cstr.toSliceConst(strings + syms[i].st_name))) continue; + if (!mem.eql(u8, name, mem.toSliceConst(u8, strings + syms[i].st_name))) continue; if (maybe_versym) |versym| { if (!checkver(maybe_verdef.?, versym[i], vername, strings)) continue; @@ -88,5 +87,5 @@ fn checkver(def_arg: *elf.Verdef, vsym_arg: i32, vername: []const u8, strings: [ def = @intToPtr(*elf.Verdef, @ptrToInt(def) + def.vd_next); } const aux = @intToPtr(*elf.Verdaux, @ptrToInt(def) + def.vd_aux); - return mem.eql(u8, vername, cstr.toSliceConst(strings + aux.vda_name)); + return mem.eql(u8, vername, mem.toSliceConst(u8, strings + aux.vda_name)); } diff --git a/std/os/test.zig b/std/os/test.zig index d4bdb06278..f7d40e1f03 100644 --- a/std/os/test.zig +++ b/std/os/test.zig @@ -3,6 +3,7 @@ const os = std.os; const testing = std.testing; const expect = std.testing.expect; const io = std.io; +const fs = std.fs; const mem = std.mem; const File = std.fs.File; const Thread = std.Thread; @@ -14,9 +15,9 @@ const AtomicRmwOp = builtin.AtomicRmwOp; const AtomicOrder = builtin.AtomicOrder; test "makePath, put some files in it, deleteTree" { - try os.makePath(a, "os_test_tmp" ++ os.path.sep_str ++ "b" ++ os.path.sep_str ++ "c"); - try io.writeFile("os_test_tmp" ++ os.path.sep_str ++ "b" ++ os.path.sep_str ++ "c" ++ os.path.sep_str ++ "file.txt", "nonsense"); - try io.writeFile("os_test_tmp" ++ os.path.sep_str ++ "b" ++ os.path.sep_str ++ "file2.txt", "blah"); + try os.makePath(a, "os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "c"); + try io.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "c" ++ fs.path.sep_str ++ "file.txt", "nonsense"); + try io.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "file2.txt", "blah"); try os.deleteTree(a, "os_test_tmp"); if (os.Dir.open(a, "os_test_tmp")) |dir| { @panic("expected error"); @@ -27,14 +28,14 @@ test "makePath, put some files in it, deleteTree" { test "access file" { try os.makePath(a, "os_test_tmp"); - if (File.access("os_test_tmp" ++ os.path.sep_str ++ "file.txt")) |ok| { + if (File.access("os_test_tmp" ++ fs.path.sep_str ++ "file.txt")) |ok| { @panic("expected error"); } else |err| { expect(err == error.FileNotFound); } - try io.writeFile("os_test_tmp" ++ os.path.sep_str ++ "file.txt", ""); - try File.access("os_test_tmp" ++ os.path.sep_str ++ "file.txt"); + try io.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "file.txt", ""); + try File.access("os_test_tmp" ++ fs.path.sep_str ++ "file.txt"); try os.deleteTree(a, "os_test_tmp"); } diff --git a/std/os/windows.zig b/std/os/windows.zig index c18adc66fa..fbb8ff42c5 100644 --- a/std/os/windows.zig +++ b/std/os/windows.zig @@ -4,6 +4,7 @@ // * When null-terminated or UTF16LE byte buffers are required, provide APIs which accept // slices as well as APIs which accept null-terminated UTF16LE byte buffers. +const builtin = @import("builtin"); const std = @import("../std.zig"); const assert = std.debug.assert; const maxInt = std.math.maxInt; @@ -1103,6 +1104,42 @@ pub fn VirtualFree(lpAddress: ?LPVOID, dwSize: usize, dwFreeType: DWORD) void { assert(kernel32.VirtualFree(lpAddress, dwSize, dwFreeType) != 0); } +pub const SetConsoleTextAttributeError = error{Unexpected}; + +pub fn SetConsoleTextAttribute(hConsoleOutput: HANDLE, wAttributes: WORD) SetConsoleTextAttributeError!void { + if (kernel32.SetConsoleTextAttribute(hConsoleOutput, wAttributes) == 0) { + switch (kernel32.GetLastError()) { + else => |err| return unexpectedError(err), + } + } +} + +pub const GetEnvironmentStringsError = error{OutOfMemory}; + +pub fn GetEnvironmentStringsW() GetEnvironmentStringsError![*]u16 { + return kernel32.GetEnvironmentStringsW() orelse return error.OutOfMemory; +} + +pub fn FreeEnvironmentStringsW(penv: [*]u16) void { + assert(kernel32.FreeEnvironmentStringsW(penv) != 0); +} + +pub const GetEnvironmentVariableError = error{ + EnvironmentVariableNotFound, + Unexpected, +}; + +pub fn GetEnvironmentVariableW(lpName: LPWSTR, lpBuffer: LPWSTR, nSize: DWORD) GetEnvironmentVariableError!DWORD { + const rc = kernel32.GetEnvironmentVariableW(lpName, lpBuffer, nSize); + if (rc == 0) { + switch (kernel32.GetLastError()) { + ERROR.ENVVAR_NOT_FOUND => return error.EnvironmentVariableNotFound, + else => |err| return unexpectedError(err), + } + } + return rc; +} + pub fn cStrToPrefixedFileW(s: [*]const u8) ![PATH_MAX_WIDE + 1]u16 { return sliceToPrefixedFileW(mem.toSliceConst(u8, s)); } @@ -1127,7 +1164,7 @@ pub fn sliceToPrefixedSuffixedFileW(s: []const u8, comptime suffix: []const u16) else => {}, } } - const start_index = if (mem.startsWith(u8, s, "\\\\") or !os.path.isAbsolute(s)) 0 else blk: { + const start_index = if (mem.startsWith(u8, s, "\\\\") or !std.fs.path.isAbsolute(s)) 0 else blk: { const prefix = []u16{ '\\', '\\', '?', '\\' }; mem.copy(u16, result[0..], prefix); break :blk prefix.len; diff --git a/std/process.zig b/std/process.zig index 1e29170e88..baccbccb10 100644 --- a/std/process.zig +++ b/std/process.zig @@ -1,13 +1,17 @@ +const builtin = @import("builtin"); const std = @import("std.zig"); const os = std.os; const BufMap = std.BufMap; const mem = std.mem; +const math = std.math; const Allocator = mem.Allocator; const assert = std.debug.assert; const testing = std.testing; pub const abort = os.abort; pub const exit = os.exit; +pub const changeCurDir = os.chdir; +pub const changeCurDirC = os.chdirC; /// Caller must free result when done. /// TODO make this go through libc when we have it @@ -15,9 +19,9 @@ pub fn getEnvMap(allocator: *Allocator) !BufMap { var result = BufMap.init(allocator); errdefer result.deinit(); - if (is_windows) { - const ptr = windows.GetEnvironmentStringsW() orelse return error.OutOfMemory; - defer assert(windows.FreeEnvironmentStringsW(ptr) != 0); + if (os.windows.is_the_target) { + const ptr = try os.windows.GetEnvironmentStringsW(); + defer os.windows.FreeEnvironmentStringsW(ptr); var i: usize = 0; while (true) { @@ -105,7 +109,7 @@ pub const GetEnvVarOwnedError = error{ /// Caller must free returned memory. /// TODO make this go through libc when we have it pub fn getEnvVarOwned(allocator: *mem.Allocator, key: []const u8) GetEnvVarOwnedError![]u8 { - if (is_windows) { + if (os.windows.is_the_target) { const key_with_null = try std.unicode.utf8ToUtf16LeWithNull(allocator, key); defer allocator.free(key_with_null); @@ -113,19 +117,15 @@ pub fn getEnvVarOwned(allocator: *mem.Allocator, key: []const u8) GetEnvVarOwned defer allocator.free(buf); while (true) { - const windows_buf_len = math.cast(windows.DWORD, buf.len) catch return error.OutOfMemory; - const result = windows.GetEnvironmentVariableW(key_with_null.ptr, buf.ptr, windows_buf_len); - - if (result == 0) { - const err = windows.GetLastError(); - return switch (err) { - windows.ERROR.ENVVAR_NOT_FOUND => error.EnvironmentVariableNotFound, - else => { - windows.unexpectedError(err) catch {}; - return error.EnvironmentVariableNotFound; - }, - }; - } + const windows_buf_len = math.cast(os.windows.DWORD, buf.len) catch return error.OutOfMemory; + const result = os.windows.GetEnvironmentVariableW( + key_with_null.ptr, + buf.ptr, + windows_buf_len, + ) catch |err| switch (err) { + error.Unexpected => return error.EnvironmentVariableNotFound, + else => return err, + }; if (result > buf.len) { buf = try allocator.realloc(buf, result); @@ -136,11 +136,11 @@ pub fn getEnvVarOwned(allocator: *mem.Allocator, key: []const u8) GetEnvVarOwned error.DanglingSurrogateHalf => return error.InvalidUtf8, error.ExpectedSecondSurrogateHalf => return error.InvalidUtf8, error.UnexpectedSecondSurrogateHalf => return error.InvalidUtf8, - error.OutOfMemory => return error.OutOfMemory, + else => return err, }; } } else { - const result = getEnvPosix(key) orelse return error.EnvironmentVariableNotFound; + const result = os.getenv(key) orelse return error.EnvironmentVariableNotFound; return mem.dupe(allocator, u8, result); } } @@ -157,16 +157,16 @@ pub const ArgIteratorPosix = struct { pub fn init() ArgIteratorPosix { return ArgIteratorPosix{ .index = 0, - .count = raw.len, + .count = os.argv.len, }; } pub fn next(self: *ArgIteratorPosix) ?[]const u8 { if (self.index == self.count) return null; - const s = raw[self.index]; + const s = os.argv[self.index]; self.index += 1; - return cstr.toSlice(s); + return mem.toSlice(u8, s); } pub fn skip(self: *ArgIteratorPosix) bool { @@ -175,10 +175,6 @@ pub const ArgIteratorPosix = struct { self.index += 1; return true; } - - /// This is marked as public but actually it's only meant to be used - /// internally by zig's startup code. - pub var raw: [][*]u8 = undefined; }; pub const ArgIteratorWindows = struct { @@ -191,7 +187,7 @@ pub const ArgIteratorWindows = struct { pub const NextError = error{OutOfMemory}; pub fn init() ArgIteratorWindows { - return initWithCmdLine(windows.GetCommandLineA()); + return initWithCmdLine(os.windows.kernel32.GetCommandLineA()); } pub fn initWithCmdLine(cmd_line: [*]const u8) ArgIteratorWindows { @@ -581,3 +577,21 @@ pub fn posixGetUserInfo(name: []const u8) !UserInfo { if (amt_read < buf.len) return error.UserNotFound; } } + +pub fn getBaseAddress() usize { + switch (builtin.os) { + .linux => { + const base = os.system.getauxval(std.elf.AT_BASE); + if (base != 0) { + return base; + } + const phdr = os.system.getauxval(std.elf.AT_PHDR); + return phdr - @sizeOf(std.elf.Ehdr); + }, + .macosx, .freebsd, .netbsd => { + return @ptrToInt(&std.c._mh_execute_header); + }, + .windows => return @ptrToInt(os.windows.kernel32.GetModuleHandleW(null)), + else => @compileError("Unsupported OS"), + } +} diff --git a/std/special/bootstrap.zig b/std/special/bootstrap.zig index 86228acb9f..bf14116edf 100644 --- a/std/special/bootstrap.zig +++ b/std/special/bootstrap.zig @@ -78,7 +78,7 @@ fn posixCallMainAndExit() noreturn { while (envp_optional[envp_count]) |_| : (envp_count += 1) {} const envp = @ptrCast([*][*]u8, envp_optional)[0..envp_count]; - if (builtin.os == builtin.Os.linux) { + if (builtin.os == .linux) { // Find the beginning of the auxiliary vector const auxv = @ptrCast([*]std.elf.Auxv, envp.ptr + envp_count + 1); std.os.linux.elf_aux_maybe = auxv; @@ -98,7 +98,7 @@ fn posixCallMainAndExit() noreturn { // This is marked inline because for some reason LLVM in release mode fails to inline it, // and we want fewer call frames in stack traces. inline fn callMainWithArgs(argc: usize, argv: [*][*]u8, envp: [][*]u8) u8 { - std.os.ArgIteratorPosix.raw = argv[0..argc]; + std.os.argv = argv[0..argc]; std.os.environ = envp; return callMain(); } diff --git a/std/special/build_runner.zig b/std/special/build_runner.zig index f9a322cbeb..a0a18d21d2 100644 --- a/std/special/build_runner.zig +++ b/std/special/build_runner.zig @@ -3,15 +3,15 @@ const std = @import("std"); const builtin = @import("builtin"); const io = std.io; const fmt = std.fmt; -const os = std.os; const Builder = std.build.Builder; const mem = std.mem; +const process = std.process; const ArrayList = std.ArrayList; const warn = std.debug.warn; const File = std.fs.File; pub fn main() !void { - var arg_it = os.args(); + var arg_it = process.args(); // Here we use an ArenaAllocator backed by a DirectAllocator because a build is a short-lived, // one shot program. We don't need to waste time freeing memory and finding places to squish @@ -139,7 +139,7 @@ pub fn main() !void { error.InvalidStepName => { return usageAndErr(&builder, true, try stderr_stream); }, - error.UncleanExit => os.exit(1), + error.UncleanExit => process.exit(1), else => return err, } }; @@ -215,7 +215,7 @@ fn usage(builder: *Builder, already_ran_build: bool, out_stream: var) !void { fn usageAndErr(builder: *Builder, already_ran_build: bool, out_stream: var) void { usage(builder, already_ran_build, out_stream) catch {}; - os.exit(1); + process.exit(1); } const UnwrapArgError = error{OutOfMemory}; diff --git a/std/thread.zig b/std/thread.zig index eac6b73f7d..53f42e555f 100644 --- a/std/thread.zig +++ b/std/thread.zig @@ -227,7 +227,7 @@ pub const Thread = struct { var tls_start_offset: usize = undefined; const mmap_len = blk: { // First in memory will be the stack, which grows downwards. - var l: usize = mem.alignForward(default_stack_size, os.page_size); + var l: usize = mem.alignForward(default_stack_size, mem.page_size); stack_end_offset = l; // Above the stack, so that it can be in the same mmap call, put the Thread object. l = mem.alignForward(l, @alignOf(Thread)); diff --git a/test/cli.zig b/test/cli.zig index 1e7f1d0a73..cee030e4db 100644 --- a/test/cli.zig +++ b/test/cli.zig @@ -1,7 +1,9 @@ const std = @import("std"); const builtin = @import("builtin"); -const os = std.os; const testing = std.testing; +const process = std.process; +const fs = std.fs; +const ChildProcess = std.ChildProcess; var a: *std.mem.Allocator = undefined; @@ -12,7 +14,7 @@ pub fn main() !void { var arena = std.heap.ArenaAllocator.init(&direct_allocator.allocator); defer arena.deinit(); - var arg_it = os.args(); + var arg_it = process.args(); // skip my own exe name _ = arg_it.skip(); @@ -27,9 +29,9 @@ pub fn main() !void { std.debug.warn("Expected second argument to be cache root directory path\n"); return error.InvalidArgs; }); - const zig_exe = try os.path.resolve(a, [][]const u8{zig_exe_rel}); + const zig_exe = try fs.path.resolve(a, [][]const u8{zig_exe_rel}); - const dir_path = try os.path.join(a, [][]const u8{ cache_root, "clitest" }); + const dir_path = try fs.path.join(a, [][]const u8{ cache_root, "clitest" }); const TestFn = fn ([]const u8, []const u8) anyerror!void; const test_fns = []TestFn{ testZigInitLib, @@ -37,8 +39,8 @@ pub fn main() !void { testGodboltApi, }; for (test_fns) |testFn| { - try os.deleteTree(a, dir_path); - try os.makeDir(dir_path); + try fs.deleteTree(a, dir_path); + try fs.makeDir(dir_path); try testFn(zig_exe, dir_path); } } @@ -58,15 +60,15 @@ fn printCmd(cwd: []const u8, argv: []const []const u8) void { std.debug.warn("\n"); } -fn exec(cwd: []const u8, argv: []const []const u8) !os.ChildProcess.ExecResult { +fn exec(cwd: []const u8, argv: []const []const u8) !ChildProcess.ExecResult { const max_output_size = 100 * 1024; - const result = os.ChildProcess.exec(a, argv, cwd, null, max_output_size) catch |err| { + const result = ChildProcess.exec(a, argv, cwd, null, max_output_size) catch |err| { std.debug.warn("The following command failed:\n"); printCmd(cwd, argv); return err; }; switch (result.term) { - os.ChildProcess.Term.Exited => |code| { + .Exited => |code| { if (code != 0) { std.debug.warn("The following command exited with error code {}:\n", code); printCmd(cwd, argv); @@ -97,10 +99,10 @@ fn testZigInitExe(zig_exe: []const u8, dir_path: []const u8) !void { } fn testGodboltApi(zig_exe: []const u8, dir_path: []const u8) anyerror!void { - if (builtin.os != builtin.Os.linux or builtin.arch != builtin.Arch.x86_64) return; + if (builtin.os != .linux or builtin.arch != .x86_64) return; - const example_zig_path = try os.path.join(a, [][]const u8{ dir_path, "example.zig" }); - const example_s_path = try os.path.join(a, [][]const u8{ dir_path, "example.s" }); + const example_zig_path = try fs.path.join(a, [][]const u8{ dir_path, "example.zig" }); + const example_s_path = try fs.path.join(a, [][]const u8{ dir_path, "example.s" }); try std.io.writeFile(example_zig_path, \\// Type your code here, or load an example. @@ -114,13 +116,13 @@ fn testGodboltApi(zig_exe: []const u8, dir_path: []const u8) anyerror!void { ); const args = [][]const u8{ - zig_exe, "build-obj", - "--cache-dir", dir_path, - "--name", "example", - "--output-dir", dir_path, - "--emit", "asm", - "-mllvm", "--x86-asm-syntax=intel", - "--strip", "--release-fast", + zig_exe, "build-obj", + "--cache-dir", dir_path, + "--name", "example", + "--output-dir", dir_path, + "--emit", "asm", + "-mllvm", "--x86-asm-syntax=intel", + "--strip", "--release-fast", example_zig_path, "--disable-gen-h", }; _ = try exec(dir_path, args); diff --git a/test/tests.zig b/test/tests.zig index a17938fc3c..061d4cb55f 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -2,11 +2,9 @@ const std = @import("std"); const debug = std.debug; const warn = debug.warn; const build = std.build; -const os = std.os; -const StdIo = os.ChildProcess.StdIo; -const Term = os.ChildProcess.Term; const Buffer = std.Buffer; const io = std.io; +const fs = std.fs; const mem = std.mem; const fmt = std.fmt; const ArrayList = std.ArrayList; @@ -30,19 +28,19 @@ const TestTarget = struct { const test_targets = []TestTarget{ TestTarget{ - .os = builtin.Os.linux, - .arch = builtin.Arch.x86_64, - .abi = builtin.Abi.gnu, + .os = .linux, + .arch = .x86_64, + .abi = .gnu, }, TestTarget{ - .os = builtin.Os.macosx, - .arch = builtin.Arch.x86_64, - .abi = builtin.Abi.gnu, + .os = .macosx, + .arch = .x86_64, + .abi = .gnu, }, TestTarget{ - .os = builtin.Os.windows, - .arch = builtin.Arch.x86_64, - .abi = builtin.Abi.msvc, + .os = .windows, + .arch = .x86_64, + .abi = .msvc, }, }; @@ -114,7 +112,7 @@ pub fn addCliTests(b: *build.Builder, test_filter: ?[]const u8, modes: []const M const exe = b.addExecutable("test-cli", "test/cli.zig"); const run_cmd = exe.run(); run_cmd.addArgs([][]const u8{ - os.path.realAlloc(b.allocator, b.zig_exe) catch unreachable, + fs.path.realAlloc(b.allocator, b.zig_exe) catch unreachable, b.pathFromRoot(b.cache_root), }); @@ -301,12 +299,12 @@ pub const CompareOutputContext = struct { warn("Test {}/{} {}...", self.test_index + 1, self.context.test_index, self.name); - const child = os.ChildProcess.init(args.toSliceConst(), b.allocator) catch unreachable; + const child = std.ChildProcess.init(args.toSliceConst(), b.allocator) catch unreachable; defer child.deinit(); - child.stdin_behavior = StdIo.Ignore; - child.stdout_behavior = StdIo.Pipe; - child.stderr_behavior = StdIo.Pipe; + child.stdin_behavior = .Ignore; + child.stdout_behavior = .Pipe; + child.stderr_behavior = .Pipe; child.env_map = b.env_map; child.spawn() catch |err| debug.panic("Unable to spawn {}: {}\n", full_exe_path, @errorName(err)); @@ -324,7 +322,7 @@ pub const CompareOutputContext = struct { debug.panic("Unable to spawn {}: {}\n", full_exe_path, @errorName(err)); }; switch (term) { - Term.Exited => |code| { + .Exited => |code| { if (code != 0) { warn("Process {} exited with error code {}\n", full_exe_path, code); printInvocation(args.toSliceConst()); @@ -383,13 +381,13 @@ pub const CompareOutputContext = struct { warn("Test {}/{} {}...", self.test_index + 1, self.context.test_index, self.name); - const child = os.ChildProcess.init([][]const u8{full_exe_path}, b.allocator) catch unreachable; + const child = std.ChildProcess.init([][]const u8{full_exe_path}, b.allocator) catch unreachable; defer child.deinit(); child.env_map = b.env_map; - child.stdin_behavior = StdIo.Ignore; - child.stdout_behavior = StdIo.Ignore; - child.stderr_behavior = StdIo.Ignore; + child.stdin_behavior = .Ignore; + child.stdout_behavior = .Ignore; + child.stderr_behavior = .Ignore; const term = child.spawnAndWait() catch |err| { debug.panic("Unable to spawn {}: {}\n", full_exe_path, @errorName(err)); @@ -397,13 +395,13 @@ pub const CompareOutputContext = struct { const expected_exit_code: i32 = 126; switch (term) { - Term.Exited => |code| { + .Exited => |code| { if (code != expected_exit_code) { warn("\nProgram expected to exit with code {} " ++ "but exited with code {}\n", expected_exit_code, code); return error.TestFailed; } }, - Term.Signal => |sig| { + .Signal => |sig| { warn("\nProgram expected to exit with code {} " ++ "but instead signaled {}\n", expected_exit_code, sig); return error.TestFailed; }, @@ -459,7 +457,7 @@ pub const CompareOutputContext = struct { pub fn addCase(self: *CompareOutputContext, case: TestCase) void { const b = self.b; - const root_src = os.path.join( + const root_src = fs.path.join( b.allocator, [][]const u8{ b.cache_root, case.sources.items[0].filename }, ) catch unreachable; @@ -475,7 +473,7 @@ pub const CompareOutputContext = struct { exe.addAssemblyFile(root_src); for (case.sources.toSliceConst()) |src_file| { - const expanded_src_path = os.path.join( + const expanded_src_path = fs.path.join( b.allocator, [][]const u8{ b.cache_root, src_file.filename }, ) catch unreachable; @@ -507,7 +505,7 @@ pub const CompareOutputContext = struct { } for (case.sources.toSliceConst()) |src_file| { - const expanded_src_path = os.path.join( + const expanded_src_path = fs.path.join( b.allocator, [][]const u8{ b.cache_root, src_file.filename }, ) catch unreachable; @@ -538,7 +536,7 @@ pub const CompareOutputContext = struct { } for (case.sources.toSliceConst()) |src_file| { - const expanded_src_path = os.path.join( + const expanded_src_path = fs.path.join( b.allocator, [][]const u8{ b.cache_root, src_file.filename }, ) catch unreachable; @@ -633,7 +631,7 @@ pub const CompileErrorContext = struct { const self = @fieldParentPtr(CompileCmpOutputStep, "step", step); const b = self.context.b; - const root_src = os.path.join( + const root_src = fs.path.join( b.allocator, [][]const u8{ b.cache_root, self.case.sources.items[0].filename }, ) catch unreachable; @@ -669,13 +667,13 @@ pub const CompileErrorContext = struct { printInvocation(zig_args.toSliceConst()); } - const child = os.ChildProcess.init(zig_args.toSliceConst(), b.allocator) catch unreachable; + const child = std.ChildProcess.init(zig_args.toSliceConst(), b.allocator) catch unreachable; defer child.deinit(); child.env_map = b.env_map; - child.stdin_behavior = StdIo.Ignore; - child.stdout_behavior = StdIo.Pipe; - child.stderr_behavior = StdIo.Pipe; + child.stdin_behavior = .Ignore; + child.stdout_behavior = .Pipe; + child.stderr_behavior = .Pipe; child.spawn() catch |err| debug.panic("Unable to spawn {}: {}\n", zig_args.items[0], @errorName(err)); @@ -692,7 +690,7 @@ pub const CompileErrorContext = struct { debug.panic("Unable to spawn {}: {}\n", zig_args.items[0], @errorName(err)); }; switch (term) { - Term.Exited => |code| { + .Exited => |code| { if (code == 0) { printInvocation(zig_args.toSliceConst()); return error.CompilationIncorrectlySucceeded; @@ -823,7 +821,7 @@ pub const CompileErrorContext = struct { self.step.dependOn(&compile_and_cmp_errors.step); for (case.sources.toSliceConst()) |src_file| { - const expanded_src_path = os.path.join( + const expanded_src_path = fs.path.join( b.allocator, [][]const u8{ b.cache_root, src_file.filename }, ) catch unreachable; @@ -858,7 +856,7 @@ pub const BuildExamplesContext = struct { } var zig_args = ArrayList([]const u8).init(b.allocator); - const rel_zig_exe = os.path.relative(b.allocator, b.build_root, b.zig_exe) catch unreachable; + const rel_zig_exe = fs.path.relative(b.allocator, b.build_root, b.zig_exe) catch unreachable; zig_args.append(rel_zig_exe) catch unreachable; zig_args.append("build") catch unreachable; @@ -958,7 +956,7 @@ pub const TranslateCContext = struct { const self = @fieldParentPtr(TranslateCCmpOutputStep, "step", step); const b = self.context.b; - const root_src = os.path.join( + const root_src = fs.path.join( b.allocator, [][]const u8{ b.cache_root, self.case.sources.items[0].filename }, ) catch unreachable; @@ -976,13 +974,13 @@ pub const TranslateCContext = struct { printInvocation(zig_args.toSliceConst()); } - const child = os.ChildProcess.init(zig_args.toSliceConst(), b.allocator) catch unreachable; + const child = std.ChildProcess.init(zig_args.toSliceConst(), b.allocator) catch unreachable; defer child.deinit(); child.env_map = b.env_map; - child.stdin_behavior = StdIo.Ignore; - child.stdout_behavior = StdIo.Pipe; - child.stderr_behavior = StdIo.Pipe; + child.stdin_behavior = .Ignore; + child.stdout_behavior = .Pipe; + child.stderr_behavior = .Pipe; child.spawn() catch |err| debug.panic("Unable to spawn {}: {}\n", zig_args.toSliceConst()[0], @errorName(err)); @@ -999,14 +997,14 @@ pub const TranslateCContext = struct { debug.panic("Unable to spawn {}: {}\n", zig_args.toSliceConst()[0], @errorName(err)); }; switch (term) { - Term.Exited => |code| { + .Exited => |code| { if (code != 0) { warn("Compilation failed with exit code {}\n", code); printInvocation(zig_args.toSliceConst()); return error.TestFailed; } }, - Term.Signal => |code| { + .Signal => |code| { warn("Compilation failed with signal {}\n", code); printInvocation(zig_args.toSliceConst()); return error.TestFailed; @@ -1131,7 +1129,7 @@ pub const TranslateCContext = struct { self.step.dependOn(&translate_c_and_cmp.step); for (case.sources.toSliceConst()) |src_file| { - const expanded_src_path = os.path.join( + const expanded_src_path = fs.path.join( b.allocator, [][]const u8{ b.cache_root, src_file.filename }, ) catch unreachable; @@ -1254,7 +1252,7 @@ pub const GenHContext = struct { pub fn addCase(self: *GenHContext, case: *const TestCase) void { const b = self.b; - const root_src = os.path.join( + const root_src = fs.path.join( b.allocator, [][]const u8{ b.cache_root, case.sources.items[0].filename }, ) catch unreachable; @@ -1269,7 +1267,7 @@ pub const GenHContext = struct { obj.setBuildMode(mode); for (case.sources.toSliceConst()) |src_file| { - const expanded_src_path = os.path.join( + const expanded_src_path = fs.path.join( b.allocator, [][]const u8{ b.cache_root, src_file.filename }, ) catch unreachable; -- cgit v1.2.3 From 2b42e910bf4696032158cc7ae268d3c69d699f70 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 26 May 2019 19:56:37 -0400 Subject: behavior tests passing on Linux --- std/c.zig | 9 ++++-- std/debug.zig | 7 ++--- std/fs.zig | 4 +-- std/heap.zig | 29 +++++++++-------- std/io.zig | 6 ++-- std/io/c_out_stream.zig | 2 +- std/mem.zig | 73 +++++++++++++++++++++++++++++-------------- std/os.zig | 48 +++++++++++++++------------- std/os/bits/linux.zig | 3 +- std/os/linux.zig | 8 ++--- std/os/linux/tls.zig | 4 +-- test/stage1/behavior/cast.zig | 2 +- test/stage1/behavior/misc.zig | 5 ++- 13 files changed, 119 insertions(+), 81 deletions(-) (limited to 'std/debug.zig') diff --git a/std/c.zig b/std/c.zig index c1398bccab..9171a99ec9 100644 --- a/std/c.zig +++ b/std/c.zig @@ -1,4 +1,6 @@ const builtin = @import("builtin"); +const std = @import("std"); +const page_size = std.mem.page_size; pub use @import("os/bits.zig"); @@ -33,7 +35,7 @@ pub extern "c" fn close(fd: fd_t) c_int; pub extern "c" fn fstat(fd: fd_t, buf: *Stat) c_int; pub extern "c" fn @"fstat$INODE64"(fd: fd_t, buf: *Stat) c_int; pub extern "c" fn lseek(fd: fd_t, offset: isize, whence: c_int) isize; -pub extern "c" fn open(path: [*]const u8, oflag: c_int, ...) c_int; +pub extern "c" fn open(path: [*]const u8, oflag: c_uint, ...) c_int; pub extern "c" fn raise(sig: c_int) c_int; pub extern "c" fn read(fd: fd_t, buf: [*]u8, nbyte: usize) isize; pub extern "c" fn pread(fd: fd_t, buf: [*]u8, nbyte: usize, offset: u64) isize; @@ -42,8 +44,9 @@ pub extern "c" fn pwritev(fd: c_int, iov: [*]const iovec, iovcnt: c_int, offset: pub extern "c" fn stat(noalias path: [*]const u8, noalias buf: *Stat) c_int; pub extern "c" fn write(fd: fd_t, buf: [*]const u8, nbyte: usize) isize; pub extern "c" fn pwrite(fd: fd_t, buf: [*]const u8, nbyte: usize, offset: u64) isize; -pub extern "c" fn mmap(addr: ?*c_void, len: usize, prot: c_int, flags: c_int, fd: fd_t, offset: isize) usize; -pub extern "c" fn munmap(addr: ?*c_void, len: usize) c_int; +pub extern "c" fn mmap(addr: ?*align(page_size) c_void, len: usize, prot: c_uint, flags: c_uint, fd: fd_t, offset: isize) *c_void; +pub extern "c" fn munmap(addr: *align(page_size) c_void, len: usize) c_int; +pub extern "c" fn mprotect(addr: *align(page_size) c_void, len: usize, prot: c_uint) c_int; pub extern "c" fn unlink(path: [*]const u8) c_int; pub extern "c" fn getcwd(buf: [*]u8, size: usize) ?[*]u8; pub extern "c" fn waitpid(pid: c_int, stat_loc: *c_int, options: c_int) c_int; diff --git a/std/debug.zig b/std/debug.zig index 2c623d42f9..0b5136f7cd 100644 --- a/std/debug.zig +++ b/std/debug.zig @@ -1011,7 +1011,7 @@ fn openSelfDebugInfoPosix(allocator: *mem.Allocator) !DwarfInfo { S.self_exe_file = try fs.openSelfExe(); errdefer S.self_exe_file.close(); - const self_exe_mmap_len = try S.self_exe_file.getEndPos(); + const self_exe_mmap_len = mem.alignForward(try S.self_exe_file.getEndPos(), mem.page_size); const self_exe_mmap = try os.mmap( null, self_exe_mmap_len, @@ -1020,10 +1020,9 @@ fn openSelfDebugInfoPosix(allocator: *mem.Allocator) !DwarfInfo { S.self_exe_file.handle, 0, ); - errdefer os.munmap(self_exe_mmap, self_exe_mmap_len); + errdefer os.munmap(self_exe_mmap); - const file_mmap_slice = @intToPtr([*]const u8, self_exe_mmap)[0..self_exe_mmap_len]; - S.self_exe_mmap_seekable = io.SliceSeekableInStream.init(file_mmap_slice); + S.self_exe_mmap_seekable = io.SliceSeekableInStream.init(self_exe_mmap); return openElfDebugInfo( allocator, diff --git a/std/fs.zig b/std/fs.zig index dcb3143f79..229f3099c2 100644 --- a/std/fs.zig +++ b/std/fs.zig @@ -595,8 +595,8 @@ pub const Dir = struct { } while (true) { - const rc = os.system.getdents64(self.handle.fd, self.handle.buf.ptr, self.handle.buf.len); - switch (os.errno(rc)) { + const rc = os.linux.getdents64(self.handle.fd, self.handle.buf.ptr, self.handle.buf.len); + switch (os.linux.getErrno(rc)) { 0 => {}, os.EBADF => unreachable, os.EFAULT => unreachable, diff --git a/std/heap.zig b/std/heap.zig index 1abe1da4fb..198eb2d073 100644 --- a/std/heap.zig +++ b/std/heap.zig @@ -104,35 +104,36 @@ pub const DirectAllocator = struct { } const alloc_size = if (alignment <= mem.page_size) n else n + alignment; - const addr = os.mmap( + const slice = os.mmap( null, - alloc_size, + mem.alignForward(alloc_size, mem.page_size), os.PROT_READ | os.PROT_WRITE, os.MAP_PRIVATE | os.MAP_ANONYMOUS, -1, 0, ) catch return error.OutOfMemory; - if (alloc_size == n) return @intToPtr([*]u8, addr)[0..n]; + if (alloc_size == n) return slice; - const aligned_addr = mem.alignForward(addr, alignment); + const aligned_addr = mem.alignForward(@ptrToInt(slice.ptr), alignment); // Unmap the extra bytes that were only requested in order to guarantee // that the range of memory we were provided had a proper alignment in // it somewhere. The extra bytes could be at the beginning, or end, or both. - const unused_start_len = aligned_addr - addr; + const unused_start_len = aligned_addr - @ptrToInt(slice.ptr); if (unused_start_len != 0) { - os.munmap(addr, unused_start_len); + os.munmap(slice[0..unused_start_len]); } - const aligned_end_addr = std.mem.alignForward(aligned_addr + n, mem.page_size); - const unused_end_len = addr + alloc_size - aligned_end_addr; + const aligned_end_addr = mem.alignForward(aligned_addr + n, mem.page_size); + const unused_end_len = @ptrToInt(slice.ptr) + slice.len - aligned_end_addr; if (unused_end_len != 0) { - os.munmap(aligned_end_addr, unused_end_len); + os.munmap(@intToPtr([*]align(mem.page_size) u8, aligned_end_addr)[0..unused_end_len]); } return @intToPtr([*]u8, aligned_addr)[0..n]; } - fn shrink(allocator: *Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) []u8 { + fn shrink(allocator: *Allocator, old_mem_unaligned: []u8, old_align: u29, new_size: usize, new_align: u29) []u8 { + const old_mem = @alignCast(mem.page_size, old_mem_unaligned); if (os.windows.is_the_target) { const w = os.windows; if (new_size == 0) { @@ -165,12 +166,14 @@ pub const DirectAllocator = struct { const new_addr_end = base_addr + new_size; const new_addr_end_rounded = mem.alignForward(new_addr_end, mem.page_size); if (old_addr_end > new_addr_end_rounded) { - os.munmap(new_addr_end_rounded, old_addr_end - new_addr_end_rounded); + const ptr = @intToPtr([*]align(mem.page_size) u8, new_addr_end_rounded); + os.munmap(ptr[0 .. old_addr_end - new_addr_end_rounded]); } return old_mem[0..new_size]; } - fn realloc(allocator: *Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) ![]u8 { + fn realloc(allocator: *Allocator, old_mem_unaligned: []u8, old_align: u29, new_size: usize, new_align: u29) ![]u8 { + const old_mem = @alignCast(mem.page_size, old_mem_unaligned); if (os.windows.is_the_target) { if (old_mem.len == 0) { return alloc(allocator, new_size, new_align); @@ -231,7 +234,7 @@ pub const DirectAllocator = struct { const result = try alloc(allocator, new_size, new_align); if (old_mem.len != 0) { @memcpy(result.ptr, old_mem.ptr, std.math.min(old_mem.len, result.len)); - os.munmap(@ptrToInt(old_mem.ptr), old_mem.len); + os.munmap(old_mem); } return result; } diff --git a/std/io.zig b/std/io.zig index 41dc38f245..258961fdfc 100644 --- a/std/io.zig +++ b/std/io.zig @@ -1,12 +1,12 @@ const std = @import("std.zig"); const builtin = @import("builtin"); -const Os = builtin.Os; const c = std.c; const math = std.math; const debug = std.debug; const assert = debug.assert; const os = std.os; +const fs = std.fs; const mem = std.mem; const meta = std.meta; const trait = meta.trait; @@ -985,7 +985,7 @@ pub fn BitOutStream(endian: builtin.Endian, comptime Error: type) type { } pub const BufferedAtomicFile = struct { - atomic_file: os.AtomicFile, + atomic_file: fs.AtomicFile, file_stream: File.OutStream, buffered_stream: BufferedOutStream(File.WriteError), allocator: *mem.Allocator, @@ -1001,7 +1001,7 @@ pub const BufferedAtomicFile = struct { }; errdefer allocator.destroy(self); - self.atomic_file = try os.AtomicFile.init(dest_path, File.default_mode); + self.atomic_file = try fs.AtomicFile.init(dest_path, File.default_mode); errdefer self.atomic_file.deinit(); self.file_stream = self.atomic_file.file.outStream(); diff --git a/std/io/c_out_stream.zig b/std/io/c_out_stream.zig index cbc5254382..8b341e6937 100644 --- a/std/io/c_out_stream.zig +++ b/std/io/c_out_stream.zig @@ -37,7 +37,7 @@ pub const COutStream = struct { os.ENOSPC => return error.NoSpaceLeft, os.EPERM => return error.AccessDenied, os.EPIPE => return error.BrokenPipe, - else => return os.unexpectedErrno(@intCast(usize, errno)), + else => |err| return os.unexpectedErrno(@intCast(usize, err)), } } }; diff --git a/std/mem.zig b/std/mem.zig index 643d297191..17dec85a5b 100644 --- a/std/mem.zig +++ b/std/mem.zig @@ -585,14 +585,14 @@ pub fn readIntSlice(comptime T: type, bytes: []const u8, endian: builtin.Endian) test "comptime read/write int" { comptime { var bytes: [2]u8 = undefined; - std.mem.writeIntLittle(u16, &bytes, 0x1234); - const result = std.mem.readIntBig(u16, &bytes); + writeIntLittle(u16, &bytes, 0x1234); + const result = readIntBig(u16, &bytes); testing.expect(result == 0x3412); } comptime { var bytes: [2]u8 = undefined; - std.mem.writeIntBig(u16, &bytes, 0x1234); - const result = std.mem.readIntLittle(u16, &bytes); + writeIntBig(u16, &bytes, 0x1234); + const result = readIntLittle(u16, &bytes); testing.expect(result == 0x3412); } } @@ -1053,7 +1053,7 @@ fn testReadIntImpl() void { } } -test "std.mem.writeIntSlice" { +test "writeIntSlice" { testWriteIntImpl(); comptime testWriteIntImpl(); } @@ -1184,7 +1184,7 @@ pub fn reverse(comptime T: type, items: []T) void { } } -test "std.mem.reverse" { +test "reverse" { var arr = []i32{ 5, 3, @@ -1211,7 +1211,7 @@ pub fn rotate(comptime T: type, items: []T, amount: usize) void { reverse(T, items); } -test "std.mem.rotate" { +test "rotate" { var arr = []i32{ 5, 3, @@ -1296,14 +1296,14 @@ pub fn asBytes(ptr: var) AsBytesReturnType(@typeOf(ptr)) { return @ptrCast(AsBytesReturnType(P), ptr); } -test "std.mem.asBytes" { +test "asBytes" { const deadbeef = u32(0xDEADBEEF); const deadbeef_bytes = switch (builtin.endian) { builtin.Endian.Big => "\xDE\xAD\xBE\xEF", builtin.Endian.Little => "\xEF\xBE\xAD\xDE", }; - testing.expect(std.mem.eql(u8, asBytes(&deadbeef), deadbeef_bytes)); + testing.expect(eql(u8, asBytes(&deadbeef), deadbeef_bytes)); var codeface = u32(0xC0DEFACE); for (asBytes(&codeface).*) |*b| @@ -1323,7 +1323,7 @@ test "std.mem.asBytes" { .c = 0xDE, .d = 0xA1, }; - testing.expect(std.mem.eql(u8, asBytes(&inst), "\xBE\xEF\xDE\xA1")); + testing.expect(eql(u8, asBytes(&inst), "\xBE\xEF\xDE\xA1")); } ///Given any value, returns a copy of its bytes in an array. @@ -1331,17 +1331,17 @@ pub fn toBytes(value: var) [@sizeOf(@typeOf(value))]u8 { return asBytes(&value).*; } -test "std.mem.toBytes" { +test "toBytes" { var my_bytes = toBytes(u32(0x12345678)); switch (builtin.endian) { - builtin.Endian.Big => testing.expect(std.mem.eql(u8, my_bytes, "\x12\x34\x56\x78")), - builtin.Endian.Little => testing.expect(std.mem.eql(u8, my_bytes, "\x78\x56\x34\x12")), + builtin.Endian.Big => testing.expect(eql(u8, my_bytes, "\x12\x34\x56\x78")), + builtin.Endian.Little => testing.expect(eql(u8, my_bytes, "\x78\x56\x34\x12")), } my_bytes[0] = '\x99'; switch (builtin.endian) { - builtin.Endian.Big => testing.expect(std.mem.eql(u8, my_bytes, "\x99\x34\x56\x78")), - builtin.Endian.Little => testing.expect(std.mem.eql(u8, my_bytes, "\x99\x56\x34\x12")), + builtin.Endian.Big => testing.expect(eql(u8, my_bytes, "\x99\x34\x56\x78")), + builtin.Endian.Little => testing.expect(eql(u8, my_bytes, "\x99\x56\x34\x12")), } } @@ -1363,7 +1363,7 @@ pub fn bytesAsValue(comptime T: type, bytes: var) BytesAsValueReturnType(T, @typ return @ptrCast(BytesAsValueReturnType(T, @typeOf(bytes)), bytes); } -test "std.mem.bytesAsValue" { +test "bytesAsValue" { const deadbeef = u32(0xDEADBEEF); const deadbeef_bytes = switch (builtin.endian) { builtin.Endian.Big => "\xDE\xAD\xBE\xEF", @@ -1405,7 +1405,7 @@ test "std.mem.bytesAsValue" { pub fn bytesToValue(comptime T: type, bytes: var) T { return bytesAsValue(T, &bytes).*; } -test "std.mem.bytesToValue" { +test "bytesToValue" { const deadbeef_bytes = switch (builtin.endian) { builtin.Endian.Big => "\xDE\xAD\xBE\xEF", builtin.Endian.Little => "\xEF\xBE\xAD\xDE", @@ -1430,25 +1430,25 @@ pub fn subArrayPtr(ptr: var, comptime start: usize, comptime length: usize) SubA return @ptrCast(ReturnType, &ptr[start]); } -test "std.mem.subArrayPtr" { +test "subArrayPtr" { const a1 = "abcdef"; const sub1 = subArrayPtr(&a1, 2, 3); - testing.expect(std.mem.eql(u8, sub1.*, "cde")); + testing.expect(eql(u8, sub1.*, "cde")); var a2 = "abcdef"; var sub2 = subArrayPtr(&a2, 2, 3); - testing.expect(std.mem.eql(u8, sub2, "cde")); + testing.expect(eql(u8, sub2, "cde")); sub2[1] = 'X'; - testing.expect(std.mem.eql(u8, a2, "abcXef")); + testing.expect(eql(u8, a2, "abcXef")); } /// Round an address up to the nearest aligned address pub fn alignForward(addr: usize, alignment: usize) usize { - return (addr + alignment - 1) & ~(alignment - 1); + return alignBackward(addr + (alignment - 1), alignment); } -test "std.mem.alignForward" { +test "alignForward" { testing.expect(alignForward(1, 1) == 1); testing.expect(alignForward(2, 1) == 2); testing.expect(alignForward(1, 2) == 2); @@ -1462,3 +1462,30 @@ test "std.mem.alignForward" { testing.expect(alignForward(16, 8) == 16); testing.expect(alignForward(17, 8) == 24); } + +pub fn alignBackward(addr: usize, alignment: usize) usize { + // 000010000 // example addr + // 000001111 // subtract 1 + // 111110000 // binary not + return addr & ~(alignment - 1); +} + +pub fn isAligned(addr: usize, alignment: usize) bool { + return alignBackward(addr, alignment) == addr; +} + +test "isAligned" { + testing.expect(isAligned(0, 4)); + testing.expect(isAligned(1, 1)); + testing.expect(isAligned(2, 1)); + testing.expect(isAligned(2, 2)); + testing.expect(!isAligned(2, 4)); + testing.expect(isAligned(3, 1)); + testing.expect(!isAligned(3, 2)); + testing.expect(!isAligned(3, 4)); + testing.expect(isAligned(4, 4)); + testing.expect(isAligned(4, 2)); + testing.expect(isAligned(4, 1)); + testing.expect(!isAligned(4, 8)); + testing.expect(!isAligned(4, 16)); +} diff --git a/std/os.zig b/std/os.zig index 0efc2ebca7..1debb16234 100644 --- a/std/os.zig +++ b/std/os.zig @@ -251,7 +251,7 @@ pub fn read(fd: fd_t, buf: []u8) ReadError!usize { // Linux can return EINVAL when read amount is > 0x7ffff000 // See https://github.com/ziglang/zig/pull/743#issuecomment-363158274 // TODO audit this. Shawn Landden says that this is not actually true. - // if this logic should stay, move it to std.os.linux.sys + // if this logic should stay, move it to std.os.linux const max_buf_len = 0x7ffff000; var index: usize = 0; @@ -260,8 +260,9 @@ pub fn read(fd: fd_t, buf: []u8) ReadError!usize { const rc = system.read(fd, buf.ptr + index, want_to_read); switch (errno(rc)) { 0 => { - index += rc; - if (rc == want_to_read) continue; + const amt_read = @intCast(usize, rc); + index += amt_read; + if (amt_read == want_to_read) continue; // Read returned less than buf.len. return index; }, @@ -374,7 +375,7 @@ pub fn write(fd: fd_t, bytes: []const u8) WriteError!void { // Linux can return EINVAL when write amount is > 0x7ffff000 // See https://github.com/ziglang/zig/pull/743#issuecomment-363165856 // TODO audit this. Shawn Landden says that this is not actually true. - // if this logic should stay, move it to std.os.linux.sys + // if this logic should stay, move it to std.os.linux const max_bytes_len = 0x7ffff000; var index: usize = 0; @@ -383,7 +384,7 @@ pub fn write(fd: fd_t, bytes: []const u8) WriteError!void { const rc = system.write(fd, bytes.ptr + index, amt_to_write); switch (errno(rc)) { 0 => { - index += rc; + index += @intCast(usize, rc); continue; }, EINTR => continue, @@ -1863,14 +1864,10 @@ pub const MProtectError = error{ Unexpected, }; -/// address and length must be page-aligned -pub fn mprotect(address: usize, length: usize, protection: u32) MProtectError!void { - const negative_page_size = @bitCast(usize, -isize(mem.page_size)); - const aligned_address = address & negative_page_size; - const aligned_end = (address + length + mem.page_size - 1) & negative_page_size; - assert(address == aligned_address); - assert(length == aligned_end - aligned_address); - switch (errno(system.mprotect(address, length, protection))) { +/// `memory.len` must be page-aligned. +pub fn mprotect(memory: [*]align(mem.page_size) u8, protection: u32) MProtectError!void { + assert(mem.isAligned(memory.len, mem.page_size)); + switch (errno(system.mprotect(memory.ptr, memory.len, protection))) { 0 => return, EINVAL => unreachable, EACCES => return error.AccessDenied, @@ -1906,17 +1903,26 @@ pub const MMapError = error{ /// Map files or devices into memory. /// Use of a mapped region can result in these signals: +/// `length` must be page-aligned. /// * SIGSEGV - Attempted write into a region mapped as read-only. /// * SIGBUS - Attempted access to a portion of the buffer that does not correspond to the file -pub fn mmap(address: ?[*]u8, length: usize, prot: u32, flags: u32, fd: fd_t, offset: isize) MMapError!usize { +pub fn mmap( + ptr: ?[*]align(mem.page_size) u8, + length: usize, + prot: u32, + flags: u32, + fd: fd_t, + offset: isize, +) MMapError![]align(mem.page_size) u8 { + assert(mem.isAligned(length, mem.page_size)); const err = if (builtin.link_libc) blk: { - const rc = system.mmap(address, length, prot, flags, fd, offset); - if (rc != system.MMAP_FAILED) return rc; - break :blk system._errno().*; + const rc = std.c.mmap(ptr, length, prot, flags, fd, offset); + if (rc != MAP_FAILED) return @ptrCast([*]align(mem.page_size) u8, @alignCast(mem.page_size, rc))[0..length]; + break :blk @intCast(usize, system._errno().*); } else blk: { - const rc = system.mmap(address, length, prot, flags, fd, offset); + const rc = system.mmap(ptr, length, prot, flags, fd, offset); const err = errno(rc); - if (err == 0) return rc; + if (err == 0) return @intToPtr([*]align(mem.page_size) u8, rc)[0..length]; break :blk err; }; switch (err) { @@ -1940,8 +1946,8 @@ pub fn mmap(address: ?[*]u8, length: usize, prot: u32, flags: u32, fd: fd_t, off /// Zig's munmap function does not, for two reasons: /// * It violates the Zig principle that resource deallocation must succeed. /// * The Windows function, VirtualFree, has this restriction. -pub fn munmap(address: usize, length: usize) void { - switch (errno(system.munmap(address, length))) { +pub fn munmap(memory: []align(mem.page_size) u8) void { + switch (errno(system.munmap(memory.ptr, memory.len))) { 0 => return, EINVAL => unreachable, // Invalid parameters. ENOMEM => unreachable, // Attempted to unmap a region in the middle of an existing mapping. diff --git a/std/os/bits/linux.zig b/std/os/bits/linux.zig index b4f51f3518..5473299488 100644 --- a/std/os/bits/linux.zig +++ b/std/os/bits/linux.zig @@ -1,4 +1,5 @@ const std = @import("../../std.zig"); +const maxInt = std.math.maxInt; pub use @import("linux/errno.zig"); pub use switch (builtin.arch) { @@ -39,7 +40,7 @@ pub const PROT_EXEC = 4; pub const PROT_GROWSDOWN = 0x01000000; pub const PROT_GROWSUP = 0x02000000; -pub const MAP_FAILED = maxInt(usize); +pub const MAP_FAILED = @intToPtr(*c_void, maxInt(usize)); pub const MAP_SHARED = 0x01; pub const MAP_PRIVATE = 0x02; pub const MAP_TYPE = 0x0f; diff --git a/std/os/linux.zig b/std/os/linux.zig index 4b00433b6c..4b37399079 100644 --- a/std/os/linux.zig +++ b/std/os/linux.zig @@ -173,12 +173,12 @@ pub fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: u32, fd: i32, of return syscall6(SYS_mmap, @ptrToInt(address), length, prot, flags, @bitCast(usize, isize(fd)), @bitCast(usize, offset)); } -pub fn mprotect(address: usize, length: usize, protection: usize) usize { - return syscall3(SYS_mprotect, address, length, protection); +pub fn mprotect(address: [*]const u8, length: usize, protection: usize) usize { + return syscall3(SYS_mprotect, @ptrToInt(address), length, protection); } -pub fn munmap(address: usize, length: usize) usize { - return syscall2(SYS_munmap, address, length); +pub fn munmap(address: [*]const u8, length: usize) usize { + return syscall2(SYS_munmap, @ptrToInt(address), length); } pub fn read(fd: i32, buf: [*]u8, count: usize) usize { diff --git a/std/os/linux/tls.zig b/std/os/linux/tls.zig index f06ed144b9..a10d68663b 100644 --- a/std/os/linux/tls.zig +++ b/std/os/linux/tls.zig @@ -237,7 +237,7 @@ pub fn allocateTLS(size: usize) usize { return @ptrToInt(&main_thread_tls_buffer); } - const addr = os.mmap( + const slice = os.mmap( null, size, os.PROT_READ | os.PROT_WRITE, @@ -246,5 +246,5 @@ pub fn allocateTLS(size: usize) usize { 0, ) catch @panic("out of memory"); - return addr; + return @ptrToInt(slice.ptr); } diff --git a/test/stage1/behavior/cast.zig b/test/stage1/behavior/cast.zig index 8ed03f4936..57de1e2eeb 100644 --- a/test/stage1/behavior/cast.zig +++ b/test/stage1/behavior/cast.zig @@ -318,7 +318,7 @@ fn testCastPtrOfArrayToSliceAndPtr() void { test "cast *[1][*]const u8 to [*]const ?[*]const u8" { const window_name = [1][*]const u8{c"window name"}; const x: [*]const ?[*]const u8 = &window_name; - expect(mem.eql(u8, std.cstr.toSliceConst(x[0].?), "window name")); + expect(mem.eql(u8, std.mem.toSliceConst(u8, x[0].?), "window name")); } test "@intCast comptime_int" { diff --git a/test/stage1/behavior/misc.zig b/test/stage1/behavior/misc.zig index 4cc401a008..2ef3e02348 100644 --- a/test/stage1/behavior/misc.zig +++ b/test/stage1/behavior/misc.zig @@ -2,7 +2,6 @@ const std = @import("std"); const expect = std.testing.expect; const expectEqualSlices = std.testing.expectEqualSlices; const mem = std.mem; -const cstr = std.cstr; const builtin = @import("builtin"); const maxInt = std.math.maxInt; @@ -210,7 +209,7 @@ test "multiline C string" { c\\three ; const s2 = c"one\ntwo)\nthree"; - expect(cstr.cmp(s1, s2) == 0); + expect(std.cstr.cmp(s1, s2) == 0); } test "type equality" { @@ -363,7 +362,7 @@ test "C string concatenation" { const a = c"OK" ++ c" IT " ++ c"WORKED"; const b = c"OK IT WORKED"; - const len = cstr.len(b); + const len = mem.len(u8, b); const len_with_null = len + 1; { var i: u32 = 0; -- cgit v1.2.3 From 6be79d79aab204718116428c9b23be7de54e14da Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 27 May 2019 00:48:56 -0400 Subject: fixes for Windows and WASI --- CMakeLists.txt | 1 + std/debug.zig | 18 +- std/heap.zig | 4 +- std/os.zig | 3 +- std/os/bits/wasi.zig | 3 +- std/os/bits/windows.zig | 66 ++++- std/os/windows.zig | 543 +---------------------------------- std/os/windows/advapi32.zig | 2 +- std/os/windows/bits.zig | 527 +++++++++++++++++++++++++++++++++ std/os/windows/kernel32.zig | 2 +- std/os/windows/ntdll.zig | 2 +- std/os/windows/ole32.zig | 2 +- std/os/windows/shell32.zig | 2 +- std/pdb.zig | 2 +- std/process.zig | 4 +- std/special/bootstrap.zig | 2 +- std/statically_initialized_mutex.zig | 12 +- 17 files changed, 638 insertions(+), 557 deletions(-) create mode 100644 std/os/windows/bits.zig (limited to 'std/debug.zig') diff --git a/CMakeLists.txt b/CMakeLists.txt index 9f6af9a9de..5157406896 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -628,6 +628,7 @@ set(ZIG_STD_FILES "os/wasi.zig" "os/windows.zig" "os/windows/advapi32.zig" + "os/windows/bits.zig" "os/windows/error.zig" "os/windows/kernel32.zig" "os/windows/ntdll.zig" diff --git a/std/debug.zig b/std/debug.zig index 0b5136f7cd..b62bd5da64 100644 --- a/std/debug.zig +++ b/std/debug.zig @@ -510,7 +510,7 @@ const TtyColor = enum { /// TODO this is a special case hack right now. clean it up and maybe make it part of std.fmt fn setTtyColor(tty_color: TtyColor) void { - if (os.supportsAnsiEscapeCodes(stderr_file.handle)) { + if (stderr_file.supportsAnsiEscapeCodes()) { switch (tty_color) { TtyColor.Red => { stderr_file.write(RED) catch return; @@ -540,29 +540,29 @@ fn setTtyColor(tty_color: TtyColor) void { S.init_attrs = true; var info: windows.CONSOLE_SCREEN_BUFFER_INFO = undefined; // TODO handle error - _ = windows.GetConsoleScreenBufferInfo(stderr_file.handle, &info); + _ = windows.kernel32.GetConsoleScreenBufferInfo(stderr_file.handle, &info); S.attrs = info.wAttributes; } // TODO handle errors switch (tty_color) { TtyColor.Red => { - _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_RED | windows.FOREGROUND_INTENSITY); + _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_RED | windows.FOREGROUND_INTENSITY) catch {}; }, TtyColor.Green => { - _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_GREEN | windows.FOREGROUND_INTENSITY); + _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_GREEN | windows.FOREGROUND_INTENSITY) catch {}; }, TtyColor.Cyan => { - _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_GREEN | windows.FOREGROUND_BLUE | windows.FOREGROUND_INTENSITY); + _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_GREEN | windows.FOREGROUND_BLUE | windows.FOREGROUND_INTENSITY) catch {}; }, TtyColor.White, TtyColor.Bold => { - _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_RED | windows.FOREGROUND_GREEN | windows.FOREGROUND_BLUE | windows.FOREGROUND_INTENSITY); + _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_RED | windows.FOREGROUND_GREEN | windows.FOREGROUND_BLUE | windows.FOREGROUND_INTENSITY) catch {}; }, TtyColor.Dim => { - _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_INTENSITY); + _ = windows.SetConsoleTextAttribute(stderr_file.handle, windows.FOREGROUND_INTENSITY) catch {}; }, TtyColor.Reset => { - _ = windows.SetConsoleTextAttribute(stderr_file.handle, S.attrs); + _ = windows.SetConsoleTextAttribute(stderr_file.handle, S.attrs) catch {}; }, } } @@ -894,7 +894,7 @@ fn openSelfDebugInfoWindows(allocator: *mem.Allocator) !DebugInfo { if (this_record_len % 4 != 0) { const round_to_next_4 = (this_record_len | 0x3) + 1; const march_forward_bytes = round_to_next_4 - this_record_len; - try dbi.seekBy(march_forward_bytes); + try dbi.seekBy(@intCast(isize, march_forward_bytes)); this_record_len += march_forward_bytes; } diff --git a/std/heap.zig b/std/heap.zig index 6c6a97d037..3bae7f3c46 100644 --- a/std/heap.zig +++ b/std/heap.zig @@ -92,12 +92,14 @@ pub const DirectAllocator = struct { // obtained some memory space that will cause the next // VirtualAlloc call to fail. To handle this, we will retry // until it succeeds. - return w.VirtualAlloc( + const ptr = w.VirtualAlloc( @intToPtr(*c_void, aligned_addr), n, w.MEM_COMMIT | w.MEM_RESERVE, w.PAGE_READWRITE, ) catch continue; + + return @ptrCast([*]u8, ptr)[0..n]; }; return @ptrCast([*]u8, final_addr)[0..n]; diff --git a/std/os.zig b/std/os.zig index 087d5e12a5..84e92a15d9 100644 --- a/std/os.zig +++ b/std/os.zig @@ -99,7 +99,8 @@ pub fn getrandom(buf: []u8) GetRandomError!void { } if (linux.is_the_target) { while (true) { - switch (errno(system.getrandom(buf.ptr, buf.len, 0))) { + // Bypass libc because it's missing on even relatively new versions. + switch (linux.getErrno(linux.getrandom(buf.ptr, buf.len, 0))) { 0 => return, EINVAL => unreachable, EFAULT => unreachable, diff --git a/std/os/bits/wasi.zig b/std/os/bits/wasi.zig index cf6542a159..a6f96de31a 100644 --- a/std/os/bits/wasi.zig +++ b/std/os/bits/wasi.zig @@ -1,5 +1,4 @@ -// Declarations that are intended to be imported into the POSIX namespace. -// This includes WASI-only APIs. +use @import("../bits.zig"); pub const STDIN_FILENO = 0; pub const STDOUT_FILENO = 1; diff --git a/std/os/bits/windows.zig b/std/os/bits/windows.zig index d3558958ff..4c94853e2a 100644 --- a/std/os/bits/windows.zig +++ b/std/os/bits/windows.zig @@ -1,8 +1,65 @@ -use @import("../windows.zig"); +// The reference for these types and values is Microsoft Windows's ucrt (Universal C RunTime). + +use @import("../windows/bits.zig"); pub const fd_t = HANDLE; pub const pid_t = HANDLE; +pub const sig_atomic_t = c_int; + +/// maximum signal number + 1 +pub const NSIG = 23; + +// Signal types + +/// interrupt +pub const SIGINT = 2; + +/// illegal instruction - invalid function image +pub const SIGILL = 4; + +/// floating point exception +pub const SIGFPE = 8; + +/// segment violation +pub const SIGSEGV = 11; + +/// Software termination signal from kill +pub const SIGTERM = 15; + +/// Ctrl-Break sequence +pub const SIGBREAK = 21; + +/// abnormal termination triggered by abort call +pub const SIGABRT = 22; + +/// SIGABRT compatible with other platforms, same as SIGABRT +pub const SIGABRT_COMPAT = 6; + +// Signal action codes + +/// default signal action +pub const SIG_DFL = 0; + +/// ignore signal +pub const SIG_IGN = 1; + +/// return current value +pub const SIG_GET = 2; + +/// signal gets error +pub const SIG_SGE = 3; + +/// acknowledge +pub const SIG_ACK = 4; + +/// Signal error value (returned by signal call on error) +pub const SIG_ERR = -1; + +pub const SEEK_SET = 0; +pub const SEEK_CUR = 1; +pub const SEEK_END = 2; + pub const EPERM = 1; pub const ENOENT = 2; pub const ESRCH = 3; @@ -89,3 +146,10 @@ pub const ETIME = 137; pub const ETIMEDOUT = 138; pub const ETXTBSY = 139; pub const EWOULDBLOCK = 140; + +// These are workarounds for "use of undeclared identifier" compile errors +// TODO make the compiler even more lazy. don't emit "use of undeclared identifier" errors +// for if branches that aren't taken. +pub const SIGKILL = @compileError("Windows libc does not have this"); +pub const EDQUOT = @compileError("Windows libc does not have this"); +pub const TIOCGWINSZ = @compileError("Windows libc does not have this"); diff --git a/std/os/windows.zig b/std/os/windows.zig index acad139955..ffa647692e 100644 --- a/std/os/windows.zig +++ b/std/os/windows.zig @@ -6,537 +6,19 @@ const builtin = @import("builtin"); const std = @import("../std.zig"); +const mem = std.mem; const assert = std.debug.assert; +const math = std.math; const maxInt = std.math.maxInt; pub const is_the_target = builtin.os == .windows; - pub const advapi32 = @import("windows/advapi32.zig"); pub const kernel32 = @import("windows/kernel32.zig"); pub const ntdll = @import("windows/ntdll.zig"); pub const ole32 = @import("windows/ole32.zig"); pub const shell32 = @import("windows/shell32.zig"); -pub const ERROR = @import("windows/error.zig"); - -/// The standard input device. Initially, this is the console input buffer, CONIN$. -pub const STD_INPUT_HANDLE = maxInt(DWORD) - 10 + 1; - -/// The standard output device. Initially, this is the active console screen buffer, CONOUT$. -pub const STD_OUTPUT_HANDLE = maxInt(DWORD) - 11 + 1; - -/// The standard error device. Initially, this is the active console screen buffer, CONOUT$. -pub const STD_ERROR_HANDLE = maxInt(DWORD) - 12 + 1; - -pub const SHORT = c_short; -pub const BOOL = c_int; -pub const BOOLEAN = BYTE; -pub const BYTE = u8; -pub const CHAR = u8; -pub const DWORD = u32; -pub const FLOAT = f32; -pub const HANDLE = *c_void; -pub const HCRYPTPROV = ULONG_PTR; -pub const HINSTANCE = *@OpaqueType(); -pub const HMODULE = *@OpaqueType(); -pub const FARPROC = *@OpaqueType(); -pub const INT = c_int; -pub const LPBYTE = *BYTE; -pub const LPCH = *CHAR; -pub const LPCSTR = [*]const CHAR; -pub const LPCTSTR = [*]const TCHAR; -pub const LPCVOID = *const c_void; -pub const LPDWORD = *DWORD; -pub const LPSTR = [*]CHAR; -pub const LPTSTR = if (UNICODE) LPWSTR else LPSTR; -pub const LPVOID = *c_void; -pub const LPWSTR = [*]WCHAR; -pub const LPCWSTR = [*]const WCHAR; -pub const PVOID = *c_void; -pub const PWSTR = [*]WCHAR; -pub const SIZE_T = usize; -pub const TCHAR = if (UNICODE) WCHAR else u8; -pub const UINT = c_uint; -pub const ULONG_PTR = usize; -pub const DWORD_PTR = ULONG_PTR; -pub const UNICODE = false; -pub const WCHAR = u16; -pub const WORD = u16; -pub const LARGE_INTEGER = i64; -pub const ULONG = u32; -pub const LONG = i32; -pub const ULONGLONG = u64; -pub const LONGLONG = i64; - -pub const TRUE = 1; -pub const FALSE = 0; - -pub const INVALID_HANDLE_VALUE = @intToPtr(HANDLE, maxInt(usize)); - -pub const INVALID_FILE_ATTRIBUTES = DWORD(maxInt(DWORD)); - -pub const OVERLAPPED = extern struct { - Internal: ULONG_PTR, - InternalHigh: ULONG_PTR, - Offset: DWORD, - OffsetHigh: DWORD, - hEvent: ?HANDLE, -}; -pub const LPOVERLAPPED = *OVERLAPPED; - -pub const MAX_PATH = 260; - -// TODO issue #305 -pub const FILE_INFO_BY_HANDLE_CLASS = u32; -pub const FileBasicInfo = 0; -pub const FileStandardInfo = 1; -pub const FileNameInfo = 2; -pub const FileRenameInfo = 3; -pub const FileDispositionInfo = 4; -pub const FileAllocationInfo = 5; -pub const FileEndOfFileInfo = 6; -pub const FileStreamInfo = 7; -pub const FileCompressionInfo = 8; -pub const FileAttributeTagInfo = 9; -pub const FileIdBothDirectoryInfo = 10; -pub const FileIdBothDirectoryRestartInfo = 11; -pub const FileIoPriorityHintInfo = 12; -pub const FileRemoteProtocolInfo = 13; -pub const FileFullDirectoryInfo = 14; -pub const FileFullDirectoryRestartInfo = 15; -pub const FileStorageInfo = 16; -pub const FileAlignmentInfo = 17; -pub const FileIdInfo = 18; -pub const FileIdExtdDirectoryInfo = 19; -pub const FileIdExtdDirectoryRestartInfo = 20; - -pub const FILE_NAME_INFO = extern struct { - FileNameLength: DWORD, - FileName: [1]WCHAR, -}; - -/// Return the normalized drive name. This is the default. -pub const FILE_NAME_NORMALIZED = 0x0; - -/// Return the opened file name (not normalized). -pub const FILE_NAME_OPENED = 0x8; - -/// Return the path with the drive letter. This is the default. -pub const VOLUME_NAME_DOS = 0x0; - -/// Return the path with a volume GUID path instead of the drive name. -pub const VOLUME_NAME_GUID = 0x1; - -/// Return the path with no drive information. -pub const VOLUME_NAME_NONE = 0x4; - -/// Return the path with the volume device path. -pub const VOLUME_NAME_NT = 0x2; - -pub const SECURITY_ATTRIBUTES = extern struct { - nLength: DWORD, - lpSecurityDescriptor: ?*c_void, - bInheritHandle: BOOL, -}; -pub const PSECURITY_ATTRIBUTES = *SECURITY_ATTRIBUTES; -pub const LPSECURITY_ATTRIBUTES = *SECURITY_ATTRIBUTES; - -pub const GENERIC_READ = 0x80000000; -pub const GENERIC_WRITE = 0x40000000; -pub const GENERIC_EXECUTE = 0x20000000; -pub const GENERIC_ALL = 0x10000000; - -pub const FILE_SHARE_DELETE = 0x00000004; -pub const FILE_SHARE_READ = 0x00000001; -pub const FILE_SHARE_WRITE = 0x00000002; - -pub const CREATE_ALWAYS = 2; -pub const CREATE_NEW = 1; -pub const OPEN_ALWAYS = 4; -pub const OPEN_EXISTING = 3; -pub const TRUNCATE_EXISTING = 5; - -pub const FILE_ATTRIBUTE_ARCHIVE = 0x20; -pub const FILE_ATTRIBUTE_COMPRESSED = 0x800; -pub const FILE_ATTRIBUTE_DEVICE = 0x40; -pub const FILE_ATTRIBUTE_DIRECTORY = 0x10; -pub const FILE_ATTRIBUTE_ENCRYPTED = 0x4000; -pub const FILE_ATTRIBUTE_HIDDEN = 0x2; -pub const FILE_ATTRIBUTE_INTEGRITY_STREAM = 0x8000; -pub const FILE_ATTRIBUTE_NORMAL = 0x80; -pub const FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 0x2000; -pub const FILE_ATTRIBUTE_NO_SCRUB_DATA = 0x20000; -pub const FILE_ATTRIBUTE_OFFLINE = 0x1000; -pub const FILE_ATTRIBUTE_READONLY = 0x1; -pub const FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS = 0x400000; -pub const FILE_ATTRIBUTE_RECALL_ON_OPEN = 0x40000; -pub const FILE_ATTRIBUTE_REPARSE_POINT = 0x400; -pub const FILE_ATTRIBUTE_SPARSE_FILE = 0x200; -pub const FILE_ATTRIBUTE_SYSTEM = 0x4; -pub const FILE_ATTRIBUTE_TEMPORARY = 0x100; -pub const FILE_ATTRIBUTE_VIRTUAL = 0x10000; - -pub const PROCESS_INFORMATION = extern struct { - hProcess: HANDLE, - hThread: HANDLE, - dwProcessId: DWORD, - dwThreadId: DWORD, -}; - -pub const STARTUPINFOW = extern struct { - cb: DWORD, - lpReserved: ?LPWSTR, - lpDesktop: ?LPWSTR, - lpTitle: ?LPWSTR, - dwX: DWORD, - dwY: DWORD, - dwXSize: DWORD, - dwYSize: DWORD, - dwXCountChars: DWORD, - dwYCountChars: DWORD, - dwFillAttribute: DWORD, - dwFlags: DWORD, - wShowWindow: WORD, - cbReserved2: WORD, - lpReserved2: ?LPBYTE, - hStdInput: ?HANDLE, - hStdOutput: ?HANDLE, - hStdError: ?HANDLE, -}; - -pub const STARTF_FORCEONFEEDBACK = 0x00000040; -pub const STARTF_FORCEOFFFEEDBACK = 0x00000080; -pub const STARTF_PREVENTPINNING = 0x00002000; -pub const STARTF_RUNFULLSCREEN = 0x00000020; -pub const STARTF_TITLEISAPPID = 0x00001000; -pub const STARTF_TITLEISLINKNAME = 0x00000800; -pub const STARTF_UNTRUSTEDSOURCE = 0x00008000; -pub const STARTF_USECOUNTCHARS = 0x00000008; -pub const STARTF_USEFILLATTRIBUTE = 0x00000010; -pub const STARTF_USEHOTKEY = 0x00000200; -pub const STARTF_USEPOSITION = 0x00000004; -pub const STARTF_USESHOWWINDOW = 0x00000001; -pub const STARTF_USESIZE = 0x00000002; -pub const STARTF_USESTDHANDLES = 0x00000100; - -pub const INFINITE = 4294967295; - -pub const WAIT_ABANDONED = 0x00000080; -pub const WAIT_OBJECT_0 = 0x00000000; -pub const WAIT_TIMEOUT = 0x00000102; -pub const WAIT_FAILED = 0xFFFFFFFF; - -pub const HANDLE_FLAG_INHERIT = 0x00000001; -pub const HANDLE_FLAG_PROTECT_FROM_CLOSE = 0x00000002; - -pub const MOVEFILE_COPY_ALLOWED = 2; -pub const MOVEFILE_CREATE_HARDLINK = 16; -pub const MOVEFILE_DELAY_UNTIL_REBOOT = 4; -pub const MOVEFILE_FAIL_IF_NOT_TRACKABLE = 32; -pub const MOVEFILE_REPLACE_EXISTING = 1; -pub const MOVEFILE_WRITE_THROUGH = 8; - -pub const FILE_BEGIN = 0; -pub const FILE_CURRENT = 1; -pub const FILE_END = 2; - -pub const HEAP_CREATE_ENABLE_EXECUTE = 0x00040000; -pub const HEAP_GENERATE_EXCEPTIONS = 0x00000004; -pub const HEAP_NO_SERIALIZE = 0x00000001; - -// AllocationType values -pub const MEM_COMMIT = 0x1000; -pub const MEM_RESERVE = 0x2000; -pub const MEM_RESET = 0x80000; -pub const MEM_RESET_UNDO = 0x1000000; -pub const MEM_LARGE_PAGES = 0x20000000; -pub const MEM_PHYSICAL = 0x400000; -pub const MEM_TOP_DOWN = 0x100000; -pub const MEM_WRITE_WATCH = 0x200000; - -// Protect values -pub const PAGE_EXECUTE = 0x10; -pub const PAGE_EXECUTE_READ = 0x20; -pub const PAGE_EXECUTE_READWRITE = 0x40; -pub const PAGE_EXECUTE_WRITECOPY = 0x80; -pub const PAGE_NOACCESS = 0x01; -pub const PAGE_READONLY = 0x02; -pub const PAGE_READWRITE = 0x04; -pub const PAGE_WRITECOPY = 0x08; -pub const PAGE_TARGETS_INVALID = 0x40000000; -pub const PAGE_TARGETS_NO_UPDATE = 0x40000000; // Same as PAGE_TARGETS_INVALID -pub const PAGE_GUARD = 0x100; -pub const PAGE_NOCACHE = 0x200; -pub const PAGE_WRITECOMBINE = 0x400; - -// FreeType values -pub const MEM_COALESCE_PLACEHOLDERS = 0x1; -pub const MEM_RESERVE_PLACEHOLDERS = 0x2; -pub const MEM_DECOMMIT = 0x4000; -pub const MEM_RELEASE = 0x8000; - -pub const PTHREAD_START_ROUTINE = extern fn (LPVOID) DWORD; -pub const LPTHREAD_START_ROUTINE = PTHREAD_START_ROUTINE; - -pub const WIN32_FIND_DATAW = extern struct { - dwFileAttributes: DWORD, - ftCreationTime: FILETIME, - ftLastAccessTime: FILETIME, - ftLastWriteTime: FILETIME, - nFileSizeHigh: DWORD, - nFileSizeLow: DWORD, - dwReserved0: DWORD, - dwReserved1: DWORD, - cFileName: [260]u16, - cAlternateFileName: [14]u16, -}; - -pub const FILETIME = extern struct { - dwLowDateTime: DWORD, - dwHighDateTime: DWORD, -}; - -pub const SYSTEM_INFO = extern struct { - anon1: extern union { - dwOemId: DWORD, - anon2: extern struct { - wProcessorArchitecture: WORD, - wReserved: WORD, - }, - }, - dwPageSize: DWORD, - lpMinimumApplicationAddress: LPVOID, - lpMaximumApplicationAddress: LPVOID, - dwActiveProcessorMask: DWORD_PTR, - dwNumberOfProcessors: DWORD, - dwProcessorType: DWORD, - dwAllocationGranularity: DWORD, - wProcessorLevel: WORD, - wProcessorRevision: WORD, -}; - -pub const HRESULT = c_long; - -pub const KNOWNFOLDERID = GUID; -pub const GUID = extern struct { - Data1: c_ulong, - Data2: c_ushort, - Data3: c_ushort, - Data4: [8]u8, - - pub fn parse(str: []const u8) GUID { - var guid: GUID = undefined; - var index: usize = 0; - assert(str[index] == '{'); - index += 1; - - guid.Data1 = std.fmt.parseUnsigned(c_ulong, str[index .. index + 8], 16) catch unreachable; - index += 8; - - assert(str[index] == '-'); - index += 1; - - guid.Data2 = std.fmt.parseUnsigned(c_ushort, str[index .. index + 4], 16) catch unreachable; - index += 4; - - assert(str[index] == '-'); - index += 1; - - guid.Data3 = std.fmt.parseUnsigned(c_ushort, str[index .. index + 4], 16) catch unreachable; - index += 4; - - assert(str[index] == '-'); - index += 1; - - guid.Data4[0] = std.fmt.parseUnsigned(u8, str[index .. index + 2], 16) catch unreachable; - index += 2; - guid.Data4[1] = std.fmt.parseUnsigned(u8, str[index .. index + 2], 16) catch unreachable; - index += 2; - - assert(str[index] == '-'); - index += 1; - - var i: usize = 2; - while (i < guid.Data4.len) : (i += 1) { - guid.Data4[i] = std.fmt.parseUnsigned(u8, str[index .. index + 2], 16) catch unreachable; - index += 2; - } - - assert(str[index] == '}'); - index += 1; - return guid; - } -}; - -pub const FOLDERID_LocalAppData = GUID.parse("{F1B32785-6FBA-4FCF-9D55-7B8E7F157091}"); - -pub const KF_FLAG_DEFAULT = 0; -pub const KF_FLAG_NO_APPCONTAINER_REDIRECTION = 65536; -pub const KF_FLAG_CREATE = 32768; -pub const KF_FLAG_DONT_VERIFY = 16384; -pub const KF_FLAG_DONT_UNEXPAND = 8192; -pub const KF_FLAG_NO_ALIAS = 4096; -pub const KF_FLAG_INIT = 2048; -pub const KF_FLAG_DEFAULT_PATH = 1024; -pub const KF_FLAG_NOT_PARENT_RELATIVE = 512; -pub const KF_FLAG_SIMPLE_IDLIST = 256; -pub const KF_FLAG_ALIAS_ONLY = -2147483648; - -pub const S_OK = 0; -pub const E_NOTIMPL = @bitCast(c_long, c_ulong(0x80004001)); -pub const E_NOINTERFACE = @bitCast(c_long, c_ulong(0x80004002)); -pub const E_POINTER = @bitCast(c_long, c_ulong(0x80004003)); -pub const E_ABORT = @bitCast(c_long, c_ulong(0x80004004)); -pub const E_FAIL = @bitCast(c_long, c_ulong(0x80004005)); -pub const E_UNEXPECTED = @bitCast(c_long, c_ulong(0x8000FFFF)); -pub const E_ACCESSDENIED = @bitCast(c_long, c_ulong(0x80070005)); -pub const E_HANDLE = @bitCast(c_long, c_ulong(0x80070006)); -pub const E_OUTOFMEMORY = @bitCast(c_long, c_ulong(0x8007000E)); -pub const E_INVALIDARG = @bitCast(c_long, c_ulong(0x80070057)); - -pub const FILE_FLAG_BACKUP_SEMANTICS = 0x02000000; -pub const FILE_FLAG_DELETE_ON_CLOSE = 0x04000000; -pub const FILE_FLAG_NO_BUFFERING = 0x20000000; -pub const FILE_FLAG_OPEN_NO_RECALL = 0x00100000; -pub const FILE_FLAG_OPEN_REPARSE_POINT = 0x00200000; -pub const FILE_FLAG_OVERLAPPED = 0x40000000; -pub const FILE_FLAG_POSIX_SEMANTICS = 0x0100000; -pub const FILE_FLAG_RANDOM_ACCESS = 0x10000000; -pub const FILE_FLAG_SESSION_AWARE = 0x00800000; -pub const FILE_FLAG_SEQUENTIAL_SCAN = 0x08000000; -pub const FILE_FLAG_WRITE_THROUGH = 0x80000000; - -pub const SMALL_RECT = extern struct { - Left: SHORT, - Top: SHORT, - Right: SHORT, - Bottom: SHORT, -}; - -pub const COORD = extern struct { - X: SHORT, - Y: SHORT, -}; - -pub const CREATE_UNICODE_ENVIRONMENT = 1024; - -pub const TLS_OUT_OF_INDEXES = 4294967295; -pub const IMAGE_TLS_DIRECTORY = extern struct { - StartAddressOfRawData: usize, - EndAddressOfRawData: usize, - AddressOfIndex: usize, - AddressOfCallBacks: usize, - SizeOfZeroFill: u32, - Characteristics: u32, -}; -pub const IMAGE_TLS_DIRECTORY64 = IMAGE_TLS_DIRECTORY; -pub const IMAGE_TLS_DIRECTORY32 = IMAGE_TLS_DIRECTORY; - -pub const PIMAGE_TLS_CALLBACK = ?extern fn (PVOID, DWORD, PVOID) void; - -pub const PROV_RSA_FULL = 1; - -pub const REGSAM = ACCESS_MASK; -pub const ACCESS_MASK = DWORD; -pub const PHKEY = *HKEY; -pub const HKEY = *HKEY__; -pub const HKEY__ = extern struct { - unused: c_int, -}; -pub const LSTATUS = LONG; - -pub const FILE_NOTIFY_INFORMATION = extern struct { - NextEntryOffset: DWORD, - Action: DWORD, - FileNameLength: DWORD, - FileName: [1]WCHAR, -}; - -pub const FILE_ACTION_ADDED = 0x00000001; -pub const FILE_ACTION_REMOVED = 0x00000002; -pub const FILE_ACTION_MODIFIED = 0x00000003; -pub const FILE_ACTION_RENAMED_OLD_NAME = 0x00000004; -pub const FILE_ACTION_RENAMED_NEW_NAME = 0x00000005; - -pub const LPOVERLAPPED_COMPLETION_ROUTINE = ?extern fn (DWORD, DWORD, *OVERLAPPED) void; - -pub const FILE_LIST_DIRECTORY = 1; - -pub const FILE_NOTIFY_CHANGE_CREATION = 64; -pub const FILE_NOTIFY_CHANGE_SIZE = 8; -pub const FILE_NOTIFY_CHANGE_SECURITY = 256; -pub const FILE_NOTIFY_CHANGE_LAST_ACCESS = 32; -pub const FILE_NOTIFY_CHANGE_LAST_WRITE = 16; -pub const FILE_NOTIFY_CHANGE_DIR_NAME = 2; -pub const FILE_NOTIFY_CHANGE_FILE_NAME = 1; -pub const FILE_NOTIFY_CHANGE_ATTRIBUTES = 4; - -pub const CONSOLE_SCREEN_BUFFER_INFO = extern struct { - dwSize: COORD, - dwCursorPosition: COORD, - wAttributes: WORD, - srWindow: SMALL_RECT, - dwMaximumWindowSize: COORD, -}; - -pub const FOREGROUND_BLUE = 1; -pub const FOREGROUND_GREEN = 2; -pub const FOREGROUND_RED = 4; -pub const FOREGROUND_INTENSITY = 8; - -pub const LIST_ENTRY = extern struct { - Flink: *LIST_ENTRY, - Blink: *LIST_ENTRY, -}; - -pub const RTL_CRITICAL_SECTION_DEBUG = extern struct { - Type: WORD, - CreatorBackTraceIndex: WORD, - CriticalSection: *RTL_CRITICAL_SECTION, - ProcessLocksList: LIST_ENTRY, - EntryCount: DWORD, - ContentionCount: DWORD, - Flags: DWORD, - CreatorBackTraceIndexHigh: WORD, - SpareWORD: WORD, -}; - -pub const RTL_CRITICAL_SECTION = extern struct { - DebugInfo: *RTL_CRITICAL_SECTION_DEBUG, - LockCount: LONG, - RecursionCount: LONG, - OwningThread: HANDLE, - LockSemaphore: HANDLE, - SpinCount: ULONG_PTR, -}; - -pub const CRITICAL_SECTION = RTL_CRITICAL_SECTION; -pub const INIT_ONCE = RTL_RUN_ONCE; -pub const INIT_ONCE_STATIC_INIT = RTL_RUN_ONCE_INIT; -pub const INIT_ONCE_FN = extern fn (InitOnce: *INIT_ONCE, Parameter: ?*c_void, Context: ?*c_void) BOOL; - -pub const RTL_RUN_ONCE = extern struct { - Ptr: ?*c_void, -}; - -pub const RTL_RUN_ONCE_INIT = RTL_RUN_ONCE{ .Ptr = null }; - -pub const COINIT_APARTMENTTHREADED = COINIT.COINIT_APARTMENTTHREADED; -pub const COINIT_MULTITHREADED = COINIT.COINIT_MULTITHREADED; -pub const COINIT_DISABLE_OLE1DDE = COINIT.COINIT_DISABLE_OLE1DDE; -pub const COINIT_SPEED_OVER_MEMORY = COINIT.COINIT_SPEED_OVER_MEMORY; -pub const COINIT = extern enum { - COINIT_APARTMENTTHREADED = 2, - COINIT_MULTITHREADED = 0, - COINIT_DISABLE_OLE1DDE = 4, - COINIT_SPEED_OVER_MEMORY = 8, -}; - -/// > The maximum path of 32,767 characters is approximate, because the "\\?\" -/// > prefix may be expanded to a longer string by the system at run time, and -/// > this expansion applies to the total length. -/// from https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#maximum-path-length-limitation -pub const PATH_MAX_WIDE = 32767; +pub use @import("windows/bits.zig"); pub const CreateFileError = error{ SharingViolation, @@ -765,7 +247,7 @@ pub fn ReadFile(in_hFile: HANDLE, buffer: []u8) ReadFileError!usize { while (index < buffer.len) { const want_read_count = @intCast(DWORD, math.min(DWORD(maxInt(DWORD)), buffer.len - index)); var amt_read: DWORD = undefined; - if (kernel32.ReadFile(fd, buffer.ptr + index, want_read_count, &amt_read, null) == 0) { + if (kernel32.ReadFile(in_hFile, buffer.ptr + index, want_read_count, &amt_read, null) == 0) { switch (kernel32.GetLastError()) { ERROR.OPERATION_ABORTED => continue, ERROR.BROKEN_PIPE => return index, @@ -787,7 +269,7 @@ pub const WriteFileError = error{ /// This function is for blocking file descriptors only. For non-blocking, see /// `WriteFileAsync`. -pub fn WriteFile(in_hFile: HANDLE, bytes: []const u8) WriteFileError!void { +pub fn WriteFile(handle: HANDLE, bytes: []const u8) WriteFileError!void { var bytes_written: DWORD = undefined; // TODO replace this @intCast with a loop that writes all the bytes if (kernel32.WriteFile(handle, bytes.ptr, @intCast(u32, bytes.len), &bytes_written, null) == 0) { @@ -808,7 +290,7 @@ pub const GetCurrentDirectoryError = error{ Unexpected, }; -/// The result is a slice of out_buffer, indexed from 0. +/// The result is a slice of `buffer`, indexed from 0. pub fn GetCurrentDirectory(buffer: []u8) GetCurrentDirectoryError![]u8 { var utf16le_buf: [PATH_MAX_WIDE]u16 = undefined; const result = kernel32.GetCurrentDirectoryW(utf16le_buf.len, &utf16le_buf); @@ -821,13 +303,14 @@ pub fn GetCurrentDirectory(buffer: []u8) GetCurrentDirectoryError![]u8 { const utf16le_slice = utf16le_buf[0..result]; // Trust that Windows gives us valid UTF-16LE. var end_index: usize = 0; - var it = std.unicode.Utf16LeIterator.init(utf16le); + var it = std.unicode.Utf16LeIterator.init(utf16le_slice); while (it.nextCodepoint() catch unreachable) |codepoint| { - if (end_index + std.unicode.utf8CodepointSequenceLength(codepoint) >= out_buffer.len) + const seq_len = std.unicode.utf8CodepointSequenceLength(codepoint) catch unreachable; + if (end_index + seq_len >= buffer.len) return error.NameTooLong; - end_index += utf8Encode(codepoint, out_buffer[end_index..]) catch unreachable; + end_index += std.unicode.utf8Encode(codepoint, buffer[end_index..]) catch unreachable; } - return out_buffer[0..end_index]; + return buffer[0..end_index]; } pub const CreateSymbolicLinkError = error{Unexpected}; @@ -1218,6 +701,10 @@ pub fn QueryPerformanceCounter() u64 { return @bitCast(u64, result); } +pub fn InitOnceExecuteOnce(InitOnce: *INIT_ONCE, InitFn: INIT_ONCE_FN, Parameter: ?*c_void, Context: ?*c_void) void { + assert(kernel32.InitOnceExecuteOnce(InitOnce, InitFn, Parameter, Context) != 0); +} + pub fn cStrToPrefixedFileW(s: [*]const u8) ![PATH_MAX_WIDE + 1]u16 { return sliceToPrefixedFileW(mem.toSliceConst(u8, s)); } diff --git a/std/os/windows/advapi32.zig b/std/os/windows/advapi32.zig index 6acb8a2c88..df3220e24c 100644 --- a/std/os/windows/advapi32.zig +++ b/std/os/windows/advapi32.zig @@ -1,4 +1,4 @@ -use @import("../windows.zig"); +use @import("bits.zig"); pub extern "advapi32" stdcallcc fn RegOpenKeyExW( hKey: HKEY, diff --git a/std/os/windows/bits.zig b/std/os/windows/bits.zig new file mode 100644 index 0000000000..0bf2991903 --- /dev/null +++ b/std/os/windows/bits.zig @@ -0,0 +1,527 @@ +// Platform-dependent types and values that are used along with OS-specific APIs. + +const builtin = @import("builtin"); +const std = @import("../../std.zig"); +const assert = std.debug.assert; +const maxInt = std.math.maxInt; + +pub const ERROR = @import("error.zig"); + +/// The standard input device. Initially, this is the console input buffer, CONIN$. +pub const STD_INPUT_HANDLE = maxInt(DWORD) - 10 + 1; + +/// The standard output device. Initially, this is the active console screen buffer, CONOUT$. +pub const STD_OUTPUT_HANDLE = maxInt(DWORD) - 11 + 1; + +/// The standard error device. Initially, this is the active console screen buffer, CONOUT$. +pub const STD_ERROR_HANDLE = maxInt(DWORD) - 12 + 1; + +pub const SHORT = c_short; +pub const BOOL = c_int; +pub const BOOLEAN = BYTE; +pub const BYTE = u8; +pub const CHAR = u8; +pub const DWORD = u32; +pub const FLOAT = f32; +pub const HANDLE = *c_void; +pub const HCRYPTPROV = ULONG_PTR; +pub const HINSTANCE = *@OpaqueType(); +pub const HMODULE = *@OpaqueType(); +pub const FARPROC = *@OpaqueType(); +pub const INT = c_int; +pub const LPBYTE = *BYTE; +pub const LPCH = *CHAR; +pub const LPCSTR = [*]const CHAR; +pub const LPCTSTR = [*]const TCHAR; +pub const LPCVOID = *const c_void; +pub const LPDWORD = *DWORD; +pub const LPSTR = [*]CHAR; +pub const LPTSTR = if (UNICODE) LPWSTR else LPSTR; +pub const LPVOID = *c_void; +pub const LPWSTR = [*]WCHAR; +pub const LPCWSTR = [*]const WCHAR; +pub const PVOID = *c_void; +pub const PWSTR = [*]WCHAR; +pub const SIZE_T = usize; +pub const TCHAR = if (UNICODE) WCHAR else u8; +pub const UINT = c_uint; +pub const ULONG_PTR = usize; +pub const DWORD_PTR = ULONG_PTR; +pub const UNICODE = false; +pub const WCHAR = u16; +pub const WORD = u16; +pub const LARGE_INTEGER = i64; +pub const ULONG = u32; +pub const LONG = i32; +pub const ULONGLONG = u64; +pub const LONGLONG = i64; + +pub const TRUE = 1; +pub const FALSE = 0; + +pub const INVALID_HANDLE_VALUE = @intToPtr(HANDLE, maxInt(usize)); + +pub const INVALID_FILE_ATTRIBUTES = DWORD(maxInt(DWORD)); + +pub const OVERLAPPED = extern struct { + Internal: ULONG_PTR, + InternalHigh: ULONG_PTR, + Offset: DWORD, + OffsetHigh: DWORD, + hEvent: ?HANDLE, +}; +pub const LPOVERLAPPED = *OVERLAPPED; + +pub const MAX_PATH = 260; + +// TODO issue #305 +pub const FILE_INFO_BY_HANDLE_CLASS = u32; +pub const FileBasicInfo = 0; +pub const FileStandardInfo = 1; +pub const FileNameInfo = 2; +pub const FileRenameInfo = 3; +pub const FileDispositionInfo = 4; +pub const FileAllocationInfo = 5; +pub const FileEndOfFileInfo = 6; +pub const FileStreamInfo = 7; +pub const FileCompressionInfo = 8; +pub const FileAttributeTagInfo = 9; +pub const FileIdBothDirectoryInfo = 10; +pub const FileIdBothDirectoryRestartInfo = 11; +pub const FileIoPriorityHintInfo = 12; +pub const FileRemoteProtocolInfo = 13; +pub const FileFullDirectoryInfo = 14; +pub const FileFullDirectoryRestartInfo = 15; +pub const FileStorageInfo = 16; +pub const FileAlignmentInfo = 17; +pub const FileIdInfo = 18; +pub const FileIdExtdDirectoryInfo = 19; +pub const FileIdExtdDirectoryRestartInfo = 20; + +pub const FILE_NAME_INFO = extern struct { + FileNameLength: DWORD, + FileName: [1]WCHAR, +}; + +/// Return the normalized drive name. This is the default. +pub const FILE_NAME_NORMALIZED = 0x0; + +/// Return the opened file name (not normalized). +pub const FILE_NAME_OPENED = 0x8; + +/// Return the path with the drive letter. This is the default. +pub const VOLUME_NAME_DOS = 0x0; + +/// Return the path with a volume GUID path instead of the drive name. +pub const VOLUME_NAME_GUID = 0x1; + +/// Return the path with no drive information. +pub const VOLUME_NAME_NONE = 0x4; + +/// Return the path with the volume device path. +pub const VOLUME_NAME_NT = 0x2; + +pub const SECURITY_ATTRIBUTES = extern struct { + nLength: DWORD, + lpSecurityDescriptor: ?*c_void, + bInheritHandle: BOOL, +}; +pub const PSECURITY_ATTRIBUTES = *SECURITY_ATTRIBUTES; +pub const LPSECURITY_ATTRIBUTES = *SECURITY_ATTRIBUTES; + +pub const GENERIC_READ = 0x80000000; +pub const GENERIC_WRITE = 0x40000000; +pub const GENERIC_EXECUTE = 0x20000000; +pub const GENERIC_ALL = 0x10000000; + +pub const FILE_SHARE_DELETE = 0x00000004; +pub const FILE_SHARE_READ = 0x00000001; +pub const FILE_SHARE_WRITE = 0x00000002; + +pub const CREATE_ALWAYS = 2; +pub const CREATE_NEW = 1; +pub const OPEN_ALWAYS = 4; +pub const OPEN_EXISTING = 3; +pub const TRUNCATE_EXISTING = 5; + +pub const FILE_ATTRIBUTE_ARCHIVE = 0x20; +pub const FILE_ATTRIBUTE_COMPRESSED = 0x800; +pub const FILE_ATTRIBUTE_DEVICE = 0x40; +pub const FILE_ATTRIBUTE_DIRECTORY = 0x10; +pub const FILE_ATTRIBUTE_ENCRYPTED = 0x4000; +pub const FILE_ATTRIBUTE_HIDDEN = 0x2; +pub const FILE_ATTRIBUTE_INTEGRITY_STREAM = 0x8000; +pub const FILE_ATTRIBUTE_NORMAL = 0x80; +pub const FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 0x2000; +pub const FILE_ATTRIBUTE_NO_SCRUB_DATA = 0x20000; +pub const FILE_ATTRIBUTE_OFFLINE = 0x1000; +pub const FILE_ATTRIBUTE_READONLY = 0x1; +pub const FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS = 0x400000; +pub const FILE_ATTRIBUTE_RECALL_ON_OPEN = 0x40000; +pub const FILE_ATTRIBUTE_REPARSE_POINT = 0x400; +pub const FILE_ATTRIBUTE_SPARSE_FILE = 0x200; +pub const FILE_ATTRIBUTE_SYSTEM = 0x4; +pub const FILE_ATTRIBUTE_TEMPORARY = 0x100; +pub const FILE_ATTRIBUTE_VIRTUAL = 0x10000; + +pub const PROCESS_INFORMATION = extern struct { + hProcess: HANDLE, + hThread: HANDLE, + dwProcessId: DWORD, + dwThreadId: DWORD, +}; + +pub const STARTUPINFOW = extern struct { + cb: DWORD, + lpReserved: ?LPWSTR, + lpDesktop: ?LPWSTR, + lpTitle: ?LPWSTR, + dwX: DWORD, + dwY: DWORD, + dwXSize: DWORD, + dwYSize: DWORD, + dwXCountChars: DWORD, + dwYCountChars: DWORD, + dwFillAttribute: DWORD, + dwFlags: DWORD, + wShowWindow: WORD, + cbReserved2: WORD, + lpReserved2: ?LPBYTE, + hStdInput: ?HANDLE, + hStdOutput: ?HANDLE, + hStdError: ?HANDLE, +}; + +pub const STARTF_FORCEONFEEDBACK = 0x00000040; +pub const STARTF_FORCEOFFFEEDBACK = 0x00000080; +pub const STARTF_PREVENTPINNING = 0x00002000; +pub const STARTF_RUNFULLSCREEN = 0x00000020; +pub const STARTF_TITLEISAPPID = 0x00001000; +pub const STARTF_TITLEISLINKNAME = 0x00000800; +pub const STARTF_UNTRUSTEDSOURCE = 0x00008000; +pub const STARTF_USECOUNTCHARS = 0x00000008; +pub const STARTF_USEFILLATTRIBUTE = 0x00000010; +pub const STARTF_USEHOTKEY = 0x00000200; +pub const STARTF_USEPOSITION = 0x00000004; +pub const STARTF_USESHOWWINDOW = 0x00000001; +pub const STARTF_USESIZE = 0x00000002; +pub const STARTF_USESTDHANDLES = 0x00000100; + +pub const INFINITE = 4294967295; + +pub const WAIT_ABANDONED = 0x00000080; +pub const WAIT_OBJECT_0 = 0x00000000; +pub const WAIT_TIMEOUT = 0x00000102; +pub const WAIT_FAILED = 0xFFFFFFFF; + +pub const HANDLE_FLAG_INHERIT = 0x00000001; +pub const HANDLE_FLAG_PROTECT_FROM_CLOSE = 0x00000002; + +pub const MOVEFILE_COPY_ALLOWED = 2; +pub const MOVEFILE_CREATE_HARDLINK = 16; +pub const MOVEFILE_DELAY_UNTIL_REBOOT = 4; +pub const MOVEFILE_FAIL_IF_NOT_TRACKABLE = 32; +pub const MOVEFILE_REPLACE_EXISTING = 1; +pub const MOVEFILE_WRITE_THROUGH = 8; + +pub const FILE_BEGIN = 0; +pub const FILE_CURRENT = 1; +pub const FILE_END = 2; + +pub const HEAP_CREATE_ENABLE_EXECUTE = 0x00040000; +pub const HEAP_GENERATE_EXCEPTIONS = 0x00000004; +pub const HEAP_NO_SERIALIZE = 0x00000001; + +// AllocationType values +pub const MEM_COMMIT = 0x1000; +pub const MEM_RESERVE = 0x2000; +pub const MEM_RESET = 0x80000; +pub const MEM_RESET_UNDO = 0x1000000; +pub const MEM_LARGE_PAGES = 0x20000000; +pub const MEM_PHYSICAL = 0x400000; +pub const MEM_TOP_DOWN = 0x100000; +pub const MEM_WRITE_WATCH = 0x200000; + +// Protect values +pub const PAGE_EXECUTE = 0x10; +pub const PAGE_EXECUTE_READ = 0x20; +pub const PAGE_EXECUTE_READWRITE = 0x40; +pub const PAGE_EXECUTE_WRITECOPY = 0x80; +pub const PAGE_NOACCESS = 0x01; +pub const PAGE_READONLY = 0x02; +pub const PAGE_READWRITE = 0x04; +pub const PAGE_WRITECOPY = 0x08; +pub const PAGE_TARGETS_INVALID = 0x40000000; +pub const PAGE_TARGETS_NO_UPDATE = 0x40000000; // Same as PAGE_TARGETS_INVALID +pub const PAGE_GUARD = 0x100; +pub const PAGE_NOCACHE = 0x200; +pub const PAGE_WRITECOMBINE = 0x400; + +// FreeType values +pub const MEM_COALESCE_PLACEHOLDERS = 0x1; +pub const MEM_RESERVE_PLACEHOLDERS = 0x2; +pub const MEM_DECOMMIT = 0x4000; +pub const MEM_RELEASE = 0x8000; + +pub const PTHREAD_START_ROUTINE = extern fn (LPVOID) DWORD; +pub const LPTHREAD_START_ROUTINE = PTHREAD_START_ROUTINE; + +pub const WIN32_FIND_DATAW = extern struct { + dwFileAttributes: DWORD, + ftCreationTime: FILETIME, + ftLastAccessTime: FILETIME, + ftLastWriteTime: FILETIME, + nFileSizeHigh: DWORD, + nFileSizeLow: DWORD, + dwReserved0: DWORD, + dwReserved1: DWORD, + cFileName: [260]u16, + cAlternateFileName: [14]u16, +}; + +pub const FILETIME = extern struct { + dwLowDateTime: DWORD, + dwHighDateTime: DWORD, +}; + +pub const SYSTEM_INFO = extern struct { + anon1: extern union { + dwOemId: DWORD, + anon2: extern struct { + wProcessorArchitecture: WORD, + wReserved: WORD, + }, + }, + dwPageSize: DWORD, + lpMinimumApplicationAddress: LPVOID, + lpMaximumApplicationAddress: LPVOID, + dwActiveProcessorMask: DWORD_PTR, + dwNumberOfProcessors: DWORD, + dwProcessorType: DWORD, + dwAllocationGranularity: DWORD, + wProcessorLevel: WORD, + wProcessorRevision: WORD, +}; + +pub const HRESULT = c_long; + +pub const KNOWNFOLDERID = GUID; +pub const GUID = extern struct { + Data1: c_ulong, + Data2: c_ushort, + Data3: c_ushort, + Data4: [8]u8, + + pub fn parse(str: []const u8) GUID { + var guid: GUID = undefined; + var index: usize = 0; + assert(str[index] == '{'); + index += 1; + + guid.Data1 = std.fmt.parseUnsigned(c_ulong, str[index .. index + 8], 16) catch unreachable; + index += 8; + + assert(str[index] == '-'); + index += 1; + + guid.Data2 = std.fmt.parseUnsigned(c_ushort, str[index .. index + 4], 16) catch unreachable; + index += 4; + + assert(str[index] == '-'); + index += 1; + + guid.Data3 = std.fmt.parseUnsigned(c_ushort, str[index .. index + 4], 16) catch unreachable; + index += 4; + + assert(str[index] == '-'); + index += 1; + + guid.Data4[0] = std.fmt.parseUnsigned(u8, str[index .. index + 2], 16) catch unreachable; + index += 2; + guid.Data4[1] = std.fmt.parseUnsigned(u8, str[index .. index + 2], 16) catch unreachable; + index += 2; + + assert(str[index] == '-'); + index += 1; + + var i: usize = 2; + while (i < guid.Data4.len) : (i += 1) { + guid.Data4[i] = std.fmt.parseUnsigned(u8, str[index .. index + 2], 16) catch unreachable; + index += 2; + } + + assert(str[index] == '}'); + index += 1; + return guid; + } +}; + +pub const FOLDERID_LocalAppData = GUID.parse("{F1B32785-6FBA-4FCF-9D55-7B8E7F157091}"); + +pub const KF_FLAG_DEFAULT = 0; +pub const KF_FLAG_NO_APPCONTAINER_REDIRECTION = 65536; +pub const KF_FLAG_CREATE = 32768; +pub const KF_FLAG_DONT_VERIFY = 16384; +pub const KF_FLAG_DONT_UNEXPAND = 8192; +pub const KF_FLAG_NO_ALIAS = 4096; +pub const KF_FLAG_INIT = 2048; +pub const KF_FLAG_DEFAULT_PATH = 1024; +pub const KF_FLAG_NOT_PARENT_RELATIVE = 512; +pub const KF_FLAG_SIMPLE_IDLIST = 256; +pub const KF_FLAG_ALIAS_ONLY = -2147483648; + +pub const S_OK = 0; +pub const E_NOTIMPL = @bitCast(c_long, c_ulong(0x80004001)); +pub const E_NOINTERFACE = @bitCast(c_long, c_ulong(0x80004002)); +pub const E_POINTER = @bitCast(c_long, c_ulong(0x80004003)); +pub const E_ABORT = @bitCast(c_long, c_ulong(0x80004004)); +pub const E_FAIL = @bitCast(c_long, c_ulong(0x80004005)); +pub const E_UNEXPECTED = @bitCast(c_long, c_ulong(0x8000FFFF)); +pub const E_ACCESSDENIED = @bitCast(c_long, c_ulong(0x80070005)); +pub const E_HANDLE = @bitCast(c_long, c_ulong(0x80070006)); +pub const E_OUTOFMEMORY = @bitCast(c_long, c_ulong(0x8007000E)); +pub const E_INVALIDARG = @bitCast(c_long, c_ulong(0x80070057)); + +pub const FILE_FLAG_BACKUP_SEMANTICS = 0x02000000; +pub const FILE_FLAG_DELETE_ON_CLOSE = 0x04000000; +pub const FILE_FLAG_NO_BUFFERING = 0x20000000; +pub const FILE_FLAG_OPEN_NO_RECALL = 0x00100000; +pub const FILE_FLAG_OPEN_REPARSE_POINT = 0x00200000; +pub const FILE_FLAG_OVERLAPPED = 0x40000000; +pub const FILE_FLAG_POSIX_SEMANTICS = 0x0100000; +pub const FILE_FLAG_RANDOM_ACCESS = 0x10000000; +pub const FILE_FLAG_SESSION_AWARE = 0x00800000; +pub const FILE_FLAG_SEQUENTIAL_SCAN = 0x08000000; +pub const FILE_FLAG_WRITE_THROUGH = 0x80000000; + +pub const SMALL_RECT = extern struct { + Left: SHORT, + Top: SHORT, + Right: SHORT, + Bottom: SHORT, +}; + +pub const COORD = extern struct { + X: SHORT, + Y: SHORT, +}; + +pub const CREATE_UNICODE_ENVIRONMENT = 1024; + +pub const TLS_OUT_OF_INDEXES = 4294967295; +pub const IMAGE_TLS_DIRECTORY = extern struct { + StartAddressOfRawData: usize, + EndAddressOfRawData: usize, + AddressOfIndex: usize, + AddressOfCallBacks: usize, + SizeOfZeroFill: u32, + Characteristics: u32, +}; +pub const IMAGE_TLS_DIRECTORY64 = IMAGE_TLS_DIRECTORY; +pub const IMAGE_TLS_DIRECTORY32 = IMAGE_TLS_DIRECTORY; + +pub const PIMAGE_TLS_CALLBACK = ?extern fn (PVOID, DWORD, PVOID) void; + +pub const PROV_RSA_FULL = 1; + +pub const REGSAM = ACCESS_MASK; +pub const ACCESS_MASK = DWORD; +pub const PHKEY = *HKEY; +pub const HKEY = *HKEY__; +pub const HKEY__ = extern struct { + unused: c_int, +}; +pub const LSTATUS = LONG; + +pub const FILE_NOTIFY_INFORMATION = extern struct { + NextEntryOffset: DWORD, + Action: DWORD, + FileNameLength: DWORD, + FileName: [1]WCHAR, +}; + +pub const FILE_ACTION_ADDED = 0x00000001; +pub const FILE_ACTION_REMOVED = 0x00000002; +pub const FILE_ACTION_MODIFIED = 0x00000003; +pub const FILE_ACTION_RENAMED_OLD_NAME = 0x00000004; +pub const FILE_ACTION_RENAMED_NEW_NAME = 0x00000005; + +pub const LPOVERLAPPED_COMPLETION_ROUTINE = ?extern fn (DWORD, DWORD, *OVERLAPPED) void; + +pub const FILE_LIST_DIRECTORY = 1; + +pub const FILE_NOTIFY_CHANGE_CREATION = 64; +pub const FILE_NOTIFY_CHANGE_SIZE = 8; +pub const FILE_NOTIFY_CHANGE_SECURITY = 256; +pub const FILE_NOTIFY_CHANGE_LAST_ACCESS = 32; +pub const FILE_NOTIFY_CHANGE_LAST_WRITE = 16; +pub const FILE_NOTIFY_CHANGE_DIR_NAME = 2; +pub const FILE_NOTIFY_CHANGE_FILE_NAME = 1; +pub const FILE_NOTIFY_CHANGE_ATTRIBUTES = 4; + +pub const CONSOLE_SCREEN_BUFFER_INFO = extern struct { + dwSize: COORD, + dwCursorPosition: COORD, + wAttributes: WORD, + srWindow: SMALL_RECT, + dwMaximumWindowSize: COORD, +}; + +pub const FOREGROUND_BLUE = 1; +pub const FOREGROUND_GREEN = 2; +pub const FOREGROUND_RED = 4; +pub const FOREGROUND_INTENSITY = 8; + +pub const LIST_ENTRY = extern struct { + Flink: *LIST_ENTRY, + Blink: *LIST_ENTRY, +}; + +pub const RTL_CRITICAL_SECTION_DEBUG = extern struct { + Type: WORD, + CreatorBackTraceIndex: WORD, + CriticalSection: *RTL_CRITICAL_SECTION, + ProcessLocksList: LIST_ENTRY, + EntryCount: DWORD, + ContentionCount: DWORD, + Flags: DWORD, + CreatorBackTraceIndexHigh: WORD, + SpareWORD: WORD, +}; + +pub const RTL_CRITICAL_SECTION = extern struct { + DebugInfo: *RTL_CRITICAL_SECTION_DEBUG, + LockCount: LONG, + RecursionCount: LONG, + OwningThread: HANDLE, + LockSemaphore: HANDLE, + SpinCount: ULONG_PTR, +}; + +pub const CRITICAL_SECTION = RTL_CRITICAL_SECTION; +pub const INIT_ONCE = RTL_RUN_ONCE; +pub const INIT_ONCE_STATIC_INIT = RTL_RUN_ONCE_INIT; +pub const INIT_ONCE_FN = extern fn (InitOnce: *INIT_ONCE, Parameter: ?*c_void, Context: ?*c_void) BOOL; + +pub const RTL_RUN_ONCE = extern struct { + Ptr: ?*c_void, +}; + +pub const RTL_RUN_ONCE_INIT = RTL_RUN_ONCE{ .Ptr = null }; + +pub const COINIT_APARTMENTTHREADED = COINIT.COINIT_APARTMENTTHREADED; +pub const COINIT_MULTITHREADED = COINIT.COINIT_MULTITHREADED; +pub const COINIT_DISABLE_OLE1DDE = COINIT.COINIT_DISABLE_OLE1DDE; +pub const COINIT_SPEED_OVER_MEMORY = COINIT.COINIT_SPEED_OVER_MEMORY; +pub const COINIT = extern enum { + COINIT_APARTMENTTHREADED = 2, + COINIT_MULTITHREADED = 0, + COINIT_DISABLE_OLE1DDE = 4, + COINIT_SPEED_OVER_MEMORY = 8, +}; + +/// > The maximum path of 32,767 characters is approximate, because the "\\?\" +/// > prefix may be expanded to a longer string by the system at run time, and +/// > this expansion applies to the total length. +/// from https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#maximum-path-length-limitation +pub const PATH_MAX_WIDE = 32767; diff --git a/std/os/windows/kernel32.zig b/std/os/windows/kernel32.zig index b14e404c13..2bd1824620 100644 --- a/std/os/windows/kernel32.zig +++ b/std/os/windows/kernel32.zig @@ -1,4 +1,4 @@ -use @import("../windows.zig"); +use @import("bits.zig"); pub extern "kernel32" stdcallcc fn CancelIoEx(hFile: HANDLE, lpOverlapped: LPOVERLAPPED) BOOL; diff --git a/std/os/windows/ntdll.zig b/std/os/windows/ntdll.zig index 1b63c1e7ec..60c03ffc07 100644 --- a/std/os/windows/ntdll.zig +++ b/std/os/windows/ntdll.zig @@ -1,3 +1,3 @@ -use @import("../windows.zig"); +use @import("bits.zig"); pub extern "NtDll" stdcallcc fn RtlCaptureStackBackTrace(FramesToSkip: DWORD, FramesToCapture: DWORD, BackTrace: **c_void, BackTraceHash: ?*DWORD) WORD; diff --git a/std/os/windows/ole32.zig b/std/os/windows/ole32.zig index a69137fcab..80f5bebc36 100644 --- a/std/os/windows/ole32.zig +++ b/std/os/windows/ole32.zig @@ -1,4 +1,4 @@ -use @import("../windows.zig"); +use @import("bits.zig"); pub extern "ole32" stdcallcc fn CoTaskMemFree(pv: LPVOID) void; pub extern "ole32" stdcallcc fn CoUninitialize() void; diff --git a/std/os/windows/shell32.zig b/std/os/windows/shell32.zig index 84accecb70..9f24acc5c4 100644 --- a/std/os/windows/shell32.zig +++ b/std/os/windows/shell32.zig @@ -1,3 +1,3 @@ -use @import("../windows.zig"); +use @import("bits.zig"); pub extern "shell32" stdcallcc fn SHGetKnownFolderPath(rfid: *const KNOWNFOLDERID, dwFlags: DWORD, hToken: ?HANDLE, ppszPath: *[*]WCHAR) HRESULT; diff --git a/std/pdb.zig b/std/pdb.zig index 2822d2546e..ffe2120296 100644 --- a/std/pdb.zig +++ b/std/pdb.zig @@ -662,7 +662,7 @@ const MsfStream = struct { } fn seekBy(self: *MsfStream, len: i64) !void { - self.pos += len; + self.pos = @intCast(u64, @intCast(i64, self.pos) + len); if (self.pos >= self.blocks.len * self.block_size) return error.EOF; } diff --git a/std/process.zig b/std/process.zig index b45074d67c..7c5e7fbb07 100644 --- a/std/process.zig +++ b/std/process.zig @@ -144,7 +144,7 @@ pub fn getEnvVarOwned(allocator: *mem.Allocator, key: []const u8) GetEnvVarOwned windows_buf_len, ) catch |err| switch (err) { error.Unexpected => return error.EnvironmentVariableNotFound, - else => return err, + else => |e| return e, }; if (result > buf.len) { @@ -156,7 +156,7 @@ pub fn getEnvVarOwned(allocator: *mem.Allocator, key: []const u8) GetEnvVarOwned error.DanglingSurrogateHalf => return error.InvalidUtf8, error.ExpectedSecondSurrogateHalf => return error.InvalidUtf8, error.UnexpectedSecondSurrogateHalf => return error.InvalidUtf8, - else => return err, + else => |e| return e, }; } } else { diff --git a/std/special/bootstrap.zig b/std/special/bootstrap.zig index bf14116edf..45ce23c00f 100644 --- a/std/special/bootstrap.zig +++ b/std/special/bootstrap.zig @@ -62,7 +62,7 @@ extern fn WinMainCRTStartup() noreturn { if (!builtin.single_threaded) { _ = @import("bootstrap_windows_tls.zig"); } - std.os.windows.ExitProcess(callMain()); + std.os.windows.kernel32.ExitProcess(callMain()); } // TODO https://github.com/ziglang/zig/issues/265 diff --git a/std/statically_initialized_mutex.zig b/std/statically_initialized_mutex.zig index 9c59fccefe..9c8b91c632 100644 --- a/std/statically_initialized_mutex.zig +++ b/std/statically_initialized_mutex.zig @@ -23,7 +23,7 @@ pub const StaticallyInitializedMutex = switch (builtin.os) { mutex: *StaticallyInitializedMutex, pub fn release(self: Held) void { - windows.LeaveCriticalSection(&self.mutex.lock); + windows.kernel32.LeaveCriticalSection(&self.mutex.lock); } }; @@ -40,20 +40,20 @@ pub const StaticallyInitializedMutex = switch (builtin.os) { Context: ?*c_void, ) windows.BOOL { const lock = @ptrCast(*windows.CRITICAL_SECTION, @alignCast(@alignOf(windows.CRITICAL_SECTION), Parameter)); - windows.InitializeCriticalSection(lock); + windows.kernel32.InitializeCriticalSection(lock); return windows.TRUE; } /// TODO: once https://github.com/ziglang/zig/issues/287 is solved and std.Mutex has a better /// implementation of a runtime initialized mutex, remove this function. pub fn deinit(self: *StaticallyInitializedMutex) void { - assert(windows.InitOnceExecuteOnce(&self.init_once, initCriticalSection, &self.lock, null) != 0); - windows.DeleteCriticalSection(&self.lock); + windows.InitOnceExecuteOnce(&self.init_once, initCriticalSection, &self.lock, null); + windows.kernel32.DeleteCriticalSection(&self.lock); } pub fn acquire(self: *StaticallyInitializedMutex) Held { - assert(windows.InitOnceExecuteOnce(&self.init_once, initCriticalSection, &self.lock, null) != 0); - windows.EnterCriticalSection(&self.lock); + windows.InitOnceExecuteOnce(&self.init_once, initCriticalSection, &self.lock, null); + windows.kernel32.EnterCriticalSection(&self.lock); return Held{ .mutex = self }; } }, -- cgit v1.2.3 From abf959a0c90418c383cc4f10242dbcc2a2e974fd Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 27 May 2019 02:16:05 -0400 Subject: fix debug builds of WASI --- doc/langref.html.in | 2 +- std/debug.zig | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) (limited to 'std/debug.zig') diff --git a/doc/langref.html.in b/doc/langref.html.in index 45b85abad8..61db4ed598 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -4531,7 +4531,7 @@ const mem = std.mem; test "cast *[1][*]const u8 to [*]const ?[*]const u8" { const window_name = [1][*]const u8{c"window name"}; const x: [*]const ?[*]const u8 = &window_name; - assert(mem.eql(u8, std.cstr.toSliceConst(x[0].?), "window name")); + assert(mem.eql(u8, std.mem.toSliceConst(u8, x[0].?), "window name")); } {#code_end#} {#header_close#} diff --git a/std/debug.zig b/std/debug.zig index b62bd5da64..e2f4a67755 100644 --- a/std/debug.zig +++ b/std/debug.zig @@ -85,6 +85,10 @@ fn wantTtyColor() bool { /// TODO multithreaded awareness pub fn dumpCurrentStackTrace(start_addr: ?usize) void { const stderr = getStderrStream() catch return; + if (os.wasi.is_the_target) { + stderr.print("Unable to dump stack trace: unimplemented on WASI\n") catch return; + return; + } const debug_info = getSelfDebugInfo() catch |err| { stderr.print("Unable to dump stack trace: Unable to open debug info: {}\n", @errorName(err)) catch return; return; @@ -147,6 +151,10 @@ pub fn captureStackTrace(first_address: ?usize, stack_trace: *builtin.StackTrace /// TODO multithreaded awareness pub fn dumpStackTrace(stack_trace: builtin.StackTrace) void { const stderr = getStderrStream() catch return; + if (os.wasi.is_the_target) { + stderr.print("Unable to dump stack trace: unimplemented on WASI\n") catch return; + return; + } const debug_info = getSelfDebugInfo() catch |err| { stderr.print("Unable to dump stack trace: Unable to open debug info: {}\n", @errorName(err)) catch return; return; -- cgit v1.2.3