[PATCH STABLE v2] posix: always seek to EOF when opening a file in append mode

Augie Fackler raf at durin42.com
Mon Jul 8 17:13:14 UTC 2019


# HG changeset patch
# User Augie Fackler <augie at google.com>
# Date 1562605940 14400
#      Mon Jul 08 13:12:20 2019 -0400
# Branch stable
# Node ID 8fcc0959fc813801606c449d30ab4f97a5fb3177
# Parent  93fed084ce360a8ed696b9be7d3b7fe8e47a1b5f
posix: always seek to EOF when opening a file in append mode

Python 3 already does this, so skip it there.

Consider the program:

   #include <stdio.h>

   int main() {
     FILE *f = fopen("narf", "w");
     fprintf(f, "narf\n");
     fclose(f);

     f = fopen("narf", "a");
     printf("%ld\n", ftell(f));
     fprintf(f, "troz\n");
     printf("%ld\n", ftell(f));

     return 0;
   }

on macOS, FreeBSD, and Linux with glibc, this program prints

   5
   10

but on musl libc (Alpine Linux and probably others) this prints

   0
   10

By my reading of
https://pubs.opengroup.org/onlinepubs/009695399/functions/fopen.html
this is technically correct, specifically:

> Opening a file with append mode (a as the first character in the
> mode argument) shall cause all subsequent writes to the file to be
> forced to the then current end-of-file, regardless of intervening
> calls to fseek().

in other words, the file position doesn't really matter in append-mode
files, and we can't depend on it being at all meaningful unless we
perform a seek() before tell() after open(..., 'a'). Experimentally
after a .write() we can do a .tell() and it'll always be reasonable,
but I'm unclear from reading the specification if that's a smart thing
to rely on. This matches what we do on Windows and what Python 3 does
for free, so let's just be consistent. Thanks to Yuya for the idea.

diff --git a/mercurial/posix.py b/mercurial/posix.py
--- a/mercurial/posix.py
+++ b/mercurial/posix.py
@@ -30,7 +30,6 @@ from . import (
 
 osutil = policy.importmod(r'osutil')
 
-posixfile = open
 normpath = os.path.normpath
 samestat = os.path.samestat
 try:
@@ -52,6 +51,19 @@ expandglobs = False
 umask = os.umask(0)
 os.umask(umask)
 
+if not pycompat.ispy3:
+    def posixfile(name, mode=r'r', buffering=-1):
+        fp = open(name, mode=mode, buffering=buffering)
+        # The position when opening in append mode is implementation defined, so
+        # make it consistent by always seeking to the end.
+        if r'a' in mode:
+            fp.seek(0, os.SEEK_END)
+        return fp
+else:
+    # The underlying file object seeks as required in Python 3:
+    # https://github.com/python/cpython/blob/v3.7.3/Modules/_io/fileio.c#L474
+    posixfile = open
+
 def split(p):
     '''Same as posixpath.split, but faster
 


More information about the Mercurial-devel mailing list