[PATCH 2 of 5] posix: simplify checkexec check

Mads Kiilerich mads at kiilerich.com
Thu Nov 17 13:44:18 EST 2016


# HG changeset patch
# User Mads Kiilerich <madski at unity3d.com>
# Date 1421194526 -3600
#      Wed Jan 14 01:15:26 2015 +0100
# Node ID f383046bc26d39d307523521de2df5885ba111ff
# Parent  1b5e959ebd859c27b3369124c926a512e222545c
posix: simplify checkexec check

Use a slightly simpler logic that in some cases can avoid an unnecessary chmod
and stat.

Instead of flipping the X bits, make it more clear that we rely on no X bits
being set on initial file creation, and that at least some of them stick after
they all have been set.

diff --git a/mercurial/posix.py b/mercurial/posix.py
--- a/mercurial/posix.py
+++ b/mercurial/posix.py
@@ -166,16 +166,16 @@ def checkexec(path):
         fh, fn = tempfile.mkstemp(dir=cachedir, prefix='hg-checkexec-')
         try:
             os.close(fh)
-            m = os.stat(fn).st_mode & 0o777
-            new_file_has_exec = m & EXECFLAGS
-            os.chmod(fn, m ^ EXECFLAGS)
-            exec_flags_cannot_flip = ((os.stat(fn).st_mode & 0o777) == m)
+            m = os.stat(fn).st_mode
+            if m & EXECFLAGS:
+                return False
+            os.chmod(fn, m & 0o777 | EXECFLAGS)
+            return os.stat(fn).st_mode & EXECFLAGS
         finally:
             os.unlink(fn)
     except (IOError, OSError):
         # we don't care, the user probably won't be able to commit anyway
         return False
-    return not (new_file_has_exec or exec_flags_cannot_flip)
 
 def checklink(path):
     """check whether the given path is on a symlink-capable filesystem"""


More information about the Mercurial-devel mailing list