aboutsummaryrefslogtreecommitdiff
path: root/src/watcher.zig
blob: 04959b413e4c6d11ebd40c27383f10b2275e28dd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
const Fs = @import("./fs.zig");
const std = @import("std");
usingnamespace @import("global.zig");
const sync = @import("sync.zig");
const options = @import("./options.zig");

const os = std.os;
const KEvent = std.os.Kevent;

pub const WatchItem = struct {
    file_path: string,
    // filepath hash for quick comparison
    hash: u32,
    eventlist_index: u32,
    loader: options.Loader,
    fd: StoredFileDescriptorType,
    count: u32,
};

pub const WatchEvent = struct {
    index: u32,
    op: Op,

    pub fn fromKEvent(this: *WatchEvent, kevent: *const KEvent) void {
        this.op.delete = (kevent.fflags & std.os.NOTE_DELETE) > 0;
        this.op.metadata = (kevent.fflags & std.os.NOTE_ATTRIB) > 0;
        this.op.rename = (kevent.fflags & std.os.NOTE_RENAME) > 0;
        this.op.write = (kevent.fflags & std.os.NOTE_WRITE) > 0;
        this.index = @truncate(u32, kevent.udata);
    }

    pub const Op = packed struct {
        delete: bool = false,
        metadata: bool = false,
        rename: bool = false,
        write: bool = false,
    };
};

pub const Watchlist = std.MultiArrayList(WatchItem);

// This implementation only works on macOS, for now.
// The Internet seems to suggest basically always using FSEvents instead of kqueue
// It seems like the main concern is max open file descriptors
// Since we adjust the ulimit already, I think we can avoid that.
pub fn NewWatcher(comptime ContextType: type) type {
    return struct {
        const Watcher = @This();

        const KEventArrayList = std.ArrayList(KEvent);

        watchlist: Watchlist,
        watched_count: usize = 0,
        mutex: sync.Mutex,

        // Internal
        changelist: [128]KEvent = undefined,

        // User-facing
        watch_events: [128]WatchEvent = undefined,

        // Everything being watched
        eventlist: [8096]KEvent = undefined,
        eventlist_used: usize = 0,

        fs: *Fs.FileSystem,
        // this is what kqueue knows about
        fd: StoredFileDescriptorType,
        ctx: ContextType,
        allocator: *std.mem.Allocator,
        watchloop_handle: ?std.Thread.Id = null,
        cwd: string,

        pub const HashType = u32;

        pub fn getHash(filepath: string) HashType {
            return @truncate(HashType, std.hash.Wyhash.hash(0, filepath));
        }

        pub fn init(ctx: ContextType, fs: *Fs.FileSystem, allocator: *std.mem.Allocator) !*Watcher {
            var watcher = try allocator.create(Watcher);
            watcher.* = Watcher{
                .fs = fs,
                .fd = 0,
                .allocator = allocator,
                .watched_count = 0,
                .ctx = ctx,
                .watchlist = Watchlist{},
                .mutex = sync.Mutex.init(),
                .cwd = fs.top_level_dir,
            };

            return watcher;
        }

        pub fn getQueue(this: *Watcher) !StoredFileDescriptorType {
            if (this.fd == 0) {
                this.fd = try os.kqueue();
                if (this.fd == 0) {
                    return error.WatcherFailed;
                }
            }

            return this.fd;
        }

        pub fn start(this: *Watcher) !void {
            _ = try this.getQueue();
            std.debug.assert(this.watchloop_handle == null);
            _ = try std.Thread.spawn(.{}, Watcher.watchLoop, .{this});
        }

        // This must only be called from the watcher thread
        pub fn watchLoop(this: *Watcher) !void {
            this.watchloop_handle = std.Thread.getCurrentId();
            var stdout = std.io.getStdOut();
            var stderr = std.io.getStdErr();
            var output_source = Output.Source.init(stdout, stderr);
            Output.Source.set(&output_source);
            Output.enable_ansi_colors = stderr.isTty();

            defer Output.flush();
            if (FeatureFlags.verbose_watcher) Output.prettyln("Watcher started", .{});

            this._watchLoop() catch |err| {
                Output.prettyErrorln("<r>Watcher crashed: <red><b>{s}<r>", .{@errorName(err)});

                this.watchloop_handle = null;
                std.os.close(this.fd);
                this.fd = 0;
                return;
            };
        }

        fn _watchLoop(this: *Watcher) !void {
            const time = std.time;

            // poll at 1 second intervals if it hasn't received any events.
            // var timeout_spec = null;
            std.debug.assert(this.fd > 0);

            var changelist_array: [1]KEvent = std.mem.zeroes([1]KEvent);
            var changelist = &changelist_array;
            while (true) {
                defer Output.flush();
                var code = std.os.system.kevent(
                    try this.getQueue(),
                    @as([*]KEvent, changelist),
                    0,
                    @as([*]KEvent, changelist),
                    1,

                    null,
                );

                var watchevents = this.watch_events[0..1];
                for (changelist) |event, i| {
                    watchevents[i].fromKEvent(&event);
                }

                this.ctx.onFileUpdate(watchevents, this.watchlist);
            }
        }

        pub fn indexOf(this: *Watcher, hash: u32) ?usize {
            for (this.watchlist.items(.hash)) |other, i| {
                if (hash == other) {
                    return i;
                }
            }
            return null;
        }

        pub fn addFile(
            this: *Watcher,
            fd: StoredFileDescriptorType,
            file_path: string,
            hash: u32,
            loader: options.Loader,
            comptime copy_file_path: bool,
        ) !void {
            if (this.indexOf(hash) != null) {
                return;
            }

            try this.appendFile(fd, file_path, hash, loader, copy_file_path);
        }

        pub fn appendFile(
            this: *Watcher,
            fd: StoredFileDescriptorType,
            file_path: string,
            hash: u32,
            loader: options.Loader,
            comptime copy_file_path: bool,
        ) !void {
            try this.watchlist.ensureUnusedCapacity(this.allocator, 1);

            // https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/kqueue.2.html
            var event = std.mem.zeroes(KEvent);

            event.flags = os.EV_ADD | os.EV_CLEAR | os.EV_ENABLE;
            // we want to know about the vnode
            event.filter = std.os.EVFILT_VNODE;

            // monitor:
            // - Write
            // - Rename

            // we should monitor:
            // - Delete
            event.fflags = std.os.NOTE_WRITE | std.os.NOTE_RENAME;

            // id
            event.ident = @intCast(usize, fd);

            const index = this.eventlist_used;
            this.eventlist_used += 1;
            const watchlist_id = this.watchlist.len;
            // Store the hash for fast filtering later
            event.udata = @intCast(usize, watchlist_id);
            this.eventlist[index] = event;

            // This took a lot of work to figure out the right permutation
            // Basically:
            // - We register the event here.
            // our while(true) loop above receives notification of changes to any of the events created here.
            _ = std.os.system.kevent(
                try this.getQueue(),
                this.eventlist[index .. index + 1].ptr,
                1,
                this.eventlist[index .. index + 1].ptr,
                0,
                null,
            );

            this.watchlist.appendAssumeCapacity(.{
                .file_path = if (copy_file_path) try this.allocator.dupe(u8, file_path) else file_path,
                .fd = fd,
                .hash = hash,
                .count = 0,
                .eventlist_index = @truncate(u32, index),
                .loader = loader,
            });

            if (FeatureFlags.verbose_watcher) {
                if (strings.indexOf(file_path, this.cwd)) |i| {
                    Output.prettyln("<r><d>Added <b>./{s}<r><d> to watch list.<r>", .{file_path[i + this.cwd.len ..]});
                } else {
                    Output.prettyln("<r><d>Added <b>{s}<r><d> to watch list.<r>", .{file_path});
                }
            }
        }
    };
}