Skip to content

Commit d5c9d85

Browse files
authored
Merge pull request #21597 from achan1989/issue_14324
Create/open file on WASI targets should have POLL_FD_READWRITE rights
2 parents 3284d1f + b01a5c6 commit d5c9d85

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

lib/std/fs/Dir.zig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,11 +804,14 @@ pub fn openFile(self: Dir, sub_path: []const u8, flags: File.OpenFlags) File.Ope
804804
}
805805
if (native_os == .wasi and !builtin.link_libc) {
806806
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.
807809
if (flags.isRead()) {
808810
base.FD_READ = true;
809811
base.FD_TELL = true;
810812
base.FD_SEEK = true;
811813
base.FD_FILESTAT_GET = true;
814+
base.POLL_FD_READWRITE = true;
812815
}
813816
if (flags.isWrite()) {
814817
base.FD_WRITE = true;
@@ -821,6 +824,7 @@ pub fn openFile(self: Dir, sub_path: []const u8, flags: File.OpenFlags) File.Ope
821824
base.FD_ADVISE = true;
822825
base.FD_FILESTAT_SET_TIMES = true;
823826
base.FD_FILESTAT_SET_SIZE = true;
827+
base.POLL_FD_READWRITE = true;
824828
}
825829
const fd = try posix.openatWasi(self.fd, sub_path, .{}, .{}, .{}, base, .{});
826830
return .{ .handle = fd };
@@ -982,6 +986,9 @@ pub fn createFile(self: Dir, sub_path: []const u8, flags: File.CreateFlags) File
982986
.FD_FILESTAT_SET_TIMES = true,
983987
.FD_FILESTAT_SET_SIZE = true,
984988
.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,
985992
}, .{}),
986993
};
987994
}

lib/std/os/wasi.zig

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,35 +315,77 @@ pub const SOCK = struct {
315315
};
316316

317317
pub 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.
318320
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.
319323
FD_READ: bool = false,
324+
/// The right to invoke fd_seek. This flag implies FD_TELL.
320325
FD_SEEK: bool = false,
326+
/// The right to invoke fd_fdstat_set_flags.
321327
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.
322330
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.
323333
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.
324336
FD_WRITE: bool = false,
337+
/// The right to invoke fd_advise.
325338
FD_ADVISE: bool = false,
339+
/// The right to invoke fd_allocate.
326340
FD_ALLOCATE: bool = false,
341+
/// The right to invoke path_create_directory.
327342
PATH_CREATE_DIRECTORY: bool = false,
343+
/// If PATH_OPEN is set, the right to invoke path_open with oflags_t.CREAT.
328344
PATH_CREATE_FILE: bool = false,
345+
/// The right to invoke path_link with the file descriptor as the source directory.
329346
PATH_LINK_SOURCE: bool = false,
347+
/// The right to invoke path_link with the file descriptor as the target directory.
330348
PATH_LINK_TARGET: bool = false,
349+
/// The right to invoke path_open.
331350
PATH_OPEN: bool = false,
351+
/// The right to invoke fd_readdir.
332352
FD_READDIR: bool = false,
353+
/// The right to invoke path_readlink.
333354
PATH_READLINK: bool = false,
355+
/// The right to invoke path_rename with the file descriptor as the source directory.
334356
PATH_RENAME_SOURCE: bool = false,
357+
/// The right to invoke path_rename with the file descriptor as the target directory.
335358
PATH_RENAME_TARGET: bool = false,
359+
/// The right to invoke path_filestat_get.
336360
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.
337367
PATH_FILESTAT_SET_SIZE: bool = false,
368+
/// The right to invoke path_filestat_set_times.
338369
PATH_FILESTAT_SET_TIMES: bool = false,
370+
/// The right to invoke fd_filestat_get.
339371
FD_FILESTAT_GET: bool = false,
372+
/// The right to invoke fd_filestat_set_size.
340373
FD_FILESTAT_SET_SIZE: bool = false,
374+
/// The right to invoke fd_filestat_set_times.
341375
FD_FILESTAT_SET_TIMES: bool = false,
376+
/// The right to invoke path_symlink.
342377
PATH_SYMLINK: bool = false,
378+
/// The right to invoke path_remove_directory.
343379
PATH_REMOVE_DIRECTORY: bool = false,
380+
/// The right to invoke path_unlink_file.
344381
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.
345385
POLL_FD_READWRITE: bool = false,
386+
/// The right to invoke sock_shutdown.
346387
SOCK_SHUTDOWN: bool = false,
388+
/// The right to invoke sock_accept.
347389
SOCK_ACCEPT: bool = false,
348390
_: u34 = 0,
349391
};

0 commit comments

Comments
 (0)