00db10
commit e8b9e065a1ae9d7d8b888909ae761cbd5cf7be32
00db10
Author: Siddhesh Poyarekar <siddhesh@redhat.com>
00db10
Date:   Wed Mar 19 00:42:30 2014 +0530
00db10
00db10
    Fix offset computation for append+ mode on switching from read (BZ #16724)
00db10
    
00db10
    The offset computation in write mode uses the fact that _IO_read_end
00db10
    is kept in sync with the external file offset.  This however is not
00db10
    true when O_APPEND is in effect since switching to write mode ought to
00db10
    send the external file offset to the end of file without making the
00db10
    necessary adjustment to _IO_read_end.
00db10
    
00db10
    Hence in append mode, offset computation when writing should only
00db10
    consider the effect of unflushed writes, i.e. from _IO_write_base to
00db10
    _IO_write_ptr.
00db10
00db10
diff --git glibc-2.17-c758a686/libio/Makefile glibc-2.17-c758a686/libio/Makefile
00db10
index 69c25c0..4bedfad 100644
00db10
--- glibc-2.17-c758a686/libio/Makefile
00db10
+++ glibc-2.17-c758a686/libio/Makefile
00db10
@@ -60,7 +60,8 @@ tests = tst_swprintf tst_wprintf tst_swscanf tst_wscanf tst_getwc tst_putwc   \
00db10
 	tst-wmemstream1 tst-wmemstream2 \
00db10
 	bug-memstream1 bug-wmemstream1 \
00db10
 	tst-setvbuf1 tst-popen1 tst-fgetwc bug-wsetpos tst-fseek \
00db10
-	tst-fwrite-error tst-ftell-active-handler
00db10
+	tst-fwrite-error tst-ftell-active-handler \
00db10
+	tst-ftell-append
00db10
 ifeq (yes,$(build-shared))
00db10
 # Add test-fopenloc only if shared library is enabled since it depends on
00db10
 # shared localedata objects.
00db10
diff --git glibc-2.17-c758a686/libio/fileops.c glibc-2.17-c758a686/libio/fileops.c
00db10
index cf68dbf..204cfea 100644
00db10
--- glibc-2.17-c758a686/libio/fileops.c
00db10
+++ glibc-2.17-c758a686/libio/fileops.c
00db10
@@ -91,7 +91,9 @@ extern struct __gconv_trans_data __libio_translit attribute_hidden;
00db10
 
00db10
    The position in the buffer that corresponds to the position
00db10
    in external file system is normally _IO_read_end, except in putback
00db10
-   mode, when it is _IO_save_end.
00db10
+   mode, when it is _IO_save_end and also when the file is in append mode,
00db10
+   since switching from read to write mode automatically sends the position in
00db10
+   the external file system to the end of file.
00db10
    If the field _fb._offset is >= 0, it gives the offset in
00db10
    the file as a whole corresponding to eGptr(). (?)
00db10
 
00db10
@@ -966,6 +968,14 @@ do_ftell (_IO_FILE *fp)
00db10
       /* Adjust for unflushed data.  */
00db10
       if (!was_writing)
00db10
 	offset -= fp->_IO_read_end - fp->_IO_read_ptr;
00db10
+      /* We don't trust _IO_read_end to represent the current file offset when
00db10
+	 writing in append mode because the value would have to be shifted to
00db10
+	 the end of the file during a flush.  Use the write base instead, along
00db10
+	 with the new offset we got above when we did a seek to the end of the
00db10
+	 file.  */
00db10
+      else if (append_mode)
00db10
+	offset += fp->_IO_write_ptr - fp->_IO_write_base;
00db10
+      /* For all other modes, _IO_read_end represents the file offset.  */
00db10
       else
00db10
 	offset += fp->_IO_write_ptr - fp->_IO_read_end;
00db10
     }
00db10
diff --git glibc-2.17-c758a686/libio/tst-ftell-append.c glibc-2.17-c758a686/libio/tst-ftell-append.c
00db10
new file mode 100644
00db10
index 0000000..604dc03
00db10
--- /dev/null
00db10
+++ glibc-2.17-c758a686/libio/tst-ftell-append.c
00db10
@@ -0,0 +1,169 @@
00db10
+/* Verify that ftell returns the correct value after a read and a write on a
00db10
+   file opened in a+ mode.
00db10
+   Copyright (C) 2014 Free Software Foundation, Inc.
00db10
+   This file is part of the GNU C Library.
00db10
+
00db10
+   The GNU C Library is free software; you can redistribute it and/or
00db10
+   modify it under the terms of the GNU Lesser General Public
00db10
+   License as published by the Free Software Foundation; either
00db10
+   version 2.1 of the License, or (at your option) any later version.
00db10
+
00db10
+   The GNU C Library is distributed in the hope that it will be useful,
00db10
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
00db10
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00db10
+   Lesser General Public License for more details.
00db10
+
00db10
+   You should have received a copy of the GNU Lesser General Public
00db10
+   License along with the GNU C Library; if not, see
00db10
+   <http://www.gnu.org/licenses/>.  */
00db10
+
00db10
+#include <stdio.h>
00db10
+#include <stdlib.h>
00db10
+#include <string.h>
00db10
+#include <errno.h>
00db10
+#include <unistd.h>
00db10
+#include <locale.h>
00db10
+#include <wchar.h>
00db10
+
00db10
+/* data points to either char_data or wide_data, depending on whether we're
00db10
+   testing regular file mode or wide mode respectively.  Similarly,
00db10
+   fputs_func points to either fputs or fputws.  data_len keeps track of the
00db10
+   length of the current data and file_len maintains the current file
00db10
+   length.  */
00db10
+#define BUF_LEN 4
00db10
+static void *buf;
00db10
+static char char_buf[BUF_LEN];
00db10
+static wchar_t wide_buf[BUF_LEN];
00db10
+static const void *data;
00db10
+static const char *char_data = "abcdefghijklmnopqrstuvwxyz";
00db10
+static const wchar_t *wide_data = L"abcdefghijklmnopqrstuvwxyz";
00db10
+static size_t data_len;
00db10
+static size_t file_len;
00db10
+
00db10
+typedef int (*fputs_func_t) (const void *data, FILE *fp);
00db10
+fputs_func_t fputs_func;
00db10
+
00db10
+typedef void *(*fgets_func_t) (void *s, int size, FILE *stream);
00db10
+fgets_func_t fgets_func;
00db10
+
00db10
+static int do_test (void);
00db10
+
00db10
+#define TEST_FUNCTION do_test ()
00db10
+#include "../test-skeleton.c"
00db10
+
00db10
+static FILE *
00db10
+init_file (const char *filename)
00db10
+{
00db10
+  FILE *fp = fopen (filename, "w");
00db10
+  if (fp == NULL)
00db10
+    {
00db10
+      printf ("fopen: %m\n");
00db10
+      return NULL;
00db10
+    }
00db10
+
00db10
+  int written = fputs_func (data, fp);
00db10
+
00db10
+  if (written == EOF)
00db10
+    {
00db10
+      printf ("fputs failed to write data\n");
00db10
+      fclose (fp);
00db10
+      return NULL;
00db10
+    }
00db10
+
00db10
+  file_len = data_len;
00db10
+
00db10
+  fclose (fp);
00db10
+
00db10
+  fp = fopen (filename, "a+");
00db10
+  if (fp == NULL)
00db10
+    {
00db10
+      printf ("fopen(a+): %m\n");
00db10
+      return NULL;
00db10
+    }
00db10
+
00db10
+  return fp;
00db10
+}
00db10
+
00db10
+static int
00db10
+do_one_test (const char *filename)
00db10
+{
00db10
+  FILE *fp = init_file (filename);
00db10
+
00db10
+  if (fp == NULL)
00db10
+    return 1;
00db10
+
00db10
+  void *ret = fgets_func (buf, BUF_LEN, fp);
00db10
+
00db10
+  if (ret == NULL)
00db10
+    {
00db10
+      printf ("read failed: %m\n");
00db10
+      fclose (fp);
00db10
+      return 1;
00db10
+    }
00db10
+
00db10
+  int written = fputs_func (data, fp);
00db10
+
00db10
+  if (written == EOF)
00db10
+    {
00db10
+      printf ("fputs failed to write data\n");
00db10
+      fclose (fp);
00db10
+      return 1;
00db10
+    }
00db10
+
00db10
+  file_len += data_len;
00db10
+
00db10
+  long off = ftell (fp);
00db10
+
00db10
+  if (off != file_len)
00db10
+    {
00db10
+      printf ("Incorrect offset %ld, expected %zu\n", off, file_len);
00db10
+      fclose (fp);
00db10
+      return 1;
00db10
+    }
00db10
+  else
00db10
+    printf ("Correct offset %ld after write.\n", off);
00db10
+
00db10
+  return 0;
00db10
+}
00db10
+
00db10
+/* Run the tests for regular files and wide mode files.  */
00db10
+static int
00db10
+do_test (void)
00db10
+{
00db10
+  int ret = 0;
00db10
+  char *filename;
00db10
+  int fd = create_temp_file ("tst-ftell-append-tmp.", &filename);
00db10
+
00db10
+  if (fd == -1)
00db10
+    {
00db10
+      printf ("create_temp_file: %m\n");
00db10
+      return 1;
00db10
+    }
00db10
+
00db10
+  close (fd);
00db10
+
00db10
+  /* Tests for regular files.  */
00db10
+  puts ("Regular mode:");
00db10
+  fputs_func = (fputs_func_t) fputs;
00db10
+  fgets_func = (fgets_func_t) fgets;
00db10
+  data = char_data;
00db10
+  buf = char_buf;
00db10
+  data_len = strlen (char_data);
00db10
+  ret |= do_one_test (filename);
00db10
+
00db10
+  /* Tests for wide files.  */
00db10
+  puts ("Wide mode:");
00db10
+  if (setlocale (LC_ALL, "en_US.UTF-8") == NULL)
00db10
+    {
00db10
+      printf ("Cannot set en_US.UTF-8 locale.\n");
00db10
+      return 1;
00db10
+    }
00db10
+  fputs_func = (fputs_func_t) fputws;
00db10
+  fgets_func = (fgets_func_t) fgetws;
00db10
+  data = wide_data;
00db10
+  buf = wide_buf;
00db10
+  data_len = wcslen (wide_data);
00db10
+  ret |= do_one_test (filename);
00db10
+
00db10
+  return ret;
00db10
+}
00db10
diff --git glibc-2.17-c758a686/libio/wfileops.c glibc-2.17-c758a686/libio/wfileops.c
00db10
index 3199861..f123add 100644
00db10
--- glibc-2.17-c758a686/libio/wfileops.c
00db10
+++ glibc-2.17-c758a686/libio/wfileops.c
00db10
@@ -713,9 +713,16 @@ do_ftell_wide (_IO_FILE *fp)
00db10
 	      offset += outstop - out;
00db10
 	    }
00db10
 
00db10
-	  /* _IO_read_end coincides with fp._offset, so the actual file
00db10
-	     position is fp._offset - (_IO_read_end - new_write_ptr).  */
00db10
-	  offset -= fp->_IO_read_end - fp->_IO_write_ptr;
00db10
+	  /* We don't trust _IO_read_end to represent the current file offset
00db10
+	     when writing in append mode because the value would have to be
00db10
+	     shifted to the end of the file during a flush.  Use the write base
00db10
+	     instead, along with the new offset we got above when we did a seek
00db10
+	     to the end of the file.  */
00db10
+	  if (append_mode)
00db10
+	    offset += fp->_IO_write_ptr - fp->_IO_write_base;
00db10
+	  /* For all other modes, _IO_read_end represents the file offset.  */
00db10
+	  else
00db10
+	    offset += fp->_IO_write_ptr - fp->_IO_read_end;
00db10
 	}
00db10
     }
00db10