diff options
author | David James <davidjames@google.com> | 2013-12-26 14:20:13 -0800 |
---|---|---|
committer | David James <davidjames@google.com> | 2013-12-26 14:59:00 -0800 |
commit | bf79c6618e4f51467e1d8e73915a541df660a060 (patch) | |
tree | 0a27f673b1bccdd9fac1478e078dedd4a629aa7a | |
parent | f045d49a71b64b854589c8821d8bf220c6ed9fd3 (diff) | |
download | git-repo-bf79c6618e4f51467e1d8e73915a541df660a060.tar.gz |
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
-rwxr-xr-x | repo | 25 |
1 files changed, 13 insertions, 12 deletions
@@ -110,6 +110,7 @@ REPO_MAIN = S_repo + '/main.py' # main script | |||
110 | MIN_PYTHON_VERSION = (2, 6) # minimum supported python version | 110 | MIN_PYTHON_VERSION = (2, 6) # minimum supported python version |
111 | 111 | ||
112 | 112 | ||
113 | import errno | ||
113 | import optparse | 114 | import optparse |
114 | import os | 115 | import os |
115 | import re | 116 | import re |
@@ -243,10 +244,10 @@ def _Init(args): | |||
243 | _print("fatal: invalid branch name '%s'" % branch, file=sys.stderr) | 244 | _print("fatal: invalid branch name '%s'" % branch, file=sys.stderr) |
244 | raise CloneFailure() | 245 | raise CloneFailure() |
245 | 246 | ||
246 | if not os.path.isdir(repodir): | 247 | try: |
247 | try: | 248 | os.mkdir(repodir) |
248 | os.mkdir(repodir) | 249 | except OSError as e: |
249 | except OSError as e: | 250 | if e.errno != errno.EEXIST: |
250 | _print('fatal: cannot make %s directory: %s' | 251 | _print('fatal: cannot make %s directory: %s' |
251 | % (repodir, e.strerror), file=sys.stderr) | 252 | % (repodir, e.strerror), file=sys.stderr) |
252 | # Don't raise CloneFailure; that would delete the | 253 | # Don't raise CloneFailure; that would delete the |
@@ -325,18 +326,18 @@ def NeedSetupGnuPG(): | |||
325 | 326 | ||
326 | 327 | ||
327 | def SetupGnuPG(quiet): | 328 | def SetupGnuPG(quiet): |
328 | if not os.path.isdir(home_dot_repo): | 329 | try: |
329 | try: | 330 | os.mkdir(home_dot_repo) |
330 | os.mkdir(home_dot_repo) | 331 | except OSError as e: |
331 | except OSError as e: | 332 | if e.errno != errno.EEXIST: |
332 | _print('fatal: cannot make %s directory: %s' | 333 | _print('fatal: cannot make %s directory: %s' |
333 | % (home_dot_repo, e.strerror), file=sys.stderr) | 334 | % (home_dot_repo, e.strerror), file=sys.stderr) |
334 | sys.exit(1) | 335 | sys.exit(1) |
335 | 336 | ||
336 | if not os.path.isdir(gpg_dir): | 337 | try: |
337 | try: | 338 | os.mkdir(gpg_dir, stat.S_IRWXU) |
338 | os.mkdir(gpg_dir, stat.S_IRWXU) | 339 | except OSError as e: |
339 | except OSError as e: | 340 | if e.errno != errno.EEXIST: |
340 | _print('fatal: cannot make %s directory: %s' % (gpg_dir, e.strerror), | 341 | _print('fatal: cannot make %s directory: %s' % (gpg_dir, e.strerror), |
341 | file=sys.stderr) | 342 | file=sys.stderr) |
342 | sys.exit(1) | 343 | sys.exit(1) |