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) {