fsmonitor: deal with synthetic firmlinks on macOS

Starting with macOS 10.15 (Catalina), Apple introduced a new feature called 'firmlinks' in order to separate the boot volume into two volumes, one read-only and one writable but still present them to the user as a single volume. Along with this change, Apple removed the ability to create symlinks in the root directory and replaced them with 'synthetic firmlinks'. See 'man synthetic.conf' When FSEevents reports the path of changed files, if the path involves a synthetic firmlink, the path is reported from the point of the synthetic firmlink and not the real path. For example: Real path: /System/Volumes/Data/network/working/directory/foo.txt Synthetic firmlink: /network -> /System/Volumes/Data/network FSEvents path: /network/working/directory/foo.txt This causes the FSEvents path to not match against the worktree directory. There are several ways in which synthetic firmlinks can be created: they can be defined in /etc/synthetic.conf, the automounter can create them, and there may be other means. Simply reading /etc/synthetic.conf is insufficient. No matter what process creates synthetic firmlinks, they all get created in the root directory. Therefore, in order to deal with synthetic firmlinks, the root directory is scanned and the first possible synthetic firmink that, when resolved, is a prefix of the worktree is used to map FSEvents paths to worktree paths. Signed-off-by: Eric DeCosta <edecosta@mathworks.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eric DeCosta committed Oct 4, 2022 at 17:32 UTC 12fd27df79ac5f2589b54c8f36761d066dc9a97e
6 files changed +167 -3
builtin/fsmonitor--daemon.c
+8
@@ -3,6 +3,7 @@
3 #include "parse-options.h"
4 #include "fsmonitor.h"
5 #include "fsmonitor-ipc.h"
6 +#include "fsmonitor-path-utils.h"
7 #include "compat/fsmonitor/fsm-health.h"
8 #include "compat/fsmonitor/fsm-listen.h"
9 #include "fsmonitor--daemon.h"
@@ -1282,6 +1283,11 @@ static int fsmonitor_run_daemon(void)
1283 strbuf_addstr(&state.path_worktree_watch, absolute_path(get_git_work_tree()));
1284 state.nr_paths_watching = 1;
1285
1286 + strbuf_init(&state.alias.alias, 0);
1287 + strbuf_init(&state.alias.points_to, 0);
1288 + if ((err = fsmonitor__get_alias(state.path_worktree_watch.buf, &state.alias)))
1289 + goto done;
1290 +
1291 /*
1292 * We create and delete cookie files somewhere inside the .git
1293 * directory to help us keep sync with the file system. If
@@ -1391,6 +1397,8 @@ done:
1397 strbuf_release(&state.path_gitdir_watch);
1398 strbuf_release(&state.path_cookie_prefix);
1399 strbuf_release(&state.path_ipc);
1400 + strbuf_release(&state.alias.alias);
1401 + strbuf_release(&state.alias.points_to);
1402
1403 return err;
1404 }
compat/fsmonitor/fsm-listen-darwin.c
+12 -2
@@ -26,6 +26,7 @@
26 #include "fsmonitor.h"
27 #include "fsm-listen.h"
28 #include "fsmonitor--daemon.h"
29 +#include "fsmonitor-path-utils.h"
30
31 struct fsm_listen_data
32 {
@@ -198,8 +199,9 @@ static void fsevent_callback(ConstFSEventStreamRef streamRef,
199 struct string_list cookie_list = STRING_LIST_INIT_DUP;
200 const char *path_k;
201 const char *slash;
201 - int k;
202 + char *resolved = NULL;
203 struct strbuf tmp = STRBUF_INIT;
204 + int k;
205
206 /*
207 * Build a list of all filesystem changes into a private/local
@@ -209,7 +211,12 @@ static void fsevent_callback(ConstFSEventStreamRef streamRef,
211 /*
212 * On Mac, we receive an array of absolute paths.
213 */
212 - path_k = paths[k];
214 + free(resolved);
215 + resolved = fsmonitor__resolve_alias(paths[k], &state->alias);
216 + if (resolved)
217 + path_k = resolved;
218 + else
219 + path_k = paths[k];
220
221 /*
222 * If you want to debug FSEvents, log them to GIT_TRACE_FSMONITOR.
@@ -238,6 +245,7 @@ static void fsevent_callback(ConstFSEventStreamRef streamRef,
245 fsmonitor_force_resync(state);
246 fsmonitor_batch__free_list(batch);
247 string_list_clear(&cookie_list, 0);
248 + batch = NULL;
249
250 /*
251 * We assume that any events that we received
@@ -360,12 +368,14 @@ static void fsevent_callback(ConstFSEventStreamRef streamRef,
368 }
369 }
370
371 + free(resolved);
372 fsmonitor_publish(state, batch, &cookie_list);
373 string_list_clear(&cookie_list, 0);
374 strbuf_release(&tmp);
375 return;
376
377 force_shutdown:
378 + free(resolved);
379 fsmonitor_batch__free_list(batch);
380 string_list_clear(&cookie_list, 0);
381
compat/fsmonitor/fsm-path-utils-darwin.c
+92
@@ -1,5 +1,8 @@
1 #include "fsmonitor.h"
2 #include "fsmonitor-path-utils.h"
3 +#include <dirent.h>
4 +#include <errno.h>
5 +#include <fcntl.h>
6 #include <sys/param.h>
7 #include <sys/mount.h>
8
@@ -41,3 +44,92 @@ int fsmonitor__is_fs_remote(const char *path)
44
45 return fs.is_remote;
46 }
47 +
48 +/*
49 + * Scan the root directory for synthetic firmlinks that when resolved
50 + * are a prefix of the path, stopping at the first one found.
51 + *
52 + * Some information about firmlinks and synthetic firmlinks:
53 + * https://eclecticlight.co/2020/01/23/catalina-boot-volumes/
54 + *
55 + * macOS no longer allows symlinks in the root directory; any link found
56 + * there is therefore a synthetic firmlink.
57 + *
58 + * If this function gets called often, will want to cache all the firmlink
59 + * information, but for now there is only one caller of this function.
60 + *
61 + * If there is more than one alias for the path, that is another
62 + * matter altogether.
63 + */
64 +int fsmonitor__get_alias(const char *path, struct alias_info *info)
65 +{
66 + DIR *dir;
67 + int retval = -1;
68 + const char *const root = "/";
69 + struct stat st;
70 + struct dirent *de;
71 + struct strbuf alias;
72 + struct strbuf points_to = STRBUF_INIT;
73 +
74 + dir = opendir(root);
75 + if (!dir)
76 + return error_errno(_("opendir('%s') failed"), root);
77 +
78 + strbuf_init(&alias, 256);
79 +
80 + while ((de = readdir(dir)) != NULL) {
81 + strbuf_reset(&alias);
82 + strbuf_addf(&alias, "%s%s", root, de->d_name);
83 +
84 + if (lstat(alias.buf, &st) < 0) {
85 + error_errno(_("lstat('%s') failed"), alias.buf);
86 + goto done;
87 + }
88 +
89 + if (!S_ISLNK(st.st_mode))
90 + continue;
91 +
92 + if (strbuf_readlink(&points_to, alias.buf, st.st_size) < 0) {
93 + error_errno(_("strbuf_readlink('%s') failed"), alias.buf);
94 + goto done;
95 + }
96 +
97 + if (!strncmp(points_to.buf, path, points_to.len) &&
98 + (path[points_to.len] == '/')) {
99 + strbuf_addbuf(&info->alias, &alias);
100 + strbuf_addbuf(&info->points_to, &points_to);
101 + trace_printf_key(&trace_fsmonitor,
102 + "Found alias for '%s' : '%s' -> '%s'",
103 + path, info->alias.buf, info->points_to.buf);
104 + retval = 0;
105 + goto done;
106 + }
107 + }
108 + retval = 0; /* no alias */
109 +
110 +done:
111 + strbuf_release(&alias);
112 + strbuf_release(&points_to);
113 + if (closedir(dir) < 0)
114 + return error_errno(_("closedir('%s') failed"), root);
115 + return retval;
116 +}
117 +
118 +char *fsmonitor__resolve_alias(const char *path,
119 + const struct alias_info *info)
120 +{
121 + if (!info->alias.len)
122 + return NULL;
123 +
124 + if ((!strncmp(info->alias.buf, path, info->alias.len))
125 + && path[info->alias.len] == '/') {
126 + struct strbuf tmp = STRBUF_INIT;
127 + const char *remainder = path + info->alias.len;
128 +
129 + strbuf_addbuf(&tmp, &info->points_to);
130 + strbuf_add(&tmp, remainder, strlen(remainder));
131 + return strbuf_detach(&tmp, NULL);
132 + }
133 +
134 + return NULL;
135 +}
compat/fsmonitor/fsm-path-utils-win32.c
+17
@@ -126,3 +126,20 @@ int fsmonitor__is_fs_remote(const char *path)
126 return -1;
127 return fs.is_remote;
128 }
129 +
130 +/*
131 + * No-op for now.
132 + */
133 +int fsmonitor__get_alias(const char *path, struct alias_info *info)
134 +{
135 + return 0;
136 +}
137 +
138 +/*
139 + * No-op for now.
140 + */
141 +char *fsmonitor__resolve_alias(const char *path,
142 + const struct alias_info *info)
143 +{
144 + return NULL;
145 +}
fsmonitor--daemon.h
+3
@@ -8,6 +8,7 @@
8 #include "run-command.h"
9 #include "simple-ipc.h"
10 #include "thread-utils.h"
11 +#include "fsmonitor-path-utils.h"
12
13 struct fsmonitor_batch;
14 struct fsmonitor_token_data;
@@ -43,6 +44,7 @@ struct fsmonitor_daemon_state {
44
45 struct strbuf path_worktree_watch;
46 struct strbuf path_gitdir_watch;
47 + struct alias_info alias;
48 int nr_paths_watching;
49
50 struct fsmonitor_token_data *current_token_data;
@@ -59,6 +61,7 @@ struct fsmonitor_daemon_state {
61
62 struct ipc_server_data *ipc_server_data;
63 struct strbuf path_ipc;
64 +
65 };
66
67 /*
fsmonitor-path-utils.h
+35 -1
@@ -1,13 +1,21 @@
1 #ifndef FSM_PATH_UTILS_H
2 #define FSM_PATH_UTILS_H
3
4 +#include "strbuf.h"
5 +
6 +struct alias_info
7 +{
8 + struct strbuf alias;
9 + struct strbuf points_to;
10 +};
11 +
12 struct fs_info {
13 int is_remote;
14 char *typename;
15 };
16
17 /*
10 - * Get some basic filesystem informtion for the given path
18 + * Get some basic filesystem information for the given path
19 *
20 * The caller owns the storage that is occupied by fs_info and
21 * is responsible for releasing it.
@@ -23,4 +31,30 @@ int fsmonitor__get_fs_info(const char *path, struct fs_info *fs_info);
31 */
32 int fsmonitor__is_fs_remote(const char *path);
33
34 +/*
35 + * Get the alias in given path, if any.
36 + *
37 + * Sets alias to the first alias that matches any part of the path.
38 + *
39 + * If an alias is found, info.alias and info.points_to are set to the
40 + * found mapping.
41 + *
42 + * Returns -1 on error, 0 otherwise.
43 + *
44 + * The caller owns the storage that is occupied by info.alias and
45 + * info.points_to and is responsible for releasing it.
46 + */
47 +int fsmonitor__get_alias(const char *path, struct alias_info *info);
48 +
49 +/*
50 + * Resolve the path against the given alias.
51 + *
52 + * Returns the resolved path if there is one, NULL otherwise.
53 + *
54 + * The caller owns the storage that the returned string occupies and
55 + * is responsible for releasing it.
56 + */
57 +char *fsmonitor__resolve_alias(const char *path,
58 + const struct alias_info *info);
59 +
60 #endif