diff options
| -rw-r--r-- | core/Output.zig | 19 | ||||
| -rw-r--r-- | core/config.zig | 4 | ||||
| -rw-r--r-- | core/exec.zig | 16 | ||||
| -rw-r--r-- | core/replica.zig | 95 | ||||
| -rw-r--r-- | core/root.zig | 126 | ||||
| -rw-r--r-- | core/tape.zig | 2 |
6 files changed, 249 insertions, 13 deletions
diff --git a/core/Output.zig b/core/Output.zig index 1335d8e9..42341002 100644 --- a/core/Output.zig +++ b/core/Output.zig @@ -721,17 +721,22 @@ pub fn isWalkRow(file: *const File.State, row: usize, line: []const u8) bool { pub fn openFonts(p: *Pardes, id: usize) !void { if (builtins.capabilities.font_picker) { - const arena = p.scratch.allocator(); - const font_list = fonts.list(arena, null); - var out: std.Io.Writer.Allocating = .init(p.gpa); - errdefer out.deinit(); - for (font_list) |f| - try out.writer.print(comptime config.Runtime.findAction(.font).?.word ++ " {s}\n", .{f.name}); - const content = try out.toOwnedSlice(); + // The session's machine's fonts, a question to its disk: a client's + // replay takes the session's answer, never its own. + const content = try pardes.tape.site(.fonts, p.gpa, fontsListed, .{ p.gpa, p.scratch.allocator() }); try openStepped(p, id, .{ .cmd = .Fonts }, content); } } +/// This machine's fonts, a `Font <name>` line each, in `gpa`. +fn fontsListed(gpa: std.mem.Allocator, arena: std.mem.Allocator) anyerror![]u8 { + var out: std.Io.Writer.Allocating = .init(gpa); + errdefer out.deinit(); + for (fonts.list(arena, null)) |f| + try out.writer.print(comptime config.Runtime.findAction(.font).?.word ++ " {s}\n", .{f.name}); + return out.toOwnedSlice(); +} + fn openStepped(p: *Pardes, id: usize, from: Origin, content: []u8) !void { errdefer p.gpa.free(content); const pane = p.panes[id] orelse return error.MissingPane; diff --git a/core/config.zig b/core/config.zig index 5bbe1886..f1e2a239 100644 --- a/core/config.zig +++ b/core/config.zig @@ -866,7 +866,9 @@ pub const Runtime = struct { if (text.len > 0) { if (text[0] != '/' and text[0] != '~') return false; var home_buf: [4096]u8 = undefined; - if (pardes.filesystem.deniedAbove(pardes.filesystem.expandHomeFrom(text, &home_buf, home))) return false; + // The session's disk's answer, as a failed save's (Core.zig): + // a client's replay takes it, never its own disk's. + if (pardes.tape.value(.denied, pardes.filesystem.deniedAbove, .{pardes.filesystem.expandHomeFrom(text, &home_buf, home)})) return false; } if (!state.dump_dir.set(text)) return false; }, diff --git a/core/exec.zig b/core/exec.zig index 5a9bf192..3f3b16a7 100644 --- a/core/exec.zig +++ b/core/exec.zig @@ -864,6 +864,14 @@ pub const acme_words = [_]struct { acme: []const u8, pardes: ?[]const u8, hint: .{ .acme = "Abort", .pardes = null, .hint = ": a debugging word that crashes acme" }, }; +/// The first of this machine's fonts named `name`, as its path, a NUL and +/// its name; empty for none. +fn fontNamed(arena: std.mem.Allocator, name: []const u8) []const u8 { + const matches = fonts.list(arena, name); + if (matches.len == 0) return ""; + return std.fmt.allocPrint(arena, "{s}\x00{s}", .{ matches[0].path, matches[0].name }) catch ""; +} + pub fn executeBuiltinLine(p: *Pardes, id: usize, txt: []const u8) bool { const raw = commandText(txt); if (raw.len == 0 or p.exec_depth >= max_exec_depth) return false; @@ -1008,9 +1016,11 @@ pub fn applySettingBuiltin(p: *Pardes, setting: config.Runtime.Setting, arg: ?[] .font => { if (comptime !font_picker) return; const spec = config.Runtime.FontSpec.parse(arg orelse return) orelse return; - const matches = fonts.list(p.scratch.allocator(), spec.name); - if (matches.len == 0) return; - if (!p.settings.requestFont(matches[0].path, matches[0].name, spec.size_hundredths)) return; + // The session's machine's fonts, a question to its disk: a + // client's replay takes the session's answer, never its own. + const found = pardes.tape.site(.fonts, p.scratch.allocator(), fontNamed, .{ p.scratch.allocator(), spec.name }); + const nul = std.mem.indexOfScalar(u8, found, 0) orelse return; + if (!p.settings.requestFont(found[0..nul], found[nul + 1 ..], spec.size_hundredths)) return; p.font_request_taken = false; }, // A shell that is no executable is refused, not taken and quietly diff --git a/core/replica.zig b/core/replica.zig index 80b3ab10..993c1f09 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -5262,7 +5262,8 @@ const On = enum { any, file, kind, shell, shell_normal }; /// The test below walks every word of the registry, every key and the /// gestures between panes against it: what stops and is not here fails /// it, and a row nothing stopped at says it is stale. -const may_wait = [_]struct { what: []const u8, on: On, stop: []const u8, why: []const u8 }{ +const Wait = struct { what: []const u8, on: On, stop: []const u8, why: []const u8 }; +const may_wait = [_]Wait{ // A shell's program has the keys: what reaches it is its input (a far // shell may feel like ssh). .{ .what = "*", .on = .shell, .stop = "pty_write", .why = "a key into a shell's program" }, @@ -5303,7 +5304,11 @@ const may_wait = [_]struct { what: []const u8, on: On, stop: []const u8, why: [] .{ .what = "DumpConfig", .on = .any, .stop = "the authority's own state", .why = "the config file written" }, .{ .what = "ThemeFile *", .on = .any, .stop = "the authority's own state", .why = "a theme file, the session's" }, .{ .what = "DumpThemes", .on = .any, .stop = "the authority's own state", .why = "the theme files written" }, -}; +} ++ (if (pardes.platform == .gui) [_]Wait{ + // A window's view (the font words are a window's). + .{ .what = "Font *", .on = .any, .stop = "fonts", .why = "the session's machine's fonts, its disk" }, + .{ .what = "Fonts", .on = .any, .stop = "fonts", .why = "the session's machine's fonts, its disk" }, +} else [_]Wait{}); /// The keys the walk types: every printable one, and the specials a /// frontend reads; each plain, with Ctrl and with Alt. @@ -5673,3 +5678,89 @@ test "a far client's own Look of ~/x, bare Shell and a word that runs in the ses r.dirty = false; } } + +test "a far client's replay of the session's DumpDir takes the session's answer to whether the directory may be written, not its own disk's" { + if (std.c.getuid() == 0) return error.SkipZigTest; // root writes anywhere + const gpa = std.testing.allocator; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + var dir_buf: [std.Io.Dir.max_path_bytes]u8 = undefined; + const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)]; + var ro_buf: [std.Io.Dir.max_path_bytes + 8:0]u8 = undefined; + const ro = try std.fmt.bufPrintSentinel(&ro_buf, "{s}/ro", .{dir}, 0); + // The session's machine: a directory it may not write in. + if (std.c.mkdir(ro, 0o500) != 0) return error.NoDir; + defer _ = std.c.chmod(ro, 0o700); + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + defer a.deinit(); + const r = try joinedFresh(a); + defer r.deinit(); + var line: [std.Io.Dir.max_path_bytes + 32]u8 = undefined; + const u = try gpa.dupe(u8, try a.local(.{ .command = try std.fmt.bufPrint(&line, "DumpDir {s}/dumps", .{ro}) })); + defer gpa.free(u); + try std.testing.expectEqualStrings("", a.core.settings.dump_dir.get()); + // The client's machine: the same path, and it may. + if (std.c.chmod(ro, 0o700) != 0) return error.NoChmod; + r.confirm(u); + r.settle(); + try agrees(a, r); + try std.testing.expectEqualStrings("", r.core().settings.dump_dir.get()); +} + +test "a far client's replay of the session's Fonts and Font takes the session's machine's fonts, not its own" { + if (comptime !pardes.builtins.capabilities.font_picker) return error.SkipZigTest; + const gpa = std.testing.allocator; + const fonts = @import("dev").fonts; + const Shell = @import("dev").host_io.Shell; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.createDirPath(std.testing.io, "session/.local/share/fonts"); + try tmp.dir.createDirPath(std.testing.io, "client"); + const face = try std.Io.Dir.cwd().readFileAlloc(std.testing.io, "assets/MapleMono-NF-Regular.ttf", gpa, .limited(64 << 20)); + defer gpa.free(face); + // Its family renamed, so that no face of this machine's shares its name. + for ([_][]const u8{ "Maple", "\x00M\x00a\x00p\x00l\x00e" }, [_][]const u8{ "Qzxjv", "\x00Q\x00z\x00x\x00j\x00v" }) |from, to| { + var at: usize = 0; + while (std.mem.indexOfPos(u8, face, at, from)) |i| : (at = i + to.len) @memcpy(face[i..][0..to.len], to); + } + try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "session/.local/share/fonts/seen.ttf", .data = face }); + var dir_buf: [std.Io.Dir.max_path_bytes]u8 = undefined; + const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)]; + var homes: [2][std.Io.Dir.max_path_bytes + 16:0]u8 = undefined; + const session_home = try std.fmt.bufPrintSentinel(&homes[0], "{s}/session", .{dir}, 0); + const client_home = try std.fmt.bufPrintSentinel(&homes[1], "{s}/client", .{dir}, 0); + // HOME as it was, after (a test's: runtime never sets the environment). + const was = if (std.c.getenv("HOME")) |h| try gpa.dupeSentinel(u8, std.mem.span(h), 0) else null; + defer if (was) |h| gpa.free(h); + _ = Shell.setenv("PARDES_TEST_ENVIRON_COPIED", "1", 1); + defer _ = if (was) |h| Shell.setenv("HOME", h, 1) else @import("dev").host_io.testUnsetenv("HOME"); + // The face the session's machine has and the client's lacks: its name. + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + _ = Shell.setenv("HOME", session_home, 1); + const name = for (fonts.list(arena.allocator(), null)) |f| { + if (std.mem.startsWith(u8, f.path, session_home)) break f.name; + } else return error.SkipZigTest; // not a face the scan takes + // The two machines answer differently, or this one cannot stand for two. + const session_first = fonts.list(arena.allocator(), name)[0].path; + const session_faces = fonts.list(arena.allocator(), null).len; + _ = Shell.setenv("HOME", client_home, 1); + const client_named = fonts.list(arena.allocator(), name); + if (fonts.list(arena.allocator(), null).len == session_faces and client_named.len > 0 and std.mem.eql(u8, client_named[0].path, session_first)) return error.SkipZigTest; + _ = Shell.setenv("HOME", session_home, 1); + // A window's view: the font words are a window's. + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml", .windowed = true }, TestHost.host(), false); + defer a.deinit(); + const r = try joinedFresh(a); + defer r.deinit(); + var line: [256]u8 = undefined; + const units = [_][]u8{ + try gpa.dupe(u8, try a.local(.{ .command = "Fonts" })), + try gpa.dupe(u8, try a.local(.{ .command = try std.fmt.bufPrint(&line, "Font {s} 13", .{name}) })), + }; + defer for (units) |u| gpa.free(u); + _ = Shell.setenv("HOME", client_home, 1); + for (units) |u| r.confirm(u); + r.settle(); + try agrees(a, r); +} diff --git a/core/root.zig b/core/root.zig index ae6fc454..c10a2598 100644 --- a/core/root.zig +++ b/core/root.zig @@ -593,3 +593,129 @@ test "nothing of a core is a process global: every container-level var in core/ try std.testing.expect(files > 100); try std.testing.expectEqual(@as(usize, 0), unlisted); } + +/// The lines of each file of core/ that name the machine outside a test: +/// `std.c.`, `libc.`, `std.posix.`, `getenv`, std.Io's File, Dir, net and +/// Threaded (a path's text, `Dir.path`, and `max_path_bytes` are none), and +/// `@import("dev")` and `@import("runtime")`, by file, how many and why. A +/// step's read of its own machine with no tape diverges a far client, and +/// the soak (one machine) never sees it: a new line here makes someone look. +/// The count is a file's, so it cannot tell a step's read from its host +/// half's; the reason says which the file's are. +const machine_lines = [_]struct { []const u8, usize, []const u8 }{ + .{ "Addon.zig", 48, "the process's addon libraries: the start's scan of their directories, dlopen, the crash mark; the host's, never a step's" }, + .{ "Core.zig", 5, "the session's directory read once at its start (taped), a frame log (PARDES_FRAMES), a host_io type" }, + .{ "Db.zig", 18, "the state file every session of the user shares: the authority's (`db`), off in every replica" }, + .{ "Dir.zig", 2, "a directory's listing, the function under tape.site(.dir_listing)" }, + .{ "Effect.zig", 1, "host_io, named for the host entries it performs" }, + .{ "FrameStats.zig", 7, "a frame's clock and log: frames are local, never digested" }, + .{ "Handover.zig", 29, "a window carried to another process over a socket: the session's host" }, + .{ "Kind.zig", 1, "host_io's KindJob: a kind's job is the host's" }, + .{ "Messages.zig", 6, "the message clock, read through tape.value(.clock); PARDES_NOTIME, the tests' (a step's read of the environment: set on one side only it would diverge)" }, + .{ "Output.zig", 2, "the fonts module (Fonts lists them under tape.site(.fonts)) and the build's version line" }, + .{ "Stream.zig", 14, "the disk search's worker: it runs off the step and its batches are inputs" }, + .{ "builtins.zig", 2, "Mount's probe (tape.site(.probe)) and Term's shell check (tape.site(.shell))" }, + .{ "config.zig", 6, "the default shell, read once at the start (taped); the config file read at startup and written by DumpConfig (the session's); the docs check's" }, + .{ "draw.zig", 1, "PARDES_TEST_CLOCK: a frame's, not a step's" }, + .{ "dump.zig", 4, "a dump's clock and the state file's directory (XDG, the process's directory): the session's side (a client's Restore waits)" }, + .{ "echo.zig", 1, "PARDES_REPLICA_TRACE: a trace" }, + .{ "edit_cmd.zig", 1, "Edit's pipe environment (host_io.commandEnv): kept in Pipe.edit_run, `.authority` state, read only by the host's `pipe`, which a client never calls" }, + .{ "exec.zig", 4, "kindOf's stat (tape.site(.kind)) and Shell's check (tape.site(.shell))" }, + .{ "fs.zig", 39, "the file system behind the tape's sites (open, read, write, stat, mkdir, access), the process's home (read once at the start, taped), the 9P transport" }, + .{ "hunks.zig", 1, "a test helper's file read" }, + .{ "look.zig", 6, "openLink's fork (the host's: a view's frontend opens a link) and a remote's error name" }, + .{ "memory.zig", 7, "the memory regions themselves (mmap, madvise, their locks)" }, + .{ "replica.zig", 70, "the client and session engines: sockets, threads, clocks, the blob cache, traces" }, + .{ "rollback.zig", 1, "a test helper's directory" }, + .{ "root.zig", 4, "dev's modules re-exported (host_io, file_watch, glide, fonts)" }, + .{ "selection_pipe.zig", 14, "a pipe's child processes and their files: the host's `pipe` entry" }, + .{ "tape.zig", 10, "the tape's own files and host_io's types: a recording, a replay" }, + .{ "lsp/lsp.zig", 1, "the language server's client (dev): the host's `lsp` entry" }, + .{ "ninep/ctl.zig", 1, "the session's pid in a 9P read: the session's server" }, + .{ "ninep/events.zig", 2, "the 9P event log's clock: the session's server" }, + .{ "ninep/pty.zig", 1, "a 9P exec's shell check (tape.site(.shell))" }, + .{ "ninep/screen.zig", 1, "a 9P address's text type" }, + .{ "ninep/tree.zig", 1, "the daemon's registry (/sessions, tape.site(.sessions))" }, + .{ "term/terminal.zig", 2, "the emulator's image files (TMPDIR) and its Io: the session's emulator (a client's are written by deltas)" }, +}; + +/// The lines of `src` that name the machine (`machine_lines`'s rules). +fn machineLines(src: []const u8, gpa: std.mem.Allocator, out: *std.ArrayList(usize)) !void { + const names = [_][]const u8{ "std.c.", "libc.", "std.posix.", "getenv", "std.Io.File", "std.Io.Dir", "std.Io.net", "std.Io.Threaded", "Io.File", "Io.Dir", "Io.net", "Io.Threaded" }; + const pure = [_][]const u8{ "std.Io.Dir.path.", "Io.Dir.path.", "std.Io.Dir.max_path_bytes", "Io.Dir.max_path_bytes" }; + var open: std.ArrayList(u8) = .empty; + defer open.deinit(gpa); + var tests: usize = 0; + var line: usize = 1; + var i: usize = 0; + while (i < src.len) { + const c = src[i]; + if (c == '\n') line += 1; + const at = src[i..]; + if ((c == '/' or c == '\\') and i + 1 < src.len and src[i + 1] == c) { + i = std.mem.indexOfScalarPos(u8, src, i, '\n') orelse src.len; + } else if (c == '@' and tests == 0 and (std.mem.startsWith(u8, at, "@import(\"dev\")") or std.mem.startsWith(u8, at, "@import(\"runtime\")"))) { + if (out.items.len == 0 or out.getLast() != line) try out.append(gpa, line); + i += 1; + } else if (c == '"' or c == '\'') { + i += 1; + while (i < src.len and src[i] != c) : (i += 1) if (src[i] == '\\') { + i += 1; + }; + i += 1; + } else if (c == '{') { + const kind = braceKind(src[0..i]); + if (kind == 't') tests += 1; + try open.append(gpa, kind); + i += 1; + } else if (c == '}') { + if ((open.pop() orelse 0) == 't') tests -= 1; + i += 1; + } else if (std.ascii.isAlphabetic(c) or c == '_') { + const named = for (names) |n| { + if (std.mem.startsWith(u8, at, n)) break true; + } else false; + const path = for (pure) |n| { + if (std.mem.startsWith(u8, at, n)) break true; + } else false; + if (tests == 0 and named and !path and (out.items.len == 0 or out.getLast() != line)) try out.append(gpa, line); + i = wordEnd(src, i); + } else i += 1; + } +} + +test "what core/ asks of the machine is where the list says, with why: a new line naming libc, std.c, std.posix, getenv, a std.Io file, directory or socket, dev or runtime is named" { + const gpa = std.testing.allocator; + const io = std.testing.io; + var dir = try std.Io.Dir.cwd().openDir(io, "core", .{ .iterate = true }); + defer dir.close(io); + var walker = try dir.walk(gpa); + defer walker.deinit(); + var found: std.ArrayList(usize) = .empty; + defer found.deinit(gpa); + var wrong: usize = 0; + var seen: [machine_lines.len]bool = @splat(false); + while (try walker.next(io)) |e| { + if (e.kind != .file or !std.mem.endsWith(u8, e.basename, ".zig")) continue; + const src = try e.dir.readFileAlloc(io, e.basename, gpa, .limited(16 << 20)); + defer gpa.free(src); + found.clearRetainingCapacity(); + try machineLines(src, gpa, &found); + const listed: usize = for (machine_lines, 0..) |m, k| { + if (std.mem.eql(u8, m[0], e.path)) { + seen[k] = true; + break m[1]; + } + } else 0; + if (found.items.len == listed) continue; + wrong += 1; + std.debug.print("core/{s}: {d} lines name the machine, root.zig's machine_lines says {d}:", .{ e.path, found.items.len, listed }); + for (found.items) |l| std.debug.print(" {d}", .{l}); + std.debug.print("\n a step that reads its own machine with no tape diverges a far client: tape the read (tape.site, tape.value), or count the line with why it is not a step's\n", .{}); + } + for (machine_lines, seen) |m, s| if (!s) { + wrong += 1; + std.debug.print("core/{s}: on machine_lines, and no such file\n", .{m[0]}); + }; + try std.testing.expectEqual(@as(usize, 0), wrong); +} diff --git a/core/tape.zig b/core/tape.zig index ce94f4ae..2b9272a6 100644 --- a/core/tape.zig +++ b/core/tape.zig @@ -101,6 +101,8 @@ pub const Site = enum(u16) { /// Whether a shell is installed (host_io.Shell.refusal): the session's /// disk, asked as any file is. shell, + /// The machine's fonts (Font <name>, Fonts): its disk too. + fonts, // Read in place (`value`). clock, budget, |
