From bf79c6618e4f51467e1d8e73915a541df660a060 Mon Sep 17 00:00:00 2001 From: David James Date: Thu, 26 Dec 2013 14:20:13 -0800 Subject: [PATCH] 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 --- repo | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/repo b/repo index d81cdd60..56d784fb 100755 --- a/repo +++ b/repo @@ -110,6 +110,7 @@ REPO_MAIN = S_repo + '/main.py' # main script MIN_PYTHON_VERSION = (2, 6) # minimum supported python version +import errno import optparse import os import re @@ -243,10 +244,10 @@ def _Init(args): _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 NeedSetupGnuPG(): 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)