diff options
Diffstat (limited to 'src/upgrade.zig')
| -rw-r--r-- | src/upgrade.zig | 510 |
1 files changed, 0 insertions, 510 deletions
diff --git a/src/upgrade.zig b/src/upgrade.zig deleted file mode 100644 index 460169e7..00000000 --- a/src/upgrade.zig +++ /dev/null @@ -1,510 +0,0 @@ -//! Upgrade (docs/next-daemon.md, "Phase 6: upgrade notes"): a running -//! session, and the daemon, moved onto the pardes installed since they -//! started, in place. -//! -//! At a step boundary a session commits its dump to the state file -//! (Db.zig, as `upgrade-<pid>`) and execs the path it was started from. Its pty masters, terminals, stubs and daemon -//! link stay open across the exec, named in a record on the new command -//! line: `--upgraded=<record>` ahead of the arguments it was started with. -//! The pid stays, so its shells stay its children; the new process loads -//! the dump and takes those fds back (each host's `resume`). A new build -//! that cannot load the dump execs the old one again, from the copy this -//! process left open (Linux), and with none it starts afresh with the -//! shells adopted, the dump left for a Restore by hand. A record of an older -//! build names a `.dump` file instead (0.24 reads one, and deletes it once -//! loaded). -const std = @import("std"); -const builtin = @import("builtin"); -/// libc: exec of the new build with fds held across it, and /proc/self/exe opened before it. -const libc = std.c; -const posix = std.posix; -const pardes = @import("pardes.zig"); - -pub const supported = builtin.os.tag == .linux or builtin.os.tag == .freebsd; -pub const flag = "--upgraded="; - -/// What this build says it is (`pardes --version`'s first line). -pub const version_line = if (pardes.commit) |c| "pardes " ++ pardes.version ++ " (" ++ c ++ ")" else "pardes " ++ pardes.version; - -// ---- the path it was started from ---- - -/// The path this process was started from, what Upgrade execs: a reinstall -/// puts the new build there, while /proc/self/exe names the old file, -/// deleted by then. -var exe_buf: [4096:0]u8 = undefined; -var exe_len: usize = 0; -/// The arguments it was started with (an upgrade's record left out). -var given: []const [:0]const u8 = &.{}; -/// The file at that path when it started, and the newer one last said. -var started_inode: ?u64 = null; -var told_inode: ?u64 = null; - -var located = false; - -/// Its arguments kept, at its start; an upgraded process's path is the one -/// its record names (the system would name a fallback's old binary, -/// deleted). -pub fn remember(args: []const [:0]const u8) void { - given = if (args.len > 1 and std.mem.startsWith(u8, args[1], flag)) args[2..] else if (args.len > 0) args[1..] else &.{}; - if (resumed) |*rec| if (rec.exe.len > 0 and rec.exe.len < exe_buf.len) { - @memcpy(exe_buf[0..rec.exe.len], rec.exe); - exe_buf[rec.exe.len] = 0; - exe_len = rec.exe.len; - }; -} - -/// Its path asked of the system (a readlink) and the file there now (a -/// stat), once: after the first frame, off a startup's way (the hosts' -/// loops; nothing is reinstalled within a frame of a start). -pub fn locate() void { - if (located) return; - located = true; - if (exe_len == 0) { - const n = std.process.executablePath(std.Io.Threaded.global_single_threaded.io(), &exe_buf) catch 0; - exe_buf[n] = 0; - exe_len = n; - } - if (exe()) |path| started_inode = inodeOf(path); -} - -fn inodeOf(path: [:0]const u8) ?u64 { - const st = std.Io.Dir.cwd().statFile(std.Io.Threaded.global_single_threaded.io(), path, .{}) catch return null; - return @intCast(st.inode); -} - -/// A newer build installed at that path since this process started, said -/// in a line, once a build: asked at a focus-in, where one comes back from -/// rebuilding (a stat). ponytail: explicit only, no watch and no automatic -/// upgrade. -pub fn noticeNewer(core: *pardes.Pardes) void { - if (newer()) core.setMessage(core.view().active, pardes.newer_build_words); -} - -/// Whether to say a newer build is installed now (`noticeNewer`): the -/// tty's Host answers the core's focus step with it, so its replica -/// clients take the line from the tape. -pub fn newer() bool { - const was = started_inode orelse return false; - const now = inodeOf(exe() orelse return false) orelse return false; - if (now == was or told_inode == now) return false; - told_inode = now; - return true; -} - -pub fn exe() ?[:0]const u8 { - return if (exe_len == 0) null else exe_buf[0..exe_len :0]; -} - -// ---- the record ---- - -pub const Pty = struct { pane: u8, pid: libc.pid_t, fd: c_int }; -/// A terminal and its view: the primary one's stub is daemon.stub; its -/// termios is the cooked one it is put back to at the end (it is raw now). -pub const Tty = struct { view: u8, fd: c_int, stub: c_int, primary: bool, termios: posix.termios }; - -pub const Record = struct { - /// daemon.link and daemon.stub. - link: c_int = -1, - stub: c_int = -1, - /// The binary being replaced, open (Linux): a new one that cannot load - /// the dump execs it again. - old: c_int = -1, - /// A window was open (the GUI). - window: bool = false, - ptys: [pardes.MAX_PANES]Pty = undefined, - nptys: usize = 0, - ttys: [pardes.views.max]Tty = undefined, - nttys: usize = 0, - /// The path exec'd, the one a later Upgrade execs again. - exe: []const u8 = "", - /// The state file the session's dump is in (an older build's: the - /// `.dump` file it is). - dump: []const u8 = "", - /// The dump's label there; none, `dump` is a file of its own. - label: []const u8 = "", - /// Why the new build could not load it: a start that went back to the - /// old binary says so. - refused: []const u8 = "", - /// This process's own: its dump would not load, so it started afresh - /// and adopts the shells. - fresh: bool = false, - - pub fn addPty(rec: *Record, pane: usize, pid: libc.pid_t, fd: c_int) void { - if (rec.nptys == rec.ptys.len or fd < 0) return; - rec.ptys[rec.nptys] = .{ .pane = @intCast(pane), .pid = pid, .fd = fd }; - rec.nptys += 1; - } - - pub fn addTty(rec: *Record, t: Tty) void { - if (rec.nttys == rec.ttys.len or t.fd < 0) return; - rec.ttys[rec.nttys] = t; - rec.nttys += 1; - } - - /// Every fd it names, close-on-exec (`on`) or kept across one. - pub fn closeOnExec(rec: *const Record, on: bool) void { - const set = struct { - fn f(fd: c_int, cloexec: bool) void { - if (fd >= 0) _ = libc.fcntl(fd, libc.F.SETFD, @as(c_int, if (cloexec) 1 else 0)); - } - }.f; - set(rec.link, on); - set(rec.stub, on); - set(rec.old, on); - for (rec.ptys[0..rec.nptys]) |p| set(p.fd, on); - for (rec.ttys[0..rec.nttys]) |t| { - set(t.fd, on); - set(t.stub, on); - } - } - - /// One `word value` a line, the dump's path last (its line is all of it). - pub fn encode(rec: *const Record, w: *std.Io.Writer) !void { - try w.print("link {d}\nstub {d}\nold {d}\nwindow {d}\n", .{ rec.link, rec.stub, rec.old, @intFromBool(rec.window) }); - for (rec.ptys[0..rec.nptys]) |p| try w.print("pty {d} {d} {d}\n", .{ p.pane, p.pid, p.fd }); - for (rec.ttys[0..rec.nttys]) |t| try w.print("tty {d} {d} {d} {d} {x}\n", .{ t.view, t.fd, t.stub, @intFromBool(t.primary), std.mem.asBytes(&t.termios) }); - if (rec.refused.len > 0) try w.print("refused {s}\n", .{rec.refused}); - if (rec.label.len > 0) try w.print("label {s}\n", .{rec.label}); - try w.print("exe {s}\ndump {s}\n", .{ rec.exe, rec.dump }); - } - - /// `text`'s record; its strings are slices of it. - pub fn decode(text: []const u8) !Record { - var rec: Record = .{}; - var lines = std.mem.splitScalar(u8, text, '\n'); - while (lines.next()) |line| { - if (line.len == 0) continue; - const sp = std.mem.indexOfScalar(u8, line, ' ') orelse return error.BadRecord; - const word = line[0..sp]; - const rest = line[sp + 1 ..]; - var it = std.mem.tokenizeScalar(u8, rest, ' '); - // A word of a later build's record is passed over. - switch (std.meta.stringToEnum(enum { link, stub, old, window, pty, tty, refused, label, exe, dump }, word) orelse continue) { - .link => rec.link = try std.fmt.parseInt(c_int, rest, 10), - .stub => rec.stub = try std.fmt.parseInt(c_int, rest, 10), - .old => rec.old = try std.fmt.parseInt(c_int, rest, 10), - .window => rec.window = std.mem.eql(u8, rest, "1"), - .pty => { - if (rec.nptys == rec.ptys.len) return error.BadRecord; - const pane = try std.fmt.parseInt(u8, it.next() orelse return error.BadRecord, 10); - if (pane >= pardes.MAX_PANES) return error.BadRecord; - rec.ptys[rec.nptys] = .{ - .pane = pane, - .pid = try std.fmt.parseInt(libc.pid_t, it.next() orelse return error.BadRecord, 10), - .fd = try std.fmt.parseInt(c_int, it.next() orelse return error.BadRecord, 10), - }; - rec.nptys += 1; - }, - .tty => { - if (rec.nttys == rec.ttys.len) return error.BadRecord; - var t: Tty = .{ - .view = try std.fmt.parseInt(u8, it.next() orelse return error.BadRecord, 10), - .fd = try std.fmt.parseInt(c_int, it.next() orelse return error.BadRecord, 10), - .stub = try std.fmt.parseInt(c_int, it.next() orelse return error.BadRecord, 10), - .primary = std.mem.eql(u8, it.next() orelse return error.BadRecord, "1"), - .termios = undefined, - }; - const hex = it.next() orelse return error.BadRecord; - // Another build's termios is the kernel's, of one size. - if (hex.len != 2 * @sizeOf(posix.termios) or t.view >= pardes.views.max) return error.BadRecord; - _ = try std.fmt.hexToBytes(std.mem.asBytes(&t.termios), hex); - rec.ttys[rec.nttys] = t; - rec.nttys += 1; - }, - .refused => rec.refused = rest, - .label => rec.label = rest, - .exe => rec.exe = rest, - .dump => rec.dump = rest, - } - } - if (rec.dump.len == 0) return error.BadRecord; - return rec; - } -}; - -/// The record this process was started with (`--upgraded=`), set by main. -pub var resumed: ?Record = null; - -// ---- before: asking the new binary, and the exec ---- - -pub const Checked = union(enum) { ok: []const u8, no: []const u8 }; - -/// `path --version` run as it is: a binary not there, not a pardes, or one -/// that cannot start, found before anything is let go. Its first line -/// (`pardes <version>`), or why not, in `buf`. -pub fn check(path: [:0]const u8, buf: []u8) Checked { - var pipe: [2]c_int = undefined; - if (libc.pipe(&pipe) != 0) return .{ .no = "Upgrade: no pipe to ask the new pardes its version" }; - const argv = [_:null]?[*:0]const u8{ path, "--version", null }; - const pid = libc.fork(); - if (pid < 0) { - _ = libc.close(pipe[0]); - _ = libc.close(pipe[1]); - return .{ .no = "Upgrade: no process to ask the new pardes its version" }; - } - if (pid == 0) { - // Only async-signal-safe calls between fork and exec. - _ = libc.dup2(pipe[1], 1); - const null_fd = libc.open("/dev/null", .{ .ACCMODE = .RDWR }, @as(libc.mode_t, 0)); - if (null_fd >= 0) { - _ = libc.dup2(null_fd, 0); - _ = libc.dup2(null_fd, 2); - } - _ = libc.execve(path, &argv, @ptrCast(libc.environ)); - libc._exit(127); - } - _ = libc.close(pipe[1]); - var len: usize = 0; - var waited = false; - while (len < buf.len) { - // A pardes answers --version in milliseconds; 10 s is one that hangs. - var fds = [_]libc.pollfd{.{ .fd = pipe[0], .events = libc.POLL.IN, .revents = 0 }}; - const ready = libc.poll(&fds, 1, 10_000); - if (ready < 0 and libc.errno(ready) == .INTR) continue; - if (ready <= 0) { - _ = libc.kill(pid, .KILL); - waited = true; - break; - } - const n = libc.read(pipe[0], buf[len..].ptr, buf.len - len); - if (n < 0 and libc.errno(n) == .INTR) continue; - if (n <= 0) break; - len += @intCast(n); - } - _ = libc.close(pipe[0]); - var status: c_int = 0; - while (libc.waitpid(pid, &status, 0) < 0 and libc.errno(-1) == .INTR) {} - const s: u32 = @bitCast(status); - const said = buf[0..len]; - const first = said[0 .. std.mem.indexOfScalar(u8, said, '\n') orelse said.len]; - if (!waited and libc.W.IFEXITED(s) and libc.W.EXITSTATUS(s) == 0 and std.mem.startsWith(u8, first, "pardes ")) return .{ .ok = first }; - // The words go where the line was: the rest of `buf`. - var tail: [96]u8 = undefined; - const how = if (waited) - "did not answer --version in 10 s" - else if (libc.W.IFEXITED(s) and libc.W.EXITSTATUS(s) == 127) - "would not start" - else if (libc.W.IFEXITED(s) and libc.W.EXITSTATUS(s) != 0) - std.fmt.bufPrint(&tail, "exited {d} at --version", .{libc.W.EXITSTATUS(s)}) catch unreachable // a status number fits 96 - else if (libc.W.IFSIGNALED(s)) - std.fmt.bufPrint(&tail, "died of signal {d} at --version", .{libc.W.TERMSIG(s)}) catch unreachable // a signal number fits 96 - else - "does not answer --version as a pardes does"; - return .{ .no = std.fmt.bufPrint(buf, "Upgrade: {s} {s}; this session stays as it is", .{ path[0..@min(path.len, 400)], how }) catch "Upgrade: the new pardes would not start; this session stays as it is" }; -} - -/// The binary this process runs, open, for a new one to fall back to: on -/// Linux the file even once deleted. ponytail: FreeBSD has no /proc by -/// default, so a new build there that cannot load a dump starts afresh. -fn openSelf() c_int { - if (comptime builtin.os.tag != .linux) return -1; - return libc.open("/proc/self/exe", .{ .ACCMODE = .RDONLY, .CLOEXEC = true }, @as(libc.mode_t, 0)); -} - -/// This process exec'd as `path` (or the binary open on `old`), the record -/// on its command line and the fds it names kept open. Returns only when -/// the exec failed, those fds close-on-exec again: why, in words. -pub fn exec(gpa: std.mem.Allocator, path: ?[:0]const u8, old: c_int, rec: *const Record) []const u8 { - var text: std.Io.Writer.Allocating = .init(gpa); - defer text.deinit(); - text.writer.writeAll(flag) catch return "no memory for its record"; - rec.encode(&text.writer) catch return "no memory for its record"; - text.writer.writeByte(0) catch return "no memory for its record"; - const record: [*:0]const u8 = @ptrCast(text.written().ptr); - const argv = gpa.allocSentinel(?[*:0]const u8, given.len + 2, null) catch return "no memory for its arguments"; - defer gpa.free(argv); - argv[0] = if (path) |p| p.ptr else if (exe()) |e| e.ptr else "pardes"; - argv[1] = record; - for (given, argv[2..]) |a, *slot| slot.* = a.ptr; - rec.closeOnExec(false); - const envp: [*:null]const ?[*:0]const u8 = @ptrCast(libc.environ); - // execveat by its syscall: musl (a static pardes) has no wrapper. - const e = if (path) |p| - libc.errno(libc.execve(p, argv.ptr, envp)) - else if (comptime builtin.os.tag == .linux) - std.os.linux.errno(std.os.linux.execveat(old, "", argv.ptr, envp, .{ .SYMLINK_NOFOLLOW = false, .EMPTY_PATH = true })) - else - libc.errno(@as(c_int, -1)); - rec.closeOnExec(true); - return switch (e) { - .NOENT => "it is not there", - .ACCES, .PERM => "it may not be run", - .NOEXEC => "it is not a program this system runs", - .@"2BIG" => "its record and arguments are too long", - .NOMEM => "no memory for it", - .TXTBSY => "it is still being written", - else => "the exec failed", - }; -} - -/// The state file's path an upgrade's record names (`dumpInto`'s). -var state_path: [4200]u8 = undefined; - -/// An Upgrade's dump, the terminal's and the window's alike: committed to -/// the state file as `upgrade-<pid>` (its label kept in `at`), where a -/// Restore finds it, and named in `rec`, the binary it replaces held open -/// for a new build that cannot load it. Null when it is, else why in words -/// (`why`'s). -pub fn dumpInto(core: *pardes.Pardes, rec: *Record, at: *[1024:0]u8, why: []u8) ?[]const u8 { - const path = pardes.Db.statePath(&state_path, core.settings.dump_dir.get()) orelse return "Upgrade: no directory for the session's dump; this session stays as it is"; - var name: [64]u8 = undefined; - const label = std.fmt.bufPrint(&name, "upgrade-{d}", .{std.c.getpid()}) catch unreachable; - const bytes = pardes.dump.image(core, label) catch |err| { - var words: [128]u8 = undefined; - return std.fmt.bufPrint(why, "Upgrade: the session would not dump: {s}; it stays as it is", .{pardes.Messages.errorWords(err, &words)}) catch "Upgrade: the session would not dump"; - }; - defer core.gpa.free(bytes); - const put = core.db.putDump(core, bytes, label, at[0 .. at.len - 1]) catch |err| { - var words: [128]u8 = undefined; - return std.fmt.bufPrint(why, "Upgrade: the session's dump could not be written to {s} ({s}); it stays as it is", .{ path[0..@min(path.len, 600)], pardes.Messages.errorWords(err, &words) }) catch "Upgrade: its dump could not be written"; - }; - at[put.len] = 0; - rec.dump = path; - rec.label = at[0..put.len]; - rec.old = openSelf(); - return null; -} - -/// The daemon told, 9P's last answers sent (`fs`), and this process -/// exec'd as `path` with `rec`, whose dump `dumpInto` labelled `at`. -/// Returns only when the exec failed, the binary it replaces let go (the -/// dump stays, a dump among the state file's): why, in words (`why`'s). -pub fn execAs(gpa: std.mem.Allocator, path: [:0]const u8, rec: *const Record, at: [*:0]const u8, fs: ?*@import("9p_io.zig").Listener, why: []u8) []const u8 { - _ = at; - @import("daemon.zig").upgraded(); - if (fs) |f| f.flushOut(); - const err_words = exec(gpa, path, -1, rec); - // Still here: the exec failed. - if (rec.old >= 0) _ = libc.close(rec.old); - return std.fmt.bufPrint(why, "Upgrade: {s} would not start: {s}; this session stays as it is", .{ path[0..@min(path.len, 600)], err_words }) catch "Upgrade: the new pardes would not start"; -} - -// ---- after: the new process ---- - -/// The core an upgraded process starts with: the session its dump is. A -/// build that cannot load it execs the binary it replaced to load it; with -/// none, a fresh session (`fresh`) the host adopts the shells into, the -/// dump left where a Restore finds it. `PARDES_TEST_UPGRADE_REFUSE` refuses -/// a load as such a build would (test/daemon.py's failing upgrade), in the -/// first start only, or (`all`) in the start it falls back to as well. -pub fn load(gpa: std.mem.Allocator, core_gpa: std.mem.Allocator, opts: pardes.Options, rec: *Record) !*pardes.Pardes { - var kept: [pardes.MAX_PANES]bool = @splat(false); - for (rec.ptys[0..rec.nptys]) |p| kept[p.pane] = true; - const loaded = loaded: { - if (libc.getenv("PARDES_TEST_UPGRADE_REFUSE")) |how| if (rec.refused.len == 0 or std.mem.eql(u8, std.mem.span(how), "all")) - break :loaded error.RefusedByTest; - const bytes = (if (rec.label.len > 0) pardes.Db.readDumpAt(gpa, rec.dump, rec.label) else pardes.filesystem.readFile(gpa, rec.dump)) catch |err| break :loaded err; - defer gpa.free(bytes); - break :loaded pardes.dump.resumeFromDump(core_gpa, opts, bytes, &kept); - }; - var buf: [700]u8 = undefined; - // Restore's word for the dump left behind: its label, or its file. - const again_name = if (rec.label.len > 0) rec.label else rec.dump; - if (loaded) |p| { - // An older build's dump, a file of its own, goes once loaded; a - // label stays among the state file's dumps. - if (rec.label.len == 0) { - // fs.readFile read it by this path, 0-terminated in 4096 bytes: - // a longer one was refused there (error.PathTooLong), kept. - var path_buf: [4096]u8 = undefined; - _ = libc.unlink(std.fmt.bufPrintSentinel(&path_buf, "{s}", .{rec.dump}, 0) catch unreachable); - } - // The binary it replaced, let go: held, its file (deleted) would - // keep its blocks for as long as this session runs. - if (rec.old >= 0) _ = libc.close(rec.old); - rec.old = -1; - p.setMessage(p.view().active, if (rec.refused.len > 0) - std.fmt.bufPrint(&buf, "Upgrade: the new build could not load this session ({s}); it goes on as {s}", .{ rec.refused[0..@min(rec.refused.len, 200)], version_line }) catch "Upgrade: the new build could not load this session; it goes on as before" - else - "Upgrade: now " ++ version_line); - return p; - } else |err| { - var words_buf: [128]u8 = undefined; - const why = if (err == error.RefusedByTest) "refused by PARDES_TEST_UPGRADE_REFUSE" else pardes.Messages.errorWords(err, &words_buf); - // The binary it replaced loads its own dump: tried once, never from - // a start that is that fallback already. - if (rec.old >= 0 and rec.refused.len == 0) { - var again = rec.*; - again.refused = why; - again.old = -1; - _ = exec(gpa, null, rec.old, &again); - } - if (rec.old >= 0) _ = libc.close(rec.old); - rec.old = -1; - rec.fresh = true; - var o = opts; - o.load_path = null; - const p = try pardes.Pardes.init(core_gpa, o); - p.setMessage(p.view().active, std.fmt.bufPrint(&buf, "Upgrade: no build could load the session ({s}); its shells are here, and Restore {s} brings the rest back", .{ why, again_name[0..@min(again_name.len, 400)] }) catch "Upgrade: no build could load the session; its dump is kept for a Restore"); - return p; - } -} - -test "an upgrade's record reads back as it was written, and refuses one cut short" { - var rec: Record = .{ .link = 5, .stub = 7, .old = 9, .window = true, .exe = "/a b/pardes", .dump = "/home/u/.local/share/pardes/pardes.xitdb", .label = "upgrade-1" }; - rec.addPty(3, 1234, 11); - rec.addPty(0, 99, 12); - var tio: posix.termios = std.mem.zeroes(posix.termios); - std.mem.asBytes(&tio)[0] = 0x5a; - rec.addTty(.{ .view = 2, .fd = 13, .stub = 14, .primary = true, .termios = tio }); - var buf: [4096]u8 = undefined; - var w: std.Io.Writer = .fixed(&buf); - try rec.encode(&w); - const back = try Record.decode(w.buffered()); - try std.testing.expectEqual(@as(c_int, 5), back.link); - try std.testing.expectEqual(@as(c_int, 9), back.old); - try std.testing.expect(back.window); - try std.testing.expectEqual(@as(usize, 2), back.nptys); - try std.testing.expectEqual(Pty{ .pane = 3, .pid = 1234, .fd = 11 }, back.ptys[0]); - try std.testing.expectEqual(@as(usize, 1), back.nttys); - try std.testing.expect(back.ttys[0].primary and back.ttys[0].view == 2 and back.ttys[0].stub == 14); - try std.testing.expectEqual(@as(u8, 0x5a), std.mem.asBytes(&back.ttys[0].termios)[0]); - try std.testing.expectEqualStrings("/a b/pardes", back.exe); - try std.testing.expectEqualStrings("/home/u/.local/share/pardes/pardes.xitdb", back.dump); - try std.testing.expectEqualStrings("upgrade-1", back.label); - // An older build's: its dump a file, no label. - const older = try Record.decode("link 5\nexe /a\ndump /run/u/pardes-upgrade-1.dump\n"); - try std.testing.expectEqualStrings("/run/u/pardes-upgrade-1.dump", older.dump); - try std.testing.expectEqualStrings("", older.label); - try std.testing.expectError(error.BadRecord, Record.decode("link 5\n")); - try std.testing.expectError(error.BadRecord, Record.decode("tty 0 1 2 1 abc\ndump x\n")); -} - -test "an upgrade from the build before reads the dump file its record names, and deletes it once loaded" { - const gpa = std.testing.allocator; - const p = try pardes.Pardes.init(gpa, .{ .tty_only = true }); - defer p.deinit(); - _ = try p.setTestFile("before the upgrade\n"); - const bytes = try pardes.dump.byteDump(p); - defer gpa.free(bytes); - var tmp = std.testing.tmpDir(.{}); - defer tmp.cleanup(); - try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "pardes-upgrade-1.dump", .data = bytes }); - var dir_buf: [4096]u8 = undefined; - const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)]; - var text_buf: [4300]u8 = undefined; - // The build before's record: its dump a file, no label. - var rec = try Record.decode(try std.fmt.bufPrint(&text_buf, "link -1\nstub -1\nold -1\nwindow 0\nexe /x/pardes\ndump {s}/pardes-upgrade-1.dump\n", .{dir})); - const q = try load(gpa, gpa, .{ .tty_only = true }, &rec); - defer q.deinit(); - try std.testing.expect(!rec.fresh); - const text = for (q.panes) |slot| { - if (slot) |pane| if (pane.file) |f| if (f.output == null) break f.content; - } else return error.FileLost; - try std.testing.expectEqualStrings("before the upgrade\n", text); - try std.testing.expectError(error.FileNotFound, tmp.dir.statFile(std.testing.io, "pardes-upgrade-1.dump", .{})); -} - -test "an older build's dump named by a path past 4096 bytes is refused in words and kept for a Restore" { - const gpa = std.testing.allocator; - var text_buf: [4200]u8 = undefined; - var long: [4096]u8 = undefined; - @memset(&long, 'd'); - long[0] = '/'; - var rec = try Record.decode(try std.fmt.bufPrint(&text_buf, "link -1\nstub -1\nold -1\nwindow 0\nexe /x/pardes\ndump {s}\n", .{&long})); - const q = try load(gpa, gpa, .{ .tty_only = true }, &rec); - defer q.deinit(); - try std.testing.expect(rec.fresh); - const pane = q.panes[q.view().active].?; - try std.testing.expect(std.mem.indexOf(u8, pane.msg[0..pane.msg_len], "Upgrade: no build could load the session") != null); -} |
