authorgravatar for alex_naskos@hotmail.comAlexandros Naskos <alex_naskos@hotmail.com> 2020-12-16 00:47:41+02:00
committergravatar for alex_naskos@hotmail.comAlexandros Naskos <alex_naskos@hotmail.com> 2020-12-16 00:47:41+02:00
logaa6654a4b3c64499ccb4b16e1f7257e7da14ef81
treed4e44efbc21538e21564ee781ab5f89b7c3dd987
parentda007f318b50e908d47fad8769667f5ed1264089
signature Commit is signed but in an unrecognized format.

Fix compilation for MacOS


1 files changed, 120 insertions(+), 89 deletions(-)

lib/std/fs/watch.zig+120-89
......@@ -47,10 +47,10 @@ pub fn Watch(comptime V: type) type {
4747 };
4848
4949 const KqOsData = struct {
50 file_table: FileTable,
5150 table_lock: event.Lock,
51 file_table: FileTable,
5252
53 const FileTable = std.StringHashMap(*Put);
53 const FileTable = std.StringHashMapUnmanaged(*Put);
5454 const Put = struct {
5555 putter_frame: @Frame(kqPutEvents),
5656 cancelled: bool = false,
......@@ -147,7 +147,7 @@ pub fn Watch(comptime V: type) type {
147147 .allocator = allocator,
148148 .channel = undefined,
149149 .os_data = OsData{
150 .table_lock = event.Lock.init(),
150 .table_lock = event.Lock{},
151151 .file_table = OsData.FileTable.init(allocator),
152152 },
153153 };
......@@ -160,22 +160,17 @@ pub fn Watch(comptime V: type) type {
160160 }
161161 }
162162
163 /// All addFile calls and removeFile calls must have completed.
164163 pub fn deinit(self: *Self) void {
165164 switch (builtin.os.tag) {
166165 .macos, .freebsd, .netbsd, .dragonfly, .openbsd => {
167 // TODO we need to cancel the frames before destroying the lock
168 self.os_data.table_lock.deinit();
169166 var it = self.os_data.file_table.iterator();
170167 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;
173171 self.allocator.free(entry.key);
174 self.allocator.free(entry.value);
172 self.allocator.destroy(entry.value);
175173 }
176 self.channel.deinit();
177 self.allocator.destroy(self.channel.buffer_nodes);
178 self.allocator.destroy(self);
179174 },
180175 .linux => {
181176 self.os_data.cancelled = true;
......@@ -189,9 +184,7 @@ pub fn Watch(comptime V: type) type {
189184 std.debug.assert(rc == 0);
190185 }
191186 }
192
193187 await self.os_data.putter_frame;
194 self.allocator.destroy(self);
195188 },
196189 .windows => {
197190 self.os_data.cancelled = true;
......@@ -218,12 +211,12 @@ pub fn Watch(comptime V: type) type {
218211 self.allocator.destroy(dir_entry.value);
219212 }
220213 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);
224214 },
225215 else => @compileError("Unsupported OS"),
226216 }
217 self.allocator.free(self.channel.buffer_nodes);
218 self.channel.deinit();
219 self.allocator.destroy(self);
227220 }
228221
229222 pub fn addFile(self: *Self, file_path: []const u8, value: V) !?V {
......@@ -236,91 +229,109 @@ pub fn Watch(comptime V: type) type {
236229 }
237230
238231 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);
246234
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();
251237
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;
263244 }
264245
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,
280253 };
281254
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;
283260 }
284261
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 {
286263 global_event_loop.beginOneEvent();
287
288264 defer {
289 close_op.finish();
290265 global_event_loop.finishOneEvent();
266 // @TODO: Remove this if we force close otherwise
267 os.close(fd);
291268 }
292269
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
293283 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 });
318329 }
319330 }
320331 }
321332
322333 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 ".";
324335 const basename = std.fs.path.basename(file_path);
325336
326337 const wd = try os.inotify_add_watch(
......@@ -334,6 +345,7 @@ pub fn Watch(comptime V: type) type {
334345 defer held.release();
335346
336347 const gop = try self.os_data.wd_table.getOrPut(self.allocator, wd);
348 errdefer self.os_data.wd_table.removeAssertDiscard(wd);
337349 if (!gop.found_existing) {
338350 gop.entry.value = OsData.Dir{
339351 .dirname = try self.allocator.dupe(u8, dirname),
......@@ -343,6 +355,7 @@ pub fn Watch(comptime V: type) type {
343355
344356 const dir = &gop.entry.value;
345357 const file_table_gop = try dir.file_table.getOrPut(self.allocator, basename);
358 errdefer dir.file_table.removeAssertDiscard(basename);
346359 if (file_table_gop.found_existing) {
347360 const prev_value = file_table_gop.entry.value;
348361 file_table_gop.entry.value = value;
......@@ -356,7 +369,7 @@ pub fn Watch(comptime V: type) type {
356369
357370 fn addFileWindows(self: *Self, file_path: []const u8, value: V) !?V {
358371 // 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 ".";
360373 var dirname_path_space: windows.PathSpace = undefined;
361374 dirname_path_space.len = try std.unicode.utf8ToUtf16Le(&dirname_path_space.data, dirname);
362375 dirname_path_space.data[dirname_path_space.len] = 0;
......@@ -370,10 +383,12 @@ pub fn Watch(comptime V: type) type {
370383 defer held.release();
371384
372385 const gop = try self.os_data.dir_table.getOrPut(self.allocator, dirname);
386 errdefer self.os_data.dir_table.removeAssertDiscard(dirname);
373387 if (gop.found_existing) {
374388 const dir = gop.entry.value;
375389
376390 const file_gop = try dir.file_table.getOrPut(self.allocator, basename);
391 errdefer dir.file_table.removeAssertDiscard(basename);
377392 if (file_gop.found_existing) {
378393 const prev_value = file_gop.entry.value;
379394 file_gop.entry.value = value;
......@@ -384,7 +399,6 @@ pub fn Watch(comptime V: type) type {
384399 return null;
385400 }
386401 } else {
387 errdefer _ = self.os_data.dir_table.remove(dirname);
388402 const dir_handle = try windows.OpenFile(dirname_path_space.span(), .{
389403 .dir = std.fs.cwd().fd,
390404 .access_mask = windows.FILE_LIST_DIRECTORY,
......@@ -501,10 +515,10 @@ pub fn Watch(comptime V: type) type {
501515 }
502516 }
503517
504 pub fn removeFile(self: *Self, file_path: []const u8) ?V {
518 pub fn removeFile(self: *Self, file_path: []const u8) !?V {
505519 switch (builtin.os.tag) {
506520 .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 ".";
508522 const basename = std.fs.path.basename(file_path);
509523
510524 const held = self.os_data.table_lock.acquire();
......@@ -518,7 +532,7 @@ pub fn Watch(comptime V: type) type {
518532 return null;
519533 },
520534 .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 ".";
522536 const basename = std.fs.path.basename(file_path);
523537
524538 const held = self.os_data.table_lock.acquire();
......@@ -531,7 +545,22 @@ pub fn Watch(comptime V: type) type {
531545 }
532546 return null;
533547 },
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 },
535564 else => @compileError("Unsupported OS"),
536565 }
537566 }
......@@ -685,3 +714,5 @@ fn testWriteWatchWriteDelete(allocator: *Allocator) !void {
685714 .CloseWrite => @panic("wrong event"),
686715 }
687716}
717
718// TODO Test: Add another file watch, remove the old file watch, get an event in the new