fsm-listen-darwin: ignore FSEvents caused by xattr changes on macOS

Ignore FSEvents resulting from `xattr` changes. Git does not care about xattr's or changes to xattr's, so don't waste time collecting these events in the daemon nor transmitting them to clients. Various security tools add xattrs to files and/or directories, such as to mark them as having been downloaded. We should ignore these events since it doesn't affect the content of the file/directory or the normal meta-data that Git cares about. Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff Hostetler committed May 26, 2022 at 21:47 UTC 8e8f4b814bab3f1499e50404e344f5b483c91f40
1 file changed +33 -1
compat/fsmonitor/fsm-listen-darwin.c
+33 -1
@@ -100,7 +100,7 @@ static void log_flags_set(const char *path, const FSEventStreamEventFlags flag)
100 if (flag & kFSEventStreamEventFlagItemCloned)
101 strbuf_addstr(&msg, "ItemCloned|");
102
103 - trace_printf_key(&trace_fsmonitor, "fsevent: '%s', flags=%u %s",
103 + trace_printf_key(&trace_fsmonitor, "fsevent: '%s', flags=0x%x %s",
104 path, flag, msg.buf);
105
106 strbuf_release(&msg);
@@ -125,6 +125,31 @@ static int ef_is_dropped(const FSEventStreamEventFlags ef)
125 ef & kFSEventStreamEventFlagUserDropped);
126 }
127
128 +/*
129 + * If an `xattr` change is the only reason we received this event,
130 + * then silently ignore it. Git doesn't care about xattr's. We
131 + * have to be careful here because the kernel can combine multiple
132 + * events for a single path. And because events always have certain
133 + * bits set, such as `ItemIsFile` or `ItemIsDir`.
134 + *
135 + * Return 1 if we should ignore it.
136 + */
137 +static int ef_ignore_xattr(const FSEventStreamEventFlags ef)
138 +{
139 + static const FSEventStreamEventFlags mask =
140 + kFSEventStreamEventFlagItemChangeOwner |
141 + kFSEventStreamEventFlagItemCreated |
142 + kFSEventStreamEventFlagItemFinderInfoMod |
143 + kFSEventStreamEventFlagItemInodeMetaMod |
144 + kFSEventStreamEventFlagItemModified |
145 + kFSEventStreamEventFlagItemRemoved |
146 + kFSEventStreamEventFlagItemRenamed |
147 + kFSEventStreamEventFlagItemXattrMod |
148 + kFSEventStreamEventFlagItemCloned;
149 +
150 + return ((ef & mask) == kFSEventStreamEventFlagItemXattrMod);
151 +}
152 +
153 static void fsevent_callback(ConstFSEventStreamRef streamRef,
154 void *ctx,
155 size_t num_of_events,
@@ -190,6 +215,13 @@ static void fsevent_callback(ConstFSEventStreamRef streamRef,
215 continue;
216 }
217
218 + if (ef_ignore_xattr(event_flags[k])) {
219 + trace_printf_key(&trace_fsmonitor,
220 + "ignore-xattr: '%s', flags=0x%x",
221 + path_k, event_flags[k]);
222 + continue;
223 + }
224 +
225 switch (fsmonitor_classify_path_absolute(state, path_k)) {
226
227 case IS_INSIDE_DOT_GIT_WITH_COOKIE_PREFIX: