authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-10-06 13:02:51-07:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-10-06 13:02:51-07:00
logd5c9d8529534745c9feae6b67cdc01d53f11a0d9
treefb1cb9b34e9f58e40e9097bdfdd486a186063d95
parent3284d1ffb125c460ec8c19323cb53a7b0cea5e5e
parentb01a5c6bb76c2a932efa2f99447dd31367ce8510
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #21597 from achan1989/issue_14324

Create/open file on WASI targets should have POLL_FD_READWRITE rights

2 files changed, 49 insertions(+), 0 deletions(-)

lib/std/fs/Dir.zig+7
...@@ -804,11 +804,14 @@ pub fn openFile(self: Dir, sub_path: []const u8, flags: File.OpenFlags) File.Ope...@@ -804,11 +804,14 @@ pub fn openFile(self: Dir, sub_path: []const u8, flags: File.OpenFlags) File.Ope
804 }804 }
805 if (native_os == .wasi and !builtin.link_libc) {805 if (native_os == .wasi and !builtin.link_libc) {
806 var base: std.os.wasi.rights_t = .{};806 var base: std.os.wasi.rights_t = .{};
807 // POLL_FD_READWRITE only grants extra rights if the corresponding FD_READ and/or FD_WRITE
808 // is also set.
807 if (flags.isRead()) {809 if (flags.isRead()) {
808 base.FD_READ = true;810 base.FD_READ = true;
809 base.FD_TELL = true;811 base.FD_TELL = true;
810 base.FD_SEEK = true;812 base.FD_SEEK = true;
811 base.FD_FILESTAT_GET = true;813 base.FD_FILESTAT_GET = true;
814 base.POLL_FD_READWRITE = true;
812 }815 }
813 if (flags.isWrite()) {816 if (flags.isWrite()) {
814 base.FD_WRITE = true;817 base.FD_WRITE = true;
...@@ -821,6 +824,7 @@ pub fn openFile(self: Dir, sub_path: []const u8, flags: File.OpenFlags) File.Ope...@@ -821,6 +824,7 @@ pub fn openFile(self: Dir, sub_path: []const u8, flags: File.OpenFlags) File.Ope
821 base.FD_ADVISE = true;824 base.FD_ADVISE = true;
822 base.FD_FILESTAT_SET_TIMES = true;825 base.FD_FILESTAT_SET_TIMES = true;
823 base.FD_FILESTAT_SET_SIZE = true;826 base.FD_FILESTAT_SET_SIZE = true;
827 base.POLL_FD_READWRITE = true;
824 }828 }
825 const fd = try posix.openatWasi(self.fd, sub_path, .{}, .{}, .{}, base, .{});829 const fd = try posix.openatWasi(self.fd, sub_path, .{}, .{}, .{}, base, .{});
826 return .{ .handle = fd };830 return .{ .handle = fd };
...@@ -982,6 +986,9 @@ pub fn createFile(self: Dir, sub_path: []const u8, flags: File.CreateFlags) File...@@ -982,6 +986,9 @@ pub fn createFile(self: Dir, sub_path: []const u8, flags: File.CreateFlags) File
982 .FD_FILESTAT_SET_TIMES = true,986 .FD_FILESTAT_SET_TIMES = true,
983 .FD_FILESTAT_SET_SIZE = true,987 .FD_FILESTAT_SET_SIZE = true,
984 .FD_FILESTAT_GET = true,988 .FD_FILESTAT_GET = true,
989 // POLL_FD_READWRITE only grants extra rights if the corresponding FD_READ and/or
990 // FD_WRITE is also set.
991 .POLL_FD_READWRITE = true,
985 }, .{}),992 }, .{}),
986 };993 };
987 }994 }
lib/std/os/wasi.zig+42
...@@ -315,35 +315,77 @@ pub const SOCK = struct {...@@ -315,35 +315,77 @@ pub const SOCK = struct {
315};315};
316316
317pub const rights_t = packed struct(u64) {317pub const rights_t = packed struct(u64) {
318 /// The right to invoke fd_datasync. If PATH_OPEN is set, includes the right to invoke
319 /// path_open with fdflags_t.dsync.
318 FD_DATASYNC: bool = false,320 FD_DATASYNC: bool = false,
321 /// The right to invoke fd_read and sock_recv. If FD_SEEK is set, includes the right to invoke
322 /// fd_pread.
319 FD_READ: bool = false,323 FD_READ: bool = false,
324 /// The right to invoke fd_seek. This flag implies FD_TELL.
320 FD_SEEK: bool = false,325 FD_SEEK: bool = false,
326 /// The right to invoke fd_fdstat_set_flags.
321 FD_FDSTAT_SET_FLAGS: bool = false,327 FD_FDSTAT_SET_FLAGS: bool = false,
328 /// The right to invoke fd_sync. If PATH_OPEN is set, includes the right to invoke path_open
329 /// with fdflags_t.RSYNC and fdflags_t.DSYNC.
322 FD_SYNC: bool = false,330 FD_SYNC: bool = false,
331 /// The right to invoke fd_seek in such a way that the file offset remains unaltered (i.e.
332 /// whence_t.CUR with offset zero), or to invoke fd_tell.
323 FD_TELL: bool = false,333 FD_TELL: bool = false,
334 /// The right to invoke fd_write and sock_send. If FD_SEEK is set, includes the right to invoke
335 /// fd_pwrite.
324 FD_WRITE: bool = false,336 FD_WRITE: bool = false,
337 /// The right to invoke fd_advise.
325 FD_ADVISE: bool = false,338 FD_ADVISE: bool = false,
339 /// The right to invoke fd_allocate.
326 FD_ALLOCATE: bool = false,340 FD_ALLOCATE: bool = false,
341 /// The right to invoke path_create_directory.
327 PATH_CREATE_DIRECTORY: bool = false,342 PATH_CREATE_DIRECTORY: bool = false,
343 /// If PATH_OPEN is set, the right to invoke path_open with oflags_t.CREAT.
328 PATH_CREATE_FILE: bool = false,344 PATH_CREATE_FILE: bool = false,
345 /// The right to invoke path_link with the file descriptor as the source directory.
329 PATH_LINK_SOURCE: bool = false,346 PATH_LINK_SOURCE: bool = false,
347 /// The right to invoke path_link with the file descriptor as the target directory.
330 PATH_LINK_TARGET: bool = false,348 PATH_LINK_TARGET: bool = false,
349 /// The right to invoke path_open.
331 PATH_OPEN: bool = false,350 PATH_OPEN: bool = false,
351 /// The right to invoke fd_readdir.
332 FD_READDIR: bool = false,352 FD_READDIR: bool = false,
353 /// The right to invoke path_readlink.
333 PATH_READLINK: bool = false,354 PATH_READLINK: bool = false,
355 /// The right to invoke path_rename with the file descriptor as the source directory.
334 PATH_RENAME_SOURCE: bool = false,356 PATH_RENAME_SOURCE: bool = false,
357 /// The right to invoke path_rename with the file descriptor as the target directory.
335 PATH_RENAME_TARGET: bool = false,358 PATH_RENAME_TARGET: bool = false,
359 /// The right to invoke path_filestat_get.
336 PATH_FILESTAT_GET: bool = false,360 PATH_FILESTAT_GET: bool = false,
361 /// The right to change a file's size. If PATH_OPEN is set, includes the right to invoke
362 /// path_open with oflags_t.TRUNC. Note: there is no function named path_filestat_set_size.
363 /// This follows POSIX design, which only has ftruncate and does not provide ftruncateat. While
364 /// such function would be desirable from the API design perspective, there are virtually no
365 /// use cases for it since no code written for POSIX systems would use it. Moreover,
366 /// implementing it would require multiple syscalls, leading to inferior performance.
337 PATH_FILESTAT_SET_SIZE: bool = false,367 PATH_FILESTAT_SET_SIZE: bool = false,
368 /// The right to invoke path_filestat_set_times.
338 PATH_FILESTAT_SET_TIMES: bool = false,369 PATH_FILESTAT_SET_TIMES: bool = false,
370 /// The right to invoke fd_filestat_get.
339 FD_FILESTAT_GET: bool = false,371 FD_FILESTAT_GET: bool = false,
372 /// The right to invoke fd_filestat_set_size.
340 FD_FILESTAT_SET_SIZE: bool = false,373 FD_FILESTAT_SET_SIZE: bool = false,
374 /// The right to invoke fd_filestat_set_times.
341 FD_FILESTAT_SET_TIMES: bool = false,375 FD_FILESTAT_SET_TIMES: bool = false,
376 /// The right to invoke path_symlink.
342 PATH_SYMLINK: bool = false,377 PATH_SYMLINK: bool = false,
378 /// The right to invoke path_remove_directory.
343 PATH_REMOVE_DIRECTORY: bool = false,379 PATH_REMOVE_DIRECTORY: bool = false,
380 /// The right to invoke path_unlink_file.
344 PATH_UNLINK_FILE: bool = false,381 PATH_UNLINK_FILE: bool = false,
382 /// If FD_READ is set, includes the right to invoke poll_oneoff to subscribe to
383 /// eventtype_t.FD_READ. If FD_WRITE is set, includes the right to invoke poll_oneoff to
384 /// subscribe to eventtype_t.FD_WRITE.
345 POLL_FD_READWRITE: bool = false,385 POLL_FD_READWRITE: bool = false,
386 /// The right to invoke sock_shutdown.
346 SOCK_SHUTDOWN: bool = false,387 SOCK_SHUTDOWN: bool = false,
388 /// The right to invoke sock_accept.
347 SOCK_ACCEPT: bool = false,389 SOCK_ACCEPT: bool = false,
348 _: u34 = 0,390 _: u34 = 0,
349};391};