Patchwork [2,of,5] posix: simplify checkexec check

login
register
mail settings
Submitter Mads Kiilerich
Date Nov. 17, 2016, 6:44 p.m.
Message ID <f383046bc26d39d30752.1479408258@madski>
Download mbox | patch
Permalink /patch/17621/
State Accepted
Headers show

Comments

Mads Kiilerich - Nov. 17, 2016, 6:44 p.m.
# HG changeset patch
# User Mads Kiilerich <madski@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.

Patch

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"""