[PATCH STABLE] opener: raise IOError on paths ending in '/'

Adrian Buehlmann adrian at cadifra.com
Sat Oct 30 05:56:51 CDT 2010


# HG changeset patch
# User Adrian Buehlmann <adrian at cadifra.com>
# Date 1288433569 -7200
# Branch stable
# Node ID 143169d6df8797ea1f4c9df69c3cbf7caf484386
# Parent  15ca4bfecfe343cbf53210b19c081676b2a35d3f
opener: raise IOError on paths ending in '/'

This improves 551aa6e27929 and 1634287b6ab1 by making sure we don't
call any further functions (e.g. atomictempfile) if we already know
that the path is not a valid file path.

It's also better not to call nlinks() for such malformed paths, so
let's move the path test out of the OSError exception handler.

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -859,14 +859,15 @@ class opener(object):
 
         nlink = -1
         if mode not in ("r", "rb"):
+            dirname, basename = os.path.split(f)
+            if not basename:
+                code = errno.EISDIR
+                raise IOError(code, os.strerror(code))
             try:
                 nlink = nlinks(f)
             except OSError:
                 nlink = 0
-                dirname, basename = os.path.split(f)
-                # Avoid calling makedirs when the path points to a
-                # directory -- the open will raise IOError below.
-                if basename and not os.path.isdir(dirname):
+                if not os.path.isdir(dirname):
                     makedirs(dirname, self.createmode)
             if atomictemp:
                 return atomictempfile(f, mode, self.createmode)


More information about the Mercurial-devel mailing list