aboutsummaryrefslogtreecommitdiff
path: root/src/watcher.zig
blob: 9948a01da5ae1adaf51cf32f12448d5935e24848 (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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
const Fs = @import("./fs.zig");
const std = @import("std");
usingnamespace @import("global.zig");
const options = @import("./options.zig");
const IndexType = @import("./allocators.zig").IndexType;

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

const Mutex = @import("./lock.zig").Lock;
const WatchItemIndex = u16;
const NoWatchItem: WatchItemIndex = std.math.maxInt(WatchItemIndex);
pub const WatchItem = struct {
    file_path: string,
    // filepath hash for quick comparison
    hash: u32,
    eventlist_index: u32,
    loader: options.Loader,
    fd: StoredFileDescriptorType,
    count: u32,
    parent_hash: u32,
    kind: Kind,

    pub const Kind = enum { file, directory };
};

pub const WatchEvent = struct {
    index: WatchItemIndex,
    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(WatchItemIndex, 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);
        const WATCHER_MAX_LIST = 8096;

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

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

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

        // Everything being watched
        eventlist: [WATCHER_MAX_LIST]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;

        var evict_list: [WATCHER_MAX_LIST]WatchItemIndex = undefined;

        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 = 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);
            var thread = try std.Thread.spawn(.{}, Watcher.watchLoop, .{this});
            thread.setName("File Watcher") catch {};
        }

        // 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);

            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;
            };
        }

        var evict_list_i: WatchItemIndex = 0;
        pub fn removeAtIndex(this: *Watcher, index: WatchItemIndex, hash: HashType, parents: []HashType, comptime kind: WatchItem.Kind) void {
            std.debug.assert(index != NoWatchItem);

            evict_list[evict_list_i] = index;
            evict_list_i += 1;

            if (comptime kind == .directory) {
                for (parents) |parent, i| {
                    if (parent == hash) {
                        evict_list[evict_list_i] = @truncate(WatchItemIndex, parent);
                        evict_list_i += 1;
                    }
                }
            }
        }

        pub fn flushEvictions(this: *Watcher) void {
            if (evict_list_i == 0) return;
            this.mutex.lock();
            defer this.mutex.unlock();
            defer evict_list_i = 0;

            // swapRemove messes up the order
            // But, it only messes up the order if any elements in the list appear after the item being removed
            // So if we just sort the list by the biggest index first, that should be fine
            std.sort.sort(
                WatchItemIndex,
                evict_list[0..evict_list_i],
                {},
                comptime std.sort.desc(WatchItemIndex),
            );

            var slice = this.watchlist.slice();
            var fds = slice.items(.fd);
            var last_item = NoWatchItem;

            for (evict_list[0..evict_list_i]) |item, i| {
                // catch duplicates, since the list is sorted, duplicates will appear right after each other
                if (item == last_item) continue;
                // close the file descriptors here. this should automatically remove it from being watched too.
                std.os.close(fds[item]);
                last_item = item;
            }

            last_item = NoWatchItem;
            // This is split into two passes because reading the slice while modified is potentially unsafe.
            for (evict_list[0..evict_list_i]) |item, i| {
                if (item == last_item) continue;
                this.watchlist.swapRemove(item);
                last_item = item;
            }
        }

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

            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: HashType) ?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: HashType,
            loader: options.Loader,
            dir_fd: StoredFileDescriptorType,
            comptime copy_file_path: bool,
        ) !void {
            if (this.indexOf(hash) != null) {
                return;
            }

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

        fn appendFileAssumeCapacity(
            this: *Watcher,
            fd: StoredFileDescriptorType,
            file_path: string,
            hash: HashType,
            loader: options.Loader,
            parent_hash: HashType,
            comptime copy_file_path: bool,
        ) !void {
            const index = this.eventlist_used;
            const watchlist_id = this.watchlist.len;

            if (isMac) {

                // 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 | std.os.NOTE_DELETE;

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

                this.eventlist_used += 1;

                // 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,
                .parent_hash = parent_hash,
                .kind = .file,
            });
        }

        fn appendDirectoryAssumeCapacity(
            this: *Watcher,
            fd_: StoredFileDescriptorType,
            file_path: string,
            hash: HashType,
            comptime copy_file_path: bool,
        ) !WatchItemIndex {
            const fd = brk: {
                if (fd_ > 0) break :brk fd_;

                const dir = try std.fs.openDirAbsolute(file_path, .{ .iterate = true });
                break :brk @truncate(StoredFileDescriptorType, dir.fd);
            };

            const parent_hash = Watcher.getHash(Fs.PathName.init(file_path).dirWithTrailingSlash());
            const index = this.eventlist_used;
            const watchlist_id = this.watchlist.len;

            // 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
            // - Delete
            event.fflags = std.os.NOTE_WRITE | std.os.NOTE_RENAME | std.os.NOTE_DELETE;

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

            this.eventlist_used += 1;
            // 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 = options.Loader.file,
                .parent_hash = parent_hash,
                .kind = .directory,
            });
            return @truncate(WatchItemIndex, this.watchlist.len - 1);
        }

        pub inline fn isEligibleDirectory(this: *Watcher, dir: string) bool {
            return strings.indexOf(dir, this.fs.top_level_dir) != null and strings.indexOf(dir, "node_modules") == null;
        }

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

            this.mutex.lock();
            defer this.mutex.unlock();

            try this.watchlist.ensureUnusedCapacity(this.allocator, 1);

            _ = try this.appendDirectoryAssumeCapacity(fd, file_path, hash, copy_file_path);
        }

        pub fn appendFile(
            this: *Watcher,
            fd: StoredFileDescriptorType,
            file_path: string,
            hash: HashType,
            loader: options.Loader,
            dir_fd: StoredFileDescriptorType,
            comptime copy_file_path: bool,
        ) !void {
            this.mutex.lock();
            defer this.mutex.unlock();
            std.debug.assert(file_path.len > 1);
            const pathname = Fs.PathName.init(file_path);

            const parent_dir = pathname.dirWithTrailingSlash();
            var parent_dir_hash: HashType = Watcher.getHash(parent_dir);

            var parent_watch_item: ?WatchItemIndex = null;
            const autowatch_parent_dir = (comptime FeatureFlags.watch_directories) and this.isEligibleDirectory(parent_dir);
            if (autowatch_parent_dir) {
                var watchlist_slice = this.watchlist.slice();

                if (dir_fd > 0) {
                    var fds = watchlist_slice.items(.fd);
                    if (std.mem.indexOfScalar(StoredFileDescriptorType, fds, dir_fd)) |i| {
                        parent_watch_item = @truncate(WatchItemIndex, i);
                    }
                }

                if (parent_watch_item == null) {
                    const hashes = watchlist_slice.items(.hash);
                    if (std.mem.indexOfScalar(HashType, hashes, parent_dir_hash)) |i| {
                        parent_watch_item = @truncate(WatchItemIndex, i);
                    }
                }
            }
            try this.watchlist.ensureUnusedCapacity(this.allocator, 1 + @intCast(usize, @boolToInt(parent_watch_item == null)));

            if (autowatch_parent_dir) {
                parent_watch_item = parent_watch_item orelse try this.appendDirectoryAssumeCapacity(dir_fd, parent_dir, parent_dir_hash, copy_file_path);
            }

            try this.appendFileAssumeCapacity(
                fd,
                file_path,
                hash,
                loader,
                parent_dir_hash,
                copy_file_path,
            );

            if (comptime 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});
                }
            }
        }
    };
}