Skip to content

Commit 7b8cfe3

Browse files
committed
Merge branch 'ed/fsmonitor-on-networked-macos'
By default, use of fsmonitor on a repository on networked filesystem is disabled. Add knobs to make it workable on macOS. * ed/fsmonitor-on-networked-macos: fsmonitor: fix leak of warning message fsmonitor: add documentation for allowRemote and socketDir options fsmonitor: check for compatability before communicating with fsmonitor fsmonitor: deal with synthetic firmlinks on macOS fsmonitor: avoid socket location check if using hook fsmonitor: relocate socket file if .git directory is remote fsmonitor: refactor filesystem checks to common interface
2 parents d420dda + c4f9490 commit 7b8cfe3

20 files changed

+590
-249
lines changed

Documentation/config.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,8 @@ include::config/filter.txt[]
423423

424424
include::config/fsck.txt[]
425425

426+
include::config/fsmonitor--daemon.txt[]
427+
426428
include::config/gc.txt[]
427429

428430
include::config/gitcvs.txt[]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fsmonitor.allowRemote::
2+
By default, the fsmonitor daemon refuses to work against network-mounted
3+
repositories. Setting `fsmonitor.allowRemote` to `true` overrides this
4+
behavior. Only respected when `core.fsmonitor` is set to `true`.
5+
6+
fsmonitor.socketDir::
7+
This Mac OS-specific option, if set, specifies the directory in
8+
which to create the Unix domain socket used for communication
9+
between the fsmonitor daemon and various Git commands. The directory must
10+
reside on a native Mac OS filesystem. Only respected when `core.fsmonitor`
11+
is set to `true`.

Documentation/git-fsmonitor--daemon.txt

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ git-fsmonitor{litdd}daemon(1)
33

44
NAME
55
----
6-
git-fsmonitor--daemon - A Built-in File System Monitor
6+
git-fsmonitor--daemon - A Built-in Filesystem Monitor
77

88
SYNOPSIS
99
--------
@@ -17,7 +17,7 @@ DESCRIPTION
1717
-----------
1818

1919
A daemon to watch the working directory for file and directory
20-
changes using platform-specific file system notification facilities.
20+
changes using platform-specific filesystem notification facilities.
2121

2222
This daemon communicates directly with commands like `git status`
2323
using the link:technical/api-simple-ipc.html[simple IPC] interface
@@ -63,13 +63,44 @@ CAVEATS
6363
-------
6464

6565
The fsmonitor daemon does not currently know about submodules and does
66-
not know to filter out file system events that happen within a
66+
not know to filter out filesystem events that happen within a
6767
submodule. If fsmonitor daemon is watching a super repo and a file is
6868
modified within the working directory of a submodule, it will report
6969
the change (as happening against the super repo). However, the client
7070
will properly ignore these extra events, so performance may be affected
7171
but it will not cause an incorrect result.
7272

73+
By default, the fsmonitor daemon refuses to work against network-mounted
74+
repositories; this may be overridden by setting `fsmonitor.allowRemote` to
75+
`true`. Note, however, that the fsmonitor daemon is not guaranteed to work
76+
correctly with all network-mounted repositories and such use is considered
77+
experimental.
78+
79+
On Mac OS, the inter-process communication (IPC) between various Git
80+
commands and the fsmonitor daemon is done via a Unix domain socket (UDS) -- a
81+
special type of file -- which is supported by native Mac OS filesystems,
82+
but not on network-mounted filesystems, NTFS, or FAT32. Other filesystems
83+
may or may not have the needed support; the fsmonitor daemon is not guaranteed
84+
to work with these filesystems and such use is considered experimental.
85+
86+
By default, the socket is created in the `.git` directory, however, if the
87+
`.git` directory is on a network-mounted filesystem, it will be instead be
88+
created at `$HOME/.git-fsmonitor-*` unless `$HOME` itself is on a
89+
network-mounted filesystem in which case you must set the configuration
90+
variable `fsmonitor.socketDir` to the path of a directory on a Mac OS native
91+
filesystem in which to create the socket file.
92+
93+
If none of the above directories (`.git`, `$HOME`, or `fsmonitor.socketDir`)
94+
is on a native Mac OS file filesystem the fsmonitor daemon will report an
95+
error that will cause the daemon and the currently running command to exit.
96+
97+
CONFIGURATION
98+
-------------
99+
100+
include::includes/cmd-config-section-all.txt[]
101+
102+
include::config/fsmonitor--daemon.txt[]
103+
73104
GIT
74105
---
75106
Part of the linkgit:git[1] suite

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2038,11 +2038,13 @@ ifdef FSMONITOR_DAEMON_BACKEND
20382038
COMPAT_CFLAGS += -DHAVE_FSMONITOR_DAEMON_BACKEND
20392039
COMPAT_OBJS += compat/fsmonitor/fsm-listen-$(FSMONITOR_DAEMON_BACKEND).o
20402040
COMPAT_OBJS += compat/fsmonitor/fsm-health-$(FSMONITOR_DAEMON_BACKEND).o
2041+
COMPAT_OBJS += compat/fsmonitor/fsm-ipc-$(FSMONITOR_DAEMON_BACKEND).o
20412042
endif
20422043

20432044
ifdef FSMONITOR_OS_SETTINGS
20442045
COMPAT_CFLAGS += -DHAVE_FSMONITOR_OS_SETTINGS
20452046
COMPAT_OBJS += compat/fsmonitor/fsm-settings-$(FSMONITOR_OS_SETTINGS).o
2047+
COMPAT_OBJS += compat/fsmonitor/fsm-path-utils-$(FSMONITOR_OS_SETTINGS).o
20462048
endif
20472049

20482050
ifeq ($(TCLTK_PATH),)

builtin/fsmonitor--daemon.c

Lines changed: 10 additions & 1 deletion
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
@@ -1343,7 +1349,8 @@ static int fsmonitor_run_daemon(void)
13431349
* directory.)
13441350
*/
13451351
strbuf_init(&state.path_ipc, 0);
1346-
strbuf_addstr(&state.path_ipc, absolute_path(fsmonitor_ipc__get_path()));
1352+
strbuf_addstr(&state.path_ipc,
1353+
absolute_path(fsmonitor_ipc__get_path(the_repository)));
13471354

13481355
/*
13491356
* Confirm that we can create platform-specific resources for the
@@ -1390,6 +1397,8 @@ static int fsmonitor_run_daemon(void)
13901397
strbuf_release(&state.path_gitdir_watch);
13911398
strbuf_release(&state.path_cookie_prefix);
13921399
strbuf_release(&state.path_ipc);
1400+
strbuf_release(&state.alias.alias);
1401+
strbuf_release(&state.alias.points_to);
13931402

13941403
return err;
13951404
}

compat/fsmonitor/fsm-ipc-darwin.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include "cache.h"
2+
#include "config.h"
3+
#include "strbuf.h"
4+
#include "fsmonitor.h"
5+
#include "fsmonitor-ipc.h"
6+
#include "fsmonitor-path-utils.h"
7+
8+
static GIT_PATH_FUNC(fsmonitor_ipc__get_default_path, "fsmonitor--daemon.ipc")
9+
10+
const char *fsmonitor_ipc__get_path(struct repository *r)
11+
{
12+
static const char *ipc_path = NULL;
13+
SHA_CTX sha1ctx;
14+
char *sock_dir = NULL;
15+
struct strbuf ipc_file = STRBUF_INIT;
16+
unsigned char hash[SHA_DIGEST_LENGTH];
17+
18+
if (!r)
19+
BUG("No repository passed into fsmonitor_ipc__get_path");
20+
21+
if (ipc_path)
22+
return ipc_path;
23+
24+
25+
/* By default the socket file is created in the .git directory */
26+
if (fsmonitor__is_fs_remote(r->gitdir) < 1) {
27+
ipc_path = fsmonitor_ipc__get_default_path();
28+
return ipc_path;
29+
}
30+
31+
SHA1_Init(&sha1ctx);
32+
SHA1_Update(&sha1ctx, r->worktree, strlen(r->worktree));
33+
SHA1_Final(hash, &sha1ctx);
34+
35+
repo_config_get_string(r, "fsmonitor.socketdir", &sock_dir);
36+
37+
/* Create the socket file in either socketDir or $HOME */
38+
if (sock_dir && *sock_dir) {
39+
strbuf_addf(&ipc_file, "%s/.git-fsmonitor-%s",
40+
sock_dir, hash_to_hex(hash));
41+
} else {
42+
strbuf_addf(&ipc_file, "~/.git-fsmonitor-%s", hash_to_hex(hash));
43+
}
44+
free(sock_dir);
45+
46+
ipc_path = interpolate_path(ipc_file.buf, 1);
47+
if (!ipc_path)
48+
die(_("Invalid path: %s"), ipc_file.buf);
49+
50+
strbuf_release(&ipc_file);
51+
return ipc_path;
52+
}

compat/fsmonitor/fsm-ipc-win32.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include "config.h"
2+
#include "fsmonitor-ipc.h"
3+
4+
const char *fsmonitor_ipc__get_path(struct repository *r) {
5+
static char *ret;
6+
if (!ret)
7+
ret = git_pathdup("fsmonitor--daemon.ipc");
8+
return ret;
9+
}

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

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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+
9+
int fsmonitor__get_fs_info(const char *path, struct fs_info *fs_info)
10+
{
11+
struct statfs fs;
12+
if (statfs(path, &fs) == -1) {
13+
int saved_errno = errno;
14+
trace_printf_key(&trace_fsmonitor, "statfs('%s') failed: %s",
15+
path, strerror(saved_errno));
16+
errno = saved_errno;
17+
return -1;
18+
}
19+
20+
trace_printf_key(&trace_fsmonitor,
21+
"statfs('%s') [type 0x%08x][flags 0x%08x] '%s'",
22+
path, fs.f_type, fs.f_flags, fs.f_fstypename);
23+
24+
if (!(fs.f_flags & MNT_LOCAL))
25+
fs_info->is_remote = 1;
26+
else
27+
fs_info->is_remote = 0;
28+
29+
fs_info->typename = xstrdup(fs.f_fstypename);
30+
31+
trace_printf_key(&trace_fsmonitor,
32+
"'%s' is_remote: %d",
33+
path, fs_info->is_remote);
34+
return 0;
35+
}
36+
37+
int fsmonitor__is_fs_remote(const char *path)
38+
{
39+
struct fs_info fs;
40+
if (fsmonitor__get_fs_info(path, &fs))
41+
return -1;
42+
43+
free(fs.typename);
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+
}

0 commit comments

Comments
 (0)