summaryrefslogtreecommitdiff
path: root/src/host
diff options
context:
space:
mode:
Diffstat (limited to 'src/host')
-rw-r--r--src/host/control.zig25
1 files changed, 19 insertions, 6 deletions
diff --git a/src/host/control.zig b/src/host/control.zig
index 75fb524..943316c 100644
--- a/src/host/control.zig
+++ b/src/host/control.zig
@@ -199,7 +199,7 @@ pub const Control = struct {
fn respond(self: *Control, host: *Host, conn: *Connection, req: fs.Req, reply: *fs.Reply, buffer: []u8) ![]const u8 {
// Nodes from 2^63 are the sketch's: none is served but sketch/.
if (req.node > @backingInt(File.sketch)) return error.UnknownFile;
- if (req.node & @as(u64, @bitCast(state_files.root)) != 0) return stateRespond(self, host, req, reply, buffer);
+ if (req.node & @as(u64, @bitCast(state_files.root)) != 0) return stateRespond(self, host, conn, req, reply, buffer);
const file = std.enums.fromInt(File, req.node) orelse return error.UnknownFile;
if (self.offline != null and (req.op == .write or (req.op == .open or req.op == .read) and file != .README and file != .commands and file != .ctl and file != .@"/" and file != .sketch)) return error.SketchNotRunning;
switch (req.op) {
@@ -426,7 +426,7 @@ fn verbLine(w: *std.Io.Writer, indent: []const u8, name: []const u8, args: []con
/// Under `state/`: the live State as files (state_files.zig), a write
/// run as the console's `set`.
-fn stateRespond(self: *Control, host: *Host, req: fs.Req, reply: *fs.Reply, buffer: []u8) ![]const u8 {
+fn stateRespond(self: *Control, host: *Host, conn: *Connection, req: fs.Req, reply: *fs.Reply, buffer: []u8) ![]const u8 {
const layout = if (self.offline) |o| o.layout else host.state.layout;
const node: state_files.Node = @bitCast(req.node);
if (!state_files.valid(layout, node)) return error.StateLayoutChangedWalkAgain;
@@ -444,16 +444,29 @@ fn stateRespond(self: *Control, host: *Host, req: fs.Req, reply: *fs.Reply, buff
if (mode != 0o600 or !req.truncate) return error.ReadOnly;
reply.attr = .{ .node = req.node, .mode = mode };
},
- .open, .release => {},
+ // A value is read as it was at the open: it changes every frame,
+ // and its length with it.
+ .open => if (!dir) for (&conn.snapshots, 1..) |*slot, handle| if (slot.* == null) {
+ var text: std.Io.Writer.Allocating = .init(self.allocator);
+ defer text.deinit();
+ try state_files.read(layout, if (self.offline) |o| o.bytes else host.live(), node, &text.writer);
+ slot.* = try text.toOwnedSlice();
+ reply.handle = @intCast(handle);
+ break;
+ } else return error.TooManySnapshots,
+ .release => if (req.opened and req.handle > 0 and req.handle <= conn.snapshots.len) {
+ const slot = &conn.snapshots[req.handle - 1];
+ if (slot.*) |bytes| self.allocator.free(bytes);
+ slot.* = null;
+ },
.readdir => {
if (!dir) return error.NotDirectory;
return buffer[0..state_files.readdir(layout, node, req.off, buffer)];
},
.read => {
if (dir) return error.NotFile;
- var w: std.Io.Writer = .fixed(buffer);
- state_files.read(layout, if (self.offline) |o| o.bytes else host.live(), node, &w) catch return error.ValueTooLong;
- return window(w.buffered(), req.off, req.size);
+ const bytes = if (req.handle > 0 and req.handle <= conn.snapshots.len) conn.snapshots[req.handle - 1] orelse return error.InvalidHandle else return error.InvalidHandle;
+ return window(bytes, req.off, req.size);
},
.write => {
if (mode != 0o600) return error.ReadOnly;