srctree

Andrew Kelley parent 3c221463 d06e5b43
std.fs.Dir.openFile: use wasi libc API when -lc

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

closes #19336related to #19352

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

inlinesplit
lib/std/c/wasi.zig added: 26, removed: 16, total 10
@@ -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;
 
lib/std/fs/Dir.zig added: 26, removed: 16, total 10
@@ -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;
 
lib/std/posix.zig added: 26, removed: 16, total 10
@@ -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),
}
}