safe_create_leading_directories: fix race that could give a false negative
authorSteven Walter <stevenrwalter@gmail.com>
Sun, 17 Mar 2013 14:09:27 +0000 (10:09 -0400)
committerJunio C Hamano <gitster@pobox.com>
Wed, 27 Mar 2013 04:07:42 +0000 (21:07 -0700)
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 when the directory exists, taking the same codepath as
the case where the earlier stat() succeeds and finds a directory.

Signed-off-by: Steven Walter <stevenrwalter@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
sha1_file.c
index 9152974642986de2307ec4ed6beda4efa49e8383..964c4d44ab0bca796d8366b52867555965d58b62 100644 (file)
@@ -122,8 +122,13 @@ int safe_create_leading_directories(char *path)
                        }
                }
                else if (mkdir(path, 0777)) {
-                       *pos = '/';
-                       return -1;
+                       if (errno == EEXIST &&
+                           !stat(path, &st) && S_ISDIR(st.st_mode)) {
+                               ; /* somebody created it since we checked */
+                       } else {
+                               *pos = '/';
+                               return -1;
+                       }
                }
                else if (adjust_shared_perm(path)) {
                        *pos = '/';