Commit d4c33129cf

Jakub Konka <kubkon@jakubkonka.com>
2020-05-04 21:47:33
Shuffle things around; add PreopenList.findByPath method
This commit removes `std.os.openatWasi` function, and renames it to `std.os.wasi.openat`. Additionally, the added `PreopenList.findByPath` method allows querying the list for a matching preopen by path.
1 parent dd23835
Changed files (3)
lib/std/os/wasi.zig
@@ -94,11 +94,32 @@ pub const PreopenList = struct {
         }
     }
 
+    pub fn find(self: *const Self, path: []const u8) ?*const Preopen {
+        for (self.buffer.items) |preopen| {
+            switch (preopen.@"type") {
+                PreopenType.Dir => |preopen_path| {
+                    if (mem.eql(u8, path, preopen_path)) return &preopen;
+                },
+            }
+        }
+        return null;
+    }
+
     pub fn asSlice(self: *const Self) []const Preopen {
         return self.buffer.items;
     }
 };
 
+pub fn openat(dir_fd: fd_t, file_path: []const u8, oflags: oflags_t, fdflags: fdflags_t, rights: rights_t) std.os.OpenError!fd_t {
+    var fd: fd_t = undefined;
+    switch (path_open(dir_fd, 0x0, file_path.ptr, file_path.len, oflags, rights, 0x0, fdflags, &fd)) {
+        0 => {},
+        // TODO map errors
+        else => |err| return std.os.unexpectedErrno(err),
+    }
+    return fd;
+}
+
 pub extern "wasi_snapshot_preview1" fn args_get(argv: [*][*:0]u8, argv_buf: [*]u8) errno_t;
 pub extern "wasi_snapshot_preview1" fn args_sizes_get(argc: *usize, argv_buf_size: *usize) errno_t;
 
lib/std/fs.zig
@@ -602,7 +602,7 @@ pub const Dir = struct {
             rights |= wasi.FD_WRITE | wasi.FD_DATASYNC | wasi.FD_SEEK | wasi.FD_FDSTAT_SET_FLAGS | wasi.FD_SYNC | wasi.FD_ALLOCATE | wasi.FD_ADVISE | wasi.FD_FILESTAT_SET_TIMES | wasi.FD_FILESTAT_SET_SIZE;
         }
 
-        const fd = try os.openatWasi(self.fd, sub_path, 0x0, fdflags, rights);
+        const fd = try wasi.openat(self.fd, sub_path, 0x0, fdflags, rights);
 
         return File{ .handle = fd };
     }
@@ -713,7 +713,7 @@ pub const Dir = struct {
             oflags |= wasi.O_EXCL;
         }
 
-        const fd = try os.openatWasi(self.fd, sub_path, oflags, 0x0, rights);
+        const fd = try wasi.openat(self.fd, sub_path, oflags, 0x0, rights);
 
         return File{ .handle = fd };
     }
lib/std/os.zig
@@ -923,16 +923,6 @@ pub fn openat(dir_fd: fd_t, file_path: []const u8, flags: u32, mode: mode_t) Ope
     return openatZ(dir_fd, &file_path_c, flags, mode);
 }
 
-pub fn openatWasi(dir_fd: fd_t, file_path: []const u8, oflags: wasi.oflags_t, fdflags: wasi.fdflags_t, rights: wasi.rights_t) OpenError!fd_t {
-    var fd: fd_t = undefined;
-    switch (wasi.path_open(dir_fd, 0x0, file_path.ptr, file_path.len, oflags, rights, 0x0, fdflags, &fd)) {
-        0 => {},
-        // TODO map errors
-        else => |err| return unexpectedErrno(err),
-    }
-    return fd;
-}
-
 pub const openatC = @compileError("deprecated: renamed to openatZ");
 
 /// Open and possibly create a file. Keeps trying if it gets interrupted.