Only set mode of new patch if the target file was removed before.
authorThomas Arendsen Hein <thomas@intevation.de>
Thu, 25 Oct 2007 19:40:56 +0200
changeset 5477 bff41174563f
parent 5476 b3afa6725082
child 5478 5223c360503e
child 5492 dd5ca84ed868
Only set mode of new patch if the target file was removed before. If the file is writable by the user, but owned by a different user, the chmod will otherwise fail with "Operation not permitted". Additionally make very sure that the file is only written if either the number of links is <= 1 or the file was successfully removed. Maybe this minimal COW code should be replaced by something from util.
mercurial/patch.py
--- a/mercurial/patch.py	Wed Oct 24 22:15:45 2007 +0200
+++ b/mercurial/patch.py	Thu Oct 25 19:40:56 2007 +0200
@@ -9,7 +9,7 @@
 from i18n import _
 from node import *
 import base85, cmdutil, mdiff, util, context, revlog, diffhelpers
-import cStringIO, email.Parser, os, popen2, re, sha
+import cStringIO, email.Parser, os, popen2, re, sha, errno
 import sys, tempfile, zlib
 
 class PatchError(Exception):
@@ -402,11 +402,13 @@
             st = None
             try:
                 st = os.lstat(dest)
-                if st.st_nlink > 1:
-                    os.unlink(dest)
-            except: pass
+            except OSError, inst:
+                if inst.errno != errno.ENOENT:
+                    raise
+            if st and st.st_nlink > 1:
+                os.unlink(dest)
             fp = file(dest, 'wb')
-            if st:
+            if st and st.st_nlink > 1:
                 os.chmod(dest, st.st_mode)
             fp.writelines(self.lines)
             fp.close()