summaryrefslogtreecommitdiff
path: root/src/ninep/tree.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/ninep/tree.zig')
-rw-r--r--src/ninep/tree.zig60
1 files changed, 27 insertions, 33 deletions
diff --git a/src/ninep/tree.zig b/src/ninep/tree.zig
index 24de3e76..a5444caa 100644
--- a/src/ninep/tree.zig
+++ b/src/ninep/tree.zig
@@ -473,7 +473,6 @@ pub fn stageDirent(out: *std.ArrayList(u8), gpa: std.mem.Allocator, node: u64, d
out.appendSlice(gpa, name) catch return;
}
-/// Answers a read from whatever the handler staged, windowed by offset and size.
/// The daemon's registry (/sessions), its `list` answer as it gave it.
fn daemonList(gpa: std.mem.Allocator) anyerror![]u8 {
var buf: [4096]u8 = undefined;
@@ -481,6 +480,7 @@ fn daemonList(gpa: std.mem.Allocator) anyerror![]u8 {
return gpa.dupe(u8, said);
}
+/// Answers a read from whatever the handler staged, windowed by offset and size.
pub fn stagedReply(p: *Pardes, req: Req) Reply {
const out = &p.fs.out;
const off = @min(req.off, out.items.len);
@@ -490,6 +490,14 @@ pub fn stagedReply(p: *Pardes, req: Req) Reply {
return .{ .tag = req.tag, .payload = .{ .staged = @intCast(n) } };
}
+/// Answers a read with the window of `bytes` its offset and size ask for.
+pub fn window(p: *Pardes, req: Req, bytes: []const u8) Reply {
+ const off = @min(req.off, bytes.len);
+ const n = @min(bytes.len - off, req.size);
+ p.fs.stage(p.gpa).appendSlice(p.gpa, bytes[off..][0..n]) catch return Reply.fail(req.tag, E.NOMEM);
+ return .{ .tag = req.tag, .payload = .{ .staged = @intCast(n) } };
+}
+
// ---- dispatch ----
pub fn handle(p: *Pardes, req: Req) Reply {
@@ -690,13 +698,13 @@ fn topSize(p: *Pardes, f: TopFile) u64 {
.root, .pane, .col, .screen, .new, .layout, .index, .recent, .listeners, .sessions, .kbdin, .mousein => 0,
.tag => cols.headerText(p, null).?.len,
.README => fs.help.len,
- .status => ctl.statusLen(p),
+ .status => ctl.statusText(p).len,
.look, .exec, .tagexec, .pager => ctl.resultsLen(p),
// A stream: a length would read as its end to a kernel that caches
// one (a 9ns mount's splice), and a follower's reads go past it.
.log => 0,
- .ctl => ctl.rootLen(p),
- .commands => ctl.commandsLen(p),
+ .ctl => if (ctl.settingsText(p)) |text| text.len else |_| 0,
+ .commands => if (ctl.commandsText(p)) |text| text.len else |_| 0,
.focus => focus: {
if (p.header_focus) break :focus 0;
const pn = p.panes[p.active] orelse break :focus 0;
@@ -1060,17 +1068,12 @@ fn setattr(p: *Pardes, req: Req, target: Target) Reply {
}
fn read(p: *Pardes, req: Req, target: Target) Reply {
+ const open_record = openOf(p, req);
// A /pager open's text goes in before its read answers the pane.
- if (openOf(p, req)) |o| if (o.what == .pager) ctl.fillPager(p, o.what.pager, &o.pending) catch return Reply.fail(req.tag, E.NOMEM);
+ if (open_record) |o| if (o.what == .pager) ctl.fillPager(p, o.what.pager, &o.pending) catch return Reply.fail(req.tag, E.NOMEM);
// A listing reads what it was at the open.
- if (openOf(p, req)) |o| if (o.what == .frozen) {
- const bytes = o.what.frozen;
- const off = @min(req.off, bytes.len);
- const n = @min(bytes.len - off, req.size);
- p.fs.stage(p.gpa).appendSlice(p.gpa, bytes[off..][0..n]) catch return Reply.fail(req.tag, E.NOMEM);
- return .{ .tag = req.tag, .payload = .{ .staged = @intCast(n) } };
- };
- const stream = if (openOf(p, req)) |o| switch (o.what) {
+ if (open_record) |o| if (o.what == .frozen) return window(p, req, o.what.frozen);
+ const stream = if (open_record) |o| switch (o.what) {
.log => o,
.event => |reader| if (reader) o else null,
.pty_data => o,
@@ -1080,10 +1083,9 @@ fn read(p: *Pardes, req: Req, target: Target) Reply {
// Strictly inside: a client that reads every record at one offset (the
// offset is ignored on a stream, as in Plan 9) always gets new ones.
if (s.replay) |r| if (req.off > r.off and req.off < r.off + r.bytes.len) {
- const from = r.bytes[@intCast(req.off - r.off)..];
- const n = @min(from.len, req.size);
- p.fs.stage(p.gpa).appendSlice(p.gpa, from[0..n]) catch return Reply.fail(req.tag, E.NOMEM);
- return .{ .tag = req.tag, .payload = .{ .staged = @intCast(n) } };
+ var at = req;
+ at.off = req.off - r.off;
+ return window(p, at, r.bytes);
};
const reply = readFile(p, req, target);
if (reply.status != .ok) return reply;
@@ -1099,12 +1101,7 @@ fn readFile(p: *Pardes, req: Req, target: Target) Reply {
switch (target) {
.top => |f| return switch (f) {
.kbdin, .mousein => Reply.fail(req.tag, E.PERM),
- .README => help: {
- const off = @min(req.off, fs.help.len);
- const bytes = fs.help[off..][0..@min(req.size, fs.help.len - off)];
- p.fs.stage(p.gpa).appendSlice(p.gpa, bytes) catch break :help Reply.fail(req.tag, E.NOMEM);
- break :help .{ .tag = req.tag, .payload = .{ .staged = @intCast(bytes.len) } };
- },
+ .README => window(p, req, fs.help),
.index => index: {
const out = p.fs.stage(p.gpa);
var last: u32 = 0;
@@ -1120,11 +1117,11 @@ fn readFile(p: *Pardes, req: Req, target: Target) Reply {
}
break :index stagedReply(p, req);
},
- .status => ctl.readStatus(p, req),
+ .status => window(p, req, ctl.statusText(p)),
.look, .exec, .tagexec, .pager => ctl.readResults(p, req),
.log => events.readLog(p, req),
.screen => screen.readSnapshot(p, req, null),
- .listeners => screen.readListeners(p, req),
+ .listeners => window(p, req, screen.listenersText(p)),
.sessions => sessions: {
if (comptime !(pardes.hosted and daemon.supported)) break :sessions stagedReply(p, req);
// Out of the turn: the daemon answers within 2 s, or not at all.
@@ -1133,12 +1130,11 @@ fn readFile(p: *Pardes, req: Req, target: Target) Reply {
else => failText(req.tag, E.NOENT, "no pardes daemon answers"),
};
defer p.gpa.free(said);
- p.fs.stage(p.gpa).appendSlice(p.gpa, said) catch break :sessions Reply.fail(req.tag, E.NOMEM);
- break :sessions stagedReply(p, req);
+ break :sessions window(p, req, said);
},
.focus => ctl.readFocus(p, req),
- .ctl => ctl.readRoot(p, req),
- .commands => ctl.readCommands(p, req),
+ .ctl => window(p, req, ctl.settingsText(p) catch return Reply.fail(req.tag, E.NOMEM)),
+ .commands => window(p, req, ctl.commandsText(p) catch return Reply.fail(req.tag, E.NOMEM)),
.recent => recent: {
// Every window's files: what the others wrote since, read,
// unless a step is out mid-syscall (nothing may change the
@@ -1146,14 +1142,12 @@ fn readFile(p: *Pardes, req: Req, target: Target) Reply {
if (pardes.turn.out == 0) p.store.refresh(p);
var text: std.Io.Writer.Allocating = .init(p.scratch.allocator());
pardes.panes.Output.recentText(p, &text.writer, false) catch break :recent Reply.fail(req.tag, E.NOMEM);
- p.fs.stage(p.gpa).appendSlice(p.gpa, text.written()) catch break :recent Reply.fail(req.tag, E.NOMEM);
- break :recent stagedReply(p, req);
+ break :recent window(p, req, text.written());
},
.layout => layout: {
var text: std.Io.Writer.Allocating = .init(p.scratch.allocator());
cols.writeLayout(p, &text.writer) catch break :layout Reply.fail(req.tag, E.NOMEM);
- p.fs.stage(p.gpa).appendSlice(p.gpa, text.written()) catch break :layout Reply.fail(req.tag, E.NOMEM);
- break :layout stagedReply(p, req);
+ break :layout window(p, req, text.written());
},
.tag => cols.read(p, req, null),
// The serial the open handed this fid, so that two reads of one