atomicfile: don't copy the original file if it'll be truncated
authorAlexis S. L. Carvalho <alexis@cecm.usp.br>
Mon, 09 Apr 2007 04:24:17 -0300
changeset 4331 ce52deed83bc
parent 4330 1b9fc3f48861
child 4332 4e5e1638b165
atomicfile: don't copy the original file if it'll be truncated
mercurial/util.py
--- a/mercurial/util.py	Mon Apr 09 04:24:17 2007 -0300
+++ b/mercurial/util.py	Mon Apr 09 04:24:17 2007 -0300
@@ -1005,7 +1005,7 @@
     p = base
     audit_p = audit
 
-    def mktempcopy(name):
+    def mktempcopy(name, emptyok=False):
         d, fn = os.path.split(name)
         fd, temp = tempfile.mkstemp(prefix='.%s-' % fn, dir=d)
         os.close(fd)
@@ -1019,6 +1019,8 @@
                 raise
             st_mode = 0666 & ~_umask
         os.chmod(temp, st_mode)
+        if emptyok:
+            return temp
         try:
             try:
                 ifp = posixfile(name, "rb")
@@ -1043,7 +1045,7 @@
         """the file will only be copied when rename is called"""
         def __init__(self, name, mode):
             self.__name = name
-            self.temp = mktempcopy(name)
+            self.temp = mktempcopy(name, emptyok=('w' in mode))
             posixfile.__init__(self, self.temp, mode)
         def rename(self):
             if not self.closed: