diff options
Diffstat (limited to 'src/watcher.zig')
-rw-r--r-- | src/watcher.zig | 62 |
1 files changed, 29 insertions, 33 deletions
diff --git a/src/watcher.zig b/src/watcher.zig index f088d31d2..81fb3ab0f 100644 --- a/src/watcher.zig +++ b/src/watcher.zig @@ -68,18 +68,17 @@ pub const INotify = struct { pub fn watchPath(pathname: [:0]const u8) !EventListIndex { std.debug.assert(loaded_inotify); const old_count = watch_count.fetchAdd(1, .Release); - defer if (old_count == 0) std.Thread.Futex.wake(&watch_count, 10); + defer if (old_count == 0) std.Thread.Futex.wake(&watch_count, 10); return std.os.inotify_add_watchZ(inotify_fd, pathname, watch_file_mask); } pub fn watchDir(pathname: [:0]const u8) !EventListIndex { std.debug.assert(loaded_inotify); const old_count = watch_count.fetchAdd(1, .Release); - defer if (old_count == 0) std.Thread.Futex.wake(&watch_count, 10); + defer if (old_count == 0) std.Thread.Futex.wake(&watch_count, 10); return std.os.inotify_add_watchZ(inotify_fd, pathname, watch_dir_mask); } - pub fn unwatch(wd: EventListIndex) void { std.debug.assert(loaded_inotify); _ = watch_count.fetchSub(1, .Release); @@ -97,43 +96,40 @@ pub const INotify = struct { std.debug.assert(loaded_inotify); restart: while (true) { + std.Thread.Futex.wait(&watch_count,0, null) catch unreachable; + const rc = std.os.system.read( + inotify_fd, + @ptrCast([*]u8, @alignCast(@alignOf([*]u8), &eventlist)), + @sizeOf(EventListBuffer), + ); + switch (std.os.errno(rc)) { + .SUCCESS => { + const len = @intCast(usize, rc); - std.Thread.Futex.wait(&watch_count,0, null) catch unreachable; - const rc = std.os.system.read( - inotify_fd, - @ptrCast([*]u8, @alignCast(@alignOf([*]u8), &eventlist)), - @sizeOf(EventListBuffer), - ); - - switch (std.os.errno(rc)) { - .SUCCESS => { - const len = @intCast(usize, rc); + if (len == 0) return &[_]*INotifyEvent{}; - if (len == 0) return &[_]*INotifyEvent{}; + var count: u32 = 0; + var i: u32 = 0; + while (i < len) : (i += @sizeOf(INotifyEvent)) { + const event = @ptrCast(*const INotifyEvent, @alignCast(@alignOf(*const INotifyEvent), eventlist[i..][0..@sizeOf(INotifyEvent)])); + if (event.name_len > 0) { + i += event.name_len; + } - var count: u32 = 0; - var i: u32 = 0; - while (i < len) : (i += @sizeOf(INotifyEvent)) { - const event = @ptrCast(*const INotifyEvent, @alignCast(@alignOf(*const INotifyEvent), eventlist[i..][0..@sizeOf(INotifyEvent)])); - if (event.name_len > 0) { - i += event.name_len; + eventlist_ptrs[count] = event; + count += 1; } - eventlist_ptrs[count] = event; - count += 1; - } - - return eventlist_ptrs[0..count]; - }, - .AGAIN => continue :restart, - .INVAL => return error.ShortRead, - .BADF => return error.INotifyFailedToStart, - - else => unreachable, - } + return eventlist_ptrs[0..count]; + }, + .AGAIN => continue :restart, + .INVAL => return error.ShortRead, + .BADF => return error.INotifyFailedToStart, -} + else => unreachable, + } + } unreachable; } |