All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Jeff Hostetler via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Jeff Hostetler <jeffhost@microsoft.com>,
	Jeff Hostetler <jeffhost@microsoft.com>
Subject: [PATCH 05/16] fsmonitor--daemon: refactor cookie handling for readability
Date: Fri, 11 Mar 2022 21:14:52 +0000	[thread overview]
Message-ID: <84df95be620c76afed73d1679722459e2ff32018.1647033303.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.1174.git.1647033303.gitgitgadget@gmail.com>

From: Jeff Hostetler <jeffhost@microsoft.com>

fixup! fsmonitor--daemon: use a cookie file to sync with file system

Use implicit definitions for FCIR_ enum values.

Remove const from cookie->name.

Reverse if then and else branches around open() to ease readability.

Document that we don't care about errors from close() and unlink().

Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com>
---
 builtin/fsmonitor--daemon.c | 53 +++++++++++++++++++++----------------
 1 file changed, 30 insertions(+), 23 deletions(-)

diff --git a/builtin/fsmonitor--daemon.c b/builtin/fsmonitor--daemon.c
index 97ca2a356e5..02a99ce98a2 100644
--- a/builtin/fsmonitor--daemon.c
+++ b/builtin/fsmonitor--daemon.c
@@ -109,14 +109,14 @@ static int do_as_client__status(void)
 
 enum fsmonitor_cookie_item_result {
 	FCIR_ERROR = -1, /* could not create cookie file ? */
-	FCIR_INIT = 0,
+	FCIR_INIT,
 	FCIR_SEEN,
 	FCIR_ABORT,
 };
 
 struct fsmonitor_cookie_item {
 	struct hashmap_entry entry;
-	const char *name;
+	char *name;
 	enum fsmonitor_cookie_item_result result;
 };
 
@@ -166,37 +166,44 @@ static enum fsmonitor_cookie_item_result with_lock__wait_for_cookie(
 	 * that the listener thread has seen it.
 	 */
 	fd = open(cookie_pathname.buf, O_WRONLY | O_CREAT | O_EXCL, 0600);
-	if (fd >= 0) {
-		close(fd);
-		unlink(cookie_pathname.buf);
-
-		/*
-		 * Technically, this is an infinite wait (well, unless another
-		 * thread sends us an abort).  I'd like to change this to
-		 * use `pthread_cond_timedwait()` and return an error/timeout
-		 * and let the caller do the trivial response thing, but we
-		 * don't have that routine in our thread-utils.
-		 *
-		 * After extensive beta testing I'm not really worried about
-		 * this.  Also note that the above open() and unlink() calls
-		 * will cause at least two FS events on that path, so the odds
-		 * of getting stuck are pretty slim.
-		 */
-		while (cookie->result == FCIR_INIT)
-			pthread_cond_wait(&state->cookies_cond,
-					  &state->main_lock);
-	} else {
+	if (fd < 0) {
 		error_errno(_("could not create fsmonitor cookie '%s'"),
 			    cookie->name);
 
 		cookie->result = FCIR_ERROR;
+		goto done;
 	}
 
+	/*
+	 * Technically, close() and unlink() can fail, but we don't
+	 * care here.  We only created the file to trigger a watch
+	 * event from the FS to know that when we're up to date.
+	 */
+	close(fd);
+	unlink(cookie_pathname.buf);
+
+	/*
+	 * Technically, this is an infinite wait (well, unless another
+	 * thread sends us an abort).  I'd like to change this to
+	 * use `pthread_cond_timedwait()` and return an error/timeout
+	 * and let the caller do the trivial response thing, but we
+	 * don't have that routine in our thread-utils.
+	 *
+	 * After extensive beta testing I'm not really worried about
+	 * this.  Also note that the above open() and unlink() calls
+	 * will cause at least two FS events on that path, so the odds
+	 * of getting stuck are pretty slim.
+	 */
+	while (cookie->result == FCIR_INIT)
+		pthread_cond_wait(&state->cookies_cond,
+				  &state->main_lock);
+
+done:
 	hashmap_remove(&state->cookies, &cookie->entry, NULL);
 
 	result = cookie->result;
 
-	free((char*)cookie->name);
+	free(cookie->name);
 	free(cookie);
 	strbuf_release(&cookie_pathname);
 
-- 
gitgitgadget


  parent reply	other threads:[~2022-03-11 22:43 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-11 21:14 [PATCH 00/16] Builtin FSMonitor Part 2.5 Jeff Hostetler via GitGitGadget
2022-03-11 21:14 ` [PATCH 01/16] t/test-lib: avoid using git on LHS of pipe Jeff Hostetler via GitGitGadget
2022-03-14  6:00   ` Junio C Hamano
2022-03-11 21:14 ` [PATCH 02/16] update-index: convert advise() messages back to warning() Jeff Hostetler via GitGitGadget
2022-03-14  6:08   ` Junio C Hamano
2022-03-21 18:47     ` Jeff Hostetler
2022-03-11 21:14 ` [PATCH 03/16] compat/fsmonitor/fsm-listen-darwin: split out GCC-specific declarations Jeff Hostetler via GitGitGadget
2022-03-11 21:14 ` [PATCH 04/16] t/helper/fsmonitor-client: cleanup call to parse_options() Jeff Hostetler via GitGitGadget
2022-03-14  7:58   ` Ævar Arnfjörð Bjarmason
2022-03-11 21:14 ` Jeff Hostetler via GitGitGadget [this message]
2022-03-14  8:00   ` [PATCH 05/16] fsmonitor--daemon: refactor cookie handling for readability Ævar Arnfjörð Bjarmason
2022-03-14 14:49     ` Derrick Stolee
2022-03-14 17:47       ` Junio C Hamano
2022-03-21 19:26         ` Jeff Hostetler
2022-03-11 21:14 ` [PATCH 06/16] t/perf/p7519: use grep rather than egrep in test Jeff Hostetler via GitGitGadget
2022-03-11 21:14 ` [PATCH 07/16] t/perf/p7519: cleanup coding style Jeff Hostetler via GitGitGadget
2022-03-14  8:01   ` Ævar Arnfjörð Bjarmason
2022-03-11 21:14 ` [PATCH 08/16] t7527: add parameters to start_daemon to handle args and subshell Jeff Hostetler via GitGitGadget
2022-03-14  8:03   ` Ævar Arnfjörð Bjarmason
2022-03-11 21:14 ` [PATCH 09/16] t7527: fix && chaining in matrix_try() Jeff Hostetler via GitGitGadget
2022-03-14  6:15   ` Junio C Hamano
2022-03-11 21:14 ` [PATCH 10/16] t7527: delete unused verify_status() function Jeff Hostetler via GitGitGadget
2022-03-11 21:14 ` [PATCH 11/16] fsmonitor-ipc: add _() to calls to die() Jeff Hostetler via GitGitGadget
2022-03-11 21:14 ` [PATCH 12/16] compat/fsmonitor/fsm-listen-darwin: add _() to calls to error() Jeff Hostetler via GitGitGadget
2022-03-11 21:15 ` [PATCH 13/16] compat/fsmonitor/fsm-listen-win32: " Jeff Hostetler via GitGitGadget
2022-03-11 21:15 ` [PATCH 14/16] fsmonitor--daemon: add _() to calls to die() Jeff Hostetler via GitGitGadget
2022-03-11 21:15 ` [PATCH 15/16] fsmonitor--daemon: add _() to calls to error() Jeff Hostetler via GitGitGadget
2022-03-11 21:15 ` [PATCH 16/16] fsmonitor-settings: simplify initialization of settings data Jeff Hostetler via GitGitGadget
2022-03-14 19:49   ` Junio C Hamano
2022-03-15 18:26     ` Jeff Hostetler
2022-03-15 19:06       ` Junio C Hamano
2022-03-13  8:02 ` [PATCH 00/16] Builtin FSMonitor Part 2.5 Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=84df95be620c76afed73d1679722459e2ff32018.1647033303.git.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=jeffhost@microsoft.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.