summaryrefslogtreecommitdiff
path: root/src/Store.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/Store.zig')
-rw-r--r--src/Store.zig103
1 files changed, 59 insertions, 44 deletions
diff --git a/src/Store.zig b/src/Store.zig
index 5e47d695..9c726705 100644
--- a/src/Store.zig
+++ b/src/Store.zig
@@ -78,7 +78,7 @@ cap: u64 = max_bytes,
session: u64 = 0,
pid: i32 = 0,
/// The file as read so far: its inode, generation and how far.
-inode: std.Io.File.INode = 0,
+inode: std.Io.File.INode = std.mem.zeroes(std.Io.File.INode),
gen: u64 = 0,
end: u64 = 0,
/// Its length when first read whole: a file past twice that (and 1 MiB)
@@ -468,6 +468,21 @@ fn unlist(s: *Store, gpa: std.mem.Allocator, serial: u32) void {
};
}
+/// A closed pane as Panes lists it: its serial and what its row shows.
+pub const Named = struct { serial: u32, name: []const u8 };
+
+/// This session's closed panes, the latest first (scratch; the names the
+/// store's): what a step lists of them (`tape.authority`).
+pub fn closedNames(p: *Pardes) []const Named {
+ const items = p.store.closed.items;
+ const out = p.scratch.allocator().alloc(Named, items.len) catch return &.{};
+ for (out, 0..) |*o, i| {
+ const c = items[items.len - 1 - i];
+ o.* = .{ .serial = c.serial, .name = c.name };
+ }
+ return out;
+}
+
/// The closed pane with this serial, if this session has it.
pub fn closedBySerial(s: *const Store, serial: u32) ?*Closed {
for (s.closed.items) |*c| if (c.serial == serial) return c;
@@ -498,27 +513,30 @@ pub fn keep(s: *Store, p: *Pardes, id: usize) ?Kept {
const pane = p.panes[id] orelse return null;
if (pane.fs.unannounced or pane.query != null) return null;
if (pane.file) |f| if (f.output) |o| if (o.from == .preview) return null;
+ // An output pane's record holds its text whole: one past the bound is
+ // known before a dump of it is made, which for a 256 MiB +Pager held
+ // the session up for seconds only to be thrown away.
+ if (pane.file) |f| if (f.output != null and f.content.len > max_record) return null;
const gpa = p.recentGpa();
- const arena = p.scratch.allocator();
- var dp = dump.dumpPane(p, arena, pane) catch return null;
- var zon = oneDump(p, arena, dp) catch return null;
- // A terminal's history is what makes it big: kept without it, it comes
- // back a fresh shell in its directory.
- if (zon.len > max_record) if (dp.terminal) |*t| {
- t.stream = "";
- t.stream_b64 = "";
- dp.body = "";
- zon = oneDump(p, arena, dp) catch return null;
- };
- if (zon.len > max_record) return null;
+ var owned = dump.savePane(gpa, pane, false) catch return null;
+ // A terminal's history or a file's undo is what makes it big: kept
+ // without them it comes back a fresh shell in its directory, or its
+ // file as it is on disk.
+ if (owned.len > max_record) {
+ gpa.free(owned);
+ owned = dump.savePane(gpa, pane, true) catch return null;
+ }
+ if (owned.len > max_record) {
+ gpa.free(owned);
+ return null;
+ }
const what: []const u8 = if (pane.file) |f| f.path else if (pane.pdfPath()) |pdf| pdf else if (pane.image) |iv| iv.path else if (pane.command) |c| c else pane.cwdSlice();
- var name = gpa.alloc(u8, @min(what.len, 4096)) catch return null;
- // One line, a tab its end: a newline or a tab in it is a space.
- for (what[0..name.len], 0..) |c, i| name[i] = if (c == '\n' or c == '\t') ' ' else c;
- const owned = gpa.dupe(u8, zon) catch {
- gpa.free(name);
+ var name = gpa.alloc(u8, @min(what.len, 4096)) catch {
+ gpa.free(owned);
return null;
};
+ // One line, a tab its end: a newline or a tab in it is a space.
+ for (what[0..name.len], 0..) |c, i| name[i] = if (c == '\n' or c == '\t') ' ' else c;
const at = layout.findPane(p, id);
const owner = layout.ownerOf(p, id);
return .{
@@ -548,18 +566,6 @@ pub const Kept = struct {
}
};
-/// A Dump of the one pane `dp`, as a Restore would read it.
-fn oneDump(p: *Pardes, arena: std.mem.Allocator, dp: dump.Pane) ![]const u8 {
- const ids = [_]usize{0};
- const columns = [_]dump.Column{.{ .panes = &ids }};
- const one = [_]dump.Pane{dp};
- const state: dump.State = .{ .screen = .{ .cols = p.screen_w, .rows = p.screen_h }, .columns = &columns, .panes = &one };
- try dump.validate(state);
- var out: std.Io.Writer.Allocating = .init(arena);
- try std.zon.stringify.serialize(state, .{}, &out.writer);
- return out.written();
-}
-
/// The pane `k` kept is gone, its rows given to `gainer` (a slot, after
/// they moved; null none): listed, and its record framed for the store.
pub fn close(s: *Store, p: *Pardes, k: Kept, gainer: ?usize) void {
@@ -794,25 +800,19 @@ fn compact(s: *Store, p: *Pardes, file: File, st: File.Stat, path: []const u8) v
/// from `from`. False when it is not this session's, or its record is gone
/// or bad; the keyboard on it otherwise.
pub fn reopen(s: *Store, p: *Pardes, serial: u32, from: usize) bool {
- s.refresh(p);
const gpa = p.recentGpa();
- const place = (s.closedBySerial(serial) orelse return false).*;
- const body = s.bodyOf(p, place) orelse return false;
- // Read from the file: this call's to free (`unlist` frees one held).
- defer if (place.body.len == 0) gpa.free(body);
+ // The store's: a replica has the authority's (`tape.authority`).
+ const place = pardes.tape.authority(record, .{ s, p, serial }) orelse return false;
+ const body = place.body;
const m = parseMeta(body) orelse return false;
- var parsed = dump.readZon(p.gpa, body[m.dump_at..], "closed pane") catch return false;
- defer parsed.deinit();
const free = p.freeSlot() orelse {
p.reportError(from, "Panes", error.NoPaneSlots);
return false;
};
- const src = parsed.value.panes[0];
- const pane = dump.restorePane(p, free, src) catch |err| {
+ _ = dump.loadPane(p, free, body[m.dump_at..]) catch |err| {
p.reportError(from, "Panes", err);
return false;
};
- pane.collapsed = false;
const owner = if (place.owner != 0) p.paneBySerial(place.owner) else null;
const gainer = if (place.gainer != 0) p.paneBySerial(place.gainer) else null;
const placed = if (owner) |o|
@@ -839,6 +839,21 @@ pub fn reopen(s: *Store, p: *Pardes, serial: u32, from: usize) bool {
return true;
}
+/// Where a closed pane goes back, and its record's body (scratch).
+pub const Record = struct { gainer: u32, below: bool, owner: u32, rows: u16, body: []const u8 };
+
+/// Closed pane `serial`'s record, the store read on first; null when it is
+/// not this session's, or its record is gone or bad.
+fn record(s: *Store, p: *Pardes, serial: u32) ?Record {
+ s.refresh(p);
+ const c = (s.closedBySerial(serial) orelse return null).*;
+ const body = s.bodyOf(p, c) orelse return null;
+ // Read from the file: this call's to free (`unlist` frees one held).
+ defer if (c.body.len == 0) p.recentGpa().free(body);
+ const copy = p.scratch.allocator().dupe(u8, body) catch return null;
+ return .{ .gainer = c.gainer, .below = c.below, .owner = c.owner, .rows = c.rows, .body = copy };
+}
+
/// A closed pane's record body: held here, or read from the file and
/// checked against its CRC (gpa; the caller frees it when it is not
/// `c.body`).
@@ -868,8 +883,9 @@ fn bodyOf(s: *Store, p: *Pardes, c: Closed) ?[]u8 {
return body;
}
-/// `@p<serial>` naming a pane this session closed: its serial.
-pub fn closedAddress(s: *const Store, word: []const u8) ?u32 {
+/// `@p<serial>` alone, the pane and no place in it: its serial, for a pane
+/// this session closed to come back by (`reopen` says whether it did).
+pub fn closedAddress(word: []const u8) ?u32 {
const addr = @import("config.zig").pane_addr;
if (!std.mem.startsWith(u8, word, addr)) return null;
const digits = word[addr.len..];
@@ -878,8 +894,7 @@ pub fn closedAddress(s: *const Store, word: []const u8) ?u32 {
} else digits.len;
// `@p7` alone, or `@p7:` with nothing after it: the pane, no place in it.
if (end == 0 or (end < digits.len and !(digits[end] == ':' and end + 1 == digits.len))) return null;
- const serial = std.fmt.parseInt(u32, digits[0..end], 10) catch return null;
- return if (s.closedBySerial(serial) != null) serial else null;
+ return std.fmt.parseInt(u32, digits[0..end], 10) catch null;
}
// ---- tests ----