Skip to content

Commit b2fd5d0

Browse files
ptarjangitster
authored andcommitted
fsmonitor: implement filesystem change listener for Linux
Implement fsmonitor for Linux using the inotify API, bringing it to feature parity with existing Windows and macOS implementations. The Linux implementation uses inotify to monitor filesystem events. Unlike macOS's FSEvents which can watch a single root directory, inotify requires registering watches on every directory of interest. The implementation carefully handles directory renames and moves using inotify's cookie mechanism to track IN_MOVED_FROM/IN_MOVED_TO event pairs. Key implementation details: - Uses inotify_init1(O_NONBLOCK) for non-blocking event monitoring - Maintains bidirectional hashmaps between watch descriptors and paths for efficient event processing - Handles directory creation, deletion, and renames dynamically - Detects remote filesystems (NFS, CIFS, SMB, etc.) via statfs() - Falls back to $HOME/.git-fsmonitor-* for socket when .git is remote - Creates batches lazily (only for actual file events, not cookies) to avoid spurious sequence number increments Build configuration: - Enabled via FSMONITOR_DAEMON_BACKEND=linux and FSMONITOR_OS_SETTINGS=linux - Requires NO_PTHREADS and NO_UNIX_SOCKETS to be unset - Adds HAVE_LINUX_MAGIC_H for filesystem type detection Documentation updated to note that fsmonitor.socketDir is now supported on both Mac OS and Linux, and adds a section about inotify watch limits. Testing performed: - Build succeeds with standard flags and SANITIZE=address - All t7527-builtin-fsmonitor.sh tests pass on local filesystems - Remote filesystem detection correctly rejects network mounts Issues addressed from PR #1352 (git/git) review comments: - GPLv3 ME_REMOTE macro: Rewrote remote filesystem detection from scratch using statfs() and linux/magic.h constants (no GPLv3 code) - Memory leak on inotify_init1 failure: Added FREE_AND_NULL cleanup - Unsafe hashmap iteration in dtor: Collect entries first, then modify - Missing null checks in stop_async: Added proper guard conditions - dirname() modifying argument: Create copy with xstrdup() first - Non-portable f_fsid.__val: Use memcmp() for fsid comparison - Missing worktree null check: Added BUG() for null worktree - Header updates: Use git-compat-util.h, hash_to_hex_algop() - Code style: Use xstrdup() not xmemdupz(), proper pointer style Issues addressed from PR #1667 (git/git) review comments: - EINTR handling: read() now handles both EAGAIN and EINTR - Trailing pipe in log_mask_set: Added strbuf_strip_suffix() - Unchecked add_watch return: Now logs failure in rename_dir() - String building: Consolidated strbuf operations with strbuf_addf() - Translation markers: Added _() to all error_errno() messages Based on work from git#1352 by Eric DeCosta, and git#1667 by Marziyeh Esipreh, updated to work with the current codebase and address all review feedback. Signed-off-by: Paul Tarjan <github@paulisageek.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 68cb7f9 commit b2fd5d0

File tree

10 files changed

+1173
-6
lines changed

10 files changed

+1173
-6
lines changed

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

builtin/fsmonitor--daemon.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,6 +1405,7 @@ static int fsmonitor_run_daemon(void)
14051405
done:
14061406
pthread_cond_destroy(&state.cookies_cond);
14071407
pthread_mutex_destroy(&state.main_lock);
1408+
hashmap_clear(&state.cookies);
14081409
fsm_listen__dtor(&state);
14091410
fsm_health__dtor(&state);
14101411

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+
}

compat/fsmonitor/fsm-ipc-linux.c

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

0 commit comments

Comments
 (0)