This is the mail archive of the cygwin-patches@cygwin.com mailing list for the Cygwin 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]

[PATCH] was [BUG] open(): Opening with flags O_RDONLY | O_APPEND positions the file pointer at the end of the file


Hello Corinna,

Wednesday, July 10, 2002, 7:04:59 PM, you wrote:

>> >> >Still according to the Linux man page and SUSv2, O_APPEND should be
>> >> >taken into account only when writing to the file.
>> >> >
>> >> >Having in mind that fhandler_base::write() calls SetFilePointer
>> >> >before each write, I wonder why fhandler_disk_base::open calls
>> >> >SetFilePointer when it detects O_APPEND ?
>> >> 
>> >> Good question.
>> >
>> >What if
>> >
>> >     fd = open(O_APPEND);
>> >     pos = lseek(fd, 0, SEEK_CUR);

This will likely will fail with EINVAL.

>> >
>> >?  If open() doesn't move the pointer, `pos' is incorrectly set to 0.
>>

The sole purpose of O_APPEND is to make the write-at-end-of-file
operation atomic.

Quote from http://www.opengroup.org/onlinepubs/007908799/xsh/write.html:

"If the O_APPEND flag of the file status flags is set, the file offset
will be set to the end of the file prior to each write and no intervening
file modification operation will occur between changing the file offset
and the write operation."

>> Actually, I'd argue that, in that case, lseek should have some O_APPEND
>> logic of its own.

No, there is no need. Only write() should care about O_APPEND (and it
does).

CV> Just checked the following on Linux:

[snip source]

CV> Output:

CV> pos1: 0
CV> pos2: 382
CV> pos3: 0

CV> So open() doesn't set the pointer.  And lseek() doesn't take O_APPEND
CV> into account either.

bash-2.03$ uname -a
SunOS sparc 5.8 Generic_108528-05 sun4m sparc SUNW,SPARCstation-20
bash-2.03$ ./corinna-test
pos1: 0
pos2: 24168
pos3: 0
bash-2.03$

Cygwin with the attached patch:

$ ./corinna-test.exe
pos1: 0
pos2: 382
pos3: 0


Btw I reported this on the mc-devel list and the MC maintainer
confirmed that they call open () in a wrong way and he checked
a fix for MC:

http://mail.gnome.org/archives/mc-devel/2002-July/msg00035.html


2002-07-11  Pavel Tsekov  <ptsekov@gmx.net>

            * fhandler_disk_file.cc (fhandler_disk_file::open): Don't
            move the file pointer to the end of file if O_APPEND is
            specified in the open flags.

Attachment: fhandler_disk_file.cc.patch
Description: Binary data


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