diff options
Diffstat (limited to 'core/ninep/tree.zig')
| -rw-r--r-- | core/ninep/tree.zig | 96 |
1 files changed, 62 insertions, 34 deletions
diff --git a/core/ninep/tree.zig b/core/ninep/tree.zig index 1eaaa8cb..3eeb5d3f 100644 --- a/core/ninep/tree.zig +++ b/core/ninep/tree.zig @@ -124,9 +124,45 @@ pub const out_reserve = 4 * 1024; /// (editors/acme/dat.h:373-385). An open that holds something answers its /// record's index plus one as the handle; one that holds nothing answers 0. pub const Open = struct { + /// A client's join carries it and the digest covers it (fs.Namespace), + /// but what only its reader is answered with: the listener's held + /// read and the stream's last answer. + pub const replica = .{ .authority = .{ "held", "replay" } }; /// The node it was opened on, which every use checks; 0 is a free record. node: u64 = 0, - what: union(enum) { + what: What = .{ .snapshot = null }, + /// What a write to a file of command lines left unfinished: a last line + /// with no newline yet, or an Edit block still open. A write through a + /// mount is cut at the 9P message size, so one `echo` of many lines may + /// come as several writes, cut anywhere; the rest waits for the open's + /// next write, or its release, and never runs as a fragment. + pending: std.ArrayList(u8) = .empty, + /// The panes this open's last write touched, which a read of look or + /// exec on it answers, as /net/tcp/clone answers on the open that + /// asked; a fresh open holds the session's last, from before it opened. + results: Results = .{}, + /// A read that found nothing yet (`.again`), kept to be answered when + /// this open's file has something, the way factotum keeps its log's + /// waiting reads and answers them on append (security/auth/factotum/ + /// log.c:4-52) and acme an event read (editors/acme/xfid.c:994). One at a + /// time, as acme's window keeps one `eventx`: a newer read takes the + /// place of the older. The listener fills it in and answers it + /// (runtime/9p_io.zig, `answerHeld`). + held: ?Held = null, + /// The last answer a stream open (a followed log, event, pty/data) gave, + /// and the offset it was read at. A read at an offset inside it is + /// answered from it again: bash's `read` takes a chunk, keeps a line and + /// lseeks back to just past it, and a stream has already moved on. + replay: ?Replay = null, + /// A `name` open has named its pane: one name an open, so a second line + /// on it (bash writes `printf 'a\nb\n'` a line at a time) is refused. + named: bool = false, + /// A line over 1 MiB was refused: what follows of it, through its + /// newline, is dropped rather than run as a line of its own. + discarding: bool = false, + + /// What it is an open of, and what that holds. + pub const What = union(enum) { /// /screen's frame from the open, a terminal body's history from its /// first read. snapshot: ?[]const u8, @@ -160,40 +196,27 @@ pub const Open = struct { /// An open reading a plumb port (Plumb.zig's ports, plumber(4)'s): /// the messages sent to it since the open, packed, a read each. port: struct { index: u8, queue: events.Queue = .{} }, - } = .{ .snapshot = null }, - /// What a write to a file of command lines left unfinished: a last line - /// with no newline yet, or an Edit block still open. A write through a - /// mount is cut at the 9P message size, so one `echo` of many lines may - /// come as several writes, cut anywhere; the rest waits for the open's - /// next write, or its release, and never runs as a fragment. - pending: std.ArrayList(u8) = .empty, - /// The panes this open's last write touched, which a read of look or - /// exec on it answers, as /net/tcp/clone answers on the open that - /// asked; a fresh open holds the session's last, from before it opened. - results: Results = .{}, - /// A read that found nothing yet (`.again`), kept to be answered when - /// this open's file has something, the way factotum keeps its log's - /// waiting reads and answers them on append (security/auth/factotum/ - /// log.c:4-52) and acme an event read (editors/acme/xfid.c:994). One at a - /// time, as acme's window keeps one `eventx`: a newer read takes the - /// place of the older. The listener fills it in and answers it - /// (runtime/9p_io.zig, `answerHeld`). - held: ?Held = null, - /// The last answer a stream open (a followed log, event, pty/data) gave, - /// and the offset it was read at. A read at an offset inside it is - /// answered from it again: bash's `read` takes a chunk, keeps a line and - /// lseeks back to just past it, and a stream has already moved on. - replay: ?Replay = null, - /// A `name` open has named its pane: one name an open, so a second line - /// on it (bash writes `printf 'a\nb\n'` a line at a time) is refused. - named: bool = false, - /// A line over 1 MiB was refused: what follows of it, through its - /// newline, is dropped rather than run as a line of its own. - discarding: bool = false, + + /// What a step reads of it: not the text a reader is answered from + /// (a screen, a listing, the log, a port's messages), which each + /// process makes of its own state, and a replica never answers. + pub fn digest(w: *const What, h: *pardes.walk.Hasher) void { + const tag = std.meta.activeTag(w.*); + pardes.walk.hash(@TypeOf(tag), &tag, h); + switch (w.*) { + .run => |*run| pardes.walk.hash(pty.Run, run, h), + .event => |*reader| pardes.walk.hash(bool, reader, h), + .pager => |*serial| pardes.walk.hash(u32, serial, h), + .port => |*port| pardes.walk.hash(u8, &port.index, h), + .snapshot, .log, .frozen, .pty_data, .ctl, .lines, .writes, .plumb => {}, + } + } + }; pub const Replay = struct { off: u64, bytes: []u8 }; pub const Results = struct { + pub const replica = .{ .len = .{.{ "list", "len" }} }; list: [pardes.MAX_PANES]u32 = undefined, len: u8 = 0, /// Written on: reads are a stream from the write on, whatever @@ -230,7 +253,7 @@ pub const open_slots = 64; /// The record a request's handle names, when it was opened on that node. pub fn openOf(p: *Pardes, req: Req) ?*Open { - if (req.handle == 0 or req.handle > p.fs.opens.len) return null; + if (req.handle == 0 or req.handle > p.fs.opens_len) return null; const o = &p.fs.opens[req.handle - 1]; return if (o.node == req.node) o else null; } @@ -614,7 +637,7 @@ fn runAttr(p: *Pardes, req: Req, target: Target) ?Reply.Attr { .top, .col, .port => return null, }; if (t.file != .pty_run) return null; - const run = if (req.handle != 0) openOf(p, req) else for (&p.fs.opens) |*o| { + const run = if (req.handle != 0) openOf(p, req) else for (p.fs.opens[0..p.fs.opens_len]) |*o| { if (o.node == req.node and o.what == .run) break o; } else null; if ((run orelse return null).what != .run) return null; @@ -895,7 +918,7 @@ fn open(p: *Pardes, req: Req, target: Target) Reply { .ctl => if (linesOpen(target, req)) .lines else .{ .frozen = &.{} }, // One writer of the rules at a time, as plumber(4)'s. .rules, .send => if (req.omode & 3 == 0) return .{ .tag = req.tag } else plumb: { - if (f == .rules) for (p.fs.opens) |o| if (o.node == req.node and o.what == .plumb) return failText(req.tag, E.BUSY, e_in_use); + if (f == .rules) for (p.fs.opens[0..p.fs.opens_len]) |o| if (o.node == req.node and o.what == .plumb) return failText(req.tag, E.BUSY, e_in_use); break :plumb .plumb; }, else => if (linesOpen(target, req)) .lines else return .{ .tag = req.tag }, @@ -934,7 +957,11 @@ fn open(p: *Pardes, req: Req, target: Target) Reply { // record would be another than the session's. const i = p.tape.authority(freeOpen, .{p}) orelse return Reply.fail(req.tag, E.NFILE); const o = &p.fs.opens[i]; + // Past `opens_len` a replica's record may hold what a rollback did not + // put back (rollback.zig saves records as far as it says). + if (o.node != 0) o.deinit(p.gpa); o.* = .{ .node = req.node, .what = what }; + p.fs.opens_len = @max(p.fs.opens_len, @as(u8, @intCast(i + 1))); if (resultsFile(target)) { @memcpy(o.results.list[0..p.fs.results_len], p.fs.results[0..p.fs.results_len]); o.results.len = p.fs.results_len; @@ -1074,6 +1101,7 @@ fn releaseHandle(p: *Pardes, req: Req) void { .snapshot, .log, .lines, .writes, .frozen, .pager, .plumb, .port => {}, }; o.deinit(p.gpa); + while (p.fs.opens_len > 0 and p.fs.opens[p.fs.opens_len - 1].node == 0) p.fs.opens_len -= 1; } fn setattr(p: *Pardes, req: Req, target: Target) Reply { |
