| ... | ... | @@ -47,10 +47,10 @@ pub fn Watch(comptime V: type) type { |
| 47 | 47 | }; |
| 48 | 48 | |
| 49 | 49 | const KqOsData = struct { |
| 50 | | file_table: FileTable, |
| 51 | 50 | table_lock: event.Lock, |
| 51 | file_table: FileTable, |
| 52 | 52 | |
| 53 | | const FileTable = std.StringHashMap(*Put); |
| 53 | const FileTable = std.StringHashMapUnmanaged(*Put); |
| 54 | 54 | const Put = struct { |
| 55 | 55 | putter_frame: @Frame(kqPutEvents), |
| 56 | 56 | cancelled: bool = false, |
| ... | ... | @@ -147,7 +147,7 @@ pub fn Watch(comptime V: type) type { |
| 147 | 147 | .allocator = allocator, |
| 148 | 148 | .channel = undefined, |
| 149 | 149 | .os_data = OsData{ |
| 150 | | .table_lock = event.Lock.init(), |
| 150 | .table_lock = event.Lock{}, |
| 151 | 151 | .file_table = OsData.FileTable.init(allocator), |
| 152 | 152 | }, |
| 153 | 153 | }; |
| ... | ... | @@ -160,22 +160,17 @@ pub fn Watch(comptime V: type) type { |
| 160 | 160 | } |
| 161 | 161 | } |
| 162 | 162 | |
| 163 | | /// All addFile calls and removeFile calls must have completed. |
| 164 | 163 | pub fn deinit(self: *Self) void { |
| 165 | 164 | switch (builtin.os.tag) { |
| 166 | 165 | .macos, .freebsd, .netbsd, .dragonfly, .openbsd => { |
| 167 | | // TODO we need to cancel the frames before destroying the lock |
| 168 | | self.os_data.table_lock.deinit(); |
| 169 | 166 | var it = self.os_data.file_table.iterator(); |
| 170 | 167 | while (it.next()) |entry| { |
| 171 | | entry.cancelled = true; |
| 172 | | await entry.value.putter; |
| 168 | entry.value.cancelled = true; |
| 169 | // @TODO Close the fd here? |
| 170 | await entry.value.putter_frame; |
| 173 | 171 | self.allocator.free(entry.key); |
| 174 | | self.allocator.free(entry.value); |
| 172 | self.allocator.destroy(entry.value); |
| 175 | 173 | } |
| 176 | | self.channel.deinit(); |
| 177 | | self.allocator.destroy(self.channel.buffer_nodes); |
| 178 | | self.allocator.destroy(self); |
| 179 | 174 | }, |
| 180 | 175 | .linux => { |
| 181 | 176 | self.os_data.cancelled = true; |
| ... | ... | @@ -189,9 +184,7 @@ pub fn Watch(comptime V: type) type { |
| 189 | 184 | std.debug.assert(rc == 0); |
| 190 | 185 | } |
| 191 | 186 | } |
| 192 | | |
| 193 | 187 | await self.os_data.putter_frame; |
| 194 | | self.allocator.destroy(self); |
| 195 | 188 | }, |
| 196 | 189 | .windows => { |
| 197 | 190 | self.os_data.cancelled = true; |
| ... | ... | @@ -218,12 +211,12 @@ pub fn Watch(comptime V: type) type { |
| 218 | 211 | self.allocator.destroy(dir_entry.value); |
| 219 | 212 | } |
| 220 | 213 | self.os_data.dir_table.deinit(self.allocator); |
| 221 | | self.allocator.free(self.channel.buffer_nodes); |
| 222 | | self.channel.deinit(); |
| 223 | | self.allocator.destroy(self); |
| 224 | 214 | }, |
| 225 | 215 | else => @compileError("Unsupported OS"), |
| 226 | 216 | } |
| 217 | self.allocator.free(self.channel.buffer_nodes); |
| 218 | self.channel.deinit(); |
| 219 | self.allocator.destroy(self); |
| 227 | 220 | } |
| 228 | 221 | |
| 229 | 222 | pub fn addFile(self: *Self, file_path: []const u8, value: V) !?V { |
| ... | ... | @@ -236,91 +229,109 @@ pub fn Watch(comptime V: type) type { |
| 236 | 229 | } |
| 237 | 230 | |
| 238 | 231 | fn addFileKEvent(self: *Self, file_path: []const u8, value: V) !?V { |
| 239 | | const resolved_path = try std.fs.path.resolve(self.allocator, [_][]const u8{file_path}); |
| 240 | | var resolved_path_consumed = false; |
| 241 | | defer if (!resolved_path_consumed) self.allocator.free(resolved_path); |
| 242 | | |
| 243 | | var close_op = try CloseOperation.start(self.allocator); |
| 244 | | var close_op_consumed = false; |
| 245 | | defer if (!close_op_consumed) close_op.finish(); |
| 232 | var realpath_buf: [std.fs.MAX_PATH_BYTES]u8 = undefined; |
| 233 | const realpath = try os.realpath(file_path, &realpath_buf); |
| 246 | 234 | |
| 247 | | const flags = if (comptime std.Target.current.isDarwin()) os.O_SYMLINK | os.O_EVTONLY else 0; |
| 248 | | const mode = 0; |
| 249 | | const fd = try openPosix(self.allocator, resolved_path, flags, mode); |
| 250 | | close_op.setHandle(fd); |
| 235 | const held = self.os_data.table_lock.acquire(); |
| 236 | defer held.release(); |
| 251 | 237 | |
| 252 | | var put = try self.allocator.create(OsData.Put); |
| 253 | | errdefer self.allocator.destroy(put); |
| 254 | | put.* = OsData.Put{ |
| 255 | | .value = value, |
| 256 | | .putter_frame = undefined, |
| 257 | | }; |
| 258 | | put.putter_frame = async self.kqPutEvents(close_op, put); |
| 259 | | close_op_consumed = true; |
| 260 | | errdefer { |
| 261 | | put.cancelled = true; |
| 262 | | await put.putter_frame; |
| 238 | const gop = try self.os_data.file_table.getOrPut(self.allocator, realpath); |
| 239 | errdefer self.os_data.file_table.removeAssertDiscard(realpath); |
| 240 | if (gop.found_existing) { |
| 241 | const prev_value = gop.entry.value.value; |
| 242 | gop.entry.value.value = value; |
| 243 | return prev_value; |
| 263 | 244 | } |
| 264 | 245 | |
| 265 | | const result = blk: { |
| 266 | | const held = self.os_data.table_lock.acquire(); |
| 267 | | defer held.release(); |
| 268 | | |
| 269 | | const gop = try self.os_data.file_table.getOrPut(resolved_path); |
| 270 | | if (gop.found_existing) { |
| 271 | | const prev_value = gop.kv.value.value; |
| 272 | | await gop.kv.value.putter_frame; |
| 273 | | gop.kv.value = put; |
| 274 | | break :blk prev_value; |
| 275 | | } else { |
| 276 | | resolved_path_consumed = true; |
| 277 | | gop.kv.value = put; |
| 278 | | break :blk null; |
| 279 | | } |
| 246 | gop.entry.key = try self.allocator.dupe(u8, realpath); |
| 247 | errdefer self.allocator.free(gop.entry.key); |
| 248 | gop.entry.value = try self.allocator.create(OsData.Put); |
| 249 | errdefer self.allocator.destroy(gop.entry.value); |
| 250 | gop.entry.value.* = .{ |
| 251 | .putter_frame = undefined, |
| 252 | .value = value, |
| 280 | 253 | }; |
| 281 | 254 | |
| 282 | | return result; |
| 255 | // @TODO Can I close this fd and get an error from bsdWaitKev? |
| 256 | const flags = if (comptime std.Target.current.isDarwin()) os.O_SYMLINK | os.O_EVTONLY else 0; |
| 257 | const fd = try os.open(realpath, flags, 0); |
| 258 | gop.entry.value.putter_frame = async self.kqPutEvents(fd, gop.entry.key, gop.entry.value); |
| 259 | return null; |
| 283 | 260 | } |
| 284 | 261 | |
| 285 | | fn kqPutEvents(self: *Self, close_op: *CloseOperation, put: *OsData.Put) void { |
| 262 | fn kqPutEvents(self: *Self, fd: os.fd_t, file_path: []const u8, put: *OsData.Put) void { |
| 286 | 263 | global_event_loop.beginOneEvent(); |
| 287 | | |
| 288 | 264 | defer { |
| 289 | | close_op.finish(); |
| 290 | 265 | global_event_loop.finishOneEvent(); |
| 266 | // @TODO: Remove this if we force close otherwise |
| 267 | os.close(fd); |
| 291 | 268 | } |
| 292 | 269 | |
| 270 | // We need to manually do a bsdWaitKev to access the fflags. |
| 271 | var resume_node = event.Loop.ResumeNode.Basic{ |
| 272 | .base = .{ |
| 273 | .id = .Basic, |
| 274 | .handle = @frame(), |
| 275 | .overlapped = event.Loop.ResumeNode.overlapped_init, |
| 276 | }, |
| 277 | .kev = undefined, |
| 278 | }; |
| 279 | |
| 280 | var kevs = [1]os.Kevent{undefined}; |
| 281 | const kev = &kevs[0]; |
| 282 | |
| 293 | 283 | while (!put.cancelled) { |
| 294 | | if (global_event_loop.bsdWaitKev( |
| 295 | | @intCast(usize, close_op.getHandle()), |
| 296 | | os.EVFILT_VNODE, |
| 297 | | os.NOTE_WRITE | os.NOTE_DELETE, |
| 298 | | )) |kev| { |
| 299 | | // TODO handle EV_ERROR |
| 300 | | if (kev.fflags & os.NOTE_DELETE != 0) { |
| 301 | | self.channel.put(Self.Event{ |
| 302 | | .id = Event.Id.Delete, |
| 303 | | .data = put.value, |
| 304 | | }); |
| 305 | | } else if (kev.fflags & os.NOTE_WRITE != 0) { |
| 306 | | self.channel.put(Self.Event{ |
| 307 | | .id = Event.Id.CloseWrite, |
| 308 | | .data = put.value, |
| 309 | | }); |
| 310 | | } |
| 311 | | } else |err| switch (err) { |
| 312 | | error.EventNotFound => unreachable, |
| 313 | | error.ProcessNotFound => unreachable, |
| 314 | | error.Overflow => unreachable, |
| 315 | | error.AccessDenied, error.SystemResources => |casted_err| { |
| 316 | | self.channel.put(casted_err); |
| 317 | | }, |
| 284 | kev.* = os.Kevent{ |
| 285 | .ident = @intCast(usize, fd), |
| 286 | .filter = os.EVFILT_VNODE, |
| 287 | .flags = os.EV_ADD | os.EV_ENABLE | os.EV_CLEAR | os.EV_ONESHOT | |
| 288 | os.NOTE_WRITE | os.NOTE_DELETE | os.NOTE_REVOKE, |
| 289 | .fflags = 0, |
| 290 | .data = 0, |
| 291 | .udata = @ptrToInt(&resume_node.base), |
| 292 | }; |
| 293 | suspend { |
| 294 | global_event_loop.beginOneEvent(); |
| 295 | errdefer global_event_loop.finishOneEvent(); |
| 296 | |
| 297 | const empty_kevs = &[0]os.Kevent{}; |
| 298 | _ = os.kevent(global_event_loop.os_data.kqfd, &kevs, empty_kevs, null) catch |err| switch (err) { |
| 299 | error.EventNotFound, |
| 300 | error.ProcessNotFound, |
| 301 | error.Overflow, |
| 302 | => unreachable, |
| 303 | error.AccessDenied, error.SystemResources => |e| { |
| 304 | self.channel.put(e); |
| 305 | continue; |
| 306 | }, |
| 307 | }; |
| 308 | } |
| 309 | |
| 310 | if (kev.flags & os.EV_ERROR != 0) { |
| 311 | self.channel.put(os.unexpectedErrno(os.errno(kev.data))); |
| 312 | continue; |
| 313 | } |
| 314 | |
| 315 | if (kev.fflags & os.NOTE_DELETE != 0 or kev.fflags & os.NOTE_REVOKE != 0) { |
| 316 | self.channel.put(Self.Event{ |
| 317 | .id = .Delete, |
| 318 | .data = put.value, |
| 319 | .dirname = std.fs.path.dirname(file_path) orelse "/", |
| 320 | .basename = std.fs.path.basename(file_path), |
| 321 | }); |
| 322 | } else if (kev.fflags & os.NOTE_WRITE != 0) { |
| 323 | self.channel.put(Self.Event{ |
| 324 | .id = .CloseWrite, |
| 325 | .data = put.value, |
| 326 | .dirname = std.fs.path.dirname(file_path) orelse "/", |
| 327 | .basename = std.fs.path.basename(file_path), |
| 328 | }); |
| 318 | 329 | } |
| 319 | 330 | } |
| 320 | 331 | } |
| 321 | 332 | |
| 322 | 333 | fn addFileLinux(self: *Self, file_path: []const u8, value: V) !?V { |
| 323 | | const dirname = std.fs.path.dirname(file_path) orelse "."; |
| 334 | const dirname = std.fs.path.dirname(file_path) orelse if (file_path[0] == '/') "/" else "."; |
| 324 | 335 | const basename = std.fs.path.basename(file_path); |
| 325 | 336 | |
| 326 | 337 | const wd = try os.inotify_add_watch( |
| ... | ... | @@ -334,6 +345,7 @@ pub fn Watch(comptime V: type) type { |
| 334 | 345 | defer held.release(); |
| 335 | 346 | |
| 336 | 347 | const gop = try self.os_data.wd_table.getOrPut(self.allocator, wd); |
| 348 | errdefer self.os_data.wd_table.removeAssertDiscard(wd); |
| 337 | 349 | if (!gop.found_existing) { |
| 338 | 350 | gop.entry.value = OsData.Dir{ |
| 339 | 351 | .dirname = try self.allocator.dupe(u8, dirname), |
| ... | ... | @@ -343,6 +355,7 @@ pub fn Watch(comptime V: type) type { |
| 343 | 355 | |
| 344 | 356 | const dir = &gop.entry.value; |
| 345 | 357 | const file_table_gop = try dir.file_table.getOrPut(self.allocator, basename); |
| 358 | errdefer dir.file_table.removeAssertDiscard(basename); |
| 346 | 359 | if (file_table_gop.found_existing) { |
| 347 | 360 | const prev_value = file_table_gop.entry.value; |
| 348 | 361 | file_table_gop.entry.value = value; |
| ... | ... | @@ -356,7 +369,7 @@ pub fn Watch(comptime V: type) type { |
| 356 | 369 | |
| 357 | 370 | fn addFileWindows(self: *Self, file_path: []const u8, value: V) !?V { |
| 358 | 371 | // TODO we might need to convert dirname and basename to canonical file paths ("short"?) |
| 359 | | const dirname = std.fs.path.dirname(file_path) orelse "."; |
| 372 | const dirname = std.fs.path.dirname(file_path) orelse if (file_path[0] == '/') "/" else "."; |
| 360 | 373 | var dirname_path_space: windows.PathSpace = undefined; |
| 361 | 374 | dirname_path_space.len = try std.unicode.utf8ToUtf16Le(&dirname_path_space.data, dirname); |
| 362 | 375 | dirname_path_space.data[dirname_path_space.len] = 0; |
| ... | ... | @@ -370,10 +383,12 @@ pub fn Watch(comptime V: type) type { |
| 370 | 383 | defer held.release(); |
| 371 | 384 | |
| 372 | 385 | const gop = try self.os_data.dir_table.getOrPut(self.allocator, dirname); |
| 386 | errdefer self.os_data.dir_table.removeAssertDiscard(dirname); |
| 373 | 387 | if (gop.found_existing) { |
| 374 | 388 | const dir = gop.entry.value; |
| 375 | 389 | |
| 376 | 390 | const file_gop = try dir.file_table.getOrPut(self.allocator, basename); |
| 391 | errdefer dir.file_table.removeAssertDiscard(basename); |
| 377 | 392 | if (file_gop.found_existing) { |
| 378 | 393 | const prev_value = file_gop.entry.value; |
| 379 | 394 | file_gop.entry.value = value; |
| ... | ... | @@ -384,7 +399,6 @@ pub fn Watch(comptime V: type) type { |
| 384 | 399 | return null; |
| 385 | 400 | } |
| 386 | 401 | } else { |
| 387 | | errdefer _ = self.os_data.dir_table.remove(dirname); |
| 388 | 402 | const dir_handle = try windows.OpenFile(dirname_path_space.span(), .{ |
| 389 | 403 | .dir = std.fs.cwd().fd, |
| 390 | 404 | .access_mask = windows.FILE_LIST_DIRECTORY, |
| ... | ... | @@ -501,10 +515,10 @@ pub fn Watch(comptime V: type) type { |
| 501 | 515 | } |
| 502 | 516 | } |
| 503 | 517 | |
| 504 | | pub fn removeFile(self: *Self, file_path: []const u8) ?V { |
| 518 | pub fn removeFile(self: *Self, file_path: []const u8) !?V { |
| 505 | 519 | switch (builtin.os.tag) { |
| 506 | 520 | .linux => { |
| 507 | | const dirname = std.fs.path.dirname(file_path) orelse "."; |
| 521 | const dirname = std.fs.path.dirname(file_path) orelse if (file_path[0] == '/') "/" else "."; |
| 508 | 522 | const basename = std.fs.path.basename(file_path); |
| 509 | 523 | |
| 510 | 524 | const held = self.os_data.table_lock.acquire(); |
| ... | ... | @@ -518,7 +532,7 @@ pub fn Watch(comptime V: type) type { |
| 518 | 532 | return null; |
| 519 | 533 | }, |
| 520 | 534 | .windows => { |
| 521 | | const dirname = std.fs.path.dirname(file_path) orelse "."; |
| 535 | const dirname = std.fs.path.dirname(file_path) orelse if (file_path[0] == '/') "/" else "."; |
| 522 | 536 | const basename = std.fs.path.basename(file_path); |
| 523 | 537 | |
| 524 | 538 | const held = self.os_data.table_lock.acquire(); |
| ... | ... | @@ -531,7 +545,22 @@ pub fn Watch(comptime V: type) type { |
| 531 | 545 | } |
| 532 | 546 | return null; |
| 533 | 547 | }, |
| 534 | | .macos, .freebsd, .netbsd, .dragonfly, .openbsd => @panic("TODO"), |
| 548 | .macos, .freebsd, .netbsd, .dragonfly, .openbsd => { |
| 549 | var realpath_buf: [std.fs.MAX_PATH_BYTES]u8 = undefined; |
| 550 | const realpath = try os.realpath(file_path, &realpath_buf); |
| 551 | |
| 552 | const held = self.os_data.table_lock.acquire(); |
| 553 | defer held.release(); |
| 554 | |
| 555 | const entry = self.os_data.file_table.get(realpath) orelse return null; |
| 556 | entry.value.cancelled = true; |
| 557 | // @TODO Close the fd here? |
| 558 | await entry.value.putter_frame; |
| 559 | self.allocator.free(entry.key); |
| 560 | self.allocator.destroy(entry.value); |
| 561 | |
| 562 | self.os_data.file_table.removeAssertDiscard(realpath); |
| 563 | }, |
| 535 | 564 | else => @compileError("Unsupported OS"), |
| 536 | 565 | } |
| 537 | 566 | } |
| ... | ... | @@ -685,3 +714,5 @@ fn testWriteWatchWriteDelete(allocator: *Allocator) !void { |
| 685 | 714 | .CloseWrite => @panic("wrong event"), |
| 686 | 715 | } |
| 687 | 716 | } |
| 717 | |
| 718 | // TODO Test: Add another file watch, remove the old file watch, get an event in the new |