Skip to content

Commit 03cf12d

Browse files
committed
fsmonitor: implement filesystem change listener for Linux
Implement the built-in fsmonitor daemon for Linux using the inotify API, bringing it to feature parity with the existing Windows and macOS implementations. The implementation uses inotify rather than fanotify because fanotify requires either CAP_SYS_ADMIN or CAP_PERFMON capabilities, making it unsuitable for an unprivileged user-space daemon. While inotify has the limitation of requiring a separate watch on every directory (unlike macOS's FSEvents, which can monitor an entire directory tree with a single watch), it operates without elevated privileges and provides the per-file event granularity needed for fsmonitor. The listener uses inotify_init1(O_NONBLOCK) with a poll loop that checks for events with a 50-millisecond timeout, keeping the inotify queue well-drained to minimize the risk of overflows. Bidirectional hashmaps map between watch descriptors and directory paths for efficient event resolution. Directory renames are tracked using inotify's cookie mechanism to correlate IN_MOVED_FROM and IN_MOVED_TO event pairs; a periodic check detects stale renames where the matching IN_MOVED_TO never arrived, forcing a resync. New directory creation triggers recursive watch registration to ensure all subdirectories are monitored. The IN_MASK_CREATE flag is used where available to prevent modifying existing watches, with a fallback for older kernels. When IN_MASK_CREATE is available and inotify_add_watch returns EEXIST, it means another thread or recursive scan has already registered the watch, so it is safe to ignore. Remote filesystem detection uses statfs() to identify network-mounted filesystems (NFS, CIFS, SMB, FUSE, etc.) via their magic numbers. Mount point information is read from /proc/mounts and matched against the statfs f_fsid to get accurate, human-readable filesystem type names for logging. When the .git directory is on a remote filesystem, the IPC socket falls back to $HOME or a user-configured directory via the fsmonitor.socketDir setting. Based-on-patch-by: Eric DeCosta <edecosta@mathworks.com> Based-on-patch-by: Marziyeh Esipreh <marziyeh.esipreh@gmail.com> Signed-off-by: Paul Tarjan <github@paulisageek.com>
1 parent b2aaadb commit 03cf12d

8 files changed

Lines changed: 1042 additions & 8 deletions

File tree

Documentation/config/fsmonitor--daemon.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ fsmonitor.allowRemote::
44
behavior. Only respected when `core.fsmonitor` is set to `true`.
55

66
fsmonitor.socketDir::
7-
This Mac OS-specific option, if set, specifies the directory in
7+
This Mac OS and Linux-specific option, if set, specifies the directory in
88
which to create the Unix domain socket used for communication
99
between the fsmonitor daemon and various Git commands. The directory must
10-
reside on a native Mac OS filesystem. Only respected when `core.fsmonitor`
10+
reside on a native filesystem. Only respected when `core.fsmonitor`
1111
is set to `true`.

Documentation/git-fsmonitor--daemon.adoc

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ repositories; this may be overridden by setting `fsmonitor.allowRemote` to
7676
correctly with all network-mounted repositories, so such use is considered
7777
experimental.
7878

79-
On Mac OS, the inter-process communication (IPC) between various Git
79+
On Mac OS and Linux, the inter-process communication (IPC) between various Git
8080
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,
81+
special type of file -- which is supported by native Mac OS and Linux filesystems,
8282
but not on network-mounted filesystems, NTFS, or FAT32. Other filesystems
8383
may or may not have the needed support; the fsmonitor daemon is not guaranteed
8484
to work with these filesystems and such use is considered experimental.
@@ -87,13 +87,33 @@ By default, the socket is created in the `.git` directory. However, if the
8787
`.git` directory is on a network-mounted filesystem, it will instead be
8888
created at `$HOME/.git-fsmonitor-*` unless `$HOME` itself is on a
8989
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
90+
variable `fsmonitor.socketDir` to the path of a directory on a native
9191
filesystem in which to create the socket file.
9292

9393
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
94+
is on a native filesystem the fsmonitor daemon will report an
9595
error that will cause the daemon and the currently running command to exit.
9696

97+
LINUX CAVEATS
98+
~~~~~~~~~~~~~
99+
100+
On Linux, the fsmonitor daemon uses inotify to monitor filesystem events.
101+
The inotify system has per-user limits on the number of watches that can
102+
be created. The default limit is typically 8192 watches per user.
103+
104+
For large repositories with many directories, you may need to increase
105+
this limit. Check the current limit with:
106+
107+
cat /proc/sys/fs/inotify/max_user_watches
108+
109+
To temporarily increase the limit:
110+
111+
sudo sysctl fs.inotify.max_user_watches=65536
112+
113+
To make the change permanent, add to `/etc/sysctl.conf`:
114+
115+
fs.inotify.max_user_watches=65536
116+
97117
CONFIGURATION
98118
-------------
99119
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include "git-compat-util.h"
2+
#include "config.h"
3+
#include "fsmonitor-ll.h"
4+
#include "fsm-health.h"
5+
#include "fsmonitor--daemon.h"
6+
7+
/*
8+
* The Linux fsmonitor implementation uses inotify which has its own
9+
* mechanisms for detecting filesystem unmount and other events that
10+
* would require the daemon to shutdown. Therefore, we don't need
11+
* a separate health thread like Windows does.
12+
*
13+
* These stub functions satisfy the interface requirements.
14+
*/
15+
16+
int fsm_health__ctor(struct fsmonitor_daemon_state *state UNUSED)
17+
{
18+
return 0;
19+
}
20+
21+
void fsm_health__dtor(struct fsmonitor_daemon_state *state UNUSED)
22+
{
23+
return;
24+
}
25+
26+
void fsm_health__loop(struct fsmonitor_daemon_state *state UNUSED)
27+
{
28+
return;
29+
}
30+
31+
void fsm_health__stop_async(struct fsmonitor_daemon_state *state UNUSED)
32+
{
33+
}

0 commit comments

Comments
 (0)