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