summaryrefslogtreecommitdiff
path: root/src/file_watch.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-31 06:28:48 -0300
committerGabriel Schneider <[email protected]>2026-10-31 06:28:48 -0300
commita479792a1ab2b593642d7f648194bfa23ce01dba (patch)
tree96ddee7d879a9084daa07db43b0b7910b072a785 /src/file_watch.zig
parente179cc44fe5060c76a5b00c41e53f11209f5acd9 (diff)
parent4c761893e5b3fdae422622cfa02104ab36633045 (diff)
downloadpardes-a479792a1ab2b593642d7f648194bfa23ce01dba.tar.gz
pardes-a479792a1ab2b593642d7f648194bfa23ce01dba.zip
Merge the upstream daemon and replicated client/server refactor
Diffstat (limited to 'src/file_watch.zig')
-rw-r--r--src/file_watch.zig76
1 files changed, 28 insertions, 48 deletions
diff --git a/src/file_watch.zig b/src/file_watch.zig
index ab902e5b..f640fa5f 100644
--- a/src/file_watch.zig
+++ b/src/file_watch.zig
@@ -5,9 +5,10 @@
//! is their generation. Large PDFs use metadata which changes for in-place and
//! rename-over saves, avoiding a second whole-document allocation.
//!
-//! Two kernels, one transaction: inotify on linux, kqueue on macos. `init`,
-//! `wait`, `stop`, `drain`, `markDir` and `unmarkDir` are the whole of that
-//! difference, and nothing above them knows which one it is on.
+//! Two kernels, one transaction: inotify on linux (and FreeBSD 15's, the
+//! same events), kqueue on macos. `init`, `wait`, `stop`, `drain`, `markDir`
+//! and `unmarkDir` are the whole of that difference, and nothing above them
+//! knows which one it is on.
const std = @import("std");
const builtin = @import("builtin");
const libc = std.c;
@@ -81,7 +82,7 @@ pub const Table = [shader_slot + pardes.config.Runtime.Post.max]?Watch;
/// empty and `reloadPane` returns false for every slot. The core then keeps its
/// own record of what was asked and simply never gets a `file_changed`, which
/// is what a host with no watcher has always done here.
-pub const supported = builtin.os.tag == .linux or builtin.os.tag == .macos;
+pub const supported = builtin.os.tag == .linux or builtin.os.tag == .freebsd or builtin.os.tag == .macos;
/// The wake filter's `ident` on macos. `ident` is a directory descriptor for
/// every other filter on this queue, and no descriptor is `maxInt(usize)`, so
@@ -98,7 +99,7 @@ const wake_ident: usize = std.math.maxInt(usize);
///
/// `polled` is the only flag that survives the port. On linux it adds
/// IN_NONBLOCK, because a host that finds this descriptor ready in its own
-/// `poll(2)` (detached/server.zig) must be able to stop draining it, while a
+/// `poll(2)` must be able to stop draining it, while a
/// host that parks a thread in `wait` (tty.zig, gui.zig) wants the blocking
/// read. On macos neither host needs either flag: a kqueue is not inherited
/// across `fork(2)`, so there is no CLOEXEC to ask for, and whether a wait
@@ -113,6 +114,8 @@ pub fn init(polled: bool) c_int {
linux.IN.CLOEXEC | linux.IN.NONBLOCK
else
linux.IN.CLOEXEC),
+ // FreeBSD's IN_CLOEXEC and IN_NONBLOCK are its O_ flags.
+ .freebsd => return libc.inotify_init1(if (polled) 0x00100000 | 0x4 else 0x00100000),
.macos => {
const kq = libc.kqueue();
if (kq < 0) return -1;
@@ -193,7 +196,7 @@ pub fn stop(fd: c_int) void {
pub fn drain(fd: c_int) bool {
if (fd < 0) return false;
switch (builtin.os.tag) {
- .linux => {
+ .linux, .freebsd => {
var buf: [4096]u8 = undefined;
var seen = false;
while (libc.read(fd, &buf, buf.len) > 0) seen = true;
@@ -227,7 +230,7 @@ pub fn drain(fd: c_int) bool {
/// and inode, because each EVFILT_VNODE filter needs a descriptor of its own.
fn markDir(fd: c_int, dir_z: [:0]const u8, watches: *const Table) c_int {
switch (builtin.os.tag) {
- .linux => {
+ .linux, .freebsd => {
// CLOSE_WRITE coalesces one writer's writes; MOVED_TO and CREATE
// cover rename-over and delete-then-recreate saves; DELETE and
// MOVED_FROM a file going away, said at once (File.deleted), not
@@ -366,7 +369,7 @@ fn unmark(fd: c_int, old: Watch, drop_dir: bool) void {
}
if (!drop_dir) return;
switch (builtin.os.tag) {
- .linux => _ = libc.inotify_rm_watch(fd, old.wd),
+ .linux, .freebsd => _ = libc.inotify_rm_watch(fd, old.wd),
.macos => _ = libc.close(old.wd),
else => {},
}
@@ -666,30 +669,27 @@ pub fn identify(io: std.Io, path: []const u8) !Identity {
};
}
-test "restored file watches preserve snapshots and accept later disk changes" {
+test "a restored file is watched again: one clean takes what its file became since the dump, one unsaved keeps its text" {
if (comptime !supported or !filesystem.platform_has_fs) return;
const io = std.testing.io;
const gpa = std.testing.allocator;
- for ([_]struct { dirty: bool, exists: bool }{
- .{ .dirty = false, .exists = true },
- .{ .dirty = true, .exists = true },
- .{ .dirty = true, .exists = false },
- }) |case| {
+ for ([_]bool{ false, true }) |dirty| {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
- if (case.exists) try tmp.dir.writeFile(io, .{ .sub_path = "watched", .data = "disk baseline\n" });
+ try tmp.dir.writeFile(io, .{ .sub_path = "watched", .data = "dumped\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 });
+ const source = try pardes.Pardes.init(gpa, .{ .tty_only = true });
+ defer source.deinit();
+ const id: u8 = @intCast(source.freeSlot().?);
+ const opened = try pardes.panes.File.open(source, id, path, 0);
+ if (dirty) _ = pardes.ctlfs.pane.spliceBody(source, opened, 0, 0, "unsaved ").?;
+ try pardes.dump.dumpState(source);
+ // Changed on disk between the dump and its load.
+ try tmp.dir.writeFile(io, .{ .sub_path = "watched", .data = "changed since\n" });
+ const core = try pardes.dump.initFromDump(gpa, .{ .tty_only = true }, source.dump_out.?);
defer core.deinit();
- while (core.nextEffect()) |_| {}
- const id: u8 = @intCast(core.freeSlot().?);
- const pane = try pardes.panes.File.restore(core, id, .{
- .kind = .file,
- .tag = "",
- .body = "",
- .file = .{ .path = path, .content = "restored snapshot\n", .dirty = case.dirty },
- });
+ const pane = core.panes[id].?;
const fd = init(true);
if (fd < 0) return error.NoWatcher;
defer _ = libc.close(fd);
@@ -698,36 +698,16 @@ test "restored file watches preserve snapshots and accept later disk changes" {
var armed = false;
while (core.nextEffect()) |effect| switch (effect) {
.watch => |watch| if (watch.pane == id and watch.on) {
- try std.testing.expectEqual(.baseline_disk, watch.mode);
- try std.testing.expect(!applyEffect(core, io, fd, &watches, id, watch.on, watch.mode));
+ _ = applyEffect(core, io, fd, &watches, id, watch.on, watch.mode);
armed = true;
},
else => {},
};
try std.testing.expect(armed and watches[id] != null);
- // 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, 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);
+ if (dirty) {
+ try std.testing.expectEqualStrings("unsaved dumped\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, pane.file.?.content);
- _ = reloadChanged(core, io, gpa, &watches);
- try std.testing.expectEqualStrings(restored, pane.file.?.content);
+ } else try std.testing.expectEqualStrings("changed since\n", pane.file.?.content);
}
}