mirror of
https://github.com/git/git.git
synced 2026-01-12 13:53:11 +09:00
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 https://github.com/git/git/pull/1352 by Eric DeCosta, and https://github.com/git/git/pull/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>
72 lines
1.9 KiB
C
72 lines
1.9 KiB
C
#include "git-compat-util.h"
|
|
#include "config.h"
|
|
#include "fsmonitor-ll.h"
|
|
#include "fsmonitor-ipc.h"
|
|
#include "fsmonitor-settings.h"
|
|
#include "fsmonitor-path-utils.h"
|
|
|
|
#include <libgen.h>
|
|
|
|
/*
|
|
* For the builtin FSMonitor, we create the Unix domain socket for the
|
|
* IPC in the .git directory. If the working directory is remote,
|
|
* then the socket will be created on the remote file system. This
|
|
* can fail if the remote file system does not support UDS file types
|
|
* (e.g. smbfs to a Windows server) or if the remote kernel does not
|
|
* allow a non-local process to bind() the socket. (These problems
|
|
* could be fixed by moving the UDS out of the .git directory and to a
|
|
* well-known local directory on the client machine, but care should
|
|
* be taken to ensure that $HOME is actually local and not a managed
|
|
* file share.)
|
|
*
|
|
* FAT32 and NTFS working directories are problematic too.
|
|
*
|
|
* The builtin FSMonitor uses a Unix domain socket in the .git
|
|
* directory for IPC. These Windows drive formats do not support
|
|
* Unix domain sockets, so mark them as incompatible for the daemon.
|
|
*/
|
|
static enum fsmonitor_reason check_uds_volume(struct repository *r)
|
|
{
|
|
struct fs_info fs;
|
|
const char *ipc_path = fsmonitor_ipc__get_path(r);
|
|
char *path;
|
|
char *dir;
|
|
|
|
/*
|
|
* Create a copy for dirname() since it may modify its argument.
|
|
*/
|
|
path = xstrdup(ipc_path);
|
|
dir = dirname(path);
|
|
|
|
if (fsmonitor__get_fs_info(dir, &fs) == -1) {
|
|
free(path);
|
|
return FSMONITOR_REASON_ERROR;
|
|
}
|
|
|
|
free(path);
|
|
|
|
if (fs.is_remote ||
|
|
!strcmp(fs.typename, "msdos") ||
|
|
!strcmp(fs.typename, "ntfs") ||
|
|
!strcmp(fs.typename, "vfat")) {
|
|
free(fs.typename);
|
|
return FSMONITOR_REASON_NOSOCKETS;
|
|
}
|
|
|
|
free(fs.typename);
|
|
return FSMONITOR_REASON_OK;
|
|
}
|
|
|
|
enum fsmonitor_reason fsm_os__incompatible(struct repository *r, int ipc)
|
|
{
|
|
enum fsmonitor_reason reason;
|
|
|
|
if (ipc) {
|
|
reason = check_uds_volume(r);
|
|
if (reason != FSMONITOR_REASON_OK)
|
|
return reason;
|
|
}
|
|
|
|
return FSMONITOR_REASON_OK;
|
|
}
|