diff mercurial/scmutil.py @ 29017:07be86828e79 stable

util: fix race in makedirs() Update makedirs() to ignore EEXIST in case someone else has already created the directory in question. Previously the ensuredirs() function existed, and was nearly identical to makedirs() except that it fixed this race. Unfortunately ensuredirs() was only used in 3 places, and most code uses the racy makedirs() function. This fixes makedirs() to be non-racy, and replaces calls to ensuredirs() with makedirs(). In particular, mercurial.scmutil.origpath() used the racy makedirs() code, which could cause failures during "hg update" as it tried to create backup directories. This does slightly change the behavior of call sites using ensuredirs(): previously ensuredirs() would throw EEXIST if the path existed but was a regular file instead of a directory. It did this by explicitly checking os.path.isdir() after getting EEXIST. The makedirs() code did not do this and swallowed all EEXIST errors. I kept the makedirs() behavior, since it seemed preferable to avoid the extra stat call in the common case where this directory already exists. If the path does happen to be a file, the caller will almost certainly fail with an ENOTDIR error shortly afterwards anyway. I checked the 3 existing call sites of ensuredirs(), and this seems to be the case for them.
author Adam Simpkins <simpkins@fb.com>
date Tue, 26 Apr 2016 15:32:59 -0700
parents 826d457df138
children a032ebea4e97
line wrap: on
line diff
--- a/mercurial/scmutil.py	Sun Apr 24 21:35:30 2016 +0900
+++ b/mercurial/scmutil.py	Tue Apr 26 15:32:59 2016 -0700
@@ -539,7 +539,7 @@
             # to a directory. Let the posixfile() call below raise IOError.
             if basename:
                 if atomictemp:
-                    util.ensuredirs(dirname, self.createmode, notindexed)
+                    util.makedirs(dirname, self.createmode, notindexed)
                     return util.atomictempfile(f, mode, self.createmode)
                 try:
                     if 'w' in mode:
@@ -556,7 +556,7 @@
                     if e.errno != errno.ENOENT:
                         raise
                     nlink = 0
-                    util.ensuredirs(dirname, self.createmode, notindexed)
+                    util.makedirs(dirname, self.createmode, notindexed)
                 if nlink > 0:
                     if self._trustnlink is None:
                         self._trustnlink = nlink > 1 or util.checknlink(f)
@@ -583,7 +583,7 @@
         except OSError:
             pass
 
-        util.ensuredirs(os.path.dirname(linkname), self.createmode)
+        util.makedirs(os.path.dirname(linkname), self.createmode)
 
         if self._cansymlink:
             try: