opener: always reset flags on 'w'rite
authorAdrian Buehlmann <adrian@cadifra.com>
Fri, 10 Dec 2010 15:14:05 +0100
changeset 13112 039a964dbbb3
parent 13111 560b8001f765
child 13113 a98a90023261
opener: always reset flags on 'w'rite only the patcher needs to preserve flags on write
mercurial/patch.py
mercurial/util.py
--- a/mercurial/patch.py	Thu Dec 09 16:52:14 2010 -0500
+++ b/mercurial/patch.py	Fri Dec 10 15:14:05 2010 +0100
@@ -6,7 +6,7 @@
 # This software may be used and distributed according to the terms of the
 # GNU General Public License version 2 or any later version.
 
-import cStringIO, email.Parser, os, re
+import cStringIO, email.Parser, os, errno, re
 import tempfile, zlib
 
 from i18n import _
@@ -429,10 +429,16 @@
         # Ensure supplied data ends in fname, being a regular file or
         # a symlink. cmdutil.updatedir will -too magically- take care
         # of setting it to the proper type afterwards.
+        st_mode = None
         islink = os.path.islink(fname)
         if islink:
             fp = cStringIO.StringIO()
         else:
+            try:
+                st_mode = os.lstat(fname).st_mode & 0777
+            except OSError, e:
+                if e.errno != errno.ENOENT:
+                    raise
             fp = self.opener(fname, 'w')
         try:
             if self.eolmode == 'auto':
@@ -451,6 +457,8 @@
                 fp.writelines(lines)
             if islink:
                 self.opener.symlink(fp.getvalue(), fname)
+            if st_mode is not None:
+                os.chmod(fname, st_mode)
         finally:
             fp.close()
 
--- a/mercurial/util.py	Thu Dec 09 16:52:14 2010 -0500
+++ b/mercurial/util.py	Fri Dec 10 15:14:05 2010 +0100
@@ -882,7 +882,6 @@
             mode += "b" # for that other OS
 
         nlink = -1
-        st_mode = None
         dirname, basename = os.path.split(f)
         # If basename is empty, then the path is malformed because it points
         # to a directory. Let the posixfile() call below raise IOError.
@@ -893,7 +892,6 @@
                 return atomictempfile(f, mode, self.createmode)
             try:
                 if 'w' in mode:
-                    st_mode = os.lstat(f).st_mode & 0777
                     os.unlink(f)
                     nlink = 0
                 else:
@@ -913,10 +911,7 @@
                     rename(mktempcopy(f), f)
         fp = posixfile(f, mode)
         if nlink == 0:
-            if st_mode is None:
-                self._fixfilemode(f)
-            else:
-                os.chmod(f, st_mode)
+            self._fixfilemode(f)
         return fp
 
     def symlink(self, src, dst):