zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit d06e5b4349a9670fc4aad7215c7a73b814bbf91b (tree)
parent 3c221463fd89caf08ee1bd24ea03d49e0c6e7f07
Author: Andrew Kelley <andrew@ziglang.org>
Date:   Wed, 17 Apr 2024 14:52:51 -0700

std.fs.Dir.openFile: use wasi libc API when -lc

Also removes the LOCK namespace from std.c.wasi because wasi libc does
not have flock.

closes #19336
related to #19352

Co-authored-by: Ryan Liptak <squeek502@hotmail.com>

Diffstat:
Mlib/std/c/wasi.zig | 6------
Mlib/std/fs/Dir.zig | 22+++++++++++++++-------
Mlib/std/posix.zig | 8++++++++
3 files changed, 23 insertions(+), 13 deletions(-)

diff --git a/lib/std/c/wasi.zig b/lib/std/c/wasi.zig @@ -40,12 +40,6 @@ pub const E = wasi.errno_t; pub const CLOCK = wasi.clockid_t; pub const IOV_MAX = 1024; -pub const LOCK = struct { - pub const SH = 0x1; - pub const EX = 0x2; - pub const NB = 0x4; - pub const UN = 0x8; -}; pub const S = struct { pub const IEXEC = @compileError("TODO audit this"); pub const IFBLK = 0x6000; diff --git a/lib/std/fs/Dir.zig b/lib/std/fs/Dir.zig @@ -800,7 +800,7 @@ pub fn openFile(self: Dir, sub_path: []const u8, flags: File.OpenFlags) File.Ope const path_w = try windows.sliceToPrefixedFileW(self.fd, sub_path); return self.openFileW(path_w.span(), flags); } - if (native_os == .wasi) { + if (native_os == .wasi and !builtin.link_libc) { var base: std.os.wasi.rights_t = .{}; if (flags.isRead()) { base.FD_READ = true; @@ -834,17 +834,25 @@ pub fn openFileZ(self: Dir, sub_path: [*:0]const u8, flags: File.OpenFlags) File const path_w = try windows.cStrToPrefixedFileW(self.fd, sub_path); return self.openFileW(path_w.span(), flags); }, - .wasi => { + // Use the libc API when libc is linked because it implements things + // such as opening absolute file paths. + .wasi => if (!builtin.link_libc) { return openFile(self, mem.sliceTo(sub_path, 0), flags); }, else => {}, } - var os_flags: posix.O = .{ - .ACCMODE = switch (flags.mode) { - .read_only => .RDONLY, - .write_only => .WRONLY, - .read_write => .RDWR, + var os_flags: posix.O = switch (native_os) { + .wasi => .{ + .read = flags.mode != .write_only, + .write = flags.mode != .read_only, + }, + else => .{ + .ACCMODE = switch (flags.mode) { + .read_only => .RDONLY, + .write_only => .WRONLY, + .read_write => .RDWR, + }, }, }; if (@hasField(posix.O, "CLOEXEC")) os_flags.CLOEXEC = true; diff --git a/lib/std/posix.zig b/lib/std/posix.zig @@ -1602,6 +1602,10 @@ pub fn openZ(file_path: [*:0]const u8, flags: O, perm: mode_t) OpenError!fd_t { .PERM => return error.AccessDenied, .EXIST => return error.PathAlreadyExists, .BUSY => return error.DeviceBusy, + .ILSEQ => |err| if (native_os == .wasi) + return error.InvalidUtf8 + else + return unexpectedErrno(err), else => |err| return unexpectedErrno(err), } } @@ -1771,6 +1775,10 @@ pub fn openatZ(dir_fd: fd_t, file_path: [*:0]const u8, flags: O, mode: mode_t) O .OPNOTSUPP => return error.FileLocksNotSupported, .AGAIN => return error.WouldBlock, .TXTBSY => return error.FileBusy, + .ILSEQ => |err| if (native_os == .wasi) + return error.InvalidUtf8 + else + return unexpectedErrno(err), else => |err| return unexpectedErrno(err), } }