diff options
Diffstat (limited to 'src/file_watch.zig')
| -rw-r--r-- | src/file_watch.zig | 89 |
1 files changed, 66 insertions, 23 deletions
diff --git a/src/file_watch.zig b/src/file_watch.zig index 7b8ccbef..812c1791 100644 --- a/src/file_watch.zig +++ b/src/file_watch.zig @@ -2,8 +2,8 @@ //! TTY and SDL hosts. Their event loops still own blocking waits, wake //! coalescing, and retry scheduling; this file owns the identical synchronous //! operation each wake performs. Text buffers already own the bytes whose hash -//! is their generation. Large PDFs use metadata which changes for in-place and -//! rename-over saves, avoiding a second whole-document allocation. +//! is their generation. A kind's pane (a PDF) uses metadata which changes for +//! in-place and rename-over saves, reading its file only when it changed. //! //! Two kernels, one transaction: inotify on linux (and FreeBSD 15's, the //! same events), kqueue on macos. `init`, `wait`, `stop`, `drain`, `markDir` @@ -30,14 +30,27 @@ pub const Identity = struct { }; /// The last disk generation accepted by a native shell. Text panes already -/// need their bytes, so their exact content hash is cheapest. PDF panes reopen -/// the pathname directly and use metadata, avoiding a second whole-document -/// allocation merely to discover whether MuPDF should reopen it. +/// need their bytes, so their exact content hash is cheapest. A kind's pane +/// uses metadata, its file read only once that moved (a PDF can be large). pub const Generation = union(enum) { text: u64, - pdf: ?Identity, + kind: ?Identity, }; +/// Whether a kind's file is still the bytes its pane opened from. +fn sameBytes(core: *pardes.Pardes, pane: *const pardes.Pane, path: []const u8) bool { + const k = pane.addon orelse return false; + if (k.opened == 0) return false; + const bytes = filesystem.read(core, path) catch return false; + defer core.gpa.free(bytes); + return std.hash.Wyhash.hash(0, bytes) == k.opened; +} + +fn kindPath(pane: *const pardes.Pane) ?[]const u8 { + const k = pane.addon orelse return null; + return if (k.path.len > 0) k.path else null; +} + /// One native directory mark, plus what macos needs beside it. /// /// `wd` is an inotify wd on linux and an open EVTONLY directory descriptor on @@ -421,8 +434,8 @@ pub fn unwatchAll(fd: c_int, watches: *Table) void { } /// Reconcile one marked pane. A true result requests an immediate second pass: -/// the PDF pathname changed between the stat before MuPDF reopened it and the -/// stat after, so committing either identity would lose a generation. +/// a kind's pathname changed between the stat before its pane reopened and +/// the stat after, so committing either identity would lose a generation. pub fn reloadPane( core: *pardes.Pardes, io: std.Io, @@ -439,7 +452,7 @@ pub fn reloadPane( // the next in-place save would otherwise wake nobody. A no-op elsewhere, // and a no-op here too when the inode has not moved. if (comptime builtin.os.tag == .macos) { - if (watches[id]) |*live| remarkFile(live, if (pane.file) |f| f.path else pane.pdfPath()); + if (watches[id]) |*live| remarkFile(live, if (pane.file) |f| f.path else kindPath(pane)); } if (pane.file) |file| { @@ -453,7 +466,7 @@ pub fn reloadPane( const hash = std.hash.Wyhash.hash(0, bytes); switch (watched.generation) { .text => |accepted| if (accepted == hash) return false, - .pdf => {}, + .kind => {}, } // The pane holds the disk's text already (a Zerox twin saved, its // other twin watching the same file): nothing reloaded, nothing said. @@ -479,31 +492,32 @@ pub fn reloadPane( return false; } - const path = pane.pdfPath() orelse return false; + const path = kindPath(pane) orelse return false; const before = identify(io, path) catch return false; switch (watched.generation) { - .pdf => |accepted| if (accepted) |identity| { + .kind => |accepted| if (accepted) |identity| { if (identity.eql(before)) return false; - } else if (pardes.panes.Pdf.sameBytes(core, pane)) { - // Armed over the bytes the pane opened (a text's baseline is its - // content, a PDF's is what it read): nothing to reload. Parsing - // the document again doubled every PDF's open. + } else if (sameBytes(core, pane, path)) { + // Armed over the bytes the pane opened: nothing to reload + // (opening it again doubled every PDF's open). Changed between + // the open and the arm, it reloads. if (watches[id]) |*live| if (live.serial == watched.serial) { - live.generation = .{ .pdf = before }; + live.generation = .{ .kind = before }; }; return false; }, .text => {}, } + // Its kind reads the file again (Kind.fileChanged): on the tape, no bytes. if (!core.reloadWatchedFile(@intCast(id), &.{})) return false; const current = core.panes[id] orelse return false; if (current.serial != watched.serial) return false; - const current_path = current.pdfPath() orelse return false; + const current_path = kindPath(current) orelse return false; const after = identify(io, current_path) catch return true; if (!before.eql(after)) return true; if (watches[id]) |*live| { if (live.serial == watched.serial) - live.generation = .{ .pdf = after }; + live.generation = .{ .kind = after }; } if (announce) { var msg_buf: [256]u8 = undefined; @@ -531,9 +545,9 @@ pub fn applyEffect( if (pane.file) |file| { path = file.path; generation = .{ .text = std.hash.Wyhash.hash(0, file.content) }; - } else if (pane.pdfPath()) |pdf_path| { - path = pdf_path; - generation = .{ .pdf = null }; + } else if (kindPath(pane)) |kind_path| { + path = kind_path; + generation = .{ .kind = null }; } }; watchPane(fd, watches, id, path, serial, generation); @@ -623,7 +637,7 @@ pub fn reloadTheme( const hash = std.hash.Wyhash.hash(0, bytes); switch (watched.generation) { .text => |accepted| if (accepted == hash) return false, - .pdf => unreachable, + .kind => unreachable, } if (!colors.loadThemeFile(core, watched.serial, bytes)) return false; core.invalidate(); @@ -760,6 +774,35 @@ test "ordinary file watches still reconcile changes between open and watch" { try std.testing.expectEqualStrings("opened\n", pane.file.?.content); } +test "a kind's pane whose file changed between its open and its watch opens again from it" { + if (comptime !supported or !filesystem.platform_has_fs) return; + const io = std.testing.io; + const gpa = std.testing.allocator; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(io, .{ .sub_path = "pic.ppm", .data = "opened\n" }); + var path_buf: [256]u8 = undefined; + const path = try std.fmt.bufPrint(&path_buf, ".zig-cache/tmp/{s}/pic.ppm", .{tmp.sub_path}); + const core = try pardes.Pardes.init(gpa, .{ .tty_only = true }); + defer core.deinit(); + const id: u8 = @intCast(core.freeSlot().?); + _ = try pardes.Kind.open(core, id, pardes.Kind.test_kind.of(core), path, "opened\n"); + while (core.nextEffect()) |_| {} + const fd = init(true); + if (fd < 0) return error.NoWatcher; + defer _ = libc.close(fd); + var watches: Table = @splat(null); + defer unwatchAll(fd, &watches); + // The file as it opened: armed, nothing opened again. + try std.testing.expect(!applyEffect(core, io, fd, &watches, id, true, .reconcile)); + try std.testing.expectEqual(std.hash.Wyhash.hash(0, "opened\n"), core.panes[id].?.addon.?.opened); + // Changed before the watch arms: opened again from it. + try std.testing.expect(!applyEffect(core, io, fd, &watches, id, false, .reconcile)); + try tmp.dir.writeFile(io, .{ .sub_path = "pic.ppm", .data = "changed before watch\n" }); + try std.testing.expect(!applyEffect(core, io, fd, &watches, id, true, .reconcile)); + try std.testing.expectEqual(std.hash.Wyhash.hash(0, "changed before watch\n"), core.panes[id].?.addon.?.opened); +} + test "explicit OS directory watches refresh prefix-preserving listings" { if (comptime !supported or !filesystem.platform_has_fs) return; const gpa = std.testing.allocator; |
