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