git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 05/16] fsmonitor--daemon: refactor cookie handling for readability

From
JGJeff Hostetler via GitGitGadget <gitgitgadget@gmail.com>
Date
Mar 11, 2022, 21:14 UTC
Message-ID
<84df95be620c76afed73d1679722459e2ff32018.1647033303.git.gitgitgadget@gmail.com>
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
Previous: Junio C HamanoNext: Ævar Arnfjörð Bjarmason
Message 4 of 32 in “Builtin FSMonitor Part 2.5”
  1. 00/16 Builtin FSMonitor Part 2.5Jeff Hostetler via GitGitGadget, Mar 11, 2022
  2. 01/16 t/test-lib: avoid using git on LHS of pipeJeff Hostetler via GitGitGadget, Mar 11, 2022
  3. Junio C HamanoMar 14, 2022
  4. 05/16 fsmonitor--daemon: refactor cookie handling for readabilityJeff Hostetler via GitGitGadget, Mar 11, 2022
  5. Ævar Arnfjörð BjarmasonMar 14, 2022
  6. Derrick StoleeMar 14, 2022
  7. Junio C HamanoMar 14, 2022
  8. Jeff HostetlerMar 21, 2022
  9. 02/16 update-index: convert advise() messages back to warning()Jeff Hostetler via GitGitGadget, Mar 11, 2022
  10. Junio C HamanoMar 14, 2022
  11. Jeff HostetlerMar 21, 2022
  12. 10/16 t7527: delete unused verify_status() functionJeff Hostetler via GitGitGadget, Mar 11, 2022
  13. 11/16 fsmonitor-ipc: add _() to calls to die()Jeff Hostetler via GitGitGadget, Mar 11, 2022
  14. 15/16 fsmonitor--daemon: add _() to calls to error()Jeff Hostetler via GitGitGadget, Mar 11, 2022
  15. 12/16 compat/fsmonitor/fsm-listen-darwin: add _() to calls to error()Jeff Hostetler via GitGitGadget, Mar 11, 2022
  16. 03/16 compat/fsmonitor/fsm-listen-darwin: split out GCC-specific declarationsJeff Hostetler via GitGitGadget, Mar 11, 2022
  17. 06/16 t/perf/p7519: use grep rather than egrep in testJeff Hostetler via GitGitGadget, Mar 11, 2022
  18. 07/16 t/perf/p7519: cleanup coding styleJeff Hostetler via GitGitGadget, Mar 11, 2022
  19. Ævar Arnfjörð BjarmasonMar 14, 2022
  20. 14/16 fsmonitor--daemon: add _() to calls to die()Jeff Hostetler via GitGitGadget, Mar 11, 2022
  21. 09/16 t7527: fix && chaining in matrix_try()Jeff Hostetler via GitGitGadget, Mar 11, 2022
  22. Junio C HamanoMar 14, 2022
  23. 08/16 t7527: add parameters to start_daemon to handle args and subshellJeff Hostetler via GitGitGadget, Mar 11, 2022
  24. Ævar Arnfjörð BjarmasonMar 14, 2022
  25. 04/16 t/helper/fsmonitor-client: cleanup call to parse_options()Jeff Hostetler via GitGitGadget, Mar 11, 2022
  26. Ævar Arnfjörð BjarmasonMar 14, 2022
  27. 13/16 compat/fsmonitor/fsm-listen-win32: add _() to calls to error()Jeff Hostetler via GitGitGadget, Mar 11, 2022
  28. 16/16 fsmonitor-settings: simplify initialization of settings dataJeff Hostetler via GitGitGadget, Mar 11, 2022
  29. Junio C HamanoMar 14, 2022
  30. Jeff HostetlerMar 15, 2022
  31. Junio C HamanoMar 15, 2022
  32. Junio C HamanoMar 13, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.