git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Steven Walter <stevenrwalter@gmail.com>
To: git@vger.kernel.org, gitster@pobox.com
Cc: Steven Walter <stevenrwalter@gmail.com>
Subject: [PATCH] safe_create_leading_directories: fix race that could give a false negative
Date: Sun, 17 Mar 2013 10:09:27 -0400	[thread overview]
Message-ID: <1363529367-5919-1-git-send-email-stevenrwalter@gmail.com> (raw)
In-Reply-To: <7v7gl6sfsg.fsf@alter.siamese.dyndns.org>

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.

Signed-off-by: Steven Walter <stevenrwalter@gmail.com>
---
 sha1_file.c |    9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/sha1_file.c b/sha1_file.c
index 40b2329..5668ecc 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -123,6 +123,15 @@ 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

  reply	other threads:[~2013-03-17 14:10 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2013-03-17 19:45     ` 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=1363529367-5919-1-git-send-email-stevenrwalter@gmail.com \
    --to=stevenrwalter@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.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 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).