Skip to content

Commit 928734d

Browse files
srwaltergitster
authored andcommitted
safe_create_leading_directories: fix race that could give a false negative
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7e20105 commit 928734d

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

sha1_file.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,13 @@ int safe_create_leading_directories(char *path)
122122
}
123123
}
124124
else if (mkdir(path, 0777)) {
125-
*pos = '/';
126-
return -1;
125+
if (errno == EEXIST &&
126+
!stat(path, &st) && S_ISDIR(st.st_mode)) {
127+
; /* somebody created it since we checked */
128+
} else {
129+
*pos = '/';
130+
return -1;
131+
}
127132
}
128133
else if (adjust_shared_perm(path)) {
129134
*pos = '/';

0 commit comments

Comments
 (0)