Fix os.mkdir race condition.
This code checks whether a dir exists before creating it. In between the
check and the mkdir call, it is possible that another process will have
created the directory. We have seen this bug occur many times in
practice during our 'repo init' tests.
Change-Id: Ia47d39955739aa38fd303f4e90be7b4c50d9d4ba
diff --git a/repo b/repo
index d81cdd6..56d784f 100755
--- a/repo
+++ b/repo
@@ -110,6 +110,7 @@
MIN_PYTHON_VERSION = (2, 6) # minimum supported python version
+import errno
import optparse
import os
import re
@@ -243,10 +244,10 @@
_print("fatal: invalid branch name '%s'" % branch, file=sys.stderr)
raise CloneFailure()
- if not os.path.isdir(repodir):
- try:
- os.mkdir(repodir)
- except OSError as e:
+ try:
+ os.mkdir(repodir)
+ except OSError as e:
+ if e.errno != errno.EEXIST:
_print('fatal: cannot make %s directory: %s'
% (repodir, e.strerror), file=sys.stderr)
# Don't raise CloneFailure; that would delete the
@@ -325,18 +326,18 @@
def SetupGnuPG(quiet):
- if not os.path.isdir(home_dot_repo):
- try:
- os.mkdir(home_dot_repo)
- except OSError as e:
+ try:
+ os.mkdir(home_dot_repo)
+ except OSError as e:
+ if e.errno != errno.EEXIST:
_print('fatal: cannot make %s directory: %s'
% (home_dot_repo, e.strerror), file=sys.stderr)
sys.exit(1)
- if not os.path.isdir(gpg_dir):
- try:
- os.mkdir(gpg_dir, stat.S_IRWXU)
- except OSError as e:
+ try:
+ os.mkdir(gpg_dir, stat.S_IRWXU)
+ except OSError as e:
+ if e.errno != errno.EEXIST:
_print('fatal: cannot make %s directory: %s' % (gpg_dir, e.strerror),
file=sys.stderr)
sys.exit(1)