summaryrefslogtreecommitdiff
path: root/src/host/control.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/host/control.zig')
-rw-r--r--src/host/control.zig34
1 files changed, 27 insertions, 7 deletions
diff --git a/src/host/control.zig b/src/host/control.zig
index 5722a4d..75fb524 100644
--- a/src/host/control.zig
+++ b/src/host/control.zig
@@ -18,7 +18,7 @@ const Server = fs.Server(Backend, .{ .fid_capacity = 32, .slot_capacity = 4, .na
const msize = 16384;
/// The files, a row each: what lookup, readdir and getattr answer. A
/// file's node is its number here.
-const File = enum(u64) { @"/" = 1, ctl, status, log, @"screen.png", profile, @"profile-trace", @"timing-trace", @"presentation-trace", README, commands, state = @bitCast(state_files.root) };
+const File = enum(u64) { @"/" = 1, ctl, status, log, @"screen.png", profile, @"profile-trace", @"timing-trace", @"presentation-trace", README, commands, sketch = 1 << 63, state = @bitCast(state_files.root) };
const files = std.enums.EnumArray(File, struct { mode: u16 = 0o400, doc: []const u8 }).init(.{
.@"/" = .{ .mode = 0o700, .doc = "" },
.ctl = .{ .mode = 0o600, .doc = "write a verb a line, of those below: ctl's own, the input a script plays (live, no frame of its own), the console's; any other line goes to the sketch (commands lists its words)" },
@@ -31,6 +31,7 @@ const files = std.enums.EnumArray(File, struct { mode: u16 = 0o400, doc: []const
.@"presentation-trace" = .{ .doc = "on Wayland: presentation feedback per frame, as CSV" },
.README = .{ .doc = "this: a line a file, ctl's verbs under it, a line a State file under state/" },
.commands = .{ .doc = "every verb ctl takes, a line each, the sketch's words last" },
+ .sketch = .{ .mode = 0o700, .doc = "the sketch's own files, which the host never serves: reserved, empty" },
.state = .{ .mode = 0o700, .doc = "the sketch's State: a directory per struct and array, a file per value; a read is the value, a write what set does (a jump); an array of numbers is one file of its values, blank-separated" },
});
@@ -196,11 +197,14 @@ 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);
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 != .@"/")) return error.SketchNotRunning;
+ 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) {
.lookup => {
+ if (file == .sketch) return error.UnknownFile;
if (file != .@"/") return error.NotDirectory;
const found = std.meta.stringToEnum(File, req.data) orelse return error.UnknownFile;
if (found == .@"/") return error.UnknownFile;
@@ -240,13 +244,14 @@ pub const Control = struct {
slot.* = null;
},
.readdir => {
+ if (file == .sketch) return &.{};
if (file != .@"/") return error.NotDirectory;
var used: usize = 0;
for (std.enums.values(File)[1..], 0..) |f, index| {
if (index < req.off) continue;
const name = @tagName(f);
std.mem.writeInt(u64, buffer[used..][0..8], @backingInt(f), .little);
- buffer[used + 8] = @intFromBool(f == .state);
+ buffer[used + 8] = @intFromBool(f == .state or f == .sketch);
buffer[used + 9] = @intCast(name.len);
@memcpy(buffer[used + 10 ..][0..name.len], name);
used += 10 + name.len;
@@ -282,7 +287,7 @@ pub const Control = struct {
}
break :blk writer.buffered();
},
- .@"/", .state => return error.NotFile,
+ .@"/", .state, .sketch => return error.NotFile,
};
return window(bytes, req.off, req.size);
},
@@ -366,7 +371,7 @@ pub const Control = struct {
fn attr(self: *Control, node: u64) fs.Attr {
const file: File = @fromBackingInt(@intCast(node));
- return .{ .name = @tagName(file), .node = node, .dir = file == .@"/" or file == .state, .mode = files.get(file).mode, .size = if (file == .@"screen.png") self.png.len else 0, .version = if (file == .@"screen.png") @truncate(self.generation) else 0 };
+ return .{ .name = @tagName(file), .node = node, .dir = file == .@"/" or file == .state or file == .sketch, .mode = files.get(file).mode, .size = if (file == .@"screen.png") self.png.len else 0, .version = if (file == .@"screen.png") @truncate(self.generation) else 0 };
}
pub fn capture(self: *Control, surface: *c.SDL_Surface) void {
@@ -395,8 +400,9 @@ pub const Control = struct {
fn readme(layout: @import("memory.zig").Layout, w: *std.Io.Writer) !void {
for (std.enums.values(File)[1..]) |f| {
const name = @tagName(f);
- try w.print("{s}{s}", .{ name, if (f == .state) "/" else "" });
- try w.splatByteAll(' ', @max(1, 41 -| (name.len + @intFromBool(f == .state))));
+ const dir = f == .state or f == .sketch;
+ try w.print("{s}{s}", .{ name, if (dir) "/" else "" });
+ try w.splatByteAll(' ', @max(1, 41 -| (name.len + @intFromBool(dir))));
try w.print("{s}\n", .{files.get(f).doc});
if (f == .ctl) try verbLines(w, " ", &.{});
if (f == .state) try state_files.readme(layout, w);
@@ -664,6 +670,7 @@ test "control writes take input verbs live, and the README and commands read the
try std.testing.expect(std.mem.indexOf(u8, text, "\n click X Y move there") != null);
try std.testing.expect(std.mem.indexOf(u8, text, "\n set FIELD VALUE... a field to a value") != null);
try std.testing.expect(std.mem.indexOf(u8, text, "\nstate/ the sketch's State") != null);
+ try std.testing.expect(std.mem.indexOf(u8, text, "\nsketch/ the sketch's own files") != null);
try std.testing.expect(std.mem.endsWith(u8, text, "\nstate/scene .room .street\nstate/talk/text 3 u8, blank-separated\n"));
} else {
try std.testing.expect(std.mem.startsWith(u8, text, "screenshot captures"));
@@ -672,3 +679,16 @@ test "control writes take input verbs live, and the README and commands read the
_ = try control.respond(&host, &conn, .{ .tag = 1, .op = .release, .node = @backingInt(f), .handle = handle, .opened = true }, &reply, &buf);
}
}
+
+test "control writes leave the sketch's directory and nodes alone" {
+ var control: Control = .{ .allocator = std.testing.allocator, .path = undefined, .listener = -1 };
+ var host: Host = undefined;
+ var conn: Connection = .{};
+ var reply: fs.Reply = .{ .tag = 1 };
+ var buf: [256]u8 = undefined;
+ _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .lookup, .node = 1, .data = "sketch" }, &reply, &buf);
+ try std.testing.expect(reply.attr.dir and reply.attr.node == 1 << 63);
+ try std.testing.expectEqual(@as(usize, 0), (try control.respond(&host, &conn, .{ .tag = 1, .op = .readdir, .node = 1 << 63 }, &reply, &buf)).len);
+ try std.testing.expectError(error.UnknownFile, control.respond(&host, &conn, .{ .tag = 1, .op = .lookup, .node = 1 << 63, .data = "buffer" }, &reply, &buf));
+ for ([_]u64{ (1 << 63) + 1, (1 << 63) | (1 << 62), std.math.maxInt(u64) }) |node| try std.testing.expectError(error.UnknownFile, control.respond(&host, &conn, .{ .tag = 1, .op = .getattr, .node = node }, &reply, &buf));
+}