Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fsmonitor: completing a stale patch that Implements fsmonitor for Linux #1667

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions Documentation/config/fsmonitor--daemon.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ fsmonitor.allowRemote::
behavior. Only respected when `core.fsmonitor` is set to `true`.

fsmonitor.socketDir::
This Mac OS-specific option, if set, specifies the directory in
Mac OS and Linux-specific option. If set, specifies the directory in
which to create the Unix domain socket used for communication
between the fsmonitor daemon and various Git commands. The directory must
reside on a native Mac OS filesystem. Only respected when `core.fsmonitor`
reside on a native filesystem. Only respected when `core.fsmonitor`
is set to `true`.
26 changes: 17 additions & 9 deletions Documentation/git-fsmonitor--daemon.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,31 @@ repositories; this may be overridden by setting `fsmonitor.allowRemote` to
correctly with all network-mounted repositories, so such use is considered
experimental.

On Mac OS, the inter-process communication (IPC) between various Git
On Linux and Mac OS, the inter-process communication (IPC) between various Git
commands and the fsmonitor daemon is done via a Unix domain socket (UDS) -- a
special type of file -- which is supported by native Mac OS filesystems,
but not on network-mounted filesystems, NTFS, or FAT32. Other filesystems
may or may not have the needed support; the fsmonitor daemon is not guaranteed
to work with these filesystems and such use is considered experimental.
special type of file -- which is supported by many native Linux and Mac OS
filesystems, but not on network-mounted filesystems, NTFS, or FAT32. Other
filesystems may or may not have the needed support; the fsmonitor daemon is not
guaranteed to work with these filesystems and such use is considered
experimental.

By default, the socket is created in the `.git` directory. However, if the
`.git` directory is on a network-mounted filesystem, it will instead be
created at `$HOME/.git-fsmonitor-*` unless `$HOME` itself is on a
network-mounted filesystem, in which case you must set the configuration
variable `fsmonitor.socketDir` to the path of a directory on a Mac OS native
network-mounted filesystem in which case you must set the configuration
variable `fsmonitor.socketDir` to the path of a directory on a native
filesystem in which to create the socket file.

If none of the above directories (`.git`, `$HOME`, or `fsmonitor.socketDir`)
is on a native Mac OS file filesystem the fsmonitor daemon will report an
error that will cause the daemon and the currently running command to exit.
is on a native Linux or Mac OS filesystem the fsmonitor daemon will report
an error that will cause the daemon to exit and the currently running command
to issue a warning.

On Linux, the fsmonitor daemon registers a watch for each directory in the
repository. The default per-user limit for the number of watches on most Linux
systems is 8192. This may not be sufficient for large repositories or if
multiple instances of the fsmonitor daemon are running.
See https://watchexec.github.io/docs/inotify-limits.html[Linux inotify limits] for more information.

CONFIGURATION
-------------
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2088,6 +2088,10 @@ ifdef HAVE_CLOCK_GETTIME
BASIC_CFLAGS += -DHAVE_CLOCK_GETTIME

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the Git mailing list, Jean-Noël Avila wrote (reply to this):

Hello,

Le 15/02/2024 à 11:29, Eric DeCosta via GitGitGadget a écrit :
> From: Eric DeCosta <edecosta@mathworks.com>
> > Compare the given path to the mounted filesystems. Find the mount that is
> the longest prefix of the path (if any) and determine if that mount is on a
> local or remote filesystem.
> > Signed-off-by: Eric DeCosta <edecosta@mathworks.com>
> ---
>   Makefile                                |   4 +
>   compat/fsmonitor/fsm-path-utils-linux.c | 195 ++++++++++++++++++++++++
>   compat/fsmonitor/fsm-path-utils-linux.h |  91 +++++++++++
>   config.mak.uname                        |  11 ++
>   4 files changed, 301 insertions(+)
>   create mode 100644 compat/fsmonitor/fsm-path-utils-linux.c
>   create mode 100644 compat/fsmonitor/fsm-path-utils-linux.h
> > diff --git a/Makefile b/Makefile
> index 78e874099d9..0f36a0fd83a 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -2088,6 +2088,10 @@ ifdef HAVE_CLOCK_GETTIME
>   	BASIC_CFLAGS += -DHAVE_CLOCK_GETTIME
>   endif
>   > +ifdef HAVE_LINUX_MAGIC_H
> +	BASIC_CFLAGS += -DHAVE_LINUX_MAGIC_H
> +endif
> +
>   ifdef HAVE_CLOCK_MONOTONIC
>   	BASIC_CFLAGS += -DHAVE_CLOCK_MONOTONIC
>   endif
> diff --git a/compat/fsmonitor/fsm-path-utils-linux.c b/compat/fsmonitor/fsm-path-utils-linux.c
> new file mode 100644
> index 00000000000..c21d1349532
> --- /dev/null
> +++ b/compat/fsmonitor/fsm-path-utils-linux.c
> @@ -0,0 +1,195 @@
> +#include "git-compat-util.h"
> +#include "abspath.h"
> +#include "fsmonitor.h"
> +#include "fsmonitor-path-utils.h"
> +#include "fsm-path-utils-linux.h"
> +#include <errno.h>
> +#include <mntent.h>
> +#include <sys/mount.h>
> +#include <sys/vfs.h>
> +#include <sys/statvfs.h>
> +
> +static int is_remote_fs(const char *path)
> +{
> +	struct statfs fs;
> +
> +	if (statfs(path, &fs))
> +		return error_errno(_("statfs('%s') failed"), path);

For the sake of simplifying of the work of translators, would it be wise to change this to

+	if (statfs(path, &fs))
+		/* TRANSLATORS: %s('%s') is a libc function call */
+		return error_errno(_("%s('%s') failed"), "statfs", +				path);

and generalize this to all other messages?

Thanks,

JN

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the Git mailing list, Patrick Steinhardt wrote (reply to this):

On Thu, Feb 15, 2024 at 10:29:33AM +0000, Eric DeCosta via GitGitGadget wrote:
> From: Eric DeCosta <edecosta@mathworks.com>
> 
> Compare the given path to the mounted filesystems. Find the mount that is
> the longest prefix of the path (if any) and determine if that mount is on a
> local or remote filesystem.

It would be nice to motivate this change in the commit message. Right
now it only explains what the commit does, but it does not mention at
all why that would be a good idea in the first place. Explaining that
this is part of the interface that the existing fsmonitor infrastructure
expects to exist would help.

> Signed-off-by: Eric DeCosta <edecosta@mathworks.com>
> ---
>  Makefile                                |   4 +
>  compat/fsmonitor/fsm-path-utils-linux.c | 195 ++++++++++++++++++++++++
>  compat/fsmonitor/fsm-path-utils-linux.h |  91 +++++++++++
>  config.mak.uname                        |  11 ++
>  4 files changed, 301 insertions(+)
>  create mode 100644 compat/fsmonitor/fsm-path-utils-linux.c
>  create mode 100644 compat/fsmonitor/fsm-path-utils-linux.h
> 
> diff --git a/Makefile b/Makefile
> index 78e874099d9..0f36a0fd83a 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -2088,6 +2088,10 @@ ifdef HAVE_CLOCK_GETTIME
>  	BASIC_CFLAGS += -DHAVE_CLOCK_GETTIME
>  endif
>  
> +ifdef HAVE_LINUX_MAGIC_H
> +	BASIC_CFLAGS += -DHAVE_LINUX_MAGIC_H
> +endif
> +
>  ifdef HAVE_CLOCK_MONOTONIC
>  	BASIC_CFLAGS += -DHAVE_CLOCK_MONOTONIC
>  endif
> diff --git a/compat/fsmonitor/fsm-path-utils-linux.c b/compat/fsmonitor/fsm-path-utils-linux.c
> new file mode 100644
> index 00000000000..c21d1349532
> --- /dev/null
> +++ b/compat/fsmonitor/fsm-path-utils-linux.c
> @@ -0,0 +1,195 @@
> +#include "git-compat-util.h"
> +#include "abspath.h"
> +#include "fsmonitor.h"
> +#include "fsmonitor-path-utils.h"
> +#include "fsm-path-utils-linux.h"
> +#include <errno.h>
> +#include <mntent.h>
> +#include <sys/mount.h>
> +#include <sys/vfs.h>
> +#include <sys/statvfs.h>
> +
> +static int is_remote_fs(const char *path)
> +{
> +	struct statfs fs;
> +
> +	if (statfs(path, &fs))
> +		return error_errno(_("statfs('%s') failed"), path);
> +
> +	switch (fs.f_type) {
> +	case ACFS_SUPER_MAGIC:
> +	case AFS_SUPER_MAGIC:
> +	case CEPH_SUPER_MAGIC:
> +	case CIFS_SUPER_MAGIC:
> +	case CODA_SUPER_MAGIC:
> +	case FHGFS_SUPER_MAGIC:
> +	case GFS_SUPER_MAGIC:
> +	case GPFS_SUPER_MAGIC:
> +	case IBRIX_SUPER_MAGIC:
> +	case KAFS_SUPER_MAGIC:
> +	case LUSTRE_SUPER_MAGIC:
> +	case NCP_SUPER_MAGIC:
> +	case NFS_SUPER_MAGIC:
> +	case NFSD_SUPER_MAGIC:
> +	case OCFS2_SUPER_MAGIC:
> +	case PANFS_SUPER_MAGIC:
> +	case SMB_SUPER_MAGIC:
> +	case SMB2_SUPER_MAGIC:
> +	case SNFS_SUPER_MAGIC:
> +	case VMHGFS_SUPER_MAGIC:
> +	case VXFS_SUPER_MAGIC:
> +		return 1;
> +	default:
> +		return 0;
> +	}
> +}

This list doesn't feel all that maintainable to me, but so be it if
there is no better interface available.

> +static int find_mount(const char *path, const struct statvfs *fs,
> +			struct mntent *entry)

I don't quite understand why `find_mount()` is required in the first
place. Why can't we statfs(2) the path directly? This syscall provides
the fsid and should be sufficient for us to fill in `struct fs_info`.

Explaining details like this in the commit message would help guide the
reader's expectations.

Patrick

> +{
> +	const char *const mounts = "/proc/mounts";
> +	char *rp = real_pathdup(path, 1);
> +	struct mntent *ment = NULL;
> +	struct statvfs mntfs;
> +	FILE *fp;
> +	int found = 0;
> +	int ret = 0;
> +	size_t dlen, plen, flen = 0;
> +
> +	entry->mnt_fsname = NULL;
> +	entry->mnt_dir = NULL;
> +	entry->mnt_type = NULL;
> +
> +	fp = setmntent(mounts, "r");
> 
> +	if (!fp) {
> +		free(rp);
> +		return error_errno(_("setmntent('%s') failed"), mounts);
> +	}
> +
> +	plen = strlen(rp);
> +
> +	/* read all the mount information and compare to path */
> +	while ((ment = getmntent(fp))) {
> +		if (statvfs(ment->mnt_dir, &mntfs)) {
> +			switch (errno) {
> +			case EPERM:
> +			case ESRCH:
> +			case EACCES:
> +				continue;
> +			default:
> +				error_errno(_("statvfs('%s') failed"), ment->mnt_dir);
> +				ret = -1;
> +				goto done;
> +			}
> +		}
> +
> +		/* is mount on the same filesystem and is a prefix of the path */
> +		if ((fs->f_fsid == mntfs.f_fsid) &&
> +			!strncmp(ment->mnt_dir, rp, strlen(ment->mnt_dir))) {
> +			dlen = strlen(ment->mnt_dir);
> +			if (dlen > plen)
> +				continue;
> +			/*
> +			 * look for the longest prefix (including root)
> +			 */
> +			if (dlen > flen &&
> +				((dlen == 1 && ment->mnt_dir[0] == '/') ||
> +				 (!rp[dlen] || rp[dlen] == '/'))) {
> +				flen = dlen;
> +				found = 1;
> +
> +				/*
> +				 * https://man7.org/linux/man-pages/man3/getmntent.3.html
> +				 *
> +				 * The pointer points to a static area of memory which is
> +				 * overwritten by subsequent calls to getmntent().
> +				 */
> +				free(entry->mnt_fsname);
> +				free(entry->mnt_dir);
> +				free(entry->mnt_type);
> +				entry->mnt_fsname = xstrdup(ment->mnt_fsname);
> +				entry->mnt_dir = xstrdup(ment->mnt_dir);
> +				entry->mnt_type = xstrdup(ment->mnt_type);
> +			}
> +		}
> +	}
> +
> +done:
> +	free(rp);
> +	endmntent(fp);
> +
> +	if (!found)
> +		return -1;
> +
> +	return ret;
> +}
> +
> +int fsmonitor__get_fs_info(const char *path, struct fs_info *fs_info)
> +{
> +	int ret = 0;
> +	struct mntent entry;
> +	struct statvfs fs;
> +
> +	fs_info->is_remote = -1;
> +	fs_info->typename = NULL;
> +
> +	if (statvfs(path, &fs))
> +		return error_errno(_("statvfs('%s') failed"), path);
> +
> +	if (find_mount(path, &fs, &entry) < 0) {
> +		ret = -1;
> +		goto done;
> +	}
> +
> +	trace_printf_key(&trace_fsmonitor,
> +			 "statvfs('%s') [flags 0x%08lx] '%s' '%s'",
> +			 path, fs.f_flag, entry.mnt_type, entry.mnt_fsname);
> +
> +	fs_info->is_remote = is_remote_fs(entry.mnt_dir);
> +	fs_info->typename = xstrdup(entry.mnt_fsname);
> +
> +	if (fs_info->is_remote < 0)
> +		ret = -1;
> +
> +	trace_printf_key(&trace_fsmonitor,
> +				"'%s' is_remote: %d",
> +				path, fs_info->is_remote);
> +
> +done:
> +	free(entry.mnt_fsname);
> +	free(entry.mnt_dir);
> +	free(entry.mnt_type);
> +	return ret;
> +}
> +
> +int fsmonitor__is_fs_remote(const char *path)
> +{
> +	int ret = 0;
> +	struct fs_info fs;
> +
> +	if (fsmonitor__get_fs_info(path, &fs))
> +		ret = -1;
> +	else
> +		ret = fs.is_remote;
> +
> +	free(fs.typename);
> +
> +	return ret;
> +}
> +
> +/*
> + * No-op for now.
> + */
> +int fsmonitor__get_alias(const char *path, struct alias_info *info)
> +{
> +	return 0;
> +}
> +
> +/*
> + * No-op for now.
> + */
> +char *fsmonitor__resolve_alias(const char *path,
> +		const struct alias_info *info)
> +{
> +	return NULL;
> +}
> diff --git a/compat/fsmonitor/fsm-path-utils-linux.h b/compat/fsmonitor/fsm-path-utils-linux.h
> new file mode 100644
> index 00000000000..49bdb3c4728
> --- /dev/null
> +++ b/compat/fsmonitor/fsm-path-utils-linux.h
> @@ -0,0 +1,91 @@
> +#ifndef FSM_PATH_UTILS_LINUX_H
> +#define FSM_PATH_UTILS_LINUX_H
> +#endif
> +
> +#ifdef HAVE_LINUX_MAGIC_H
> +#include <linux/magic.h>
> +#endif
> +
> +#ifndef ACFS_SUPER_MAGIC
> +#define ACFS_SUPER_MAGIC 0x61636673
> +#endif
> +
> +#ifndef AFS_SUPER_MAGIC
> +#define AFS_SUPER_MAGIC 0x5346414f
> +#endif
> +
> +#ifndef CEPH_SUPER_MAGIC
> +#define CEPH_SUPER_MAGIC 0x00c36400
> +#endif
> +
> +#ifndef CIFS_SUPER_MAGIC
> +#define CIFS_SUPER_MAGIC 0xff534d42
> +#endif
> +
> +#ifndef CODA_SUPER_MAGIC
> +#define CODA_SUPER_MAGIC 0x73757245
> +#endif
> +
> +#ifndef FHGFS_SUPER_MAGIC
> +#define FHGFS_SUPER_MAGIC 0x19830326
> +#endif
> +
> +#ifndef GFS_SUPER_MAGIC
> +#define GFS_SUPER_MAGIC 0x1161970
> +#endif
> +
> +#ifndef GPFS_SUPER_MAGIC
> +#define GPFS_SUPER_MAGIC 0x47504653
> +#endif
> +
> +#ifndef IBRIX_SUPER_MAGIC
> +#define IBRIX_SUPER_MAGIC 0x013111a8
> +#endif
> +
> +#ifndef KAFS_SUPER_MAGIC
> +#define KAFS_SUPER_MAGIC 0x6b414653
> +#endif
> +
> +#ifndef LUSTRE_SUPER_MAGIC
> +#define LUSTRE_SUPER_MAGIC 0x0bd00bd0
> +#endif
> +
> +#ifndef NCP_SUPER_MAGIC
> +#define NCP_SUPER_MAGIC 0x564c
> +#endif
> +
> +#ifndef NFS_SUPER_MAGIC
> +#define NFS_SUPER_MAGIC 0x6969
> +#endif
> +
> +#ifndef NFSD_SUPER_MAGIC
> +#define NFSD_SUPER_MAGIC 0x6e667364
> +#endif
> +
> +#ifndef OCFS2_SUPER_MAGIC
> +#define OCFS2_SUPER_MAGIC 0x7461636f
> +#endif
> +
> +#ifndef PANFS_SUPER_MAGIC
> +#define PANFS_SUPER_MAGIC 0xaad7aaea
> +#endif
> +
> +#ifndef SMB_SUPER_MAGIC
> +#define SMB_SUPER_MAGIC 0x517b
> +#endif
> +
> +#ifndef SMB2_SUPER_MAGIC
> +#define SMB2_SUPER_MAGIC 0xfe534d42
> +#endif
> +
> +#ifndef SNFS_SUPER_MAGIC
> +#define SNFS_SUPER_MAGIC 0xbeefdead
> +#endif
> +
> +#ifndef VMHGFS_SUPER_MAGIC
> +#define VMHGFS_SUPER_MAGIC 0xbacbacbc
> +#endif
> +
> +#ifndef VXFS_SUPER_MAGIC
> +#define VXFS_SUPER_MAGIC 0xa501fcf5
> +#endif
> diff --git a/config.mak.uname b/config.mak.uname
> index dacc95172dc..80d7e2a2e68 100644
> --- a/config.mak.uname
> +++ b/config.mak.uname
> @@ -68,6 +68,17 @@ ifeq ($(uname_S),Linux)
>  	ifneq ($(findstring .el7.,$(uname_R)),)
>  		BASIC_CFLAGS += -std=c99
>  	endif
> +	ifeq ($(shell test -f /usr/include/linux/magic.h && echo y),y)
> +		HAVE_LINUX_MAGIC_H = YesPlease
> +	endif
> +	# The builtin FSMonitor on Linux builds upon Simple-IPC.  Both require
> +	# Unix domain sockets and PThreads.
> +	ifndef NO_PTHREADS
> +	ifndef NO_UNIX_SOCKETS
> +	FSMONITOR_DAEMON_BACKEND = linux
> +	FSMONITOR_OS_SETTINGS = linux
> +	endif
> +	endif
>  endif
>  ifeq ($(uname_S),GNU/kFreeBSD)
>  	HAVE_ALLOCA_H = YesPlease
> -- 
> gitgitgadget
> 
> 

endif

ifdef HAVE_LINUX_MAGIC_H
BASIC_CFLAGS += -DHAVE_LINUX_MAGIC_H
endif

ifdef HAVE_CLOCK_MONOTONIC
BASIC_CFLAGS += -DHAVE_CLOCK_MONOTONIC
endif
Expand Down
24 changes: 24 additions & 0 deletions compat/fsmonitor/fsm-health-linux.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "git-compat-util.h"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the Git mailing list, Patrick Steinhardt wrote (reply to this):

On Thu, Feb 15, 2024 at 10:29:38AM +0000, marzi.esipreh via GitGitGadget wrote:
> From: "marzi.esipreh" <marzi.esipreh@uber.com>
> 
> addressed comments on 1352, rebased, resolved conflicts

Please squash these changes into the preceding commits whereever
required.

Patrick

> 
> Signed-off-by: Marzieh Esipreh <m.ispare63@gmail.com>
> ---
>  compat/fsmonitor/fsm-health-linux.c     |   2 +-
>  compat/fsmonitor/fsm-ipc-unix.c         |   6 +-
>  compat/fsmonitor/fsm-listen-linux.c     | 170 ++++++++++++------------
>  compat/fsmonitor/fsm-path-utils-linux.c |   1 +
>  compat/fsmonitor/fsm-settings-unix.c    |   3 +
>  5 files changed, 95 insertions(+), 87 deletions(-)
> 
> diff --git a/compat/fsmonitor/fsm-health-linux.c b/compat/fsmonitor/fsm-health-linux.c
> index b9f709e8548..4c291f8a066 100644
> --- a/compat/fsmonitor/fsm-health-linux.c
> +++ b/compat/fsmonitor/fsm-health-linux.c
> @@ -1,4 +1,4 @@
> -#include "cache.h"
> +#include "git-compat-util.h"
>  #include "config.h"
>  #include "fsmonitor.h"
>  #include "fsm-health.h"
> diff --git a/compat/fsmonitor/fsm-ipc-unix.c b/compat/fsmonitor/fsm-ipc-unix.c
> index eb25123fa12..70afddfd298 100644
> --- a/compat/fsmonitor/fsm-ipc-unix.c
> +++ b/compat/fsmonitor/fsm-ipc-unix.c
> @@ -1,10 +1,12 @@
> -#include "cache.h"
> +#include "git-compat-util.h"
>  #include "config.h"
>  #include "hex.h"
>  #include "strbuf.h"
>  #include "fsmonitor.h"
>  #include "fsmonitor-ipc.h"
>  #include "fsmonitor-path-utils.h"
> +#include "gettext.h"
> +#include "path.h"
>  
>  static GIT_PATH_FUNC(fsmonitor_ipc__get_default_path, "fsmonitor--daemon.ipc")
>  
> @@ -17,7 +19,7 @@ const char *fsmonitor_ipc__get_path(struct repository *r)
>  	unsigned char hash[GIT_MAX_RAWSZ];
>  
>  	if (!r)
> -		BUG("No repository passed into fsmonitor_ipc__get_path");
> +		BUG("no repository passed into fsmonitor_ipc__get_path");
>  
>  	if (ipc_path)
>  		return ipc_path;
> diff --git a/compat/fsmonitor/fsm-listen-linux.c b/compat/fsmonitor/fsm-listen-linux.c
> index e8548e4e009..84d8fb28d5d 100644
> --- a/compat/fsmonitor/fsm-listen-linux.c
> +++ b/compat/fsmonitor/fsm-listen-linux.c
> @@ -1,7 +1,10 @@
> -#include "cache.h"
> +#include "git-compat-util.h"
> +#include "config.h"
>  #include "fsmonitor.h"
>  #include "fsm-listen.h"
>  #include "fsmonitor--daemon.h"
> +#include "gettext.h"
> +#include "simple-ipc.h"
>  #include <dirent.h>
>  #include <fcntl.h>
>  #include <sys/inotify.h>
> @@ -129,15 +132,15 @@ static void remove_watch(struct watch_entry *w,
>  	hashmap_entry_init(&k1.ent, memhash(&w->wd, sizeof(int)));
>  	w1 = hashmap_remove_entry(&data->watches, &k1, ent, NULL);
>  	if (!w1)
> -		BUG("Double remove of watch for '%s'", w->dir);
> +		BUG("double remove of watch for '%s'", w->dir);
>  
>  	if (w1->cookie)
> -		BUG("Removing watch for '%s' which has a pending rename", w1->dir);
> +		BUG("removing watch for '%s' which has a pending rename", w1->dir);
>  
>  	hashmap_entry_init(&k2.ent, memhash(w->dir, strlen(w->dir)));
>  	w2 = hashmap_remove_entry(&data->revwatches, &k2, ent, NULL);
>  	if (!w2)
> -		BUG("Double remove of reverse watch for '%s'", w->dir);
> +		BUG("double remove of reverse watch for '%s'", w->dir);
>  
>  	/* w1->dir and w2->dir are interned strings, we don't own them */
>  	free(w1);
> @@ -187,7 +190,7 @@ static void add_dir_rename(uint32_t cookie, const char *path,
>  	hashmap_entry_init(&k.ent, memhash(path, strlen(path)));
>  	w = hashmap_get_entry(&data->revwatches, &k, ent, NULL);
>  	if (!w) /* should never happen */
> -		BUG("No watch for '%s'", path);
> +		BUG("no watch for '%s'", path);
>  	w->cookie = cookie;
>  
>  	/* add the pending rename to match against later */
> @@ -224,10 +227,10 @@ static void rename_dir(uint32_t cookie, const char *path,
>  			remove_watch(w, data);
>  			add_watch(path, data);
>  		} else {
> -			BUG("No matching watch");
> +			BUG("no matching watch");
>  		}
>  	} else {
> -		BUG("No matching cookie");
> +		BUG("no matching cookie");
>  	}
>  }
>  
> @@ -249,7 +252,7 @@ static int register_inotify(const char *path,
>  	if (!dir)
>  		return error_errno("opendir('%s') failed", path);
>  
> -	while ((de = readdir_skip_dot_and_dotdot(dir)) != NULL) {
> +	while ((de = readdir_skip_dot_and_dotdot(dir))) {
>  		strbuf_reset(&current);
>  		strbuf_addf(&current, "%s/%s", path, de->d_name);
>  		if (lstat(current.buf, &fs)) {
> @@ -353,7 +356,7 @@ static void log_mask_set(const char *path, u_int32_t mask)
>  	if (mask & IN_IGNORED)
>  		strbuf_addstr(&msg, "IN_IGNORED|");
>  	if (mask & IN_ISDIR)
> -		strbuf_addstr(&msg, "IN_ISDIR|");
> +		strbuf_addstr(&msg, "IN_ISDIR");
>  
>  	trace_printf_key(&trace_fsmonitor, "inotify_event: '%s', mask=%#8.8x %s",
>  				path, mask, msg.buf);
> @@ -373,8 +376,10 @@ int fsm_listen__ctor(struct fsmonitor_daemon_state *state)
>  	data->shutdown = SHUTDOWN_ERROR;
>  
>  	fd = inotify_init1(O_NONBLOCK);
> -	if (fd < 0)
> +	if (fd < 0) {
> +		FREE_AND_NULL(data);
>  		return error_errno("inotify_init1() failed");
> +	}
>  
>  	data->fd_inotify = fd;
>  
> @@ -386,12 +391,10 @@ int fsm_listen__ctor(struct fsmonitor_daemon_state *state)
>  		ret = -1;
>  	else if (register_inotify(state->path_worktree_watch.buf, state, NULL))
>  		ret = -1;
> -	else if (state->nr_paths_watching > 1) {
> -		if (add_watch(state->path_gitdir_watch.buf, data))
> -			ret = -1;
> -		else if (register_inotify(state->path_gitdir_watch.buf, state, NULL))
> -			ret = -1;
> -	}
> +	else if (state->nr_paths_watching > 1 &&
> +		 (add_watch(state->path_gitdir_watch.buf, data) ||
> +		  register_inotify(state->path_gitdir_watch.buf, state, NULL)))
> +		ret = -1;
>  
>  	if (!ret) {
>  		state->listen_error_code = 0;
> @@ -449,80 +452,80 @@ static int process_event(const char *path,
>  	const char *last_sep;
>  
>  	switch (fsmonitor_classify_path_absolute(state, path)) {
> -		case IS_INSIDE_DOT_GIT_WITH_COOKIE_PREFIX:
> -		case IS_INSIDE_GITDIR_WITH_COOKIE_PREFIX:
> -			/* Use just the filename of the cookie file. */
> -			last_sep = find_last_dir_sep(path);
> -			string_list_append(cookie_list,
> -					last_sep ? last_sep + 1 : path);
> -			break;
> -		case IS_INSIDE_DOT_GIT:
> -		case IS_INSIDE_GITDIR:
> -			break;
> -		case IS_DOT_GIT:
> -		case IS_GITDIR:
> -			/*
> -			* If .git directory is deleted or renamed away,
> -			* we have to quit.
> -			*/
> -			if (em_dir_deleted(event->mask)) {
> -				trace_printf_key(&trace_fsmonitor,
> -						"event: gitdir removed");
> -				state->listen_data->shutdown = SHUTDOWN_FORCE;
> -				goto done;
> -			}
> +	case IS_INSIDE_DOT_GIT_WITH_COOKIE_PREFIX:
> +	case IS_INSIDE_GITDIR_WITH_COOKIE_PREFIX:
> +		/* Use just the filename of the cookie file. */
> +		last_sep = find_last_dir_sep(path);
> +		string_list_append(cookie_list,
> +				last_sep ? last_sep + 1 : path);
> +		break;
> +	case IS_INSIDE_DOT_GIT:
> +	case IS_INSIDE_GITDIR:
> +		break;
> +	case IS_DOT_GIT:
> +	case IS_GITDIR:
> +		/*
> +		* If .git directory is deleted or renamed away,
> +		* we have to quit.
> +		*/
> +		if (em_dir_deleted(event->mask)) {
> +			trace_printf_key(&trace_fsmonitor,
> +					"event: gitdir removed");
> +			state->listen_data->shutdown = SHUTDOWN_FORCE;
> +			goto done;
> +		}
>  
> -			if (em_dir_renamed(event->mask)) {
> -				trace_printf_key(&trace_fsmonitor,
> -						"event: gitdir renamed");
> -				state->listen_data->shutdown = SHUTDOWN_FORCE;
> -				goto done;
> -			}
> -			break;
> -		case IS_WORKDIR_PATH:
> -			/* normal events in the working directory */
> -			if (trace_pass_fl(&trace_fsmonitor))
> -				log_mask_set(path, event->mask);
> +		if (em_dir_renamed(event->mask)) {
> +			trace_printf_key(&trace_fsmonitor,
> +					"event: gitdir renamed");
> +			state->listen_data->shutdown = SHUTDOWN_FORCE;
> +			goto done;
> +		}
> +		break;
> +	case IS_WORKDIR_PATH:
> +		/* normal events in the working directory */
> +		if (trace_pass_fl(&trace_fsmonitor))
> +			log_mask_set(path, event->mask);
>  
> -			rel = path + state->path_worktree_watch.len + 1;
> -			fsmonitor_batch__add_path(batch, rel);
> +		rel = path + state->path_worktree_watch.len + 1;
> +		fsmonitor_batch__add_path(batch, rel);
>  
> -			if (em_dir_deleted(event->mask))
> -				break;
> +		if (em_dir_deleted(event->mask))
> +			break;
>  
> -			/* received IN_MOVE_FROM, add tracking for expected IN_MOVE_TO */
> -			if (em_rename_dir_from(event->mask))
> -				add_dir_rename(event->cookie, path, state->listen_data);
> +		/* received IN_MOVE_FROM, add tracking for expected IN_MOVE_TO */
> +		if (em_rename_dir_from(event->mask))
> +			add_dir_rename(event->cookie, path, state->listen_data);
>  
> -			/* received IN_MOVE_TO, update watch to reflect new path */
> -			if (em_rename_dir_to(event->mask)) {
> -				rename_dir(event->cookie, path, state->listen_data);
> -				if (register_inotify(path, state, batch)) {
> -					state->listen_data->shutdown = SHUTDOWN_ERROR;
> -					goto done;
> -				}
> +		/* received IN_MOVE_TO, update watch to reflect new path */
> +		if (em_rename_dir_to(event->mask)) {
> +			rename_dir(event->cookie, path, state->listen_data);
> +			if (register_inotify(path, state, batch)) {
> +				state->listen_data->shutdown = SHUTDOWN_ERROR;
> +				goto done;
>  			}
> +		}
>  
> -			if (em_dir_created(event->mask)) {
> -				if (add_watch(path, state->listen_data)) {
> -					state->listen_data->shutdown = SHUTDOWN_ERROR;
> -					goto done;
> -				}
> -				if (register_inotify(path, state, batch)) {
> -					state->listen_data->shutdown = SHUTDOWN_ERROR;
> -					goto done;
> -				}
> +		if (em_dir_created(event->mask)) {
> +			if (add_watch(path, state->listen_data)) {
> +				state->listen_data->shutdown = SHUTDOWN_ERROR;
> +				goto done;
>  			}
> -			break;
> -		case IS_OUTSIDE_CONE:
> -		default:
> -			trace_printf_key(&trace_fsmonitor,
> -					"ignoring '%s'", path);
> -			break;
> +			if (register_inotify(path, state, batch)) {
> +				state->listen_data->shutdown = SHUTDOWN_ERROR;
> +				goto done;
> +			}
> +		}
> +		break;
> +	case IS_OUTSIDE_CONE:
> +	default:
> +		trace_printf_key(&trace_fsmonitor,
> +				"ignoring '%s'", path);
> +		break;
>  	}
>  	return 0;
> -done:
> -	return -1;
> +	done:
> +		return -1;
>  }
>  
>  /*
> @@ -531,7 +534,7 @@ static int process_event(const char *path,
>   */
>  static void handle_events(struct fsmonitor_daemon_state *state)
>  {
> -	 /* See https://man7.org/linux/man-pages/man7/inotify.7.html */
> +	/* See https://man7.org/linux/man-pages/man7/inotify.7.html */
>  	char buf[4096]
>  		__attribute__ ((aligned(__alignof__(struct inotify_event))));
>  
> @@ -539,13 +542,12 @@ static void handle_events(struct fsmonitor_daemon_state *state)
>  	struct fsmonitor_batch *batch = NULL;
>  	struct string_list cookie_list = STRING_LIST_INIT_DUP;
>  	struct watch_entry k, *w;
> -	struct strbuf path;
>  	const struct inotify_event *event;
>  	int fd = state->listen_data->fd_inotify;
>  	ssize_t len;
>  	char *ptr, *p;
>  
> -	strbuf_init(&path, PATH_MAX);
> +	struct strbuf path = STRBUF_INIT;
>  
>  	for(;;) {
>  		len = read(fd, buf, sizeof(buf));
> @@ -581,7 +583,7 @@ static void handle_events(struct fsmonitor_daemon_state *state)
>  
>  			w = hashmap_get_entry(&watches, &k, ent, NULL);
>  			if (!w) /* should never happen */
> -				BUG("No watch for '%s'", event->name);
> +				BUG("no watch for '%s'", event->name);
>  
>  			/* directory watch was removed */
>  			if (em_remove_watch(event->mask)) {
> diff --git a/compat/fsmonitor/fsm-path-utils-linux.c b/compat/fsmonitor/fsm-path-utils-linux.c
> index c21d1349532..0e3b33ffa48 100644
> --- a/compat/fsmonitor/fsm-path-utils-linux.c
> +++ b/compat/fsmonitor/fsm-path-utils-linux.c
> @@ -3,6 +3,7 @@
>  #include "fsmonitor.h"
>  #include "fsmonitor-path-utils.h"
>  #include "fsm-path-utils-linux.h"
> +#include "gettext.h"
>  #include <errno.h>
>  #include <mntent.h>
>  #include <sys/mount.h>
> diff --git a/compat/fsmonitor/fsm-settings-unix.c b/compat/fsmonitor/fsm-settings-unix.c
> index d16dca89416..c9b75aa44fe 100644
> --- a/compat/fsmonitor/fsm-settings-unix.c
> +++ b/compat/fsmonitor/fsm-settings-unix.c
> @@ -1,6 +1,9 @@
> +#include "git-compat-util.h"
> +#include "config.h"
>  #include "fsmonitor.h"
>  #include "fsmonitor-ipc.h"
>  #include "fsmonitor-path-utils.h"
> +#include <stdint.h>
>  
>   /*
>   * For the builtin FSMonitor, we create the Unix domain socket for the
> -- 
> gitgitgadget
> 

#include "config.h"
#include "fsmonitor.h"
#include "fsm-health.h"
#include "fsmonitor--daemon.h"

int fsm_health__ctor(struct fsmonitor_daemon_state *state)
{
return 0;
}

void fsm_health__dtor(struct fsmonitor_daemon_state *state)
{
return;
}

void fsm_health__loop(struct fsmonitor_daemon_state *state)
{
return;
}

void fsm_health__stop_async(struct fsmonitor_daemon_state *state)
{
}
57 changes: 1 addition & 56 deletions compat/fsmonitor/fsm-ipc-darwin.c
Original file line number Diff line number Diff line change
@@ -1,56 +1 @@
#include "git-compat-util.h"
#include "config.h"
#include "gettext.h"
#include "hex.h"
#include "path.h"
#include "repository.h"
#include "strbuf.h"
#include "fsmonitor-ll.h"
#include "fsmonitor-ipc.h"
#include "fsmonitor-path-utils.h"

static GIT_PATH_FUNC(fsmonitor_ipc__get_default_path, "fsmonitor--daemon.ipc")

const char *fsmonitor_ipc__get_path(struct repository *r)
{
static const char *ipc_path = NULL;
git_SHA_CTX sha1ctx;
char *sock_dir = NULL;
struct strbuf ipc_file = STRBUF_INIT;
unsigned char hash[GIT_MAX_RAWSZ];

if (!r)
BUG("No repository passed into fsmonitor_ipc__get_path");

if (ipc_path)
return ipc_path;


/* By default the socket file is created in the .git directory */
if (fsmonitor__is_fs_remote(r->gitdir) < 1) {
ipc_path = fsmonitor_ipc__get_default_path();
return ipc_path;
}

git_SHA1_Init(&sha1ctx);
git_SHA1_Update(&sha1ctx, r->worktree, strlen(r->worktree));
git_SHA1_Final(hash, &sha1ctx);

repo_config_get_string(r, "fsmonitor.socketdir", &sock_dir);

/* Create the socket file in either socketDir or $HOME */
if (sock_dir && *sock_dir) {
strbuf_addf(&ipc_file, "%s/.git-fsmonitor-%s",
sock_dir, hash_to_hex(hash));
} else {
strbuf_addf(&ipc_file, "~/.git-fsmonitor-%s", hash_to_hex(hash));
}
free(sock_dir);

ipc_path = interpolate_path(ipc_file.buf, 1);
if (!ipc_path)
die(_("Invalid path: %s"), ipc_file.buf);

strbuf_release(&ipc_file);
return ipc_path;
}
#include "fsm-ipc-unix.c"
1 change: 1 addition & 0 deletions compat/fsmonitor/fsm-ipc-linux.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "fsm-ipc-unix.c"
55 changes: 55 additions & 0 deletions compat/fsmonitor/fsm-ipc-unix.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include "git-compat-util.h"
#include "config.h"
#include "hex.h"
#include "strbuf.h"
#include "fsmonitor.h"
#include "fsmonitor-ipc.h"
#include "fsmonitor-path-utils.h"
#include "gettext.h"
#include "path.h"

static GIT_PATH_FUNC(fsmonitor_ipc__get_default_path, "fsmonitor--daemon.ipc")

const char *fsmonitor_ipc__get_path(struct repository *r)
{
static const char *ipc_path = NULL;
git_SHA_CTX sha1ctx;
char *sock_dir = NULL;
struct strbuf ipc_file = STRBUF_INIT;
unsigned char hash[GIT_MAX_RAWSZ];

if (!r)
BUG("no repository passed into fsmonitor_ipc__get_path");

if (ipc_path)
return ipc_path;


/* By default the socket file is created in the .git directory */
if (fsmonitor__is_fs_remote(r->gitdir) < 1) {
ipc_path = fsmonitor_ipc__get_default_path();
return ipc_path;
}

git_SHA1_Init(&sha1ctx);
git_SHA1_Update(&sha1ctx, r->worktree, strlen(r->worktree));
git_SHA1_Final(hash, &sha1ctx);

repo_config_get_string(r, "fsmonitor.socketdir", &sock_dir);

/* Create the socket file in either socketDir or $HOME */
if (sock_dir && *sock_dir) {
strbuf_addf(&ipc_file, "%s/.git-fsmonitor-%s",
sock_dir, hash_to_hex(hash));
} else {
strbuf_addf(&ipc_file, "~/.git-fsmonitor-%s", hash_to_hex(hash));
}
free(sock_dir);

ipc_path = interpolate_path(ipc_file.buf, 1);
if (!ipc_path)
die(_("Invalid path: %s"), ipc_file.buf);

strbuf_release(&ipc_file);
return ipc_path;
}