This is the mail archive of the
mailing list for the glibc project.
Re: [PATCH 1/2] Move offset to end of file when fdopen is in "a" mode (#16532)
- From: Siddhesh Poyarekar <siddhesh at redhat dot com>
- To: Rich Felker <dalias at aerifal dot cx>
- Cc: libc-alpha at sourceware dot org
- Date: Tue, 11 Feb 2014 21:55:01 +0530
- Subject: Re: [PATCH 1/2] Move offset to end of file when fdopen is in "a" mode (#16532)
- Authentication-results: sourceware.org; auth=none
- References: <20140211083800 dot GB1424 at spoyarek dot pnq dot redhat dot com> <20140211095455 dot GV15627 at brightrain dot aerifal dot cx> <20140211100558 dot GF1424 at spoyarek dot pnq dot redhat dot com> <20140211152706 dot GW15627 at brightrain dot aerifal dot cx>
On Tue, Feb 11, 2014 at 10:27:06AM -0500, Rich Felker wrote:
> > It shouldn't make a difference. The only reason to seek to the end is
> > to record the offset of the file end into the FILE object. If an
> > lseek moves it elsewhere, we don't care. Data will be appended to the
> > end of the file in any case since we add an O_APPEND to the fd.
> Then what if you write() to the fd after calling fdopen?
Ugh, no. I guess this will cause problems with r or w too, where
fdopen records the current position (instead of the end as is the case
with 'a') in _offset and the offset moves from under it. In fact,
this would probably break 'r' even without the ftell optimization. w
and a would have been fine since they defaulted to flushing (and hence
calling lseek) before that patch.
> > > We had a similar bug in musl (but not the same; it manifested in more
> > > cases than the glibc one) and I fixed it by doing the repositioning at
> > > ftello time when the stream is append-mode and there's unwritten
> > > buffered data:
> > >
> > > http://git.musl-libc.org/cgit/musl/commit/?id=3af2edee150484940916eba1984f78c3b965dd05
> > This is precisely what my earlier ftell optimization avoided whenever
> > it could. It is still avoidable for append, but not for append-update
> > (a+) at least in the initial case.
> I think it's a little bit different, at least according to your
> earlier description. You said you were avoiding fflush; we don't
> fflush but just lseek. In any case, it's not possible to avoid some
> call to lseek here unless you can prove the FILE (and not a different
> FILE or the underlying fd) is the active handle.
Yeah, we call fflush as a means to get a consistent view when we're in
write mode and that refreshes the offset too. Let me take another
crack at this. Thanks for your comments.