Skip to content

Commit 12fd27d

Browse files
edecosta-mwgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8f44976 commit 12fd27d

File tree

6 files changed

+167
-3
lines changed

6 files changed

+167
-3
lines changed

builtin/fsmonitor--daemon.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "parse-options.h"
44
#include "fsmonitor.h"
55
#include "fsmonitor-ipc.h"
6+
#include "fsmonitor-path-utils.h"
67
#include "compat/fsmonitor/fsm-health.h"
78
#include "compat/fsmonitor/fsm-listen.h"
89
#include "fsmonitor--daemon.h"
@@ -1282,6 +1283,11 @@ static int fsmonitor_run_daemon(void)
12821283
strbuf_addstr(&state.path_worktree_watch, absolute_path(get_git_work_tree()));
12831284
state.nr_paths_watching = 1;
12841285

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+
12851291
/*
12861292
* We create and delete cookie files somewhere inside the .git
12871293
* directory to help us keep sync with the file system. If
@@ -1391,6 +1397,8 @@ static int fsmonitor_run_daemon(void)
13911397
strbuf_release(&state.path_gitdir_watch);
13921398
strbuf_release(&state.path_cookie_prefix);
13931399
strbuf_release(&state.path_ipc);
1400+
strbuf_release(&state.alias.alias);
1401+
strbuf_release(&state.alias.points_to);
13941402

13951403
return err;
13961404
}

compat/fsmonitor/fsm-listen-darwin.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "fsmonitor.h"
2727
#include "fsm-listen.h"
2828
#include "fsmonitor--daemon.h"
29+
#include "fsmonitor-path-utils.h"
2930

3031
struct fsm_listen_data
3132
{
@@ -198,8 +199,9 @@ static void fsevent_callback(ConstFSEventStreamRef streamRef,
198199
struct string_list cookie_list = STRING_LIST_INIT_DUP;
199200
const char *path_k;
200201
const char *slash;
201-
int k;
202+
char *resolved = NULL;
202203
struct strbuf tmp = STRBUF_INIT;
204+
int k;
203205

204206
/*
205207
* Build a list of all filesystem changes into a private/local
@@ -209,7 +211,12 @@ static void fsevent_callback(ConstFSEventStreamRef streamRef,
209211
/*
210212
* On Mac, we receive an array of absolute paths.
211213
*/
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];
213220

214221
/*
215222
* If you want to debug FSEvents, log them to GIT_TRACE_FSMONITOR.
@@ -238,6 +245,7 @@ static void fsevent_callback(ConstFSEventStreamRef streamRef,
238245
fsmonitor_force_resync(state);
239246
fsmonitor_batch__free_list(batch);
240247
string_list_clear(&cookie_list, 0);
248+
batch = NULL;
241249

242250
/*
243251
* We assume that any events that we received
@@ -360,12 +368,14 @@ static void fsevent_callback(ConstFSEventStreamRef streamRef,
360368
}
361369
}
362370

371+
free(resolved);
363372
fsmonitor_publish(state, batch, &cookie_list);
364373
string_list_clear(&cookie_list, 0);
365374
strbuf_release(&tmp);
366375
return;
367376

368377
force_shutdown:
378+
free(resolved);
369379
fsmonitor_batch__free_list(batch);
370380
string_list_clear(&cookie_list, 0);
371381

compat/fsmonitor/fsm-path-utils-darwin.c

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#include "fsmonitor.h"
22
#include "fsmonitor-path-utils.h"
3+
#include <dirent.h>
4+
#include <errno.h>
5+
#include <fcntl.h>
36
#include <sys/param.h>
47
#include <sys/mount.h>
58

@@ -41,3 +44,92 @@ int fsmonitor__is_fs_remote(const char *path)
4144

4245
return fs.is_remote;
4346
}
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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,20 @@ int fsmonitor__is_fs_remote(const char *path)
126126
return -1;
127127
return fs.is_remote;
128128
}
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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "run-command.h"
99
#include "simple-ipc.h"
1010
#include "thread-utils.h"
11+
#include "fsmonitor-path-utils.h"
1112

1213
struct fsmonitor_batch;
1314
struct fsmonitor_token_data;
@@ -43,6 +44,7 @@ struct fsmonitor_daemon_state {
4344

4445
struct strbuf path_worktree_watch;
4546
struct strbuf path_gitdir_watch;
47+
struct alias_info alias;
4648
int nr_paths_watching;
4749

4850
struct fsmonitor_token_data *current_token_data;
@@ -59,6 +61,7 @@ struct fsmonitor_daemon_state {
5961

6062
struct ipc_server_data *ipc_server_data;
6163
struct strbuf path_ipc;
64+
6265
};
6366

6467
/*

fsmonitor-path-utils.h

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
#ifndef FSM_PATH_UTILS_H
22
#define FSM_PATH_UTILS_H
33

4+
#include "strbuf.h"
5+
6+
struct alias_info
7+
{
8+
struct strbuf alias;
9+
struct strbuf points_to;
10+
};
11+
412
struct fs_info {
513
int is_remote;
614
char *typename;
715
};
816

917
/*
10-
* Get some basic filesystem informtion for the given path
18+
* Get some basic filesystem information for the given path
1119
*
1220
* The caller owns the storage that is occupied by fs_info and
1321
* is responsible for releasing it.
@@ -23,4 +31,30 @@ int fsmonitor__get_fs_info(const char *path, struct fs_info *fs_info);
2331
*/
2432
int fsmonitor__is_fs_remote(const char *path);
2533

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+
2660
#endif

0 commit comments

Comments
 (0)