This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH 2/2] Fix file offsets when in append update mode (a+) (#16532)


On Tue, Feb 11, 2014 at 10:22:06AM +0100, Andreas Schwab wrote:
> Siddhesh Poyarekar <siddhesh@redhat.com> writes:
> 
> > adb26fae introduced an optimization in ftell that allowed it to skip a
> > flush when it would compute the file offset based on the position in
> > the buffer and the offset in the file.  This breaks for files opened
> > in "a+" mode because these streams have the special requirement of
> > being able to read from the beginning, but append at the end.  So if
> > we were reading and then writing without flushing the buffer, the
> > offsets recorded in the stream would be incorrect.  Other update modes
> > (r+ or w+) don't have this caveat since they explicitly need to have a
> > flush or fseek between the read/write switch.
> 
> I don't see how a+ is to be treated differently from r+/w+
> wrt. switching between read and write.

Sorry, that description is wrong.  When a file is opened in a+, its
read offset is at 0 (or whatever the offset was in the fd in case of
fdopen) and it is possible to just start writing directly and output
will be appended to the end.  As a result, an unflushed write would
not change the offset in the stream and give the incorrect result.
The initial state for r+ and w+ does not have this problem since both
reads and writes start from the same position.  The general behaviour
on switching between read and write is indeed the same for a+, r+ and
w+.

So the problem I'm trying to solve is specifically the initial case.
Subsequent cases should follow the same requirement of calling an
fseek (or rewind, etc.) or flush, which would sync up the offset in
the stream with the underlying file.  The ideal solution would be to
identify this initial case and not flush the stream for every ftell
for a+, but I could not figure out a clean way to do it.

Siddhesh


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]