diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/Db.zig | 389 | ||||
| -rw-r--r-- | src/Query.zig | 2 | ||||
| -rw-r--r-- | src/Store.zig | 304 | ||||
| -rw-r--r-- | src/dump.zig | 17 | ||||
| -rw-r--r-- | src/main.zig | 18 | ||||
| -rw-r--r-- | src/ninep/tree.zig | 2 | ||||
| -rw-r--r-- | src/pardes.zig | 44 | ||||
| -rw-r--r-- | src/replica.zig | 2 |
8 files changed, 444 insertions, 334 deletions
diff --git a/src/Db.zig b/src/Db.zig new file mode 100644 index 00000000..e496945f --- /dev/null +++ b/src/Db.zig @@ -0,0 +1,389 @@ +//! What pardes keeps on disk, through xitdb (docs/next-store.md): the +//! user's state file, `pardes.xitdb` in the DumpDir ($XDG_DATA_HOME/pardes, +//! else ~/.local/share/pardes), which every session of the user's shares. +//! Its root is a map: +//! +//! recent Recent's list (recent.zig), a line a file, the newest first +//! +//! A session reads Recent at its start, and again on use (Recent, /recent) +//! when the file's committed size says another session committed since. A +//! step that changed it writes it at its end in one transaction under the +//! file's lock, what another session committed since read and merged under +//! this session's changes first (`recent.List.merge`). Held by another +//! session, the lock is tried again at the next step: a frame never waits +//! for one. Recent's commits are not fsynced, as its file never was: a crash +//! keeps every one committed, a power loss may lose the last. +//! +//! A file there that is no database of pardes's (another program's, one a +//! power loss tore) is kept beside it as `pardes.xitdb.broken` and a new one +//! started: pardes always starts. The test host's is a `.memory` database, +//! the same code (`Kind.memory`). +const Db = @This(); + +const std = @import("std"); +const xitdb = @import("xitdb"); +const pardes = @import("pardes.zig"); +const recent = @import("recent.zig"); +const Pardes = pardes.Pardes; + +pub const Disk = xitdb.Database(.buffered_file, u256); +pub const Memory = xitdb.Database(.memory, u256); + +/// Where a session keeps what outlives it: nowhere (a unit test, a replica, +/// a replay), its user's state file, or memory (the test host's). +pub const Kind = enum { off, disk, memory }; + +pub const file_name = "pardes.xitdb"; +/// The hash every key of these databases is: a field's or a label's name +/// hashed, a blob keyed by its content's. +const hash_id = xitdb.HashId.fromBytes("blk3"); +/// Recent's lines are read this far at most (200 of the longest paths). +const recent_max = 1 << 20; + +kind: Kind = .off, +open: ?*Open = null, +/// The state file's committed size when this session last read or wrote +/// Recent: another session's commit moves it. +seen: u64 = 0, +/// Recent was read (or there was none): only then is it written, so a +/// session never writes over a list it did not read. +loaded: bool = false, + +/// The open database. Its own allocation: a database points at its buffer, +/// and a Restore's new core takes it over from the old one. +pub const Open = struct { + gpa: std.mem.Allocator, + buffer: std.Io.Writer.Allocating, + where: union(enum) { + disk: struct { db: Disk, file: std.Io.File, path: []u8 }, + memory: struct { db: Memory }, + }, +}; + +fn ioOf() std.Io { + return std.Io.Threaded.global_single_threaded.io(); +} + +/// A name's key: its BLAKE3 hash. +pub fn key(name: []const u8) u256 { + var out: [32]u8 = undefined; + std.crypto.hash.Blake3.hash(name, &out, .{}); + return std.mem.readInt(u256, &out, .big); +} + +pub fn deinit(db: *Db) void { + const o = db.open orelse return; + db.open = null; + switch (o.where) { + .disk => |*d| { + d.file.close(ioOf()); + o.gpa.free(d.path); + }, + .memory => {}, + } + o.buffer.deinit(); + o.gpa.destroy(o); +} + +/// The state file's path for the DumpDir setting `dir_setting`, made in +/// `buf`; null with no home to keep it in. +pub fn statePath(buf: []u8, dir_setting: []const u8) ?[]const u8 { + var dir_buf: [4096]u8 = undefined; + const dir = pardes.dump.directory(&dir_buf, dir_setting) orelse return null; + return std.fmt.bufPrint(buf, "{s}/" ++ file_name, .{dir}) catch null; +} + +/// The database, opened (made, when its file is not there) at its first use +/// and again once the DumpDir names another: null when this session keeps +/// none, or it cannot be had now (the next use tries again). +fn opened(db: *Db, p: *Pardes) ?*Open { + switch (db.kind) { + .off => return null, + .disk => { + var buf: [4200]u8 = undefined; + const path = statePath(&buf, p.settings.dump_dir.get()) orelse return null; + if (db.open) |o| { + if (std.mem.eql(u8, o.where.disk.path, path)) return o; + // The DumpDir moved: its own file, read afresh. + db.deinit(); + db.seen = 0; + } + db.open = openDisk(p.recentGpa(), path) catch return null; + }, + .memory => if (db.open == null) { + const gpa = p.recentGpa(); + const o = gpa.create(Open) catch return null; + o.* = .{ .gpa = gpa, .buffer = .init(gpa), .where = undefined }; + o.where = .{ .memory = .{ .db = Memory.init(ioOf(), .{ .buffer = &o.buffer, .hash_id = hash_id }) catch { + o.buffer.deinit(); + gpa.destroy(o); + return null; + } } }; + db.open = o; + }, + } + return db.open; +} + +/// The state file at `path`, its directory made: a new one is written under +/// the file's lock (two sessions starting at once write one header), and a +/// file that is no database of pardes's is put aside for a new one. +fn openDisk(gpa: std.mem.Allocator, path: []const u8) !*Open { + pardes.turn.yield(); + defer pardes.turn.back(); + const io = ioOf(); + const cwd = std.Io.Dir.cwd(); + if (std.fs.path.dirname(path)) |dir| try cwd.createDirPath(io, dir); + const o = try gpa.create(Open); + errdefer gpa.destroy(o); + o.* = .{ .gpa = gpa, .buffer = .init(gpa), .where = undefined }; + errdefer o.buffer.deinit(); + const own = try gpa.dupe(u8, path); + errdefer gpa.free(own); + var tried_aside = false; + while (true) { + const file = try cwd.createFile(io, path, .{ .read = true, .truncate = false }); + const fresh = (file.length(io) catch 0) == 0; + // Another session making it now: it has its header in a moment. + if (fresh and !(file.tryLock(io, .exclusive) catch false)) { + file.close(io); + return error.WouldBlock; + } + const made = Disk.init(io, .{ .file = file, .buffer = &o.buffer, .fsync = false, .hash_id = hash_id }); + if (fresh) file.unlock(io); + if (made) |d| { + o.where = .{ .disk = .{ .db = d, .file = file, .path = own } }; + return o; + } else |err| { + file.close(io); + switch (err) { + error.InvalidDatabase, error.InvalidVersion, error.InvalidHashSize, error.TruncatedDatabase, error.InvalidEnumTag, error.EndOfStream => {}, + else => return err, + } + if (tried_aside) return err; + tried_aside = true; + var aside_buf: [4300]u8 = undefined; + try cwd.rename(path, cwd, try std.fmt.bufPrint(&aside_buf, "{s}.broken", .{path}), io); + } + } +} + +fn committed(o: *Open) !u64 { + pardes.turn.yield(); + defer pardes.turn.back(); + return switch (o.where) { + inline else => |*w| w.db.committedSize(), + }; +} + +/// The file's lock, taken (`wait`: waited for): false when another session +/// holds it. A database in memory is this process's alone. +fn lock(o: *Open, wait: bool) bool { + const file = switch (o.where) { + .disk => |d| d.file, + .memory => return true, + }; + pardes.turn.yield(); + defer pardes.turn.back(); + if (wait) { + file.lock(ioOf(), .exclusive) catch return false; + return true; + } + return file.tryLock(ioOf(), .exclusive) catch false; +} + +fn unlock(o: *Open) void { + switch (o.where) { + .disk => |d| d.file.unlock(ioOf()), + .memory => {}, + } +} + +// ---- Recent ---- + +/// Recent's lines, `gpa`'s; null when the database holds none. +fn recentText(o: *Open, gpa: std.mem.Allocator) !?[]const u8 { + pardes.turn.yield(); + defer pardes.turn.back(); + switch (o.where) { + inline else => |*w| { + const DB = @TypeOf(w.db); + const root = try DB.HashMap(.read_only).init(w.db.rootCursor().readOnly()); + const c = (try root.getCursor(key("recent"))) orelse return null; + return try c.readBytesAlloc(gpa, recent_max); + }, + } +} + +fn putRecent(o: *Open, text: []const u8) !void { + pardes.turn.yield(); + defer pardes.turn.back(); + switch (o.where) { + inline else => |*w| { + const DB = @TypeOf(w.db); + try w.db.runContext(struct { + text: []const u8, + pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { + const root = try DB.HashMap(.read_write).init(cursor.*); + try root.put(key("recent"), .{ .bytes = ctx.text }); + } + }{ .text = text }); + }, + } +} + +/// What other sessions committed to Recent since this one last read or +/// 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; + db.loaded = true; + db.mergeRecent(p, o); +} + +fn mergeRecent(db: *Db, p: *Pardes, o: *Open) void { + const size = committed(o) catch return; + if (size == db.seen) return; + const gpa = p.recentGpa(); + const text = recentText(o, gpa) catch return; + defer if (text) |t| gpa.free(t); + p.recent.merge(gpa, text orelse ""); + db.seen = size; +} + +/// Recent written when it changed, at the end of a step and at the end +/// (`wait`: the lock is waited for there, never in a step). +pub fn writeRecent(db: *Db, p: *Pardes, wait: bool) void { + if (!p.recent.dirty) return; + if (db.kind != .off) written: { + if (!db.loaded) return; + const o = db.opened(p) orelse break :written; + // Another session's lock: the next step tries again. + if (!lock(o, wait)) return; + defer unlock(o); + db.mergeRecent(p, o); + p.recent.changedFirst(); + const gpa = p.recentGpa(); + var text: std.Io.Writer.Allocating = .init(gpa); + defer text.deinit(); + const entries = p.recent.entries.items; + for (entries[0..@min(entries.len, recent.cap)]) |e| recent.List.writeEntry(e, &text.writer) catch break :written; + // The disk refused it: kept for no one, as before. + putRecent(o, text.written()) catch break :written; + db.seen = committed(o) catch 0; + } + for (p.recent.entries.items) |*e| e.changed = false; + p.recent.dirty = false; +} + +// ---- tests ---- + +const testing = std.testing; + +/// A window keeping its state in `dir` (never the user's DumpDir). +fn testWindow(dir: []const u8) !*Pardes { + var config: [4200]u8 = undefined; + return Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 48, .state = .disk, .startup_config = try std.fmt.bufPrint(&config, "DumpDir {s}\n", .{dir}) }); +} + +fn testDir(tmp: *std.testing.TmpDir, buf: *[std.fs.max_path_bytes]u8) ![]const u8 { + return buf[0..try tmp.dir.realPath(testing.io, buf)]; +} + +fn fileSize(dir: []const u8) u64 { + var buf: [4200]u8 = undefined; + const path = std.fmt.bufPrint(&buf, "{s}/" ++ file_name, .{dir}) catch return 0; + const st = std.Io.Dir.cwd().statFile(testing.io, path, .{}) catch return 0; + return st.size; +} + +test "state file: two windows share Recent, each reading the other's commits on use; a busy lock delays, never loses" { + var tmp = testing.tmpDir(.{}); + defer tmp.cleanup(); + var dir_buf: [std.fs.max_path_bytes]u8 = undefined; + const dir = try testDir(&tmp, &dir_buf); + const a = try testWindow(dir); + defer a.deinit(); + const b = try testWindow(dir); + defer b.deinit(); + a.recent.opened(a.recentGpa(), "/w/a.zig"); + a.saveRecent(); + b.recent.opened(b.recentGpa(), "/w/b.zig"); + b.saveRecent(); + a.recent.closed(a.recentGpa(), "/w/a.zig", 7, 3, 0, 0); + a.saveRecent(); + // Each has both, in the order they were written: a closed last, then b. + a.db.readRecent(a); + b.db.readRecent(b); + for ([_]*Pardes{ a, b }) |w| { + try testing.expectEqual(@as(usize, 2), w.recent.entries.items.len); + try testing.expectEqualStrings("/w/a.zig", w.recent.entries.items[0].path); + try testing.expectEqualStrings("/w/b.zig", w.recent.entries.items[1].path); + try testing.expectEqual(@as(u32, 7), w.recent.get("/w/a.zig").?.line); + } + // Another window holding the file's lock: b's change waits in memory... + var path_buf: [4200]u8 = undefined; + const lock_file = try std.Io.Dir.cwd().openFile(testing.io, try std.fmt.bufPrint(&path_buf, "{s}/" ++ file_name, .{dir}), .{ .mode = .read_write }); + try lock_file.lock(testing.io, .exclusive); + b.recent.opened(b.recentGpa(), "/w/c.zig"); + const size = fileSize(dir); + b.saveRecent(); + try testing.expectEqual(size, fileSize(dir)); + try testing.expect(b.recent.dirty); + // ...and is written at the next step after it is let go. + lock_file.close(testing.io); + b.saveRecent(); + try testing.expect(fileSize(dir) > size); + a.db.readRecent(a); + try testing.expectEqualStrings("/w/c.zig", a.recent.entries.items[0].path); + // A window started now reads the same list. + const c = try testWindow(dir); + defer c.deinit(); + try testing.expectEqual(@as(usize, 3), c.recent.entries.items.len); + for (a.recent.entries.items, c.recent.entries.items) |x, y| try testing.expectEqualStrings(x.path, y.path); +} + +test "state file: Recent holds the newest 200, the newest first, and a window started on it reads them so" { + var tmp = testing.tmpDir(.{}); + defer tmp.cleanup(); + var dir_buf: [std.fs.max_path_bytes]u8 = undefined; + const dir = try testDir(&tmp, &dir_buf); + const a = try testWindow(dir); + defer a.deinit(); + var name: [32]u8 = undefined; + for (0..recent.cap + 100) |i| { + const file = std.fmt.bufPrint(&name, "/w/file{d}.zig", .{i}) catch unreachable; + a.recent.opened(a.recentGpa(), file); + a.recent.closed(a.recentGpa(), file, 1, 1, 0, 0); + a.saveRecent(); + } + try testing.expectEqual(@as(usize, recent.cap), a.recent.entries.items.len); + const text = (try recentText(a.db.open.?, testing.allocator)).?; + defer testing.allocator.free(text); + try testing.expectEqual(@as(usize, recent.cap), std.mem.count(u8, text, "\n")); + try testing.expect(std.mem.startsWith(u8, text, "1 1 0 0 /w/file299.zig\n")); + const b = try testWindow(dir); + defer b.deinit(); + try testing.expectEqual(@as(usize, recent.cap), b.recent.entries.items.len); + try testing.expectEqualStrings("/w/file299.zig", b.recent.entries.items[0].path); + try testing.expectEqualStrings("/w/file100.zig", b.recent.entries.items[recent.cap - 1].path); +} + +test "state file: one that is no database of pardes's never stops pardes: it is kept aside, and a new one started" { + 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.writeFile(testing.io, .{ .sub_path = file_name, .data = "\x00\xffnot a database at all\n7 3 0 0 /w/ok.zig\n" }); + const a = try testWindow(dir); + defer a.deinit(); + try testing.expectEqual(@as(usize, 0), a.recent.entries.items.len); + a.recent.opened(a.recentGpa(), "/w/a.zig"); + a.saveRecent(); + const kept = try tmp.dir.readFileAlloc(testing.io, file_name ++ ".broken", testing.allocator, .limited(1 << 20)); + defer testing.allocator.free(kept); + try testing.expect(std.mem.startsWith(u8, kept, "\x00\xffnot a database")); + const b = try testWindow(dir); + defer b.deinit(); + try testing.expectEqual(@as(usize, 1), b.recent.entries.items.len); + try testing.expectEqualStrings("/w/a.zig", b.recent.entries.items[0].path); +} diff --git a/src/Query.zig b/src/Query.zig index 88091bf7..1524212c 100644 --- a/src/Query.zig +++ b/src/Query.zig @@ -629,7 +629,7 @@ const closed_word = " (closed)"; /// Every session's files, as a Recent query lists them: what the others /// wrote since, read first. The session's (`tape.authority`): in `out`. fn recentRows(p: *Pardes, out: *std.Io.Writer.Allocating) []const u8 { - p.store.refresh(p); + p.db.readRecent(p); Output.recentText(p, &out.writer, true) catch {}; return out.written(); } diff --git a/src/Store.zig b/src/Store.zig index b1e8abd9..9267dc3b 100644 --- a/src/Store.zig +++ b/src/Store.zig @@ -1,21 +1,8 @@ -//! What outlives a pane: Recent's file, which every session of the user's -//! shares, and the panes this session closed, which Panes brings back. -//! -//! Recent's file (Options.recent_path, $XDG_STATE_HOME/pardes/recent) is -//! the list, a line a file, the newest first: `<line> <col> <aline> <acol> -//! <path>` (recent.zig). A session reads it at start, and again on use -//! (Recent, /recent) when a stat says another session wrote it since. A -//! step that changed the list writes it at its end: `flock` on the file -//! (held by another session, the next step tries again: a frame never -//! waits for one), the file read again under this session's changes -//! (`recent.List.merge`) when another session wrote it since, and the -//! whole list written over it in place. A line that is no entry is passed -//! over, so a missing, unreadable or foreign file is an empty list (and a -//! line a crash tore, one entry fewer): pardes always starts. -//! -//! The closed panes are this session's alone, and in memory: each a -//! one-pane Dump (dump.savePane) and where it was, the newest 64 within -//! 64 MiB. Restore keeps them (`clone`); the session's end drops them. +//! The panes this session closed, which Panes brings back: this session's +//! alone, and in memory. Each is a one-pane Dump (dump.savePane) and where +//! it was, the newest 64 within 64 MiB. Restore keeps them (`clone`); the +//! session's end drops them. Recent, which every session shares, is in the +//! state file (Db.zig). const Store = @This(); const std = @import("std"); @@ -23,7 +10,6 @@ const pardes = @import("pardes.zig"); const dump = @import("dump.zig"); const layout = @import("layout.zig"); const exec = @import("exec.zig"); -const recent = @import("recent.zig"); const Pardes = pardes.Pardes; /// This session keeps this many closed panes, the newest. @@ -33,8 +19,6 @@ pub const max_closed = 64; pub const max_dump: usize = 8 << 20; /// The closed panes' dumps together are never longer: the oldest go. pub const max_bytes: usize = 64 << 20; -/// Recent's file is read this far at most (200 lines of the longest paths). -const max_file = 1 << 20; /// A pane closed in this session, as Panes lists it and brings it back. pub const Closed = struct { @@ -54,36 +38,12 @@ pub const Closed = struct { dump: []u8, }; -/// Recent's file; null, nothing is kept past the session. -path: ?[]const u8 = null, -/// The file as this session last read or wrote it: a stat that says -/// otherwise is another session's write. -inode: std.Io.File.INode = 0, -size: u64 = 0, -mtime: i96 = 0, -/// Read once at start (or there was none): only then is it written, so a -/// session never writes over a list it did not read. -loaded: bool = false, /// This session's closed panes, oldest first (recentGpa's). closed: std.ArrayList(Closed) = .empty, /// The panes closing now went to another window (Handover.finish): open /// there, they are no closed panes of this session, so none is kept. away: bool = false, -fn ioOf() std.Io { - return std.Io.Threaded.global_single_threaded.io(); -} - -/// One host file call, `f(args)`, made out of the turn (pardes.turn): a -/// file under a mount of this session's own tree is answered by this -/// session. Only the call: while a step is out nothing may change the core, -/// so Recent changes between these, never inside one. -const host = pardes.tape.out; - -const cwd = std.Io.Dir.cwd; -const File = std.Io.File; -const Dir = std.Io.Dir; - pub fn deinit(s: *Store, gpa: std.mem.Allocator) void { for (s.closed.items) |c| freeClosed(gpa, c); s.closed.deinit(gpa); @@ -116,112 +76,6 @@ pub fn clone(s: *const Store, gpa: std.mem.Allocator) Store { return copy; } -// ---- Recent's file ---- - -/// Recent read at start: the file, and the store it replaced. -pub fn load(s: *Store, p: *Pardes) void { - const path = s.path orelse return; - s.loaded = true; - s.refresh(p); - // ponytail: the store this file replaced (until 2026-11-05), a log - // whose `@r` records' bodies are this file's lines, oldest first: read - // once more, over the file's list, while it is the newer of the two - // (an older pardes still running writes it, so it is left there). - const io = ioOf(); - var old_buf: [4096 + 8]u8 = undefined; - const old = std.fmt.bufPrint(&old_buf, "{s}/store", .{std.fs.path.dirname(path) orelse return}) catch return; - const st = host(Dir.statFile, .{ cwd(), io, old, Dir.StatFileOptions{} }) catch return; - if (st.mtime.nanoseconds <= s.mtime) return; - const gpa = p.recentGpa(); - const bytes = host(Dir.readFileAlloc, .{ cwd(), io, old, gpa, std.Io.Limit.limited(128 << 20) }) catch return; - defer gpa.free(bytes); - var lines = std.mem.splitScalar(u8, bytes, '\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); - } -} - -/// On use: what other sessions wrote since this one last read or wrote -/// the file, read. One stat when nothing changed. -pub fn refresh(s: *Store, p: *Pardes) void { - const path = s.path orelse return; - if (!s.loaded) return; - const io = ioOf(); - const st = host(Dir.statFile, .{ cwd(), io, path, Dir.StatFileOptions{} }) catch return; - if (st.inode == s.inode and st.size == s.size and st.mtime.nanoseconds == s.mtime) return; - const file = host(Dir.openFile, .{ cwd(), io, path, Dir.OpenFileOptions{} }) catch return; - defer host(File.close, .{ file, io }); - s.read(p, file); -} - -/// `file` read into Recent, under this session's changes, when it is not -/// as this session last read or wrote it; then noted as read. -fn read(s: *Store, p: *Pardes, file: File) void { - const io = ioOf(); - const st = host(File.stat, .{ file, io }) catch return; - if (st.inode == s.inode and st.size == s.size and st.mtime.nanoseconds == s.mtime) return; - const gpa = p.recentGpa(); - const buf = gpa.alloc(u8, @min(st.size, max_file)) catch return; - defer gpa.free(buf); - const n = host(File.readPositionalAll, .{ file, io, buf, 0 }) catch return; - p.recent.merge(gpa, buf[0..n]); - s.inode = st.inode; - s.size = st.size; - s.mtime = st.mtime.nanoseconds; -} - -/// Recent written out when it changed, at the end of a step and at the -/// end (`wait`: the lock is waited for there, never in a step). -pub fn flush(s: *Store, p: *Pardes, wait: bool) void { - if (!p.recent.dirty) return; - if (s.path) |path| { - if (!s.loaded) return; - // Another session's lock: the next step tries again. - if (!s.write(p, path, wait)) return; - } - // Written, or nowhere to write it (or the disk refused it: kept for - // no one, as before). - for (p.recent.entries.items) |*e| e.changed = false; - p.recent.dirty = false; -} - -/// Under the file's lock: the file read again when another session wrote -/// it since, this session's changes over it, the whole list written in -/// place. False when another session holds the lock (and `wait` is not -/// set). -fn write(s: *Store, p: *Pardes, path: []const u8, wait: bool) bool { - const io = ioOf(); - const file = host(Dir.createFile, .{ cwd(), io, path, Dir.CreateFileOptions{ .read = true, .truncate = false } }) catch return true; - // Closing it lets go of the lock. - defer host(File.close, .{ file, io }); - if (wait) { - host(File.lock, .{ file, io, File.Lock.exclusive }) catch return true; - } else if (!(host(File.tryLock, .{ file, io, File.Lock.exclusive }) catch return true)) return false; - s.read(p, file); - p.recent.changedFirst(); - const gpa = p.recentGpa(); - var text: std.Io.Writer.Allocating = .init(gpa); - defer text.deinit(); - const entries = p.recent.entries.items; - for (entries[0..@min(entries.len, recent.cap)]) |e| recent.List.writeEntry(e, &text.writer) catch return true; - // In place: a reader that meets it half written reads it again once - // its stat moves on, and a line a crash tore is passed over. Out of - // disk, it is kept for no one, as before. - host(File.writePositionalAll, .{ file, io, text.written(), 0 }) catch return true; - host(File.setLength, .{ file, io, text.written().len }) catch return true; - // This session's own write is not read back at the next refresh. - const st = host(File.stat, .{ file, io }) catch return true; - s.inode = st.inode; - s.size = st.size; - s.mtime = st.mtime.nanoseconds; - return true; -} - // ---- the closed panes ---- /// Pane `id`, about to close, kept: what it was (a one-pane Dump) and @@ -402,140 +256,10 @@ pub fn closedAddress(word: []const u8) ?u32 { const testing = std.testing; -/// Recent's file of a test, in its own directory (never the user's). -fn testPath(tmp: *std.testing.TmpDir) ![]u8 { - var base_buf: [std.fs.max_path_bytes]u8 = undefined; - const dir = base_buf[0..try tmp.dir.realPath(testing.io, &base_buf)]; - return std.fs.path.join(testing.allocator, &.{ dir, "recent" }); -} - -fn testWindow(path: []const u8) !*Pardes { - return Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 48, .recent_path = path }); -} - -fn fileSize(path: []const u8) u64 { - const st = std.Io.Dir.cwd().statFile(testing.io, path, .{}) catch return 0; - return st.size; -} - -test "store: two windows share Recent, each reading the other's writes on use; a busy lock delays, never loses" { - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - const path = try testPath(&tmp); - defer testing.allocator.free(path); - const a = try testWindow(path); - defer a.deinit(); - const b = try testWindow(path); - defer b.deinit(); - a.recent.opened(a.recentGpa(), "/w/a.zig"); - a.saveRecent(); - b.recent.opened(b.recentGpa(), "/w/b.zig"); - b.saveRecent(); - a.recent.closed(a.recentGpa(), "/w/a.zig", 7, 3, 0, 0); - a.saveRecent(); - // Each has both, in the order they were written: a closed last, then b. - a.store.refresh(a); - b.store.refresh(b); - for ([_]*Pardes{ a, b }) |w| { - try testing.expectEqual(@as(usize, 2), w.recent.entries.items.len); - try testing.expectEqualStrings("/w/a.zig", w.recent.entries.items[0].path); - try testing.expectEqualStrings("/w/b.zig", w.recent.entries.items[1].path); - try testing.expectEqual(@as(u32, 7), w.recent.get("/w/a.zig").?.line); - } - // Another window holding the file's lock: b's change waits in memory... - const lock = try std.Io.Dir.cwd().openFile(testing.io, path, .{ .mode = .read_write }); - try lock.lock(testing.io, .exclusive); - b.recent.opened(b.recentGpa(), "/w/c.zig"); - const size = fileSize(path); - b.saveRecent(); - try testing.expectEqual(size, fileSize(path)); - try testing.expect(b.recent.dirty); - // ...and is written at the next step after it is let go. - lock.close(testing.io); - b.saveRecent(); - try testing.expect(fileSize(path) > size); - a.store.refresh(a); - try testing.expectEqualStrings("/w/c.zig", a.recent.entries.items[0].path); - // A window started now reads the same list. - const c = try testWindow(path); - defer c.deinit(); - try testing.expectEqual(@as(usize, 3), c.recent.entries.items.len); - for (a.recent.entries.items, c.recent.entries.items) |x, y| try testing.expectEqualStrings(x.path, y.path); -} - -test "store: Recent's file holds the newest 200, the newest first, and a window started on it reads them so" { - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - const path = try testPath(&tmp); - defer testing.allocator.free(path); - const a = try testWindow(path); - defer a.deinit(); - var name: [32]u8 = undefined; - for (0..recent.cap + 100) |i| { - const file = std.fmt.bufPrint(&name, "/w/file{d}.zig", .{i}) catch unreachable; - a.recent.opened(a.recentGpa(), file); - a.recent.closed(a.recentGpa(), file, 1, 1, 0, 0); - a.saveRecent(); - } - try testing.expectEqual(@as(usize, recent.cap), a.recent.entries.items.len); - const text = try std.Io.Dir.cwd().readFileAlloc(testing.io, path, testing.allocator, .limited(1 << 20)); - defer testing.allocator.free(text); - try testing.expectEqual(@as(usize, recent.cap), std.mem.count(u8, text, "\n")); - try testing.expect(std.mem.startsWith(u8, text, "1 1 0 0 /w/file299.zig\n")); - const b = try testWindow(path); - defer b.deinit(); - try testing.expectEqual(@as(usize, recent.cap), b.recent.entries.items.len); - try testing.expectEqualStrings("/w/file299.zig", b.recent.entries.items[0].path); - try testing.expectEqualStrings("/w/file100.zig", b.recent.entries.items[recent.cap - 1].path); -} - -test "store: a foreign file never stops pardes: its lines that are Recent's are read, the next write mends it" { - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - const path = try testPath(&tmp); - defer testing.allocator.free(path); - try std.Io.Dir.cwd().writeFile(testing.io, .{ .sub_path = path, .data = "\x00\xffnot a list at all\n7 3 0 0 /w/ok.zig\n@r 9 zz\n1 2 /w/short.zig\n" }); - const a = try testWindow(path); - defer a.deinit(); - try testing.expectEqual(@as(usize, 1), a.recent.entries.items.len); - try testing.expectEqual(@as(u32, 7), a.recent.get("/w/ok.zig").?.line); - a.recent.opened(a.recentGpa(), "/w/a.zig"); - a.saveRecent(); - const text = try std.Io.Dir.cwd().readFileAlloc(testing.io, path, testing.allocator, .limited(1 << 20)); - defer testing.allocator.free(text); - try testing.expectEqualStrings("0 0 0 0 /w/a.zig\n7 3 0 0 /w/ok.zig\n", text); -} - -test "store: the store Recent's file replaced is read once, its newest records first, over the file's list" { - var tmp = testing.tmpDir(.{}); - defer tmp.cleanup(); - const path = try testPath(&tmp); - defer testing.allocator.free(path); - // An older list, then the store written after it: its records oldest - // first, a closed pane's dump between them. - try std.Io.Dir.cwd().writeFile(testing.io, .{ .sub_path = path, .data = "5 5 0 0 /w/listed.zig\n" }); - const store = try std.fmt.allocPrint(testing.allocator, "{s}/store", .{std.fs.path.dirname(path).?}); - defer testing.allocator.free(store); - try std.Io.Dir.cwd().writeFile(testing.io, .{ .sub_path = 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" }); - { - // Made newer than the list, as on a disk where both were written. - const f = try std.Io.Dir.cwd().openFile(testing.io, store, .{ .mode = .read_write }); - defer f.close(testing.io); - const st = try f.stat(testing.io); - try f.setTimestamps(testing.io, .{ .modify_timestamp = .{ .new = .{ .nanoseconds = st.mtime.nanoseconds + std.time.ns_per_s } } }); - } - const a = try testWindow(path); - defer a.deinit(); - try testing.expectEqual(@as(usize, 3), a.recent.entries.items.len); - try testing.expectEqualStrings("/w/old.zig", a.recent.entries.items[0].path); - try testing.expectEqual(@as(u32, 9), a.recent.entries.items[0].line); - try testing.expectEqualStrings("/w/both.zig", a.recent.entries.items[1].path); - try testing.expectEqualStrings("/w/listed.zig", a.recent.entries.items[2].path); - // Written to the file at the step's end; read from it alone after. - a.saveRecent(); - const b = try testWindow(path); - defer b.deinit(); - for (a.recent.entries.items, b.recent.entries.items) |x, y| try testing.expectEqualStrings(x.path, y.path); +/// A window keeping its state in `dir` (never the user's DumpDir). +fn testWindow(dir: []const u8) !*Pardes { + var config: [4200]u8 = undefined; + return Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 48, .state = .disk, .startup_config = try std.fmt.bufPrint(&config, "DumpDir {s}\n", .{dir}) }); } test "store: this session keeps its newest 64 closed panes" { @@ -561,9 +285,9 @@ test "store: this session keeps its newest 64 closed panes" { test "store: a closed pane comes back where it was, its rows from the pane that took them, nothing else moving; Panes lists it, marked" { var tmp = testing.tmpDir(.{}); defer tmp.cleanup(); - const path = try testPath(&tmp); - defer testing.allocator.free(path); - const p = try testWindow(path); + var dir_buf: [std.fs.max_path_bytes]u8 = undefined; + const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; + const p = try testWindow(dir); defer p.deinit(); _ = try p.setTestFile("alpha\nbeta\n"); p.newScratchBelow(0); @@ -615,8 +339,8 @@ test "store: a closed pane comes back where it was, its rows from the pane that } else false; try testing.expect(found); try testing.expect(p.store.closedBySerial(again) == null); - // Another session on the same file lists none of this one's. - const w = try testWindow(path); + // Another session on the same state file lists none of this one's. + const w = try testWindow(dir); defer w.deinit(); try testing.expectEqual(@as(usize, 0), w.store.closed.items.len); } diff --git a/src/dump.zig b/src/dump.zig index 1b3bcb5c..3ded45ca 100644 --- a/src/dump.zig +++ b/src/dump.zig @@ -385,7 +385,7 @@ pub fn upgradePath(buf: *[1024:0]u8, dir_setting: []const u8) ?[:0]const u8 { return std.fmt.bufPrintSentinel(buf, "{s}/pardes-upgrade-{d}.dump", .{ dir, std.c.getpid() }, 0) catch null; } -fn loadAndRevive(gpa: std.mem.Allocator, opts: Options, bytes: []const u8, previous: ?*const Pardes, kept: ?*const [MAX_PANES]bool) !*Pardes { +fn loadAndRevive(gpa: std.mem.Allocator, opts: Options, bytes: []const u8, previous: ?*Pardes, kept: ?*const [MAX_PANES]bool) !*Pardes { var notes: std.ArrayList(u8) = .empty; defer notes.deinit(gpa); var on_disk: OnDisk = @splat(false); @@ -408,9 +408,10 @@ fn loadAndRevive(gpa: std.mem.Allocator, opts: Options, bytes: []const u8, previ /// A loaded core given to this host (with `revivePane` for each of its /// panes): the store and Recent read (a Restore's carried from the session -/// it replaces) and its ThemeFile asked for again. An upgrade's (`resumed`) -/// keeps its views, whose clients come back with it, and its settings. -fn reviveSession(p: *Pardes, previous: ?*const Pardes, resumed: bool) !void { +/// it replaces, with its state file) and its ThemeFile asked for again. An +/// upgrade's (`resumed`) keeps its views, whose clients come back with it, +/// and its settings. +fn reviveSession(p: *Pardes, previous: ?*Pardes, resumed: bool) !void { // ponytail: one client loads it, so it is one view, the live one's; a // replica joining (phase 5) takes `load`'s, every view. if (!resumed) views.only(p); @@ -424,9 +425,13 @@ fn reviveSession(p: *Pardes, previous: ?*const Pardes, resumed: bool) !void { p.cell_pixels = old.cell_pixels; p.row_metrics = old.row_metrics; p.native_images = old.native_images; + // Its panes close here, each noted where it was, as at its end. + for (old.panes) |slot| if (slot) |pane| old.noteClosed(pane); p.recent = old.recent.clone(p.recentGpa()); p.store = old.store.clone(p.recentGpa()); - } else p.loadRecent(); + p.db = old.db; + old.db = .{}; + } config.tab_now = p.settings.tab; // A ThemeFile is a request the host answers by reading the file again; // until it has (and for good if it fails) the theme the dump had shows. @@ -443,6 +448,8 @@ fn reviveSession(p: *Pardes, previous: ?*const Pardes, resumed: bool) !void { // reflows would lose where a terminal's view was. p.sync(); if (!resumed) exec.applyStartupConfig(p); + // After the startup config, whose DumpDir says where the state file is. + if (previous == null) p.loadRecent(); colors.finishThemeInitialization(p); p.sync(); _ = p.takeHaptic(); // see init: a restored session is not a gesture diff --git a/src/main.zig b/src/main.zig index 20c29ed8..3b7d6abe 100644 --- a/src/main.zig +++ b/src/main.zig @@ -743,7 +743,7 @@ fn nativeMain(std_init: std.process.Init) !void { opts.startup_config = found.bytes; opts.startup_config_path = found.path; opts.config_dir = found.dir; - opts.recent_path = recentPath(init.io, arena, init.environ_map); + opts.state = .disk; 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,19 +1099,3 @@ test { if (comptime pardes.platform == .gui) _ = @import("gui/gui.zig"); if (comptime pardes.font_picker) _ = @import("fonts.zig"); } - -/// Where Recent's file, which every session shares, is kept (Store.zig): -/// $XDG_STATE_HOME/pardes/recent, else ~/.local/state/pardes/recent, the -/// directory made if need be; null with no home to keep it in (Recent then -/// lasts the session). -fn recentPath(io: std.Io, arena: std.mem.Allocator, env: *const std.process.Environ.Map) ?[]const u8 { - const dir = dir: { - if (env.get("XDG_STATE_HOME")) |base| if (base.len != 0 and std.fs.path.isAbsolute(base)) - break :dir std.fs.path.join(arena, &.{ base, "pardes" }) catch return null; - const home = env.get("HOME") orelse return null; - if (home.len == 0) return null; - break :dir std.fs.path.join(arena, &.{ home, ".local", "state", "pardes" }) catch return null; - }; - std.Io.Dir.cwd().createDirPath(io, dir) catch return null; - return std.fs.path.join(arena, &.{ dir, "recent" }) catch null; -} diff --git a/src/ninep/tree.zig b/src/ninep/tree.zig index 66a2c0bb..690d1d1f 100644 --- a/src/ninep/tree.zig +++ b/src/ninep/tree.zig @@ -1097,7 +1097,7 @@ fn readFile(p: *Pardes, req: Req, target: Target) Reply { // Every window's files: what the others wrote since, read, // unless a step is out mid-syscall (nothing may change the // core under it): then the list as it was. - if (pardes.turn.out == 0) p.store.refresh(p); + if (pardes.turn.out == 0) p.db.readRecent(p); var text: std.Io.Writer.Allocating = .init(p.scratch.allocator()); pardes.panes.Output.recentText(p, &text.writer, false) catch break :recent Reply.fail(req.tag, E.NOMEM); break :recent window(p, req, text.written()); diff --git a/src/pardes.zig b/src/pardes.zig index 16dd794e..b42bf643 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -19,6 +19,7 @@ pub const tagline = @import("tagline.zig"); pub const exec = @import("exec.zig"); const recent_files = @import("recent.zig"); const Store = @import("Store.zig"); +const Db = @import("Db.zig"); pub const mouse = @import("mouse.zig"); pub const normal = @import("normal.zig"); pub const edit = @import("edit.zig"); @@ -449,6 +450,7 @@ test { _ = @import("upgrade.zig"); _ = @import("recent.zig"); _ = @import("Store.zig"); + _ = @import("Db.zig"); _ = @import("pager.zig"); _ = @import("Places.zig"); _ = @import("FrameStats.zig"); @@ -3223,10 +3225,10 @@ pub const Options = struct { ninep_tcp: ?[]const u8 = null, mounts: []const filesystem.Mount = &.{}, config_dir: ?[]const u8 = null, - /// Recent's file, which every session of the user's shares (Store.zig). - /// Set by the host that has a state directory; none, Recent lasts as - /// long as the session. - recent_path: ?[]const u8 = null, + /// Where what outlives the session is kept (Db.zig): the user's state + /// 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, startup_config_path: ?[]const u8 = null, image_allocator: ?std.mem.Allocator = null, pdf_allocator: ?std.mem.Allocator = null, @@ -3303,9 +3305,10 @@ pub const Pardes = struct { /// The files opened lately, closed ones too (recent.zig): `Recent`, /// /recent, and the jumplist's way back to a closed pane. recent: recent_files.List = .{}, - /// Recent's file, shared with every other session, and the panes this - /// session closed, which Panes brings back (Store.zig). + /// The panes this session closed, which Panes brings back (Store.zig). store: Store = .{}, + /// The user's state file, which every session shares: Recent's (Db.zig). + db: Db = .{}, /// acme's Incl: the directories a look at a name that is nowhere else /// (`stdio.h`, `<foo.h>`) is tried in, in order. The session's, seeded /// with C's (/usr/include, /usr/local/include), kept in a dump. @@ -3602,11 +3605,11 @@ pub const Pardes = struct { "opts", "dump_out", }, .derived = .{ "shell_rows", "diff_output", "locations_cache", "retired_panes", "tty_filter_palette", "body_chrome" }, - // The host's handles, the 9P server's fids and log, Recent's file - // every session of the user shares (and Recent read from it) with - // this session's closed panes, and the files of a host with no file + // The host's handles, the 9P server's fids and log, the state file + // every session of the user shares (and Recent read from it), this + // session's closed panes, and the files of a host with no file // system of its own. - .authority = .{ "host", "fs", "store", "recent", "fallback" }, + .authority = .{ "host", "fs", "store", "recent", "db", "fallback" }, .len = .{ .{ "jumps", "njumps" }, .{ "look_src", "n_look_src" }, .{ "leader_keys", "leader_n" }, .{ "col_x", "ncol" }, .{ "col_w", "ncol" }, .{ "session_dir", "session_dir_len" } }, }; @@ -3644,6 +3647,7 @@ pub const Pardes = struct { .scratch = .init(gpa), .frame_arena = .init(opts.frame_allocator orelse gpa), .fallback = .{ .gpa = gpa }, + .db = .{ .kind = opts.state }, }; errdefer p.deinit(); p.fs.started = ctlfs.events.now(); @@ -3759,7 +3763,6 @@ pub const Pardes = struct { }, } p.sync(); - p.loadRecent(); for (config.default_incl) |dir| { const copy = try gpa.dupe(u8, dir); p.incl.append(gpa, copy) catch |err| { @@ -3768,6 +3771,8 @@ pub const Pardes = struct { }; } exec.applyStartupConfig(p); + // After the startup config, whose DumpDir says where the state file is. + p.loadRecent(); colors.finishThemeInitialization(p); p.sync(); // A config init that opens a file with `Look …` armed the pulse before @@ -3795,17 +3800,17 @@ pub const Pardes = struct { return std.heap.smp_allocator; } - /// Recent's file read once at start: a new session's. + /// Recent read from the state file once at start: a new session's. pub fn loadRecent(p: *Pardes) void { - // A replica's store is its own, in memory: the recording session's - // file is every session's of its user (src/tape.zig). - p.store = .{ .path = if (tape.replaying()) null else p.opts.recent_path }; - p.store.load(p); + // A replica's Recent is its own, in memory: the recording session's + // state file is every session's of its user (src/tape.zig). + if (tape.replaying()) p.db.kind = .off; + p.db.readRecent(p); } /// What changed written out, at the end of a step and at the end. pub fn saveRecent(p: *Pardes) void { - p.store.flush(p, false); + p.db.writeRecent(p, false); } /// Whether `path` is one Recent keeps: a file on this host's disk, not @@ -3817,7 +3822,7 @@ pub const Pardes = struct { /// A file, PDF or image pane closing: its place noted in Recent, when /// Recent keeps it. - fn noteClosed(p: *Pardes, pane: *const Pane) void { + pub fn noteClosed(p: *Pardes, pane: *const Pane) void { const d = recentPlace(pane) orelse return; // The host asked first, always: what Recent holds is the // authority's, and a step that asked only when it did not know the @@ -3900,7 +3905,8 @@ pub const Pardes = struct { if (std.c.getenv("PARDES_FRAMES")) |path| p.frames.dump(std.mem.span(path)); // Every file open now is closed with its dot, and the list kept. for (p.panes) |slot| if (slot) |pane| p.noteClosed(pane); - p.store.flush(p, true); + p.db.writeRecent(p, true); + p.db.deinit(); p.store.deinit(p.recentGpa()); p.recent.deinit(p.recentGpa()); for (p.incl.items) |dir| p.gpa.free(dir); diff --git a/src/replica.zig b/src/replica.zig index 82cd83f9..59b0dbb3 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -669,7 +669,7 @@ pub const Replica = struct { o.image_allocator = o.image_allocator orelse std.heap.c_allocator; o.pdf_allocator = o.pdf_allocator orelse std.heap.c_allocator; // The session's closed panes and Recent are the authority's. - o.recent_path = null; + o.state = .off; // Its emulators off the epoch, as `terminal.create` makes a screen // replica's: deltas write them while a copy is held. pardes.dump.terminal_gpa = terminal.emulator_gpa; |
