authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-04-17 14:52:51-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-04-18 17:49:05-07:00
logd06e5b4349a9670fc4aad7215c7a73b814bbf91b
tree0f3eba0ea2f93d44fd6e726c05f65655cb100efd
parent3c221463fd89caf08ee1bd24ea03d49e0c6e7f07

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>

3 files changed, 23 insertions(+), 13 deletions(-)

lib/std/c/wasi.zig-6
...@@ -40,12 +40,6 @@ pub const E = wasi.errno_t;...@@ -40,12 +40,6 @@ pub const E = wasi.errno_t;
4040
41pub const CLOCK = wasi.clockid_t;41pub const CLOCK = wasi.clockid_t;
42pub const IOV_MAX = 1024;42pub const IOV_MAX = 1024;
43pub const LOCK = struct {
44 pub const SH = 0x1;
45 pub const EX = 0x2;
46 pub const NB = 0x4;
47 pub const UN = 0x8;
48};
49pub const S = struct {43pub const S = struct {
50 pub const IEXEC = @compileError("TODO audit this");44 pub const IEXEC = @compileError("TODO audit this");
51 pub const IFBLK = 0x6000;45 pub const IFBLK = 0x6000;
lib/std/fs/Dir.zig+15-7
...@@ -800,7 +800,7 @@ pub fn openFile(self: Dir, sub_path: []const u8, flags: File.OpenFlags) File.Ope...@@ -800,7 +800,7 @@ pub fn openFile(self: Dir, sub_path: []const u8, flags: File.OpenFlags) File.Ope
800 const path_w = try windows.sliceToPrefixedFileW(self.fd, sub_path);800 const path_w = try windows.sliceToPrefixedFileW(self.fd, sub_path);
801 return self.openFileW(path_w.span(), flags);801 return self.openFileW(path_w.span(), flags);
802 }802 }
803 if (native_os == .wasi) {803 if (native_os == .wasi and !builtin.link_libc) {
804 var base: std.os.wasi.rights_t = .{};804 var base: std.os.wasi.rights_t = .{};
805 if (flags.isRead()) {805 if (flags.isRead()) {
806 base.FD_READ = true;806 base.FD_READ = true;
...@@ -834,17 +834,25 @@ pub fn openFileZ(self: Dir, sub_path: [*:0]const u8, flags: File.OpenFlags) File...@@ -834,17 +834,25 @@ pub fn openFileZ(self: Dir, sub_path: [*:0]const u8, flags: File.OpenFlags) File
834 const path_w = try windows.cStrToPrefixedFileW(self.fd, sub_path);834 const path_w = try windows.cStrToPrefixedFileW(self.fd, sub_path);
835 return self.openFileW(path_w.span(), flags);835 return self.openFileW(path_w.span(), flags);
836 },836 },
837 .wasi => {837 // Use the libc API when libc is linked because it implements things
838 // such as opening absolute file paths.
839 .wasi => if (!builtin.link_libc) {
838 return openFile(self, mem.sliceTo(sub_path, 0), flags);840 return openFile(self, mem.sliceTo(sub_path, 0), flags);
839 },841 },
840 else => {},842 else => {},
841 }843 }
842844
843 var os_flags: posix.O = .{845 var os_flags: posix.O = switch (native_os) {
844 .ACCMODE = switch (flags.mode) {846 .wasi => .{
845 .read_only => .RDONLY,847 .read = flags.mode != .write_only,
846 .write_only => .WRONLY,848 .write = flags.mode != .read_only,
847 .read_write => .RDWR,849 },
850 else => .{
851 .ACCMODE = switch (flags.mode) {
852 .read_only => .RDONLY,
853 .write_only => .WRONLY,
854 .read_write => .RDWR,
855 },
848 },856 },
849 };857 };
850 if (@hasField(posix.O, "CLOEXEC")) os_flags.CLOEXEC = true;858 if (@hasField(posix.O, "CLOEXEC")) os_flags.CLOEXEC = true;
lib/std/posix.zig+8
...@@ -1602,6 +1602,10 @@ pub fn openZ(file_path: [*:0]const u8, flags: O, perm: mode_t) OpenError!fd_t {...@@ -1602,6 +1602,10 @@ pub fn openZ(file_path: [*:0]const u8, flags: O, perm: mode_t) OpenError!fd_t {
1602 .PERM => return error.AccessDenied,1602 .PERM => return error.AccessDenied,
1603 .EXIST => return error.PathAlreadyExists,1603 .EXIST => return error.PathAlreadyExists,
1604 .BUSY => return error.DeviceBusy,1604 .BUSY => return error.DeviceBusy,
1605 .ILSEQ => |err| if (native_os == .wasi)
1606 return error.InvalidUtf8
1607 else
1608 return unexpectedErrno(err),
1605 else => |err| return unexpectedErrno(err),1609 else => |err| return unexpectedErrno(err),
1606 }1610 }
1607 }1611 }
...@@ -1771,6 +1775,10 @@ pub fn openatZ(dir_fd: fd_t, file_path: [*:0]const u8, flags: O, mode: mode_t) O...@@ -1771,6 +1775,10 @@ pub fn openatZ(dir_fd: fd_t, file_path: [*:0]const u8, flags: O, mode: mode_t) O
1771 .OPNOTSUPP => return error.FileLocksNotSupported,1775 .OPNOTSUPP => return error.FileLocksNotSupported,
1772 .AGAIN => return error.WouldBlock,1776 .AGAIN => return error.WouldBlock,
1773 .TXTBSY => return error.FileBusy,1777 .TXTBSY => return error.FileBusy,
1778 .ILSEQ => |err| if (native_os == .wasi)
1779 return error.InvalidUtf8
1780 else
1781 return unexpectedErrno(err),
1774 else => |err| return unexpectedErrno(err),1782 else => |err| return unexpectedErrno(err),
1775 }1783 }
1776 }1784 }