//! docs/typ/reference.typ, Mounts : sessions mounting each other's //! trees over their sockets, at startup and at run time. const std = @import("std"); const process = @import("../process.zig"); const Conn = process.Conn; const Detached = process.Detached; const has = process.has; const check = process.check; pub const cases = .{ .{ "mounts: --mount at startup reads a peer's panes and its disk, a Save through it lands there, and a path not there is a miss in the log", startup }, .{ "mounts: a session listening on TCP and QUIC too (-Dquic=true) is mounted over QUIC, saved and got through, its screen read, and unmounted, v4 and v6, detached and in a pty", quic }, .{ "mounts: Mount and Unmount at run time, refused in words when mounted already or in use; a session's /n lists its mounts; a file through a mount opens at startup", runtime }, }; /// A session whose pane `serial` holds "replaced\nevent\n", as fs.py's /// `first` had one when its peers mounted it. fn peer(gpa: std.mem.Allocator, exe: [:0]const u8, a: std.mem.Allocator) !struct { Detached, *Conn, u32 } { var d = try Detached.start(gpa, exe, "first", .{}); errdefer d.deinit(); const c = try Conn.dial(gpa, d.socket()); const serial = try c.newPane(); try c.write(try std.fmt.allocPrint(a, "/pane/{d}/body", .{serial}), "replaced\nevent\n"); return .{ d, c, serial }; } fn startup(gpa: std.mem.Allocator, exe: [:0]const u8) !void { var arena = std.heap.ArenaAllocator.init(gpa); defer arena.deinit(); const a = arena.allocator(); var first, const client, const serial = try peer(gpa, exe, a); defer first.deinit(); defer client.deinit(gpa); const address = first.abs("pardes-9p-first.sock"); const own = first.abs("pardes-9p-second.sock"); var second = try Detached.start(gpa, exe, "second", .{ .root = first.root(), .args = &.{ try std.fmt.allocPrintSentinel(a, "--mount=peer={s}", .{address}, 0), try std.fmt.allocPrintSentinel(a, "--mount=own={s}", .{own}, 0) }, }); defer second.deinit(); const remote = try Conn.dial(gpa, second.socket()); defer remote.deinit(gpa); var opened = try remote.look(a, "/n/own/pane/1/body", null); try std.testing.expectEqualStrings("initial\n", try remote.pane(a, opened, "body")); opened = try remote.look(a, try std.fmt.allocPrint(a, "/n/peer/pane/{d}/body", .{serial}), null); try std.testing.expectEqualStrings("replaced\nevent\n", try remote.pane(a, opened, "body")); try remote.replace(try std.fmt.allocPrint(a, "/pane/{d}/body", .{opened}), "saved to peer\n"); try remote.exec(opened, "Save\n"); try std.testing.expectEqualStrings("saved to peer\n", try client.pane(a, serial, "body")); var large: std.ArrayList(u8) = .empty; for (0..60000) |_| try large.appendSlice(a, "mounted file contents\n"); try first.put("large.txt", large.items); opened = try remote.look(a, try std.fmt.allocPrint(a, "/n/peer/os{s}", .{first.abs("large.txt")}), null); try check(std.mem.eql(u8, try remote.pane(a, opened, "body"), large.items), "the mounted file is not whole", .{}); const edited = try std.fmt.allocPrint(a, "{s}edited\n", .{large.items[0 .. large.items.len - 3]}); try remote.replace(try std.fmt.allocPrint(a, "/pane/{d}/body", .{opened}), edited); try remote.exec(opened, "Save\n"); try check(std.mem.eql(u8, try first.get(a, "large.txt"), edited), "the Save through the mount is not on disk", .{}); try check(std.mem.eql(u8, try client.read(a, try std.fmt.allocPrint(a, "/os{s}", .{first.abs("large.txt")})), edited), "the peer's /os disagrees", .{}); // A path not there under a peer's mount is a miss, as any path to no // file is: the write answered, the err logged. try remote.write("/look", try std.fmt.allocPrint(a, "/n/peer/os{s}\n", .{first.abs("no-such-file.txt")})); try check(has(try remote.read(a, "/log"), "no such file"), "no miss logged", .{}); try client.exec(serial, "Del\n"); for (try client.list(a, "/pane")) |n| try check(!std.mem.eql(u8, n, try std.fmt.allocPrint(a, "{d}", .{serial})), "Del left the pane", .{}); } fn runtime(gpa: std.mem.Allocator, exe: [:0]const u8) !void { var arena = std.heap.ArenaAllocator.init(gpa); defer arena.deinit(); const a = arena.allocator(); var first, const client, _ = try peer(gpa, exe, a); defer first.deinit(); defer client.deinit(gpa); const address = first.abs("pardes-9p-first.sock"); { var d = try Detached.start(gpa, exe, "runtime", .{ .root = first.root() }); defer d.deinit(); const remote = try Conn.dial(gpa, d.socket()); defer remote.deinit(gpa); const own_address = first.abs("pardes-9p-runtime.sock"); const control = try remote.newPane(); try remote.exec(control, try std.fmt.allocPrint(a, "Mount peer {s}\n", .{address})); var opened = try remote.look(a, "/n/peer/pane/1/body", control); try std.testing.expectEqualStrings(try client.read(a, "/pane/1/body"), try remote.pane(a, opened, "body")); // A builtin that fails through exec fails the write (one rule). try remote.execRefused(control, try std.fmt.allocPrint(a, "Mount peer {s}\n", .{own_address}), "already mounted"); try remote.execRefused(control, "Unmount peer\n", "mount in use"); // a pane is open on it try remote.replace(try std.fmt.allocPrint(a, "/pane/{d}/body", .{opened}), "runtime mounted Save\n"); try remote.exec(opened, "Save\n"); try std.testing.expectEqualStrings("runtime mounted Save\n", try client.read(a, "/pane/1/body")); try std.testing.expectEqualStrings("initial\n", try remote.read(a, "/pane/1/body")); const roots = try remote.look(a, "/n", control); try check(has(try remote.pane(a, roots, "body"), "/n/peer/\n"), "/n lists no peer", .{}); try remote.exec(opened, "Del\n"); try remote.exec(control, "Unmount peer\n"); try remote.write(try std.fmt.allocPrint(a, "/pane/{d}/ctl", .{roots}), "get\n"); try std.testing.expectEqualStrings("/n/os/\n/n/self/\n", try remote.pane(a, roots, "body")); try remote.exec(control, try std.fmt.allocPrint(a, "Mount peer {s}\n", .{own_address})); opened = try remote.look(a, "/n/peer/pane/1/body", control); try std.testing.expectEqualStrings("initial\n", try remote.pane(a, opened, "body")); } var d = try Detached.start(gpa, exe, "mounted-startup", .{ .root = first.root(), .args = &.{try std.fmt.allocPrintSentinel(a, "--mount=peer={s}", .{address}, 0)}, .file = "/n/peer/pane/1/body:1", }); defer d.deinit(); try d.files.expect("/pane/1/body", "runtime mounted Save\n"); } /// The port of /listeners' `!!` line. fn port(listeners: []const u8, net: []const u8) !u16 { var rows = std.mem.tokenizeScalar(u8, listeners, '\n'); while (rows.next()) |row| { var parts = std.mem.splitScalar(u8, row, '!'); if (!std.mem.eql(u8, parts.first(), net)) continue; _ = parts.next() orelse return error.BadListener; const p = try std.fmt.parseInt(u16, parts.next() orelse return error.BadListener, 10); try check(parts.next() == null and p > 0, "listener {s}", .{row}); return p; } return error.NoListener; } fn quic(gpa: std.mem.Allocator, exe: [:0]const u8) !void { if (!try process.quicIncluded(gpa, exe)) { std.debug.print("SKIP a build without QUIC (-Dquic=true)\n", .{}); return; } for ([_]bool{ false, true }) |tty| for ([_][:0]const u8{ "127.0.0.1", "::1" }) |host| { var arena = std.heap.ArenaAllocator.init(gpa); defer arena.deinit(); const a = arena.allocator(); const args = [_][:0]const u8{ try std.fmt.allocPrintSentinel(a, "--9p-tcp=tcp!{s}!0", .{host}, 0), try std.fmt.allocPrintSentinel(a, "--9p-quic=quic!{s}!0", .{host}, 0), }; var d = try Detached.start(gpa, exe, if (tty) "quic-root" else "server", .{ .args = if (tty) &.{} else &args }); defer d.deinit(); var term: ?process.Child = null; defer if (term) |*t| t.deinit(); if (tty) term = try d.startTty(exe, "server", &args); const address = d.abs("pardes-9p-server.sock"); const unix = try Conn.dial(gpa, address); defer unix.deinit(gpa); const listeners = try unix.read(a, "/listeners"); const tcp_port = try port(listeners, "tcp"); const quic_port = try port(listeners, "quic"); try std.testing.expectEqual(@as(usize, 3), std.mem.count(u8, listeners, "\n")); for ([_][]const u8{ try std.fmt.allocPrint(a, "unix!{s}\n", .{address}), try std.fmt.allocPrint(a, "tcp!{s}!{d}\n", .{ host, tcp_port }), try std.fmt.allocPrint(a, "quic!{s}!{d}\n", .{ host, quic_port }), }) |row| try check(has(listeners, row), "no {s} in {s}", .{ row, listeners }); const tcp = try Conn.dialTcp(gpa, host, tcp_port); defer tcp.deinit(gpa); const unix_body = try unix.hold("/pane/1/body", 0); defer unix.clunk(unix_body); const tcp_body = try tcp.hold("/pane/1/body", 0); defer tcp.clunk(tcp_body); var client = try Detached.start(gpa, exe, "client", .{ .root = d.root() }); defer client.deinit(); const remote = try Conn.dial(gpa, client.socket()); defer remote.deinit(gpa); const control = try remote.newPane(); try remote.exec(control, try std.fmt.allocPrint(a, "Mount peer quic!{s}!{d}\n", .{ host, quic_port })); const opened = try remote.look(a, "/n/peer/pane/1/body", control); const body = try std.fmt.allocPrint(a, "/pane/{d}/body", .{opened}); try std.testing.expectEqualStrings("initial\n", try remote.read(a, body)); var payload: std.ArrayList(u8) = .empty; try payload.appendSlice(a, "saved café λ\n"); try payload.appendNTimes(a, 'x', 3 * 8192 + 27); try payload.append(a, '\n'); try remote.replace(body, payload.items); try remote.exec(opened, "Save\n"); try std.testing.expectEqualStrings(payload.items[0..@min(payload.items.len, unix.cl.maxRead())], try unix.readFid(unix_body, 0, 65536)); try check(std.mem.eql(u8, try tcp.read(a, "/pane/1/body"), payload.items), "TCP reads another body", .{}); try tcp.replace("/pane/1/body", "TCP to QUIC Get\n"); try remote.write(try std.fmt.allocPrint(a, "/pane/{d}/ctl", .{opened}), "get\n"); try std.testing.expectEqualStrings("TCP to QUIC Get\n", try remote.read(a, body)); try std.testing.expectEqualStrings("TCP to QUIC Get\n", try unix.readFid(unix_body, 0, 65536)); try std.testing.expectEqualStrings("TCP to QUIC Get\n", try tcp.readFid(tcp_body, 0, 65536)); // The peer's own screen, whole, over QUIC: its pane's tag, not its // text, whose place after the body shrank depends on a frame drawn. const screen_pane = try remote.look(a, "/n/peer/screen", control); const json = try std.json.parseFromSliceLeaky(std.json.Value, a, try remote.pane(a, screen_pane, "body"), .{}); const cells = json.object.get("cells").?.array.items; try std.testing.expectEqual(@as(usize, @intCast(json.object.get("cols").?.integer * json.object.get("rows").?.integer)), cells.len); var text: std.ArrayList(u8) = .empty; for (cells) |cell| try text.appendSlice(a, cell.array.items[0].string); try check(has(text.items, "server.txt Save"), "the peer's screen has no tag", .{}); var large: std.ArrayList(u8) = .empty; for (0..20000) |_| try large.appendSlice(a, "normal λ line\n"); try d.put("large.txt", large.items); const large_pane = try remote.look(a, try std.fmt.allocPrint(a, "/n/peer/os{s}", .{d.abs("large.txt")}), control); try check(std.mem.eql(u8, try remote.pane(a, large_pane, "body"), large.items), "the large file is not whole", .{}); try remote.replace(try std.fmt.allocPrint(a, "/pane/{d}/body", .{large_pane}), "QUIC file saved\n"); try remote.exec(large_pane, "Save\n"); try std.testing.expectEqualStrings("QUIC file saved\n", try d.get(a, "large.txt")); for ([_]u32{ opened, screen_pane, large_pane }) |n| try remote.exec(n, "Del\n"); try remote.exec(control, "Unmount peer\n"); const roots = try remote.look(a, "/n", control); try std.testing.expectEqualStrings("/n/os/\n/n/self/\n", try remote.pane(a, roots, "body")); try std.testing.expectEqualStrings("TCP to QUIC Get\n", try unix.readFid(unix_body, 0, 65536)); try std.testing.expectEqualStrings("TCP to QUIC Get\n", try tcp.readFid(tcp_body, 0, 65536)); // Not checked: a session Looking at its OWN tree through a QUIC // mount: QUIC is answered on the editor's thread, so that Look waits // on the loop it runs in (features.txt, "Left as they are"). }; }