From ccce5502b65a747421afed5946335cec073f1a0e Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Tue, 23 May 2023 00:41:20 +0200 Subject: [PATCH] Check the file type after open() in os.openFile() when using WASI When the `O_DIRECTORY` flag is not present, Wasmtime now opens files and directories the same way, and returns a descriptor in any case. So, explicitly check the file type after opening it, and return `err.IsDir` if what we actually have is a directory. Fixes the `lib/std/fs` `"file operations on directories"` test on WASI with Wasmtime 9.0.0 --- lib/std/fs.zig | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/std/fs.zig b/lib/std/fs.zig index 041e49d5498d..c5439d71e505 100644 --- a/lib/std/fs.zig +++ b/lib/std/fs.zig @@ -1135,6 +1135,11 @@ pub const Dir = struct { w.RIGHT.FD_FILESTAT_SET_SIZE; } const fd = try os.openatWasi(self.fd, sub_path, 0x0, 0x0, fdflags, base, 0x0); + errdefer os.close(fd); + const fstat = try os.fstat(fd); + const is_dir = fstat.mode & os.S.IFMT == os.S.IFDIR; + if (is_dir) return error.IsDir; + return File{ .handle = fd }; } @@ -1192,6 +1197,11 @@ pub const Dir = struct { }); } } + if (builtin.target.os.tag == .wasi) { + const fstat = try os.fstat(fd); + const is_dir = fstat.mode & os.S.IFMT == os.S.IFDIR; + if (is_dir) return error.IsDir; + } if (has_flock_open_flags and flags.lock_nonblocking) { var fl_flags = os.fcntl(fd, os.F.GETFL, 0) catch |err| switch (err) {