diff --git a/README.md b/README.md index c627200..e621735 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ A lightweight and cross-platform file watcher for your Zig projects. You can run the [examples](./examples/) like so: ```sh -zig build run- +zig build run- -- ``` ### Usage diff --git a/examples/basic.zig b/examples/basic.zig index 02f2917..0c78c01 100644 --- a/examples/basic.zig +++ b/examples/basic.zig @@ -15,10 +15,19 @@ fn watcherThread(watcher: *fzwatch.Watcher) !void { pub fn main() !void { const allocator = std.heap.page_allocator; + const args = try std.process.argsAlloc(allocator); + defer std.process.argsFree(allocator, args); + + if (args.len < 2) { + std.debug.print("Usage: {s} \n", .{args[0]}); + return error.InvalidArgument; + } + const target_file = args[1]; + var watcher = try fzwatch.Watcher.init(allocator); defer watcher.deinit(); - try watcher.addFile("README.md"); + try watcher.addFile(target_file); watcher.setCallback(callback, null); const thread = try std.Thread.spawn(.{}, watcherThread, .{&watcher}); diff --git a/examples/context.zig b/examples/context.zig index 9e8e42d..c4a29b8 100644 --- a/examples/context.zig +++ b/examples/context.zig @@ -17,9 +17,9 @@ const Object = struct { } } - pub fn init(allocator: std.mem.Allocator) !Object { + pub fn init(allocator: std.mem.Allocator, target_file: [:0]u8) !Object { var watcher = try fzwatch.Watcher.init(allocator); - try watcher.addFile("README.md"); + try watcher.addFile(target_file); return Object{ .allocator = allocator, @@ -45,7 +45,17 @@ const Object = struct { pub fn main() !void { const allocator = std.heap.page_allocator; - var obj = try Object.init(allocator); + + const args = try std.process.argsAlloc(allocator); + defer std.process.argsFree(allocator, args); + + if (args.len < 2) { + std.debug.print("Usage: {s} \n", .{args[0]}); + return error.InvalidArgument; + } + const target_file = args[1]; + + var obj = try Object.init(allocator, target_file); defer obj.deinit(); try obj.start(); diff --git a/src/watchers/linux.zig b/src/watchers/linux.zig index 82774cb..2dc646c 100644 --- a/src/watchers/linux.zig +++ b/src/watchers/linux.zig @@ -37,10 +37,11 @@ pub const LinuxWatcher = struct { } pub fn addFile(self: *LinuxWatcher, path: []const u8) !void { + try std.fs.cwd().access(path, .{}); _ = try std.posix.inotify_add_watch( self.inotify.fd, path, - std.os.linux.IN.MODIFY, + std.os.linux.IN.MODIFY | std.os.linux.IN.CLOSE_WRITE | std.os.linux.IN.ATTRIB | std.os.linux.IN.MOVE_SELF | std.os.linux.IN.DELETE_SELF, ); try self.paths.append(self.allocator, path); @@ -102,14 +103,12 @@ pub const LinuxWatcher = struct { } else if (ev.wd > self.paths.items.len + self.inotify.offset) return error.InvalidWatchDescriptor; - if (ev.mask & std.os.linux.IN.IGNORED == 0 and ev.mask & std.os.linux.IN.MODIFY == 0) - continue; - const index = @as(usize, @intCast(@max(0, ev.wd))) - self.inotify.offset; // Editors like vim create temporary files when saving // So we have to re-add the file to the watcher - if (ev.mask & std.os.linux.IN.IGNORED != 0) + if (ev.mask & (std.os.linux.IN.DELETE_SELF | std.os.linux.IN.MOVE_SELF | std.os.linux.IN.IGNORED) != 0) { try self.addFile(self.paths.items[index]); + } if (self.callback) |callback| callback(self.context, .modified); } }