diff options
| author | Jakub Konka <kubkon@jakubkonka.com> | 2020-05-05 15:07:21 +0200 |
|---|---|---|
| committer | Jakub Konka <kubkon@jakubkonka.com> | 2020-05-05 15:08:52 +0200 |
| commit | 558bb24601c6de5f05cded4e99b795e85bb3b8e6 (patch) | |
| tree | c14c072c0421ef1b8253064419732941dd6e6e8b /lib/std/os | |
| parent | d4c33129cf86441a59079e15fd887711fae8d924 (diff) | |
| download | zig-558bb24601c6de5f05cded4e99b795e85bb3b8e6.tar.gz zig-558bb24601c6de5f05cded4e99b795e85bb3b8e6.zip | |
Move preopen and path wasi helpers to std.fs.wasi module
Previously, the path and preopens helpers were prototyped in `std.os.wasi`
module, but since they are higher-level abstraction over wasi, they belong in
`std.fs.wasi` module.
Diffstat (limited to 'lib/std/os')
| -rw-r--r-- | lib/std/os/wasi.zig | 100 |
1 files changed, 0 insertions, 100 deletions
diff --git a/lib/std/os/wasi.zig b/lib/std/os/wasi.zig index cb0ad5c3c6..23df66f385 100644 --- a/lib/std/os/wasi.zig +++ b/lib/std/os/wasi.zig @@ -2,9 +2,7 @@ // * typenames -- https://github.com/WebAssembly/WASI/blob/master/phases/snapshot/witx/typenames.witx // * module -- https://github.com/WebAssembly/WASI/blob/master/phases/snapshot/witx/wasi_snapshot_preview1.witx const std = @import("std"); -const mem = std.mem; const assert = std.debug.assert; -const Allocator = mem.Allocator; pub usingnamespace @import("bits.zig"); @@ -22,104 +20,6 @@ comptime { pub const iovec_t = iovec; pub const ciovec_t = iovec_const; -pub const PreopenType = enum { - Dir, -}; - -pub const Preopen = struct { - fd: fd_t, - @"type": union(PreopenType) { - Dir: []const u8, - }, - - const Self = @This(); - - pub fn newDir(fd: fd_t, path: []const u8) Self { - return Self{ - .fd = fd, - .@"type" = .{ .Dir = path }, - }; - } -}; - -pub const PreopenList = struct { - const InnerList = std.ArrayList(Preopen); - - buffer: InnerList, - - const Self = @This(); - pub const Error = std.os.UnexpectedError || Allocator.Error; - - pub fn init(allocator: *Allocator) Self { - return Self{ .buffer = InnerList.init(allocator) }; - } - - pub fn deinit(pm: Self) void { - for (pm.buffer.items) |preopen| { - switch (preopen.@"type") { - PreopenType.Dir => |path| pm.buffer.allocator.free(path), - } - } - pm.buffer.deinit(); - } - - pub fn populate(self: *Self) Error!void { - errdefer self.deinit(); - var fd: fd_t = 3; // start fd has to be beyond stdio fds - - while (true) { - var buf: prestat_t = undefined; - switch (fd_prestat_get(fd, &buf)) { - ESUCCESS => {}, - ENOTSUP => { - // not a preopen, so keep going - continue; - }, - EBADF => { - // OK, no more fds available - break; - }, - else => |err| return std.os.unexpectedErrno(err), - } - const preopen_len = buf.u.dir.pr_name_len; - const path_buf = try self.buffer.allocator.alloc(u8, preopen_len); - mem.set(u8, path_buf, 0); - switch (fd_prestat_dir_name(fd, path_buf.ptr, preopen_len)) { - ESUCCESS => {}, - else => |err| return std.os.unexpectedErrno(err), - } - const preopen = Preopen.newDir(fd, path_buf); - try self.buffer.append(preopen); - fd += 1; - } - } - - 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; |
