[PATCH 1 of 3] vfs: seek to the end of file when opening in append mode

Matt Harbison mharbison72 at gmail.com
Wed Feb 4 03:49:38 UTC 2015


# HG changeset patch
# User Matt Harbison <matt_harbison at yahoo.com>
# Date 1422725984 18000
#      Sat Jan 31 12:39:44 2015 -0500
# Node ID f78eca3cb69711a33cf70b0a3f84f3fe8f730e43
# Parent  e1dbe0b215ae137eec53ceb12440536d570a83d2
vfs: seek to the end of file when opening in append mode

The position is implementation defined when opening in append mode, and it seems
like Linux sets it to EOF while Windows keeps it at zero.  This has caused
problems in the past when a file is opened and tell() is immediately called,
such as 48c232873a54 and 6bf93440a717.

diff --git a/mercurial/scmutil.py b/mercurial/scmutil.py
--- a/mercurial/scmutil.py
+++ b/mercurial/scmutil.py
@@ -20,6 +20,8 @@
 systemrcpath = scmplatform.systemrcpath
 userrcpath = scmplatform.userrcpath
 
+_SEEK_END = 2 # os.SEEK_END was introduced in Python 2.5
+
 class status(tuple):
     '''Named tuple with a list of files per status. The 'deleted', 'unknown'
        and 'ignored' properties are only relevant to the working copy.
@@ -398,7 +400,13 @@
             if basename:
                 if atomictemp:
                     util.ensuredirs(dirname, self.createmode, notindexed)
-                    return util.atomictempfile(f, mode, self.createmode)
+                    fp = util.atomictempfile(f, mode, self.createmode)
+
+                    # The position when opening in append mode is implementation
+                    # defined, so make it consistent across all platforms.
+                    if 'a' in mode:
+                        fp.seek(0, _SEEK_END)
+                    return fp
                 try:
                     if 'w' in mode:
                         util.unlink(f)
@@ -424,6 +432,12 @@
         fp = util.posixfile(f, mode)
         if nlink == 0:
             self._fixfilemode(f)
+
+        # The position when opening in append mode is implementation defined, so
+        # make it consistent across all platforms.
+        if 'a' in mode:
+            fp.seek(0, _SEEK_END)
+
         return fp
 
     def symlink(self, src, dst):


More information about the Mercurial-devel mailing list