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

[PATCH] safe_create_leading_directories: fix race that could give a false negative

From
Steven Walter <stevenrwalter@gmail.com>
Date
Mar 16, 2013, 19:30 UTC
Message-ID
<1363462256-5823-1-git-send-email-stevenrwalter@gmail.com>
If two processes are racing to create the same directory tree, they will
both see that the directory doesn't exist, both try to mkdir(), and one
of them will fail.  This is okay, as we only care that the directory
gets created.  So, we add a check for EEXIST from mkdir, and continue if
the directory now exists.
---
 sha1_file.c |    7 +++++++
 1 file changed, 7 insertions(+)
diff --git a/sha1_file.c b/sha1_file.c
index 40b2329..c7b7fec 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -123,6 +123,13 @@ int safe_create_leading_directories(char *path)
 			}
 		}
 		else if (mkdir(path, 0777)) {
+			if (errno == EEXIST) {
+				/* We could be racing with another process to
+				 * create the directory.  As long as the
+				 * directory gets created, we don't care. */
+				if (stat(path, &st) && S_ISDIR(st.st_mode))
+					continue;
+			}
 			*pos = '/';
 			return -1;
 		}
-- 
1.7.10.4
Next: Junio C Hamano
Message 1 of 4 in “safe_create_leading_directories: fix race that could give a false negative”
  1. safe_create_leading_directories: fix race that could give a false negativeSteven Walter, Mar 16, 2013
  2. Junio C HamanoMar 17, 2013
  3. safe_create_leading_directories: fix race that could give a false negativeSteven Walter, Mar 17, 2013
  4. Junio C HamanoMar 17, 2013

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.