diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-30 17:35:06 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-30 17:35:06 -0300 |
| commit | 6d8da8721141284cf33bb901168b26135893eb86 (patch) | |
| tree | 791d63401d4e85fbca1d597a8ce0326e70efd545 /src/file_watch.zig | |
| parent | efd43a3a2cf03bb741366331158b368abf9ac583 (diff) | |
| parent | bf6810a8f9d6cc82050e7d9c0cfb06358301b574 (diff) | |
| download | pardes-6d8da8721141284cf33bb901168b26135893eb86.tar.gz pardes-6d8da8721141284cf33bb901168b26135893eb86.zip | |
Merge latest upstream into the macOS branch for Zig 0.17
Retain the macOS branch history and local Acme selection color while adopting upstream's current core and platform changes. Pin Zig and ZLS to 0.17.0, and fetch the existing zstbi compatibility commit through upstream after its original fork was removed.
Diffstat (limited to 'src/file_watch.zig')
| -rw-r--r-- | src/file_watch.zig | 170 |
1 files changed, 151 insertions, 19 deletions
diff --git a/src/file_watch.zig b/src/file_watch.zig index b2b7e380..ab902e5b 100644 --- a/src/file_watch.zig +++ b/src/file_watch.zig @@ -13,8 +13,9 @@ const builtin = @import("builtin"); const libc = std.c; const linux = std.os.linux; const pardes = @import("pardes.zig"); +const colors = @import("colors.zig"); const filesystem = @import("fs.zig"); -const message = pardes.Pardes.Message; +const message = pardes.Messages.Message; pub const Identity = struct { inode: std.Io.File.INode, @@ -59,13 +60,21 @@ pub const Watch = struct { kq: c_int = -1, serial: u32, generation: Generation, + /// The path last read while watched (its hash): its going away later is + /// a deletion to say, where a path never there (a pane renamed to a new + /// name) is not. + existed: ?u64 = null, }; /// The final slot is the one non-pane watched payload: the active ThemeFile. /// Sharing the table also shares directory descriptors correctly when a user /// happens to open that .zon as an ordinary document. pub const theme_slot = pardes.MAX_PANES; -pub const Table = [pardes.MAX_PANES + 1]?Watch; +/// After it, one slot a post chain file (ShaderBuild.zig): marked only so +/// a save there wakes the host, which has the files read again; no +/// generation of theirs is kept here. +pub const shader_slot = theme_slot + 1; +pub const Table = [shader_slot + pardes.config.Runtime.Post.max]?Watch; /// Whether this OS has a watcher at all. Off it every entry point below /// returns the inert answer: `watchPath` installs nothing, so `watches` stays @@ -220,8 +229,10 @@ fn markDir(fd: c_int, dir_z: [:0]const u8, watches: *const Table) c_int { switch (builtin.os.tag) { .linux => { // CLOSE_WRITE coalesces one writer's writes; MOVED_TO and CREATE - // cover rename-over and delete-then-recreate saves. - const mask = linux.IN.CLOSE_WRITE | linux.IN.MOVED_TO | linux.IN.CREATE | linux.IN.ONLYDIR; + // cover rename-over and delete-then-recreate saves; DELETE and + // MOVED_FROM a file going away, said at once (File.deleted), not + // when something else next changes in the directory. + const mask = linux.IN.CLOSE_WRITE | linux.IN.MOVED_TO | linux.IN.CREATE | linux.IN.DELETE | linux.IN.MOVED_FROM | linux.IN.ONLYDIR; return libc.inotify_add_watch(fd, dir_z, mask); }, .macos => { @@ -382,7 +393,11 @@ fn watchPath( } const declared_path = path orelse return; const watched_path = filesystem.localPath(declared_path) orelse return; - const stat = std.Io.Dir.cwd().statFile(std.Io.Threaded.global_single_threaded.io(), watched_path, .{}) catch null; + // The path may lie in a mount this editor serves, and marking it is a + // stat and a lookup out there: the turn goes out with them. + pardes.turn.yield(); + defer pardes.turn.back(); + const stat = @import("fs.zig").statPath(std.Io.Threaded.global_single_threaded.io(), watched_path, .{}) catch null; const dir = if (stat != null and stat.?.kind == .directory) watched_path else std.fs.path.dirname(watched_path) orelse "."; var dir_buf: [4096:0]u8 = undefined; if (dir.len >= dir_buf.len) return; @@ -412,6 +427,13 @@ pub fn watchPane( watchPath(fd, watches, id, path, serial, generation); } +/// Mark the post chain files' directories, one slot each, dropping the +/// marks of files no longer in it. +pub fn watchShaders(fd: c_int, watches: *Table, paths: []const []const u8) void { + for (0..pardes.config.Runtime.Post.max) |i| + watchPath(fd, watches, shader_slot + i, if (i < paths.len) paths[i] else null, 0, .{ .text = 0 }); +} + /// 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. @@ -435,13 +457,26 @@ pub fn reloadPane( } if (pane.file) |file| { - const bytes = filesystem.read(core, file.path) catch return false; + const bytes = filesystem.read(core, file.path) catch |err| { + // Gone from under the pane: said once, the text kept. + if (err == error.FileNotFound and watched.existed == std.hash.Wyhash.hash(0, file.path)) pardes.panes.File.deleted(core, @intCast(id)); + return false; + }; + if (watches[id]) |*live| live.existed = std.hash.Wyhash.hash(0, file.path); defer core.gpa.free(bytes); const hash = std.hash.Wyhash.hash(0, bytes); switch (watched.generation) { .text => |accepted| if (accepted == hash) return false, .pdf => {}, } + // The pane holds the disk's text already (a Zerox twin saved, its + // other twin watching the same file): nothing reloaded, nothing said. + if (std.mem.eql(u8, file.content, bytes)) { + if (watches[id]) |*live| if (live.serial == watched.serial) { + live.generation = .{ .text = hash }; + }; + return false; + } if (!core.reloadWatchedFile(@intCast(id), bytes)) return false; const current = core.panes[id] orelse return false; if (current.serial != watched.serial) return false; @@ -461,7 +496,17 @@ pub fn reloadPane( const path = pane.pdfPath() orelse return false; const before = identify(io, path) catch return false; switch (watched.generation) { - .pdf => |accepted| if (accepted) |identity| if (identity.eql(before)) return false, + .pdf => |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. + if (watches[id]) |*live| if (live.serial == watched.serial) { + live.generation = .{ .pdf = before }; + }; + return false; + }, .text => {}, } if (!core.reloadWatchedFile(@intCast(id), &.{})) return false; @@ -512,6 +557,7 @@ pub fn applyEffect( const bytes = filesystem.read(core, file.path) catch return false; defer core.gpa.free(bytes); watched.generation = .{ .text = std.hash.Wyhash.hash(0, bytes) }; + watched.existed = std.hash.Wyhash.hash(0, file.path); return false; }; return on and watches[id] != null and reloadPane(core, io, watches, id, false); @@ -547,13 +593,20 @@ pub fn applyThemeEffect( ) bool { watchPath(fd, watches, theme_slot, null, 0, .{ .text = 0 }); if (!on) return false; - const request = core.themeFileRequest(generation) orelse return false; + const request = colors.themeFileRequest(core, generation) orelse return false; + // What fails here is the ThemeFile write's failure too, which waits on + // this (the one-failure rule): late, as a Save's. + const pane = request.pane; const bytes = filesystem.readFile(gpa, request.path) catch |err| { - core.failThemeFile(generation, err); + colors.failThemeFile(core, generation, err); + core.noteLateFailure(pane); return false; }; defer gpa.free(bytes); - if (!core.loadThemeFile(generation, bytes, animate)) return false; + if (!colors.loadThemeFile(core, generation, bytes, animate)) { + core.noteLateFailure(pane); + return false; + } const hash = std.hash.Wyhash.hash(0, bytes); watchPath(fd, watches, theme_slot, request.path, generation, .{ .text = hash }); if (watches[theme_slot] == null) return false; @@ -570,14 +623,15 @@ pub fn reloadTheme( announce: bool, ) bool { const watched = watches[theme_slot] orelse return false; - const request = core.themeFileRequest(watched.serial) orelse return false; + const request = colors.themeFileRequest(core, watched.serial) orelse return false; // Same re-arm as reloadPane's, for the same reason: a .zon saved by rename // moves the inode the file filter is on. if (comptime builtin.os.tag == .macos) { if (watches[theme_slot]) |*entry| remarkFile(entry, request.path); } const bytes = filesystem.readFile(gpa, request.path) catch |err| { - core.failThemeFile(watched.serial, err); + core.invalidate(); + colors.failThemeFile(core, watched.serial, err); return false; }; defer gpa.free(bytes); @@ -586,7 +640,8 @@ pub fn reloadTheme( .text => |accepted| if (accepted == hash) return false, .pdf => unreachable, } - if (!core.loadThemeFile(watched.serial, bytes, true)) return false; + if (!colors.loadThemeFile(core, watched.serial, bytes, true)) return false; + core.invalidate(); const live = if (watches[theme_slot]) |*entry| entry else return false; if (live.serial != watched.serial) return false; live.generation = .{ .text = hash }; @@ -599,7 +654,9 @@ pub fn reloadTheme( pub fn identify(io: std.Io, path: []const u8) !Identity { const native = filesystem.localPath(path) orelse return error.NonLocalPath; - const stat = try std.Io.Dir.cwd().statFile(io, native, .{}); + pardes.turn.yield(); + defer pardes.turn.back(); + const stat = try @import("fs.zig").statPath(io, native, .{}); if (stat.kind != .file) return error.NotFile; return .{ .inode = stat.inode, @@ -648,22 +705,64 @@ test "restored file watches preserve snapshots and accept later disk changes" { else => {}, }; try std.testing.expect(armed and watches[id] != null); - try std.testing.expectEqualStrings("restored snapshot\n", pane.file.?.content); + // A clean pane's text is its file's, read at the Restore: a dump + // that kept other text (an older dump) is not what comes back. + const restored: []const u8 = if (case.dirty) "restored snapshot\n" else "disk baseline\n"; + try std.testing.expectEqualStrings(restored, pane.file.?.content); try std.testing.expectEqual(@as(usize, 0), pane.file.?.history.undo_len); try std.testing.expectEqual(case.dirty, pane.file.?.revision != pane.file.?.saved_revision); _ = reloadChanged(core, io, gpa, &watches); - try std.testing.expectEqualStrings("restored snapshot\n", pane.file.?.content); + try std.testing.expectEqualStrings(restored, pane.file.?.content); try tmp.dir.writeFile(io, .{ .sub_path = "watched", .data = "later disk save\n" }); _ = reloadChanged(core, io, gpa, &watches); + // A snapshot restored with unsaved text keeps it: a later disk + // change is said, never loaded over it (File.changed). + if (case.dirty) { + try std.testing.expectEqualStrings("restored snapshot\n", pane.file.?.content); + try std.testing.expect(pane.file.?.disk_newer != null); + continue; + } try std.testing.expectEqualStrings("later disk save\n", pane.file.?.content); pardes.panes.File.undo(core, pane); - try std.testing.expectEqualStrings("restored snapshot\n", pane.file.?.content); + try std.testing.expectEqualStrings(restored, pane.file.?.content); _ = reloadChanged(core, io, gpa, &watches); - try std.testing.expectEqualStrings("restored snapshot\n", pane.file.?.content); + try std.testing.expectEqualStrings(restored, pane.file.?.content); } } +test "a Zerox twin's save wakes its other twin's watch, which says nothing: their text is the file's already" { + if (comptime !supported or !filesystem.platform_has_fs) return; + const io = std.testing.io; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + try tmp.dir.writeFile(io, .{ .sub_path = "twinned", .data = "before\n" }); + var path_buf: [256]u8 = undefined; + const path = try std.fmt.bufPrint(&path_buf, ".zig-cache/tmp/{s}/twinned", .{tmp.sub_path}); + const core = try pardes.Pardes.init(std.testing.allocator, .{ .tty_only = true }); + defer core.deinit(); + const pane = try core.setTestFile("before\n"); + core.gpa.free(pane.file.?.path); + pane.file.?.path = try core.gpa.dupe(u8, path); + try std.testing.expect(core.executeBuiltinLine(0, "Zerox")); + const twin: u8 = for (core.panes, 0..) |slot, id| { + if (id != 0) if (slot) |other| if (other.file != null) break @intCast(id); + } else return error.NoTwin; + const fd = init(true); + if (fd < 0) return error.NoWatcher; + defer _ = libc.close(fd); + var watches: Table = @splat(null); + defer watchPane(fd, &watches, twin, null, core.panes[twin].?.serial, .{ .text = 0 }); + watchPane(fd, &watches, twin, path, core.panes[twin].?.serial, .{ .text = std.hash.Wyhash.hash(0, "before\n") }); + // Edited in the first and saved: the file is the twins' text now. + pardes.panes.File.setContent(core, &pane.file.?, try core.gpa.dupe(u8, "after\n")); + try tmp.dir.writeFile(io, .{ .sub_path = "twinned", .data = "after\n" }); + _ = reloadPane(core, io, &watches, twin, true); + const other = core.panes[twin].?; + try std.testing.expect(std.mem.indexOf(u8, other.msg[0..other.msg_len], "reloaded") == null); + try std.testing.expectEqualStrings("after\n", other.file.?.content); +} + test "ordinary file watches still reconcile changes between open and watch" { if (comptime !supported or !filesystem.platform_has_fs) return; const io = std.testing.io; @@ -776,7 +875,7 @@ test "theme watch reloads valid ZON and keeps the last theme across a bad save" const core = try pardes.Pardes.init(gpa, .{ .tty_only = true }); defer core.deinit(); while (core.nextEffect()) |_| {} - core.requestThemeFile(0, path); + colors.requestThemeFile(core, 0, path); const request = while (core.nextEffect()) |effect| switch (effect) { .theme_file => |theme_file| break theme_file, else => {}, @@ -812,3 +911,36 @@ test "theme watch reloads valid ZON and keeps the last theme across a bad save" try std.testing.expect(!reloadTheme(core, gpa, &watches, false)); try std.testing.expectEqual(@as(?[3]u8, .{ 9, 8, 7 }), core.theme().bg); } + +test "a watched file deleted wakes the watch at once, and its pane says so" { + if (comptime !supported or !filesystem.platform_has_fs or builtin.os.tag != .linux) 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 = "watched", .data = "here\n" }); + var path_buf: [256]u8 = undefined; + const path = try std.fmt.bufPrint(&path_buf, ".zig-cache/tmp/{s}/watched", .{tmp.sub_path}); + const core = try pardes.Pardes.init(gpa, .{ .tty_only = true }); + defer core.deinit(); + while (core.nextEffect()) |_| {} + const id: u8 = @intCast(core.freeSlot().?); + const pane = try pardes.panes.File.open(core, id, path, 0); + const fd = init(true); + if (fd < 0) return error.NoWatcher; + defer _ = libc.close(fd); + var watches: Table = @splat(null); + defer watchPane(fd, &watches, id, null, pane.serial, .{ .text = 0 }); + while (core.nextEffect()) |effect| switch (effect) { + .watch => |watch| if (watch.pane == id and watch.on) { + _ = applyEffect(core, io, fd, &watches, id, watch.on, watch.mode); + }, + else => {}, + }; + _ = drain(fd); + try tmp.dir.deleteFile(io, "watched"); + var pfd = [_]libc.pollfd{.{ .fd = fd, .events = libc.POLL.IN, .revents = 0 }}; + try std.testing.expect(libc.poll(&pfd, 1, 1000) > 0); + _ = reloadChanged(core, io, gpa, &watches); + try std.testing.expect(pane.file.?.disk_gone); +} |
