diff mbox series

[v2,04/16] fsmonitor: refactor refresh callback on directory events

Message ID 5516670e30e26c5b50c67b69e48e3e8a5e0d8990.1708658300.git.gitgitgadget@gmail.com (mailing list archive)
State Superseded
Headers show
Series FSMonitor edge cases on case-insensitive file systems | expand

Commit Message

Jeff Hostetler Feb. 23, 2024, 3:18 a.m. UTC
From: Jeff Hostetler <jeffhostetler@github.com>

Move the code to handle directory FSEvents (containing pathnames with
a trailing slash) into a helper function.

Signed-off-by: Jeff Hostetler <jeffhostetler@github.com>
---
 fsmonitor.c | 52 ++++++++++++++++++++++++++++++----------------------
 1 file changed, 30 insertions(+), 22 deletions(-)

Comments

Junio C Hamano Feb. 23, 2024, 8:18 a.m. UTC | #1
"Jeff Hostetler via GitGitGadget" <gitgitgadget@gmail.com> writes:

> From: Jeff Hostetler <jeffhostetler@github.com>
>
> Move the code to handle directory FSEvents (containing pathnames with
> a trailing slash) into a helper function.
>
> Signed-off-by: Jeff Hostetler <jeffhostetler@github.com>
> ---
>  fsmonitor.c | 52 ++++++++++++++++++++++++++++++----------------------
>  1 file changed, 30 insertions(+), 22 deletions(-)

Nothing unexpected to see here.  Looking good.

> diff --git a/fsmonitor.c b/fsmonitor.c
> index f670c509378..6fecae9aeb2 100644
> --- a/fsmonitor.c
> +++ b/fsmonitor.c
> @@ -183,6 +183,35 @@ static int query_fsmonitor_hook(struct repository *r,
>  	return result;
>  }
>  
> +static void handle_path_with_trailing_slash(
> +	struct index_state *istate, const char *name, int pos)
> +{
> +	int i;
> +
> +	/*
> +	 * The daemon can decorate directory events, such as
> +	 * moves or renames, with a trailing slash if the OS
> +	 * FS Event contains sufficient information, such as
> +	 * MacOS.
> +	 *
> +	 * Use this to invalidate the entire cone under that
> +	 * directory.
> +	 *
> +	 * We do not expect an exact match because the index
> +	 * does not normally contain directory entries, so we
> +	 * start at the insertion point and scan.
> +	 */
> +	if (pos < 0)
> +		pos = -pos - 1;
> +
> +	/* Mark all entries for the folder invalid */
> +	for (i = pos; i < istate->cache_nr; i++) {
> +		if (!starts_with(istate->cache[i]->name, name))
> +			break;
> +		istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
> +	}
> +}
> +
>  static void fsmonitor_refresh_callback(struct index_state *istate, char *name)
>  {
>  	int i, len = strlen(name);
> @@ -193,28 +222,7 @@ static void fsmonitor_refresh_callback(struct index_state *istate, char *name)
>  			 name, pos);
>  
>  	if (name[len - 1] == '/') {
> -		/*
> -		 * The daemon can decorate directory events, such as
> -		 * moves or renames, with a trailing slash if the OS
> -		 * FS Event contains sufficient information, such as
> -		 * MacOS.
> -		 *
> -		 * Use this to invalidate the entire cone under that
> -		 * directory.
> -		 *
> -		 * We do not expect an exact match because the index
> -		 * does not normally contain directory entries, so we
> -		 * start at the insertion point and scan.
> -		 */
> -		if (pos < 0)
> -			pos = -pos - 1;
> -
> -		/* Mark all entries for the folder invalid */
> -		for (i = pos; i < istate->cache_nr; i++) {
> -			if (!starts_with(istate->cache[i]->name, name))
> -				break;
> -			istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
> -		}
> +		handle_path_with_trailing_slash(istate, name, pos);
>  
>  		/*
>  		 * We need to remove the traling "/" from the path
diff mbox series

Patch

diff --git a/fsmonitor.c b/fsmonitor.c
index f670c509378..6fecae9aeb2 100644
--- a/fsmonitor.c
+++ b/fsmonitor.c
@@ -183,6 +183,35 @@  static int query_fsmonitor_hook(struct repository *r,
 	return result;
 }
 
+static void handle_path_with_trailing_slash(
+	struct index_state *istate, const char *name, int pos)
+{
+	int i;
+
+	/*
+	 * The daemon can decorate directory events, such as
+	 * moves or renames, with a trailing slash if the OS
+	 * FS Event contains sufficient information, such as
+	 * MacOS.
+	 *
+	 * Use this to invalidate the entire cone under that
+	 * directory.
+	 *
+	 * We do not expect an exact match because the index
+	 * does not normally contain directory entries, so we
+	 * start at the insertion point and scan.
+	 */
+	if (pos < 0)
+		pos = -pos - 1;
+
+	/* Mark all entries for the folder invalid */
+	for (i = pos; i < istate->cache_nr; i++) {
+		if (!starts_with(istate->cache[i]->name, name))
+			break;
+		istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
+	}
+}
+
 static void fsmonitor_refresh_callback(struct index_state *istate, char *name)
 {
 	int i, len = strlen(name);
@@ -193,28 +222,7 @@  static void fsmonitor_refresh_callback(struct index_state *istate, char *name)
 			 name, pos);
 
 	if (name[len - 1] == '/') {
-		/*
-		 * The daemon can decorate directory events, such as
-		 * moves or renames, with a trailing slash if the OS
-		 * FS Event contains sufficient information, such as
-		 * MacOS.
-		 *
-		 * Use this to invalidate the entire cone under that
-		 * directory.
-		 *
-		 * We do not expect an exact match because the index
-		 * does not normally contain directory entries, so we
-		 * start at the insertion point and scan.
-		 */
-		if (pos < 0)
-			pos = -pos - 1;
-
-		/* Mark all entries for the folder invalid */
-		for (i = pos; i < istate->cache_nr; i++) {
-			if (!starts_with(istate->cache[i]->name, name))
-				break;
-			istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
-		}
+		handle_path_with_trailing_slash(istate, name, pos);
 
 		/*
 		 * We need to remove the traling "/" from the path