summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Db.zig104
-rw-r--r--src/dump.zig31
-rw-r--r--src/main.zig12
-rw-r--r--src/pardes.zig4
-rw-r--r--src/upgrade.zig25
5 files changed, 170 insertions, 6 deletions
diff --git a/src/Db.zig b/src/Db.zig
index 846c8d96..95a4eede 100644
--- a/src/Db.zig
+++ b/src/Db.zig
@@ -264,18 +264,57 @@ fn putRecent(o: *Open, text: []const u8) !void {
/// wrote it, read under this session's changes: at the start, and on use.
pub fn readRecent(db: *Db, p: *Pardes) void {
const o = db.opened(p) orelse return;
+ const first = !db.loaded;
db.loaded = true;
- db.mergeRecent(p, o);
+ const held = db.mergeRecent(p, o);
+ // A state file that holds no Recent yet: the files before it, once.
+ if (first and held != null and !held.?) if (p.opts.recent_import) |dir| importRecent(p, dir);
}
-fn mergeRecent(db: *Db, p: *Pardes, o: *Open) void {
- const size = committed(o) catch return;
- if (size == db.seen) return;
+/// Whether the database holds Recent; null when nothing was read (nothing
+/// committed since the last read, or it could not be).
+fn mergeRecent(db: *Db, p: *Pardes, o: *Open) ?bool {
+ const size = committed(o) catch return null;
+ if (size == db.seen) return null;
const gpa = p.recentGpa();
- const text = recentText(o, gpa) catch return;
+ const text = recentText(o, gpa) catch return null;
defer if (text) |t| gpa.free(t);
p.recent.merge(gpa, text orelse "");
db.seen = size;
+ return text != null;
+}
+
+/// Recent as the files before the state file kept it, in `dir`: 0.24's
+/// `recent` (200 lines, the newest first), else the `store` before it (a
+/// log whose `@r` records are such lines, the oldest first). Both are left
+/// where they are. ponytail: for a release or two, while those files are
+/// about.
+fn importRecent(p: *Pardes, dir: []const u8) void {
+ const gpa = p.recentGpa();
+ const io = ioOf();
+ var path_buf: [4200]u8 = undefined;
+ pardes.turn.yield();
+ const listed = std.Io.Dir.cwd().readFileAlloc(io, std.fmt.bufPrint(&path_buf, "{s}/recent", .{dir}) catch "", gpa, .limited(recent_max)) catch null;
+ const logged = if (listed == null) std.Io.Dir.cwd().readFileAlloc(io, std.fmt.bufPrint(&path_buf, "{s}/store", .{dir}) catch "", gpa, .limited(128 << 20)) catch null else null;
+ pardes.turn.back();
+ if (listed) |text| {
+ defer gpa.free(text);
+ p.recent.merge(gpa, text);
+ // Written to the state file at the next step: read once.
+ for (p.recent.entries.items) |*e| e.changed = true;
+ p.recent.dirty = p.recent.entries.items.len > 0;
+ } else if (logged) |text| {
+ defer gpa.free(text);
+ var lines = std.mem.splitScalar(u8, text, '\n');
+ while (lines.next()) |line| {
+ const e = recent.List.parseLine(line) orelse continue;
+ const open = if (p.recent.get(e.path)) |here| here.open else false;
+ p.recent.opened(gpa, e.path);
+ p.recent.closed(gpa, e.path, e.nums[0], e.nums[1], e.nums[2], e.nums[3]);
+ // Open here, it stays open: the cap never drops it.
+ if (open) p.recent.opened(gpa, e.path);
+ }
+ }
}
/// Recent written when it changed, at the end of a step and at the end
@@ -290,7 +329,7 @@ pub fn writeRecent(db: *Db, p: *Pardes, wait: bool) void {
error.NoStateFile => break :written,
};
defer unlock(o);
- db.mergeRecent(p, o);
+ _ = db.mergeRecent(p, o);
p.recent.changedFirst();
const gpa = p.recentGpa();
var text: std.Io.Writer.Allocating = .init(gpa);
@@ -1160,3 +1199,56 @@ test "state file: a Dump makes every directory missing on the way to its DumpDir
var label_buf: [64]u8 = undefined;
try testing.expect(p.db.labelled(p, p.last_dump.?, &label_buf) != null);
}
+
+test "state file: Recent is read once from the file it replaced, else from the store before that, both left where they are" {
+ var tmp = testing.tmpDir(.{});
+ defer tmp.cleanup();
+ var dir_buf: [std.fs.max_path_bytes]u8 = undefined;
+ const dir = try testDir(&tmp, &dir_buf);
+ try tmp.dir.createDirPath(testing.io, "old");
+ try tmp.dir.createDirPath(testing.io, "older");
+ try tmp.dir.createDirPath(testing.io, "a");
+ try tmp.dir.createDirPath(testing.io, "b");
+ // 0.24's Recent: 200 lines, the newest first.
+ try tmp.dir.writeFile(testing.io, .{ .sub_path = "old/recent", .data = "7 3 0 0 /w/newest.zig\n1 1 0 0 /w/older.zig\n" });
+ // Before it, the store: its `@r` records' bodies the same lines, oldest
+ // first, a closed pane's record between them.
+ try tmp.dir.writeFile(testing.io, .{ .sub_path = "older/store", .data = "@h 31 3c6ce28a\npardes-store 1 ca2a9583be7d1ba3\n@r 20 0\n1 1 0 0 /w/old.zig\n@c 12 0\n0 1 7 0 0 0 0 9\t/w/x\n.{}\n@r 21 0\n2 2 0 0 /w/both.zig\n@r 20 0\n9 9 0 0 /w/old.zig\n" });
+ var buf: [3][4200]u8 = undefined;
+ const old = try std.fmt.bufPrint(&buf[0], "{s}/old", .{dir});
+ const older = try std.fmt.bufPrint(&buf[1], "{s}/older", .{dir});
+ const window = struct {
+ fn of(state: []const u8, import: []const u8) !*Pardes {
+ var config: [4200]u8 = undefined;
+ return Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 48, .state = .disk, .recent_import = import, .startup_config = try std.fmt.bufPrint(&config, "DumpDir {s}\n", .{state}) });
+ }
+ }.of;
+ {
+ const a = try window(try std.fmt.bufPrint(&buf[2], "{s}/a", .{dir}), old);
+ defer a.deinit();
+ try testing.expectEqual(@as(usize, 2), a.recent.entries.items.len);
+ try testing.expectEqualStrings("/w/newest.zig", a.recent.entries.items[0].path);
+ try testing.expectEqual(@as(u32, 7), a.recent.get("/w/newest.zig").?.line);
+ a.saveRecent();
+ }
+ // Read once: what the old file says after is not the state file's.
+ try tmp.dir.writeFile(testing.io, .{ .sub_path = "old/recent", .data = "5 5 0 0 /w/later.zig\n" });
+ {
+ const a = try window(try std.fmt.bufPrint(&buf[2], "{s}/a", .{dir}), old);
+ defer a.deinit();
+ try testing.expectEqual(@as(usize, 2), a.recent.entries.items.len);
+ try testing.expect(a.recent.get("/w/later.zig") == null);
+ }
+ // No `recent` there: the store's, its newest records first.
+ {
+ const b = try window(try std.fmt.bufPrint(&buf[2], "{s}/b", .{dir}), older);
+ defer b.deinit();
+ try testing.expectEqual(@as(usize, 2), b.recent.entries.items.len);
+ try testing.expectEqualStrings("/w/old.zig", b.recent.entries.items[0].path);
+ try testing.expectEqual(@as(u32, 9), b.recent.entries.items[0].line);
+ try testing.expectEqualStrings("/w/both.zig", b.recent.entries.items[1].path);
+ }
+ // Both left where they were.
+ _ = try tmp.dir.statFile(testing.io, "old/recent", .{});
+ _ = try tmp.dir.statFile(testing.io, "older/store", .{});
+}
diff --git a/src/dump.zig b/src/dump.zig
index 8847152d..facd9b9a 100644
--- a/src/dump.zig
+++ b/src/dump.zig
@@ -853,3 +853,34 @@ test "a session's byte dump read by name, field by field as another build reads
try std.testing.expectEqualStrings("", l.notes.items);
try std.testing.expectEqual(walk.digest(Pardes, p), walk.digest(Pardes, q));
}
+
+test "an old dump's file, a byte dump, is read by -l and by a Restore of its path, for one release" {
+ const gpa = std.testing.allocator;
+ const th = @import("ninep/testing.zig");
+ const p = try Pardes.init(gpa, .{ .tty_only = true });
+ defer p.deinit();
+ _ = try p.setTestFile("kept in a .dump\n");
+ const bytes = try byteDump(p);
+ defer gpa.free(bytes);
+ try std.testing.expect(std.mem.startsWith(u8, bytes, "pardes-dump"));
+ // -l: the file read whole, and loaded.
+ const q = try initFromDump(gpa, .{ .tty_only = true }, bytes);
+ defer q.deinit();
+ 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("kept in a .dump\n", text);
+ // Restore of its path, as a word in a tag or a script gives it.
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "old.dump", .data = bytes });
+ var dir_buf: [4096]u8 = undefined;
+ const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)];
+ var line: [4200]u8 = undefined;
+ const answer = th.wr(q, @backingInt(ctlfs.TopFile.ctl), try std.fmt.bufPrint(&line, "Restore {s}/old.dump\n", .{dir}));
+ try std.testing.expectEqual(ctlfs.Status.ok, answer.reply.status);
+ const name = q.takeRestore().?;
+ const again = try restoreBytes(q, gpa, name);
+ defer gpa.free(again);
+ try std.testing.expectEqualSlices(u8, bytes, again);
+}
diff --git a/src/main.zig b/src/main.zig
index 3b7d6abe..afa72881 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -744,6 +744,7 @@ fn nativeMain(std_init: std.process.Init) !void {
opts.startup_config_path = found.path;
opts.config_dir = found.dir;
opts.state = .disk;
+ opts.recent_import = oldRecentDir(arena, init.environ_map);
if (found.dir) |d| @import("crash.zig").setDir(d);
if (attach) |name| if (std.mem.lastIndexOfScalar(u8, name, ':')) |colon| {
remoteAttach(init, opts, name[0..colon], name[colon + 1 ..]);
@@ -1099,3 +1100,14 @@ test {
if (comptime pardes.platform == .gui) _ = @import("gui/gui.zig");
if (comptime pardes.font_picker) _ = @import("fonts.zig");
}
+
+/// Where Recent's file (0.24) and the store before it were kept:
+/// $XDG_STATE_HOME/pardes, else ~/.local/state/pardes. A state file that
+/// holds no Recent yet reads them once (Db.zig).
+fn oldRecentDir(arena: std.mem.Allocator, env: *const std.process.Environ.Map) ?[]const u8 {
+ if (env.get("XDG_STATE_HOME")) |base| if (base.len != 0 and std.fs.path.isAbsolute(base))
+ return std.fs.path.join(arena, &.{ base, "pardes" }) catch null;
+ const home = env.get("HOME") orelse return null;
+ if (home.len == 0) return null;
+ return std.fs.path.join(arena, &.{ home, ".local", "state", "pardes" }) catch null;
+}
diff --git a/src/pardes.zig b/src/pardes.zig
index 571f7db4..462d9481 100644
--- a/src/pardes.zig
+++ b/src/pardes.zig
@@ -3229,6 +3229,10 @@ pub const Options = struct {
/// file, set by a host that has one; none, Recent lasts as long as the
/// session; memory, the test host's.
state: Db.Kind = .off,
+ /// Where Recent's files were before the state file (0.24's `recent`, the
+ /// `store` before it): a state file that holds no Recent yet reads them
+ /// once (Db.zig). Set by a host that had them.
+ recent_import: ?[]const u8 = null,
startup_config_path: ?[]const u8 = null,
image_allocator: ?std.mem.Allocator = null,
pdf_allocator: ?std.mem.Allocator = null,
diff --git a/src/upgrade.zig b/src/upgrade.zig
index 212a8176..b4003aa6 100644
--- a/src/upgrade.zig
+++ b/src/upgrade.zig
@@ -470,3 +470,28 @@ test "an upgrade's record reads back as it was written, and refuses one cut shor
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", .{}));
+}