git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] safe_create_leading_directories: fix race that could give a false negative
@ 2013-03-16 19:30 Steven Walter
  2013-03-17  6:26 ` Junio C Hamano
  0 siblings, 1 reply; 4+ messages in thread
From: Steven Walter @ 2013-03-16 19:30 UTC (permalink / raw)
  To: git; +Cc: Steven Walter

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

^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2013-03-17 19:46 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-03-16 19:30 [PATCH] safe_create_leading_directories: fix race that could give a false negative Steven Walter
2013-03-17  6:26 ` Junio C Hamano
2013-03-17 14:09   ` Steven Walter
2013-03-17 19:45     ` Junio C Hamano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).