Commit fae4af9e1c

Jakub Konka <kubkon@jakubkonka.com>
2020-05-18 17:01:02
Make mode_t a 0-byte type in WASI
1 parent 4081206
Changed files (3)
lib
std
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,
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
 
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,