Subject: Re: [PATCH 7/7] fsmonitor: addressed comments for patch 1352

From: Patrick Steinhardt <[email-masked]>
Date: Thu, 15 Feb 2024 14:49:38 +0100

On Thu, Feb 15, 2024 at 10:29:38AM +0000, marzi.esipreh via GitGitGadget wrote:
> From: "marzi.esipreh" <[email-masked]>
> 
> addressed comments on 1352, rebased, resolved conflicts

Please squash these changes into the preceding commits whereever
required.

Patrick

> 
> Signed-off-by: Marzieh Esipreh <[email-masked]>
> ---
>  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
>