Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(fsevents): correctly handle unknown events #6217

Merged
merged 2 commits into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
3.5.0 (unreleased)
------------------

- macOS: Handle unknown fsevents without crashing (#6217, @rgrinberg)

- Enable file watching on MacOS SDK < 10.13. (#6218, @rgrinberg)

- Sandbox running cinaps actions starting from cinaps 1.1 (#6176, @rgrinberg)
Expand Down
7 changes: 3 additions & 4 deletions src/dune_file_watcher/dune_file_watcher.ml
Original file line number Diff line number Diff line change
Expand Up @@ -514,10 +514,9 @@ let fsevents ?exclusion_paths ~latency ~paths scheduler f =
fsevents

let fsevents_standard_event event path =
let action = Fsevents.Event.action event in
let kind =
match action with
| Unknown -> Fs_memo_event.Unknown
match Fsevents.Event.action event with
| Rename | Unknown -> Fs_memo_event.Unknown
| Create -> Created
| Remove -> Deleted
| Modify ->
Expand All @@ -540,7 +539,7 @@ let create_fsevents ?(latency = 0.2) ~(scheduler : Scheduler.t) () =
else
match Fsevents.Event.action event with
| Remove -> None
| Unknown | Create | Modify ->
| Rename | Unknown | Create | Modify ->
Option.map (Fs_sync.consume_event sync_table path) ~f:(fun id ->
Event.Sync id))
in
Expand Down
6 changes: 4 additions & 2 deletions src/fsevents/fsevents.ml
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,11 @@ module Event = struct
let kind t = kind t.flags

type action =
| Unknown
| Create
| Remove
| Modify
| Rename
| Unknown

external action : Int32.t -> action = "dune_fsevents_action"

Expand All @@ -209,7 +210,8 @@ module Event = struct
| Create -> "Create"
| Remove -> "Remove"
| Modify -> "Modify"
| Unknown -> "Unknown")
| Unknown -> "Unknown"
| Rename -> "Rename")

let to_dyn t =
let open Dyn in
Expand Down
8 changes: 4 additions & 4 deletions src/fsevents/fsevents.mli
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ module Event : sig
val kind : t -> kind

type action =
| Create (* [path t] guaranteed to exist *)
| Remove (* [path t] guaranteed to be absent *)
| Modify (* [path t] guaranteed to exist *)
| Rename
| Unknown
(** multiple actions merged into one by debouncing or an uninformative
"rename". inspect the FS to see what happened *)
| Create (* [path t] guaranteed to exist *)
| Remove (* [path t] guaranteed to be absent *)
| Modify
(* [path t] guaranteed to exist *)

val dyn_of_action : action -> Dyn.t

Expand Down
18 changes: 5 additions & 13 deletions src/fsevents/fsevents_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,24 +270,16 @@ CAMLprim value dune_fsevents_action(value v_flags) {
CAMLlocal1(v_action);

uint32_t flags = Int32_val(v_flags) & action_mask;
int count = __builtin_popcount(flags);

flags = Int32_val(v_flags);
if (count >= 2 || flags & kFSEventStreamEventFlagItemRenamed) {
// we don't bother tracking renamed acurately for now. macos makes it
// tricky by not telling is which path is created and which one is deleted.
// it is possible to reverse engineer this from the chain of inodes in the
// events, but it's also error prone as inodes can be reused. so for now, we
// avoid this is and treat renamed as unknown
if (flags & kFSEventStreamEventFlagItemCreated) {
v_action = Val_int(0);
} else if (flags & kFSEventStreamEventFlagItemCreated) {
v_action = Val_int(1);
} else if (flags & kFSEventStreamEventFlagItemRemoved) {
v_action = Val_int(2);
v_action = Val_int(1);
} else if (flags & kFSEventStreamEventFlagItemModified) {
v_action = Val_int(2);
} else if (flags & kFSEventStreamEventFlagItemRenamed) {
v_action = Val_int(3);
} else {
caml_failwith("fsevents: unexpected event action");
v_action = Val_int(4);
}

CAMLreturn(v_action);
Expand Down