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
signaturelock-open 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 {...@@ -47,10 +47,10 @@ pub fn Watch(comptime V: type) type {
47 };47 };
4848
49 const KqOsData = struct {49 const KqOsData = struct {
50 file_table: FileTable,
51 table_lock: event.Lock,50 table_lock: event.Lock,
51 file_table: FileTable,
5252
53 const FileTable = std.StringHashMap(*Put);53 const FileTable = std.StringHashMapUnmanaged(*Put);
54 const Put = struct {54 const Put = struct {
55 putter_frame: @Frame(kqPutEvents),55 putter_frame: @Frame(kqPutEvents),
56 cancelled: bool = false,56 cancelled: bool = false,
...@@ -147,7 +147,7 @@ pub fn Watch(comptime V: type) type {...@@ -147,7 +147,7 @@ pub fn Watch(comptime V: type) type {
147 .allocator = allocator,147 .allocator = allocator,
148 .channel = undefined,148 .channel = undefined,
149 .os_data = OsData{149 .os_data = OsData{
150 .table_lock = event.Lock.init(),150 .table_lock = event.Lock{},
151 .file_table = OsData.FileTable.init(allocator),151 .file_table = OsData.FileTable.init(allocator),
152 },152 },
153 };153 };
...@@ -160,22 +160,17 @@ pub fn Watch(comptime V: type) type {...@@ -160,22 +160,17 @@ pub fn Watch(comptime V: type) type {
160 }160 }
161 }161 }
162162
163 /// All addFile calls and removeFile calls must have completed.
164 pub fn deinit(self: *Self) void {163 pub fn deinit(self: *Self) void {
165 switch (builtin.os.tag) {164 switch (builtin.os.tag) {
166 .macos, .freebsd, .netbsd, .dragonfly, .openbsd => {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 var it = self.os_data.file_table.iterator();166 var it = self.os_data.file_table.iterator();
170 while (it.next()) |entry| {167 while (it.next()) |entry| {
171 entry.cancelled = true;168 entry.value.cancelled = true;
172 await entry.value.putter;169 // @TODO Close the fd here?
170 await entry.value.putter_frame;
173 self.allocator.free(entry.key);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 .linux => {175 .linux => {
181 self.os_data.cancelled = true;176 self.os_data.cancelled = true;
...@@ -189,9 +184,7 @@ pub fn Watch(comptime V: type) type {...@@ -189,9 +184,7 @@ pub fn Watch(comptime V: type) type {
189 std.debug.assert(rc == 0);184 std.debug.assert(rc == 0);
190 }185 }
191 }186 }
192
193 await self.os_data.putter_frame;187 await self.os_data.putter_frame;
194 self.allocator.destroy(self);
195 },188 },
196 .windows => {189 .windows => {
197 self.os_data.cancelled = true;190 self.os_data.cancelled = true;
...@@ -218,12 +211,12 @@ pub fn Watch(comptime V: type) type {...@@ -218,12 +211,12 @@ pub fn Watch(comptime V: type) type {
218 self.allocator.destroy(dir_entry.value);211 self.allocator.destroy(dir_entry.value);
219 }212 }
220 self.os_data.dir_table.deinit(self.allocator);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 else => @compileError("Unsupported OS"),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 }
228221
229 pub fn addFile(self: *Self, file_path: []const u8, value: V) !?V {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,91 +229,109 @@ pub fn Watch(comptime V: type) type {
236 }229 }
237230
238 fn addFileKEvent(self: *Self, file_path: []const u8, value: V) !?V {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});232 var realpath_buf: [std.fs.MAX_PATH_BYTES]u8 = undefined;
240 var resolved_path_consumed = false;233 const realpath = try os.realpath(file_path, &realpath_buf);
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();
246234
247 const flags = if (comptime std.Target.current.isDarwin()) os.O_SYMLINK | os.O_EVTONLY else 0;235 const held = self.os_data.table_lock.acquire();
248 const mode = 0;236 defer held.release();
249 const fd = try openPosix(self.allocator, resolved_path, flags, mode);
250 close_op.setHandle(fd);
251237
252 var put = try self.allocator.create(OsData.Put);238 const gop = try self.os_data.file_table.getOrPut(self.allocator, realpath);
253 errdefer self.allocator.destroy(put);239 errdefer self.os_data.file_table.removeAssertDiscard(realpath);
254 put.* = OsData.Put{240 if (gop.found_existing) {
255 .value = value,241 const prev_value = gop.entry.value.value;
256 .putter_frame = undefined,242 gop.entry.value.value = value;
257 };243 return prev_value;
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;
263 }244 }
264245
265 const result = blk: {246 gop.entry.key = try self.allocator.dupe(u8, realpath);
266 const held = self.os_data.table_lock.acquire();247 errdefer self.allocator.free(gop.entry.key);
267 defer held.release();248 gop.entry.value = try self.allocator.create(OsData.Put);
268249 errdefer self.allocator.destroy(gop.entry.value);
269 const gop = try self.os_data.file_table.getOrPut(resolved_path);250 gop.entry.value.* = .{
270 if (gop.found_existing) {251 .putter_frame = undefined,
271 const prev_value = gop.kv.value.value;252 .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 }
280 };253 };
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;
283 }260 }
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 {
286 global_event_loop.beginOneEvent();263 global_event_loop.beginOneEvent();
287
288 defer {264 defer {
289 close_op.finish();
290 global_event_loop.finishOneEvent();265 global_event_loop.finishOneEvent();
266 // @TODO: Remove this if we force close otherwise
267 os.close(fd);
291 }268 }
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
293 while (!put.cancelled) {283 while (!put.cancelled) {
294 if (global_event_loop.bsdWaitKev(284 kev.* = os.Kevent{
295 @intCast(usize, close_op.getHandle()),285 .ident = @intCast(usize, fd),
296 os.EVFILT_VNODE,286 .filter = os.EVFILT_VNODE,
297 os.NOTE_WRITE | os.NOTE_DELETE,287 .flags = os.EV_ADD | os.EV_ENABLE | os.EV_CLEAR | os.EV_ONESHOT |
298 )) |kev| {288 os.NOTE_WRITE | os.NOTE_DELETE | os.NOTE_REVOKE,
299 // TODO handle EV_ERROR289 .fflags = 0,
300 if (kev.fflags & os.NOTE_DELETE != 0) {290 .data = 0,
301 self.channel.put(Self.Event{291 .udata = @ptrToInt(&resume_node.base),
302 .id = Event.Id.Delete,292 };
303 .data = put.value,293 suspend {
304 });294 global_event_loop.beginOneEvent();
305 } else if (kev.fflags & os.NOTE_WRITE != 0) {295 errdefer global_event_loop.finishOneEvent();
306 self.channel.put(Self.Event{296
307 .id = Event.Id.CloseWrite,297 const empty_kevs = &[0]os.Kevent{};
308 .data = put.value,298 _ = os.kevent(global_event_loop.os_data.kqfd, &kevs, empty_kevs, null) catch |err| switch (err) {
309 });299 error.EventNotFound,
310 }300 error.ProcessNotFound,
311 } else |err| switch (err) {301 error.Overflow,
312 error.EventNotFound => unreachable,302 => unreachable,
313 error.ProcessNotFound => unreachable,303 error.AccessDenied, error.SystemResources => |e| {
314 error.Overflow => unreachable,304 self.channel.put(e);
315 error.AccessDenied, error.SystemResources => |casted_err| {305 continue;
316 self.channel.put(casted_err);306 },
317 },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 }
321332
322 fn addFileLinux(self: *Self, file_path: []const u8, value: V) !?V {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 const basename = std.fs.path.basename(file_path);335 const basename = std.fs.path.basename(file_path);
325336
326 const wd = try os.inotify_add_watch(337 const wd = try os.inotify_add_watch(
...@@ -334,6 +345,7 @@ pub fn Watch(comptime V: type) type {...@@ -334,6 +345,7 @@ pub fn Watch(comptime V: type) type {
334 defer held.release();345 defer held.release();
335346
336 const gop = try self.os_data.wd_table.getOrPut(self.allocator, wd);347 const gop = try self.os_data.wd_table.getOrPut(self.allocator, wd);
348 errdefer self.os_data.wd_table.removeAssertDiscard(wd);
337 if (!gop.found_existing) {349 if (!gop.found_existing) {
338 gop.entry.value = OsData.Dir{350 gop.entry.value = OsData.Dir{
339 .dirname = try self.allocator.dupe(u8, dirname),351 .dirname = try self.allocator.dupe(u8, dirname),
...@@ -343,6 +355,7 @@ pub fn Watch(comptime V: type) type {...@@ -343,6 +355,7 @@ pub fn Watch(comptime V: type) type {
343355
344 const dir = &gop.entry.value;356 const dir = &gop.entry.value;
345 const file_table_gop = try dir.file_table.getOrPut(self.allocator, basename);357 const file_table_gop = try dir.file_table.getOrPut(self.allocator, basename);
358 errdefer dir.file_table.removeAssertDiscard(basename);
346 if (file_table_gop.found_existing) {359 if (file_table_gop.found_existing) {
347 const prev_value = file_table_gop.entry.value;360 const prev_value = file_table_gop.entry.value;
348 file_table_gop.entry.value = value;361 file_table_gop.entry.value = value;
...@@ -356,7 +369,7 @@ pub fn Watch(comptime V: type) type {...@@ -356,7 +369,7 @@ pub fn Watch(comptime V: type) type {
356369
357 fn addFileWindows(self: *Self, file_path: []const u8, value: V) !?V {370 fn addFileWindows(self: *Self, file_path: []const u8, value: V) !?V {
358 // TODO we might need to convert dirname and basename to canonical file paths ("short"?)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 var dirname_path_space: windows.PathSpace = undefined;373 var dirname_path_space: windows.PathSpace = undefined;
361 dirname_path_space.len = try std.unicode.utf8ToUtf16Le(&dirname_path_space.data, dirname);374 dirname_path_space.len = try std.unicode.utf8ToUtf16Le(&dirname_path_space.data, dirname);
362 dirname_path_space.data[dirname_path_space.len] = 0;375 dirname_path_space.data[dirname_path_space.len] = 0;
...@@ -370,10 +383,12 @@ pub fn Watch(comptime V: type) type {...@@ -370,10 +383,12 @@ pub fn Watch(comptime V: type) type {
370 defer held.release();383 defer held.release();
371384
372 const gop = try self.os_data.dir_table.getOrPut(self.allocator, dirname);385 const gop = try self.os_data.dir_table.getOrPut(self.allocator, dirname);
386 errdefer self.os_data.dir_table.removeAssertDiscard(dirname);
373 if (gop.found_existing) {387 if (gop.found_existing) {
374 const dir = gop.entry.value;388 const dir = gop.entry.value;
375389
376 const file_gop = try dir.file_table.getOrPut(self.allocator, basename);390 const file_gop = try dir.file_table.getOrPut(self.allocator, basename);
391 errdefer dir.file_table.removeAssertDiscard(basename);
377 if (file_gop.found_existing) {392 if (file_gop.found_existing) {
378 const prev_value = file_gop.entry.value;393 const prev_value = file_gop.entry.value;
379 file_gop.entry.value = value;394 file_gop.entry.value = value;
...@@ -384,7 +399,6 @@ pub fn Watch(comptime V: type) type {...@@ -384,7 +399,6 @@ pub fn Watch(comptime V: type) type {
384 return null;399 return null;
385 }400 }
386 } else {401 } else {
387 errdefer _ = self.os_data.dir_table.remove(dirname);
388 const dir_handle = try windows.OpenFile(dirname_path_space.span(), .{402 const dir_handle = try windows.OpenFile(dirname_path_space.span(), .{
389 .dir = std.fs.cwd().fd,403 .dir = std.fs.cwd().fd,
390 .access_mask = windows.FILE_LIST_DIRECTORY,404 .access_mask = windows.FILE_LIST_DIRECTORY,
...@@ -501,10 +515,10 @@ pub fn Watch(comptime V: type) type {...@@ -501,10 +515,10 @@ pub fn Watch(comptime V: type) type {
501 }515 }
502 }516 }
503517
504 pub fn removeFile(self: *Self, file_path: []const u8) ?V {518 pub fn removeFile(self: *Self, file_path: []const u8) !?V {
505 switch (builtin.os.tag) {519 switch (builtin.os.tag) {
506 .linux => {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 const basename = std.fs.path.basename(file_path);522 const basename = std.fs.path.basename(file_path);
509523
510 const held = self.os_data.table_lock.acquire();524 const held = self.os_data.table_lock.acquire();
...@@ -518,7 +532,7 @@ pub fn Watch(comptime V: type) type {...@@ -518,7 +532,7 @@ pub fn Watch(comptime V: type) type {
518 return null;532 return null;
519 },533 },
520 .windows => {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 const basename = std.fs.path.basename(file_path);536 const basename = std.fs.path.basename(file_path);
523537
524 const held = self.os_data.table_lock.acquire();538 const held = self.os_data.table_lock.acquire();
...@@ -531,7 +545,22 @@ pub fn Watch(comptime V: type) type {...@@ -531,7 +545,22 @@ pub fn Watch(comptime V: type) type {
531 }545 }
532 return null;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 else => @compileError("Unsupported OS"),564 else => @compileError("Unsupported OS"),
536 }565 }
537 }566 }
...@@ -685,3 +714,5 @@ fn testWriteWatchWriteDelete(allocator: *Allocator) !void {...@@ -685,3 +714,5 @@ fn testWriteWatchWriteDelete(allocator: *Allocator) !void {
685 .CloseWrite => @panic("wrong event"),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