diff options
| -rw-r--r-- | src/ninep/ctl.zig | 161 | ||||
| -rw-r--r-- | test/behaviour.zig | 1 | ||||
| -rw-r--r-- | test/behaviour/event.zig | 55 | ||||
| -rw-r--r-- | test/behaviour/other_files.zig | 42 | ||||
| -rw-r--r-- | test/behaviour/panes.zig | 44 | ||||
| -rw-r--r-- | test/session.zig | 55 |
6 files changed, 197 insertions, 161 deletions
diff --git a/src/ninep/ctl.zig b/src/ninep/ctl.zig index 66f30ff4..7441afeb 100644 --- a/src/ninep/ctl.zig +++ b/src/ninep/ctl.zig @@ -1487,57 +1487,6 @@ test "Undo and Redo on a pane's ctl step its body through its edits" { try testing.expect(th.logHas(p, "Undo: nothing to undo (x4)\n")); } -test "a second lock is refused until the holder unlocks or closes, and binds nobody else" { - const p = try withFile(testing.allocator, "one\ntwo\n"); - defer p.deinit(); - const serial = serialOf(p); - const ctl_node = Node.of(serial, .ctl); - const w = struct { - fn ctl(pp: *Pardes, node: u64, h: u32, data: []const u8) th.Answer { - return call(pp, .{ .tag = 4, .op = .write, .node = node, .handle = h, .data = data }); - } - }; - const a = call(p, .{ .tag = 1, .op = .open, .node = ctl_node, .omode = 2 }).reply.handle; - const b = call(p, .{ .tag = 2, .op = .open, .node = ctl_node, .omode = 1 }).reply.handle; - try testing.expect(a != 0 and b != 0 and a != b); - // Reading the status line holds nothing, so it cannot lock either. - try testing.expectEqual(@as(u32, 0), call(p, .{ .tag = 3, .op = .open, .node = ctl_node }).reply.handle); - try testing.expectEqual(E.INVAL, wr(p, ctl_node, "lock\n").errno()); - - try testing.expectEqual(Status.ok, w.ctl(p, ctl_node, a, "lock\n").reply.status); - try testing.expectEqual(Status.ok, w.ctl(p, ctl_node, a, "lock\n").reply.status); - // Another open's lock is refused at once, and so is its unlock; writes - // to the pane's other files are not. - try testing.expectEqualStrings(e_locked, w.ctl(p, ctl_node, b, "lock\n").reply.ename); - try testing.expectEqual(E.BUSY, w.ctl(p, ctl_node, b, "lock\n").errno()); - try testing.expectEqualStrings(e_not_locked, w.ctl(p, ctl_node, b, "unlock\n").reply.ename); - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .addr), "1").reply.status); - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .data), "ONE\n").reply.status); - try testing.expectEqualStrings("ONE\ntwo\n", p.panes[0].?.file.?.content); - - // Once the holder unlocks, the other's try takes it. - try testing.expectEqual(Status.ok, w.ctl(p, ctl_node, a, "unlock\n").reply.status); - try testing.expectEqual(Status.ok, w.ctl(p, ctl_node, b, "lock\n").reply.status); - try testing.expectEqual(E.BUSY, w.ctl(p, ctl_node, a, "lock\nunlock\n").errno()); - // Closing the holder gives it up. - _ = call(p, .{ .tag = 5, .op = .release, .node = ctl_node, .handle = b }); - try testing.expectEqual(Status.ok, w.ctl(p, ctl_node, a, "lock\nunlock\nlock\n").reply.status); - try testing.expectEqual(E.INVAL, w.ctl(p, ctl_node, a, "unlock\nunlock\n").errno()); - try testing.expectEqual(@as(?u32, a), p.panes[0].?.fs.lock); - - // The lock lives and dies with the pane. - const other = try th.newPane(p); - const other_ctl = Node.of(other, .ctl); - const c = call(p, .{ .tag = 6, .op = .open, .node = other_ctl, .omode = 1 }).reply.handle; - try testing.expectEqual(Status.ok, w.ctl(p, other_ctl, c, "lock\n").reply.status); - try testing.expectEqual(Status.ok, th.rmdir(p, Node.of(other, .dir)).reply.status); - try testing.expectEqual(E.NOENT, w.ctl(p, other_ctl, c, "lock\n").errno()); - for ([_]u64{ ctl_node, other_ctl }, [_]u32{ a, c }) |node, h| - _ = call(p, .{ .tag = 7, .op = .release, .node = node, .handle = h }); - try testing.expect(p.panes[0].?.fs.lock == null); - for (p.fs.opens) |o| try testing.expect(o.node == 0); -} - test "Edit through a pane's ctl renames every foo in one undo step, and a failed one changes nothing" { const p = try withFile(testing.allocator, "foo x foo y foo\n"); defer p.deinit(); @@ -2243,62 +2192,6 @@ test "a look line's trailing colon is dropped, and a look whose address fails ne } -test "a refused Del written back through event fails that write, EIO, with its err" { - const gpa = testing.allocator; - const p = try withFile(gpa, "text\n"); - defer p.deinit(); - const serial = serialOf(p); - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .body), "more\n").reply.status); // unsaved - const event = Node.of(serial, .event); - const h = call(p, .{ .tag = 1, .op = .open, .node = event }).reply.handle; - defer _ = call(p, .{ .tag = 9, .op = .release, .node = event, .handle = h }); - try testing.expectEqual(Status.ok, wr(p, Node.of(serial, .exec), "Del\n").reply.status); - const record = call(p, .{ .tag = 2, .op = .read, .node = event, .handle = h, .size = 256 }).bytes; - var copy: [256]u8 = undefined; - @memcpy(copy[0..record.len], record); - const back = wr(p, event, copy[0..record.len]); - try testing.expectEqual(E.IO, back.errno()); - try testing.expect(std.mem.indexOf(u8, back.reply.ename, "Modified") != null); - try testing.expect(p.paneBySerial(serial) != null); -} - -test "a pane's own look and exec go to the event reader holding it, which writes them back to act" { - const gpa = testing.allocator; - const p = try withFile(gpa, "Msg routed\n"); - defer p.deinit(); - const serial = serialOf(p); - const event = Node.of(serial, .event); - const h = call(p, .{ .tag = 1, .op = .open, .node = event }).reply.handle; - try testing.expectEqual(tree.Status.ok, wr(p, Node.of(serial, .exec), "Msg routed\n").reply.status); - // Not done: the reader has it, as it would a middle click. - const pane = p.panes[0].?; - try testing.expect(std.mem.indexOf(u8, pane.msg[0..pane.msg_len], "routed") == null); - const record = call(p, .{ .tag = 2, .op = .read, .node = event, .handle = h, .size = 256 }).bytes; - // Flag 1: its first word is a builtin; no 2, no expansion follows. - try testing.expectEqualStrings("FX0 0 1 10 Msg routed\n", record); - // Written back, it is done. - try testing.expectEqual(tree.Status.ok, wr(p, event, record).reply.status); - try testing.expect(std.mem.indexOf(u8, pane.msg[0..pane.msg_len], "routed") != null); - try testing.expectEqual(tree.Status.ok, wr(p, Node.of(serial, .look), "one.txt:3\n").reply.status); - try testing.expectEqualStrings("FL0 0 4 9 one.txt:3\n", call(p, .{ .tag = 3, .op = .read, .node = event, .handle = h, .size = 256 }).bytes); - // The root's is a click at the pane with the keyboard, this one. - try testing.expectEqual(tree.Status.ok, wr(p, root_exec, "Msg rooted\n").reply.status); - try testing.expect(std.mem.indexOf(u8, pane.msg[0..pane.msg_len], "rooted") == null); - try testing.expectEqualStrings("FX0 0 1 10 Msg rooted\n", call(p, .{ .tag = 5, .op = .read, .node = event, .handle = h, .size = 256 }).bytes); - // A click in its tag is the reader's too, an `x` record, as acme's is; - // written back, it is done. - try testing.expectEqual(tree.Status.ok, wr(p, Node.of(serial, .tagexec), "Msg tagged\n").reply.status); - try testing.expect(std.mem.indexOf(u8, pane.msg[0..pane.msg_len], "tagged") == null); - const tagged = call(p, .{ .tag = 6, .op = .read, .node = event, .handle = h, .size = 256 }).bytes; - try testing.expectEqualStrings("Fx0 0 1 10 Msg tagged\n", tagged); - try testing.expectEqual(tree.Status.ok, wr(p, event, tagged).reply.status); - try testing.expect(std.mem.indexOf(u8, pane.msg[0..pane.msg_len], "tagged") != null); - _ = call(p, .{ .tag = 4, .op = .release, .node = event, .handle = h }); - // With no reader, the root's and the pane's act at once. - try testing.expectEqual(tree.Status.ok, wr(p, Node.of(serial, .exec), "Msg direct\n").reply.status); - try testing.expect(std.mem.indexOf(u8, pane.msg[0..pane.msg_len], "direct") != null); -} - test "look and exec read back what the last command touched, a ctl write's too" { const gpa = testing.allocator; const p = try withFile(gpa, "x\n"); @@ -4295,60 +4188,6 @@ test "a refused Delcol changes nothing: no pane, no focus" { try testing.expect(th.logHas(p, "unsaved ")); } -test "pager answers its +Pager on the open that asked, whatever another client exec'd meanwhile" { - const p = try withFile(testing.allocator, "x\n"); - defer p.deinit(); - const pager = @backingInt(tree.TopFile.pager); - const exec = @backingInt(tree.TopFile.exec); - const asked = call(p, .{ .tag = 1, .op = .open, .node = pager, .omode = 2 }).reply.handle; - try testing.expectEqual(Status.ok, call(p, .{ .tag = 2, .op = .write, .node = pager, .handle = asked, .data = "/tmp\n" }).reply.status); - const paged = for (p.panes) |slot| { - const pn = slot orelse continue; - if (pn.file) |f| if (std.mem.eql(u8, f.path, "/tmp/+Pager")) break pn.serial; - } else return error.NoPager; - // Another client execs between the request and its read: a pane of its own. - const racer = call(p, .{ .tag = 3, .op = .open, .node = exec, .omode = 2 }).reply.handle; - try testing.expectEqual(Status.ok, call(p, .{ .tag = 4, .op = .write, .node = exec, .handle = racer, .data = "New\n" }).reply.status); - var line: [16]u8 = undefined; - const want = try std.fmt.bufPrint(&line, "{d}\n", .{paged}); - // A fresh open reads the session's last answer, the racer's pane: what - // `pardes -` once read, and wrote its text into. - try testing.expect(!std.mem.eql(u8, want, rd(p, exec, 0, 64).bytes)); - try testing.expectEqualStrings(want, call(p, .{ .tag = 5, .op = .read, .node = pager, .handle = asked, .size = 64 }).bytes); - _ = call(p, .{ .tag = 6, .op = .release, .node = pager, .handle = asked, .opened = true }); - // Refused, an open answers nothing, not the session's last answer. - const refused = call(p, .{ .tag = 7, .op = .open, .node = pager, .omode = 2 }).reply.handle; - try testing.expectEqual(E.INVAL, call(p, .{ .tag = 8, .op = .write, .node = pager, .handle = refused, .data = "relative\n" }).errno()); - try testing.expectEqualStrings("", call(p, .{ .tag = 9, .op = .read, .node = pager, .handle = refused, .size = 64 }).bytes); - _ = call(p, .{ .tag = 10, .op = .release, .node = pager, .handle = refused, .opened = true }); - // Resolved: `/tmp/../tmp/.` is /tmp, a +Pager of its own beside the - // one made above, which keeps its page. - const resolved = call(p, .{ .tag = 11, .op = .open, .node = pager, .omode = 2 }).reply.handle; - try testing.expectEqual(Status.ok, call(p, .{ .tag = 12, .op = .write, .node = pager, .handle = resolved, .data = "/tmp/../tmp/.\n" }).reply.status); - const second = call(p, .{ .tag = 13, .op = .read, .node = pager, .handle = resolved, .size = 64 }).bytes; - try testing.expect(!std.mem.eql(u8, want, second)); - const second_pane = p.panes[p.paneBySerial(try std.fmt.parseInt(u32, std.mem.trim(u8, second, "\n"), 10)).?].?; - try testing.expectEqualStrings("/tmp/+Pager", second_pane.file.?.path); - try testing.expect(p.paneBySerial(paged) != null); - _ = call(p, .{ .tag = 14, .op = .release, .node = pager, .handle = resolved, .opened = true }); - // One that may not be written is refused, permission denied (Linux's /proc/1). - if (comptime @import("builtin").os.tag == .linux) try testing.expectEqual(E.PERM, wr(p, pager, "/proc/1\n").errno()); - // One directory a write, absolute, and there. - try testing.expectEqual(E.INVAL, wr(p, pager, "/a\n/b\n").errno()); - // One directory an open: one refused is not followed by another, as a - // mount sends two lines in two writes. (After one taken, what follows - // is the paged text: the test below.) - const twice = call(p, .{ .tag = 20, .op = .open, .node = pager, .omode = 2 }).reply.handle; - try testing.expectEqual(E.INVAL, call(p, .{ .tag = 21, .op = .write, .node = pager, .handle = twice, .data = "relative\n" }).errno()); - try testing.expectEqual(E.INVAL, call(p, .{ .tag = 22, .op = .write, .node = pager, .handle = twice, .data = "/tmp\n" }).errno()); - _ = call(p, .{ .tag = 23, .op = .release, .node = pager, .handle = twice }); - const relative = wr(p, pager, "relative\n"); - try testing.expectEqual(E.INVAL, relative.errno()); - try testing.expectEqualStrings("pager: invalid directory \"relative\": must be absolute", relative.reply.ename); - try testing.expectEqual(E.NOENT, wr(p, pager, "/tmp/pardes-surely-missing-dir\n").errno()); - try testing.expectEqual(E.NOTDIR, wr(p, pager, "/etc/passwd\n").errno()); -} - test "what a /pager open writes after its directory is the text: plain in the body, its colours the pane's, clean, in by the read" { const p = try withFile(testing.allocator, "x\n"); defer p.deinit(); diff --git a/test/behaviour.zig b/test/behaviour.zig index 67a22668..49245d82 100644 --- a/test/behaviour.zig +++ b/test/behaviour.zig @@ -5,6 +5,7 @@ test { _ = @import("behaviour/columns_and_tags.zig"); _ = @import("behaviour/edit.zig"); + _ = @import("behaviour/event.zig"); _ = @import("behaviour/log.zig"); _ = @import("behaviour/look_and_exec.zig"); _ = @import("behaviour/mounts.zig"); diff --git a/test/behaviour/event.zig b/test/behaviour/event.zig new file mode 100644 index 00000000..8aebe8f4 --- /dev/null +++ b/test/behaviour/event.zig @@ -0,0 +1,55 @@ +//! docs/typ/reference.typ, event <event>. +const std = @import("std"); +const Session = @import("session").Session; + +fn has(hay: []const u8, needle: []const u8) bool { + return std.mem.indexOf(u8, hay, needle) != null; +} + +test "event: a pane's own look and exec go to the event reader holding it, which writes them back to act" { + var s = try Session.initFile(std.testing.allocator, "Msg routed\n"); + defer s.deinit(); + const n = try s.focused(); + const ev = try s.hold(s.at("/pane/{d}/event", .{n}), 2); + try s.write(s.at("/pane/{d}/exec", .{n}), "Msg routed\n"); + // Not done: the reader has it, as it would a middle click. + try std.testing.expect(!has(try s.read("/log"), "routed")); + const record = try s.readFid(ev); + // Flag 1: its first word is a builtin; no 2, no expansion follows. + try std.testing.expectEqualStrings("FX0 0 1 10 Msg routed\n", record); + // Written back, it is done. + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(ev, record)); + try std.testing.expect(has(try s.read("/log"), s.at("msg {d} routed\n", .{n}))); + try s.write(s.at("/pane/{d}/look", .{n}), "one.txt:3\n"); + try std.testing.expectEqualStrings("FL0 0 4 9 one.txt:3\n", try s.readFid(ev)); + // The root's is a click at the pane with the keyboard, this one. + try s.write("/exec", "Msg rooted\n"); + try std.testing.expect(!has(try s.read("/log"), "rooted")); + try std.testing.expectEqualStrings("FX0 0 1 10 Msg rooted\n", try s.readFid(ev)); + // A click in its tag is the reader's too, an `x` record, as acme's is; + // written back, it is done. + try s.write(s.at("/pane/{d}/tagexec", .{n}), "Msg tagged\n"); + try std.testing.expect(!has(try s.read("/log"), "tagged")); + const tagged = try s.readFid(ev); + try std.testing.expectEqualStrings("Fx0 0 1 10 Msg tagged\n", tagged); + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(ev, tagged)); + try std.testing.expect(has(try s.read("/log"), s.at("msg {d} tagged\n", .{n}))); + s.clunk(ev); + // With no reader, the root's and the pane's act at once. + try s.write(s.at("/pane/{d}/exec", .{n}), "Msg direct\n"); + try std.testing.expect(has(try s.read("/log"), s.at("msg {d} direct\n", .{n}))); +} + +test "event: a refused Del written back through event fails that write, EIO, with its err" { + var s = try Session.initFile(std.testing.allocator, "text\n"); + defer s.deinit(); + const n = try s.focused(); + try s.write(s.at("/pane/{d}/body", .{n}), "more\n"); // unsaved + const ev = try s.hold(s.at("/pane/{d}/event", .{n}), 2); + defer s.clunk(ev); + try s.write(s.at("/pane/{d}/exec", .{n}), "Del\n"); + const record = try s.readFid(ev); + try std.testing.expectEqual(@as(u16, 5), try s.writeFid(ev, record)); + try std.testing.expect(has(s.said, "Modified")); + _ = try s.read(s.at("/pane/{d}/ctl", .{n})); // still there +} diff --git a/test/behaviour/other_files.zig b/test/behaviour/other_files.zig index c0f9c08f..8625df3b 100644 --- a/test/behaviour/other_files.zig +++ b/test/behaviour/other_files.zig @@ -28,3 +28,45 @@ test "other-files: kbdin's Kalt rune is Alt on the key after it alone, and an Al try s.keys("i\u{F015}zy\x1b"); try s.expect(s.at("/pane/{d}/body", .{try s.focused()}), "yx\n"); } + +test "other-files: pager answers its +Pager on the open that asked, whatever another client exec'd meanwhile, and refuses a directory it cannot page in" { + var s = try Session.initFile(std.testing.allocator, "x\n"); + defer s.deinit(); + const asked = try s.hold("/pager", 2); + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(asked, "/tmp\n")); + // Another client execs between the request and its read: a pane of its own. + const racer = try s.hold("/exec", 2); + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(racer, "New\n")); + s.clunk(racer); + const paged = try s.readFid(asked); + s.clunk(asked); + // A fresh open reads the session's last answer, the racer's pane. + try std.testing.expect(!std.mem.eql(u8, paged, try s.read("/exec"))); + try s.expect(s.at("/pane/{s}/name", .{std.mem.trimEnd(u8, paged, "\n")}), "/tmp/+Pager\n"); + // Refused, an open answers nothing, not the session's last answer. + const refused = try s.hold("/pager", 2); + try std.testing.expectEqual(@as(u16, 22), try s.writeFid(refused, "relative\n")); + try std.testing.expectEqualStrings("", try s.readFid(refused)); + s.clunk(refused); + // Resolved: `/tmp/../tmp/.` is /tmp, a +Pager of its own beside the + // one made above, which keeps its page. + const resolved = try s.hold("/pager", 2); + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(resolved, "/tmp/../tmp/.\n")); + const second = try s.readFid(resolved); + s.clunk(resolved); + try std.testing.expect(!std.mem.eql(u8, paged, second)); + try s.expect(s.at("/pane/{s}/name", .{std.mem.trimEnd(u8, second, "\n")}), "/tmp/+Pager\n"); + try s.expect(s.at("/pane/{s}/name", .{std.mem.trimEnd(u8, paged, "\n")}), "/tmp/+Pager\n"); + // One that may not be written is refused, permission denied (Linux's /proc/1). + try std.testing.expectEqual(@as(u16, 1), try s.refused("/pager", "/proc/1\n")); + // One directory an open: one refused is not followed by another, as a + // mount sends two lines in two writes. + const twice = try s.hold("/pager", 2); + try std.testing.expectEqual(@as(u16, 22), try s.writeFid(twice, "relative\n")); + try std.testing.expectEqual(@as(u16, 22), try s.writeFid(twice, "/tmp\n")); + s.clunk(twice); + try std.testing.expectEqual(@as(u16, 22), try s.refused("/pager", "relative\n")); + try std.testing.expectEqualStrings("pager: invalid directory \"relative\": must be absolute", s.said); + try std.testing.expectEqual(@as(u16, 2), try s.refused("/pager", "/tmp/pardes-surely-missing-dir\n")); + try std.testing.expectEqual(@as(u16, 20), try s.refused("/pager", "/etc/passwd\n")); +} diff --git a/test/behaviour/panes.zig b/test/behaviour/panes.zig index d48ff812..9aa28c6a 100644 --- a/test/behaviour/panes.zig +++ b/test/behaviour/panes.zig @@ -115,3 +115,47 @@ test "panes: the pane ctl takes acme's verbs and the pane's builtins, and refuse try std.testing.expectEqual(@as(u16, 2), try s.refused(s.at("/pane/{d}/ctl", .{other}), "Del\nMsg after\n")); try std.testing.expectError(error.Refused, s.read(s.at("/pane/{d}/ctl", .{other}))); } + +test "panes: a second lock is refused until the holder unlocks or closes, and binds nobody else" { + var s = try Session.initFile(std.testing.allocator, "one\ntwo\n"); + defer s.deinit(); + const n = try s.focused(); + const ctl = s.at("/pane/{d}/ctl", .{n}); + const a = try s.hold(ctl, 2); + const b = try s.hold(ctl, 1); + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(a, "lock\n")); + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(a, "lock\n")); + // Another open's lock is refused at once, and so is its unlock; writes + // to the pane's other files are not. + try std.testing.expectEqual(@as(u16, 16), try s.writeFid(b, "lock\n")); + try std.testing.expectEqualStrings("file in use: another open of this ctl holds the lock", s.said); + try std.testing.expectEqual(@as(u16, 22), try s.writeFid(b, "unlock\n")); + try std.testing.expectEqualStrings("pane not locked by this open", s.said); + try s.write(s.at("/pane/{d}/addr", .{n}), "1"); + try s.write(s.at("/pane/{d}/data", .{n}), "ONE\n"); + try s.expect(s.at("/pane/{d}/body", .{n}), "ONE\ntwo\n"); + // Once the holder unlocks, the other's try takes it. + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(a, "unlock\n")); + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(b, "lock\n")); + try std.testing.expectEqual(@as(u16, 16), try s.writeFid(a, "lock\nunlock\n")); + // Closing the holder gives it up. + s.clunk(b); + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(a, "lock\nunlock\nlock\n")); + try std.testing.expectEqual(@as(u16, 22), try s.writeFid(a, "unlock\nunlock\n")); + // Still a's: another open is refused it. + const c = try s.hold(ctl, 1); + try std.testing.expectEqual(@as(u16, 16), try s.writeFid(c, "lock\n")); + s.clunk(c); + // The lock lives and dies with the pane. + const other = try s.newPane(); + const d = try s.hold(s.at("/pane/{d}/ctl", .{other}), 1); + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(d, "lock\n")); + try s.write(s.at("/pane/{d}/ctl", .{other}), "Del\n"); + try std.testing.expectEqual(@as(u16, 2), try s.writeFid(d, "lock\n")); + s.clunk(d); + s.clunk(a); + // a's close gave its lock up too. + const e = try s.hold(ctl, 1); + try std.testing.expectEqual(@as(u16, 0), try s.writeFid(e, "lock\n")); + s.clunk(e); +} diff --git a/test/session.zig b/test/session.zig index 8b3c5c41..aa6288ff 100644 --- a/test/session.zig +++ b/test/session.zig @@ -182,6 +182,61 @@ pub const Session = struct { fn pardesOf(s: *Session) *Pardes { return @ptrCast(@alignCast(s.core)); } + + // ninep: + + /// An open kept until `clunk`, as an event reader or a lock holder + /// keeps one. + pub const Fid = struct { node: u64, handle: u32 }; + + /// `path` opened (omode 0 read, 1 write, 2 both) and held. + pub fn hold(s: *Session, path: []const u8, omode: u8) !Fid { + const node, const h = try s.open(path, omode); + return .{ .node = node, .handle = h }; + } + + /// One read on a held open (an event record, an answer). + pub fn readFid(s: *Session, f: Fid) ![]const u8 { + const r = s.serve(.{ .tag = 0, .op = .read, .node = f.node, .handle = f.handle, .size = 1 << 16 }); + if (r.status != .ok) return refusal("a held open", r); + return s.arena.allocator().dupe(u8, pardesOf(s).fsPayload(r)); + } + + /// One write on a held open: 0 when taken, else its errno, its words in `said`. + pub fn writeFid(s: *Session, f: Fid, data: []const u8) !u16 { + const r = s.serve(.{ .tag = 0, .op = .write, .node = f.node, .handle = f.handle, .data = data }); + if (r.status == .ok) return 0; + s.said = try s.arena.allocator().dupe(u8, r.ename); + return r.errno; + } + + pub fn clunk(s: *Session, f: Fid) void { + _ = s.serve(.{ .tag = 0, .op = .release, .node = f.node, .handle = f.handle, .opened = true }); + } + + /// A held open's file truncated, as an OTRUNC open (`> data`) does. + pub fn truncate(s: *Session, f: Fid) !void { + const r = s.serve(.{ .tag = 0, .op = .setattr, .node = f.node, .truncate = true }); + if (r.status != .ok) return refusal("truncate", r); + } + + /// One read of `size` bytes at `off`, on an open of its own. + pub fn readAt(s: *Session, path: []const u8, off: u64, size: u32) ![]const u8 { + const f = try s.hold(path, 0); + defer s.clunk(f); + const r = s.serve(.{ .tag = 0, .op = .read, .node = f.node, .handle = f.handle, .off = off, .size = size }); + if (r.status != .ok) return refusal(path, r); + return s.arena.allocator().dupe(u8, pardesOf(s).fsPayload(r)); + } + + /// What a stat of `path` answers: its size, mode and qid version. + pub fn stat(s: *Session, path: []const u8) !struct { size: u64, mode: u16, version: u32 } { + const f = try s.hold(path, 0); + defer s.clunk(f); + const r = s.serve(.{ .tag = 0, .op = .getattr, .node = f.node }); + if (r.status != .ok) return refusal(path, r); + return .{ .size = r.attr.size, .mode = r.attr.mode, .version = r.attr.version }; + } }; fn refusal(path: []const u8, r: Reply) error{Refused} { |
