mirror of
https://sourceware.org/git/glibc.git
synced 2025-08-01 10:06:57 +03:00
Use cached offset in ftell when reliable
The cached offset is reliable to use in ftell when the stream handle is active. We can consider a stream as being active when there is unflushed data. However, even in this case, we can use the cached offset only when the stream is not being written to in a+ mode, because this case may have unflushed data and a stale offset; the previous read could have sent it off somewhere other than the end of the file. There were a couple of adjustments necessary to get this to work. Firstly, fdopen now ceases to use _IO_attach_fd because it sets the offset cache to the current file position. This is not correct because there could be changes to the file descriptor before the stream handle is activated, which would not get reflected. A similar offset caching action is done in _IO_fwide, claiming that wide streams have 'problems' with the file offsets. There don't seem to be any obvious problems with not having the offset cache available, other than that it will have to be queried in a subsequent read/write/seek. I have removed this as well. The testsuite passes successfully with these changes on x86_64.
This commit is contained in:
@ -141,9 +141,6 @@ _IO_new_fdopen (fd, mode)
|
||||
#ifdef _IO_MTSAFE_IO
|
||||
new_f->fp.file._lock = &new_f->lock;
|
||||
#endif
|
||||
/* Set up initially to use the `maybe_mmap' jump tables rather than using
|
||||
__fopen_maybe_mmap to do it, because we need them in place before we
|
||||
call _IO_file_attach or else it will allocate a buffer immediately. */
|
||||
_IO_no_init (&new_f->fp.file, 0, 0, &new_f->wd,
|
||||
#ifdef _G_HAVE_MMAP
|
||||
(use_mmap && (read_write & _IO_NO_WRITES))
|
||||
@ -159,13 +156,12 @@ _IO_new_fdopen (fd, mode)
|
||||
#if !_IO_UNIFIED_JUMPTABLES
|
||||
new_f->fp.vtable = NULL;
|
||||
#endif
|
||||
if (_IO_file_attach ((_IO_FILE *) &new_f->fp, fd) == NULL)
|
||||
{
|
||||
_IO_setb (&new_f->fp.file, NULL, NULL, 0);
|
||||
_IO_un_link (&new_f->fp);
|
||||
free (new_f);
|
||||
return NULL;
|
||||
}
|
||||
/* We only need to record the fd because _IO_file_init will have unset the
|
||||
offset. It is important to unset the cached offset because the real
|
||||
offset in the file could change between now and when the handle is
|
||||
activated and we would then mislead ftell into believing that we have a
|
||||
valid offset. */
|
||||
new_f->fp.file._fileno = fd;
|
||||
new_f->fp.file._flags &= ~_IO_DELETE_DONT_CLOSE;
|
||||
|
||||
_IO_mask_flags (&new_f->fp.file, read_write,
|
||||
|
Reference in New Issue
Block a user