fsmonitor: on macOS also emit NFC spelling for NFD pathname

Emit NFC or NFC and NFD spellings of pathnames on macOS. MacOS is Unicode composition insensitive, so NFC and NFD spellings are treated as aliases and collide. While the spelling of pathnames in filesystem events depends upon the underlying filesystem, such as APFS, HFS+ or FAT32, the OS enforces such collisions regardless of filesystem. Teach the daemon to always report the NFC spelling and to report the NFD spelling when stored in that format on the disk. This is slightly more general than "core.precomposeUnicode". 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 d6d58ff8abd382226980f00360e12e4bf91456e4
1 file changed +31 -2
compat/fsmonitor/fsm-listen-darwin.c
+31 -2
@@ -155,6 +155,35 @@ static int ef_ignore_xattr(const FSEventStreamEventFlags ef)
155 return ((ef & mask) == kFSEventStreamEventFlagItemXattrMod);
156 }
157
158 +/*
159 + * On MacOS we have to adjust for Unicode composition insensitivity
160 + * (where NFC and NFD spellings are not respected). The different
161 + * spellings are essentially aliases regardless of how the path is
162 + * actually stored on the disk.
163 + *
164 + * This is related to "core.precomposeUnicode" (which wants to try
165 + * to hide NFD completely and treat everything as NFC). Here, we
166 + * don't know what the value the client has (or will have) for this
167 + * config setting when they make a query, so assume the worst and
168 + * emit both when the OS gives us an NFD path.
169 + */
170 +static void my_add_path(struct fsmonitor_batch *batch, const char *path)
171 +{
172 + char *composed;
173 +
174 + /* add the NFC or NFD path as received from the OS */
175 + fsmonitor_batch__add_path(batch, path);
176 +
177 + /* if NFD, also add the corresponding NFC spelling */
178 + composed = (char *)precompose_string_if_needed(path);
179 + if (!composed || composed == path)
180 + return;
181 +
182 + fsmonitor_batch__add_path(batch, composed);
183 + free(composed);
184 +}
185 +
186 +
187 static void fsevent_callback(ConstFSEventStreamRef streamRef,
188 void *ctx,
189 size_t num_of_events,
@@ -305,7 +334,7 @@ static void fsevent_callback(ConstFSEventStreamRef streamRef,
334
335 if (!batch)
336 batch = fsmonitor_batch__new();
308 - fsmonitor_batch__add_path(batch, rel);
337 + my_add_path(batch, rel);
338 }
339
340 if (event_flags[k] & kFSEventStreamEventFlagItemIsDir) {
@@ -318,7 +347,7 @@ static void fsevent_callback(ConstFSEventStreamRef streamRef,
347
348 if (!batch)
349 batch = fsmonitor_batch__new();
321 - fsmonitor_batch__add_path(batch, tmp.buf);
350 + my_add_path(batch, tmp.buf);
351 }
352
353 break;