-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatch.lua
More file actions
422 lines (357 loc) · 10.9 KB
/
Copy pathwatch.lua
File metadata and controls
422 lines (357 loc) · 10.9 KB
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
-- shared/watch.lua - File watching using inotify
-- Provides chokidar-like file watching with event emitter
local ffi = require("ffi")
local bit = require("bit")
local log = require("shared.log")
local emitter = require("shared.emitter")
ffi.cdef[[
// inotify
int inotify_init(void);
int inotify_init1(int flags);
int inotify_add_watch(int fd, const char *pathname, uint32_t mask);
int inotify_rm_watch(int fd, int wd);
// File operations
int read(int fd, void *buf, size_t count);
int close(int fd);
int fcntl(int fd, int cmd, ...);
// Poll
struct pollfd {
int fd;
short events;
short revents;
};
int poll(struct pollfd *fds, unsigned long nfds, int timeout);
// Stat for directory detection
struct stat {
unsigned long st_dev;
unsigned long st_ino;
unsigned long st_nlink;
unsigned int st_mode;
unsigned int st_uid;
unsigned int st_gid;
unsigned int __pad0;
unsigned long st_rdev;
long st_size;
long st_blksize;
long st_blocks;
long st_atime;
long st_atime_nsec;
long st_mtime;
long st_mtime_nsec;
long st_ctime;
long st_ctime_nsec;
long __unused[3];
};
int stat(const char *pathname, struct stat *statbuf);
// Directory reading
typedef struct DIR DIR;
struct dirent {
unsigned long d_ino;
long d_off;
unsigned short d_reclen;
unsigned char d_type;
char d_name[256];
};
DIR *opendir(const char *name);
struct dirent *readdir(DIR *dirp);
int closedir(DIR *dirp);
]]
-- inotify event masks
local IN_ACCESS = 0x00000001
local IN_MODIFY = 0x00000002
local IN_ATTRIB = 0x00000004
local IN_CLOSE_WRITE = 0x00000008
local IN_CLOSE_NOWRITE = 0x00000010
local IN_OPEN = 0x00000020
local IN_MOVED_FROM = 0x00000040
local IN_MOVED_TO = 0x00000080
local IN_CREATE = 0x00000100
local IN_DELETE = 0x00000200
local IN_DELETE_SELF = 0x00000400
local IN_MOVE_SELF = 0x00000800
local IN_UNMOUNT = 0x00002000
local IN_Q_OVERFLOW = 0x00004000
local IN_IGNORED = 0x00008000
local IN_ISDIR = 0x40000000
-- fcntl commands
local F_GETFL = 3
local F_SETFL = 4
local O_NONBLOCK = 2048
-- poll events
local POLLIN = 0x0001
-- inotify_init1 flags
local IN_NONBLOCK = 2048
local IN_CLOEXEC = 524288
-- inotify event structure size
local INOTIFY_EVENT_SIZE = 16 -- sizeof(struct inotify_event) without name
-- S_IFDIR for directory detection
local S_IFDIR = 0x4000
local S_IFMT = 0xF000
local watch = {}
-- Watcher class
local Watcher = {}
Watcher.__index = Watcher
-- Create a new file watcher
-- opts:
-- persistent: keep running even if all watches are removed (default: true)
-- recursive: watch directories recursively (default: false)
-- ignored: patterns to ignore (array of strings or functions)
-- poll_interval: poll interval in ms for manual polling (default: 100)
function watch.new(opts)
opts = opts or {}
local fd = ffi.C.inotify_init1(IN_NONBLOCK + IN_CLOEXEC)
if fd < 0 then
log.error("Failed to initialize inotify")
return nil
end
local self = setmetatable({
_fd = fd,
_watches = {}, -- wd -> { path, recursive }
_path_to_wd = {}, -- path -> wd
_emitter = emitter.new(),
_persistent = opts.persistent ~= false,
_recursive = opts.recursive or false,
_ignored = opts.ignored or {},
_poll_interval = opts.poll_interval or 100,
_running = false,
_closed = false,
}, Watcher)
return self
end
-- Check if a path is a directory
local function is_directory(path)
local stat_buf = ffi.new("struct stat")
if ffi.C.stat(path, stat_buf) == 0 then
return bit.band(stat_buf.st_mode, S_IFMT) == S_IFDIR
end
return false
end
-- List directory contents
local function list_dir(path)
local entries = {}
local dir = ffi.C.opendir(path)
if dir == nil then
return entries
end
while true do
local entry = ffi.C.readdir(dir)
if entry == nil then
break
end
local name = ffi.string(entry.d_name)
if name ~= "." and name ~= ".." then
table.insert(entries, name)
end
end
ffi.C.closedir(dir)
return entries
end
-- Check if path should be ignored
function Watcher:_should_ignore(path)
for _, pattern in ipairs(self._ignored) do
if type(pattern) == "string" then
if path:match(pattern) then
return true
end
elseif type(pattern) == "function" then
if pattern(path) then
return true
end
end
end
return false
end
-- Add a watch for a path
function Watcher:add(path, opts)
opts = opts or {}
if self._closed then
log.warn("Watcher is closed")
return false
end
if self:_should_ignore(path) then
return false
end
-- Check if already watching
if self._path_to_wd[path] then
return true
end
local mask = IN_MODIFY + IN_CREATE + IN_DELETE + IN_MOVE_SELF +
IN_MOVED_FROM + IN_MOVED_TO + IN_ATTRIB + IN_DELETE_SELF
local wd = ffi.C.inotify_add_watch(self._fd, path, mask)
if wd < 0 then
log.warn("Failed to add watch for: %s", path)
return false
end
local recursive = opts.recursive
if recursive == nil then
recursive = self._recursive
end
self._watches[wd] = {
path = path,
recursive = recursive,
}
self._path_to_wd[path] = wd
self._emitter:emit("add", path)
-- If recursive and directory, add watches for subdirectories
if recursive and is_directory(path) then
local entries = list_dir(path)
for _, name in ipairs(entries) do
local subpath = path .. "/" .. name
if is_directory(subpath) then
self:add(subpath, { recursive = true })
end
end
end
return true
end
-- Remove a watch for a path
function Watcher:unwatch(path)
if self._closed then
return false
end
local wd = self._path_to_wd[path]
if not wd then
return false
end
ffi.C.inotify_rm_watch(self._fd, wd)
self._watches[wd] = nil
self._path_to_wd[path] = nil
self._emitter:emit("unwatch", path)
return true
end
-- Register event listener
-- Events: "change", "add", "unlink", "addDir", "unlinkDir", "error", "ready"
function Watcher:on(event, callback)
self._emitter:on(event, callback)
return self
end
-- Remove event listener
function Watcher:off(event, callback)
self._emitter:off(event, callback)
return self
end
-- Map inotify events to watcher events
local function get_event_type(mask, is_dir)
if bit.band(mask, IN_CREATE) ~= 0 then
return is_dir and "addDir" or "add"
elseif bit.band(mask, IN_DELETE) ~= 0 or bit.band(mask, IN_MOVED_FROM) ~= 0 then
return is_dir and "unlinkDir" or "unlink"
elseif bit.band(mask, IN_MODIFY) ~= 0 then
return "change"
elseif bit.band(mask, IN_MOVED_TO) ~= 0 then
return is_dir and "addDir" or "add"
elseif bit.band(mask, IN_ATTRIB) ~= 0 then
return "change"
elseif bit.band(mask, IN_DELETE_SELF) ~= 0 or bit.band(mask, IN_MOVE_SELF) ~= 0 then
return is_dir and "unlinkDir" or "unlink"
end
return nil
end
-- Process pending events (non-blocking)
function Watcher:poll()
if self._closed then
return
end
local buf_size = 4096
local buf = ffi.new("char[?]", buf_size)
while true do
local len = ffi.C.read(self._fd, buf, buf_size)
if len <= 0 then
break
end
local offset = 0
while offset < len do
-- Parse inotify_event structure
local wd = ffi.cast("int*", buf + offset)[0]
local mask = ffi.cast("uint32_t*", buf + offset + 4)[0]
-- cookie at offset + 8 (unused, for rename tracking)
local name_len = ffi.cast("uint32_t*", buf + offset + 12)[0]
local name = ""
if name_len > 0 then
name = ffi.string(buf + offset + INOTIFY_EVENT_SIZE)
end
local watch_info = self._watches[wd]
if watch_info then
local full_path = watch_info.path
if name ~= "" then
full_path = watch_info.path .. "/" .. name
end
local is_dir = bit.band(mask, IN_ISDIR) ~= 0
local event_type = get_event_type(mask, is_dir)
if event_type and not self:_should_ignore(full_path) then
self._emitter:emit(event_type, full_path)
-- Auto-add watches for new directories if recursive
if event_type == "addDir" and watch_info.recursive then
self:add(full_path, { recursive = true })
end
-- Clean up watches for deleted directories
if event_type == "unlinkDir" then
self:unwatch(full_path)
end
end
-- Handle watch removal
if bit.band(mask, IN_IGNORED) ~= 0 then
self._watches[wd] = nil
self._path_to_wd[watch_info.path] = nil
end
end
offset = offset + INOTIFY_EVENT_SIZE + name_len
end
end
end
-- Check if there are pending events (for use in external event loops)
function Watcher:has_events()
if self._closed then
return false
end
local pfd = ffi.new("struct pollfd[1]")
pfd[0].fd = self._fd
pfd[0].events = POLLIN
pfd[0].revents = 0
local ret = ffi.C.poll(pfd, 1, 0)
return ret > 0 and bit.band(pfd[0].revents, POLLIN) ~= 0
end
-- Get the file descriptor (for integration with external event loops)
function Watcher:get_fd()
return self._fd
end
-- Close the watcher
function Watcher:close()
if self._closed then
return
end
self._running = false
self._closed = true
-- Remove all watches
for wd, _ in pairs(self._watches) do
ffi.C.inotify_rm_watch(self._fd, wd)
end
self._watches = {}
self._path_to_wd = {}
ffi.C.close(self._fd)
self._emitter:emit("close")
end
-- Convenience function to watch a single path
-- Returns a watcher configured for that path
function watch.watch(path, opts)
opts = opts or {}
local w = watch.new(opts)
if w then
w:add(path, opts)
w._emitter:emit("ready")
end
return w
end
-- Convenience function to watch multiple paths
function watch.watch_many(paths, opts)
opts = opts or {}
local w = watch.new(opts)
if w then
for _, path in ipairs(paths) do
w:add(path, opts)
end
w._emitter:emit("ready")
end
return w
end
return watch