From fae4af9e1cf0383a74da808678d9369553bd878b Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Mon, 18 May 2020 17:01:02 +0200 Subject: [PATCH] Make mode_t a 0-byte type in WASI --- lib/std/fs/file.zig | 4 ++-- lib/std/os.zig | 4 ++-- lib/std/os/bits/wasi.zig | 44 +++++++++++++++++++++++++++++++++++++--- 3 files changed, 45 insertions(+), 7 deletions(-) diff --git a/lib/std/fs/file.zig b/lib/std/fs/file.zig index 1546461898..7e6a7dde63 100644 --- a/lib/std/fs/file.zig +++ b/lib/std/fs/file.zig @@ -30,6 +30,7 @@ pub const File = struct { pub const default_mode = switch (builtin.os.tag) { .windows => 0, + .wasi => 0, else => 0o666, }; @@ -267,11 +268,10 @@ pub const File = struct { const atime = st.atime(); const mtime = st.mtime(); const ctime = st.ctime(); - const m = if (builtin.os.tag == .wasi) 0 else st.mode; return Stat{ .inode = st.ino, .size = @bitCast(u64, st.size), - .mode = m, + .mode = st.mode, .atime = @as(i64, atime.tv_sec) * std.time.ns_per_s + atime.tv_nsec, .mtime = @as(i64, mtime.tv_sec) * std.time.ns_per_s + mtime.tv_nsec, .ctime = @as(i64, ctime.tv_sec) * std.time.ns_per_s + ctime.tv_nsec, diff --git a/lib/std/os.zig b/lib/std/os.zig index b259bb00db..bc8803e3b0 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -2904,9 +2904,9 @@ pub const FStatError = error{ pub fn fstat(fd: fd_t) FStatError!Stat { if (builtin.os.tag == .wasi) { - var stat: Stat = undefined; + var stat: wasi.filestat_t = undefined; switch (wasi.fd_filestat_get(fd, &stat)) { - wasi.ESUCCESS => return stat, + wasi.ESUCCESS => return Stat.fromFilestat(stat), wasi.EINVAL => unreachable, wasi.EBADF => unreachable, // Always a race condition. wasi.ENOMEM => return error.SystemResources, diff --git a/lib/std/os/bits/wasi.zig b/lib/std/os/bits/wasi.zig index 880cf934de..270345c53b 100644 --- a/lib/std/os/bits/wasi.zig +++ b/lib/std/os/bits/wasi.zig @@ -3,11 +3,11 @@ pub const STDIN_FILENO = 0; pub const STDOUT_FILENO = 1; pub const STDERR_FILENO = 2; -pub const mode_t = u32; +pub const mode_t = u0; pub const time_t = i64; // match https://github.com/CraneStation/wasi-libc -pub const timespec = extern struct { +pub const timespec = struct { tv_sec: time_t, tv_nsec: isize, @@ -26,7 +26,45 @@ pub const timespec = extern struct { } }; -pub const Stat = filestat_t; +pub const Stat = struct { + dev: device_t, + ino: inode_t, + mode: mode_t, + filetype: filetype_t, + nlink: linkcount_t, + size: filesize_t, + atim: timespec, + mtim: timespec, + ctim: timespec, + + const Self = @This(); + + pub fn fromFilestat(stat: filestat_t) Self { + return Self{ + .dev = stat.dev, + .ino = stat.ino, + .mode = 0, + .filetype = stat.filetype, + .nlink = stat.nlink, + .size = stat.size, + .atim = stat.atime(), + .mtim = stat.mtime(), + .ctim = stat.ctime(), + }; + } + + pub fn atime(self: Self) timespec { + return self.atim; + } + + pub fn mtime(self: Self) timespec { + return self.mtim; + } + + pub fn ctime(self: Self) timespec { + return self.ctim; + } +}; pub const AT_REMOVEDIR: u32 = 1; // there's no AT_REMOVEDIR in WASI, but we simulate here to match other OSes