|
| 1 | +const std = @import("std"); |
| 2 | +const uv = @import("uv.zig"); |
| 3 | +const coro = @import("zigcoro"); |
| 4 | + |
| 5 | +const log = std.log.scoped(.uvzig); |
| 6 | + |
| 7 | +pub const File = struct { |
| 8 | + loop: *uv.uv_loop_t, |
| 9 | + fd: c_int, |
| 10 | + |
| 11 | + const Data = struct { |
| 12 | + frame: coro.Frame, |
| 13 | + done: bool = false, |
| 14 | + fn init() @This() { |
| 15 | + return .{ .frame = coro.xframe() }; |
| 16 | + } |
| 17 | + }; |
| 18 | + |
| 19 | + pub fn open(loop: *uv.uv_loop_t, path: [:0]const u8, flags: c_int, mode: c_int) !@This() { |
| 20 | + log.debug("open {s} flags={b} mode={o}", .{ path, flags, mode }); |
| 21 | + var data = Data.init(); |
| 22 | + var req = uv.uv_fs_t{}; |
| 23 | + defer uv.uv_fs_req_cleanup(&req); |
| 24 | + uv.setReqData(&req, &data); |
| 25 | + try uv.check(uv.uv_fs_open(loop, &req, path, flags, mode, xresume)); |
| 26 | + while (!data.done) coro.xsuspend(); |
| 27 | + try uv.check(req.result); |
| 28 | + |
| 29 | + return .{ |
| 30 | + .loop = loop, |
| 31 | + .fd = @intCast(req.result), |
| 32 | + }; |
| 33 | + } |
| 34 | + |
| 35 | + pub fn close(self: @This()) void { |
| 36 | + var req = uv.uv_fs_t{}; |
| 37 | + defer uv.uv_fs_req_cleanup(&req); |
| 38 | + var data = Data.init(); |
| 39 | + uv.setReqData(&req, &data); |
| 40 | + uv.check(uv.uv_fs_close(self.loop, &req, self.fd, xresume)) catch {}; |
| 41 | + coro.xsuspend(); |
| 42 | + } |
| 43 | + |
| 44 | + fn xresume(req: [*c]uv.uv_fs_t) callconv(.C) void { |
| 45 | + const data = uv.getReqData(req, Data); |
| 46 | + data.done = true; |
| 47 | + coro.xresume(data.frame); |
| 48 | + } |
| 49 | + |
| 50 | + const Error = error{ |
| 51 | + Read, |
| 52 | + Write, |
| 53 | + } || uv.Error; |
| 54 | + const Reader = std.io.Reader(@This(), Error, read); |
| 55 | + |
| 56 | + fn read(self: @This(), buf: []u8) Error!usize { |
| 57 | + log.debug("read {d}", .{buf.len}); |
| 58 | + var req = uv.uv_fs_t{}; |
| 59 | + defer uv.uv_fs_req_cleanup(&req); |
| 60 | + var data = Data.init(); |
| 61 | + uv.setReqData(&req, &data); |
| 62 | + var uvbuf = uv.newbuf(buf); |
| 63 | + try uv.check(uv.uv_fs_read(self.loop, &req, self.fd, &uvbuf, 1, -1, xresume)); |
| 64 | + coro.xsuspend(); |
| 65 | + if (req.result < 0) { |
| 66 | + return Error.Read; |
| 67 | + } else { |
| 68 | + return @intCast(req.result); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + const Writer = std.io.Writer(@This(), Error, write); |
| 73 | + |
| 74 | + fn write(self: @This(), buf: []const u8) Error!usize { |
| 75 | + log.debug("write", .{}); |
| 76 | + var req = uv.uv_fs_t{}; |
| 77 | + defer uv.uv_fs_req_cleanup(&req); |
| 78 | + var data = Data.init(); |
| 79 | + uv.setReqData(&req, &data); |
| 80 | + var uvbuf = uv.newbuf(@constCast(buf)); |
| 81 | + try uv.check(uv.uv_fs_write(self.loop, &req, self.fd, &uvbuf, 1, -1, xresume)); |
| 82 | + coro.xsuspend(); |
| 83 | + if (req.result < 0) { |
| 84 | + return Error.Write; |
| 85 | + } else { |
| 86 | + return @intCast(req.result); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + pub fn writer(self: @This()) Writer { |
| 91 | + return .{ .context = self }; |
| 92 | + } |
| 93 | + |
| 94 | + pub fn reader(self: @This()) Reader { |
| 95 | + return .{ .context = self }; |
| 96 | + } |
| 97 | + |
| 98 | + pub fn stat(self: @This()) !uv.uv_stat_t { |
| 99 | + var req = uv.uv_fs_t{}; |
| 100 | + defer uv.uv_fs_req_cleanup(&req); |
| 101 | + var data = Data.init(); |
| 102 | + uv.setReqData(&req, &data); |
| 103 | + try uv.check(uv.uv_fs_fstat(self.loop, &req, self.fd, xresume)); |
| 104 | + coro.xsuspend(); |
| 105 | + try uv.check(req.result); |
| 106 | + return uv.uv_fs_get_statbuf(&req).*; |
| 107 | + } |
| 108 | + |
| 109 | + pub fn fsync(self: @This()) !void { |
| 110 | + var req = uv.uv_fs_t{}; |
| 111 | + defer uv.uv_fs_req_cleanup(&req); |
| 112 | + var data = Data.init(); |
| 113 | + uv.setReqData(&req, &data); |
| 114 | + try uv.check(uv.uv_fs_fsync(self.loop, &req, self.fd, xresume)); |
| 115 | + coro.xsuspend(); |
| 116 | + try uv.check(req.result); |
| 117 | + } |
| 118 | + |
| 119 | + pub fn sendfile(self: @This(), dst: c_int, src_offset: ?usize, n: usize) !usize { |
| 120 | + log.debug("send {d}", .{n}); |
| 121 | + var req = uv.uv_fs_t{}; |
| 122 | + defer uv.uv_fs_req_cleanup(&req); |
| 123 | + var data = Data.init(); |
| 124 | + uv.setReqData(&req, &data); |
| 125 | + |
| 126 | + try uv.check(uv.uv_fs_sendfile(self.loop, &req, dst, self.fd, @intCast(src_offset orelse 0), n, xresume)); |
| 127 | + coro.xsuspend(); |
| 128 | + |
| 129 | + try uv.check(req.result); |
| 130 | + return @intCast(req.result); |
| 131 | + } |
| 132 | + |
| 133 | + // TODO: |
| 134 | + // chown |
| 135 | + // utime |
| 136 | + // chmod |
| 137 | + // datasync |
| 138 | +}; |
| 139 | + |
| 140 | +pub const fs = struct { |
| 141 | + pub fn mkdir(loop: *uv.uv_loop_t, path: [:0]const u8, mode: c_int) !void { |
| 142 | + var req: uv_fs_cb = undefined; |
| 143 | + req.init(); |
| 144 | + defer req.deinit(); |
| 145 | + try uv.check(uv.uv_fs_mkdir(loop, &req.req, path.ptr, mode, uv_fs_cb.cb)); |
| 146 | + coro.xsuspend(); |
| 147 | + } |
| 148 | + |
| 149 | + pub fn mkdtemp(loop: *uv.uv_loop_t, tpl: [:0]const u8, out: []u8) ![:0]const u8 { |
| 150 | + var req: uv_fs_cb = undefined; |
| 151 | + req.init(); |
| 152 | + defer req.deinit(); |
| 153 | + try uv.check(uv.uv_fs_mkdtemp(loop, &req.req, tpl.ptr, uv_fs_cb.cb)); |
| 154 | + coro.xsuspend(); |
| 155 | + const n = std.mem.len(req.req.path); |
| 156 | + if (n >= out.len) return error.BufTooSmall; |
| 157 | + std.mem.copyForwards(u8, out, req.req.path[0..n]); |
| 158 | + out[n] = 0; |
| 159 | + return out[0..n :0]; |
| 160 | + } |
| 161 | + |
| 162 | + const TmpFileInfo = struct { |
| 163 | + path: ?[:0]const u8, |
| 164 | + fd: uv.uv_file, |
| 165 | + }; |
| 166 | + pub fn mkstemp(loop: *uv.uv_loop_t, tpl: [:0]const u8, out: ?[]u8) !TmpFileInfo { |
| 167 | + var req: uv_fs_cb = undefined; |
| 168 | + req.init(); |
| 169 | + defer req.deinit(); |
| 170 | + try uv.check(uv.uv_fs_mkstemp(loop, &req.req, tpl.ptr, uv_fs_cb.cb)); |
| 171 | + coro.xsuspend(); |
| 172 | + var info = TmpFileInfo{ .path = null, .fd = @intCast(req.req.result) }; |
| 173 | + if (out) |buf| { |
| 174 | + const n = std.mem.len(req.req.path); |
| 175 | + if (n >= buf.len) return error.BufTooSmall; |
| 176 | + std.mem.copyForwards(u8, buf, req.req.path[0..n]); |
| 177 | + buf[n] = 0; |
| 178 | + info.path = buf[0..n :0]; |
| 179 | + } |
| 180 | + return info; |
| 181 | + } |
| 182 | +}; |
| 183 | + |
| 184 | +const uv_fs_cb = struct { |
| 185 | + frame: coro.Frame, |
| 186 | + req: uv.uv_fs_t, |
| 187 | + |
| 188 | + fn init(self: *@This()) void { |
| 189 | + uv.setReqData(&self.req, self); |
| 190 | + self.frame = coro.xframe(); |
| 191 | + } |
| 192 | + |
| 193 | + fn deinit(self: @This()) void { |
| 194 | + uv.uv_fs_req_cleanup(@constCast(&self.req)); |
| 195 | + } |
| 196 | + |
| 197 | + fn cb(req: [*c]uv.uv_fs_t) callconv(.C) void { |
| 198 | + const self = uv.getReqData(req, @This()); |
| 199 | + coro.xresume(self.frame); |
| 200 | + } |
| 201 | +}; |
0 commit comments