Bug 43178

Summary: pwrite() should not be affected by O_APPEND
Product: File System Reporter: Michael Kerrisk (mtk.manpages)
Component: VFSAssignee: fs_vfs
Status: NEW ---    
Severity: normal CC: alan, szg00000, tss
Priority: P1    
Hardware: All   
OS: Linux   
Kernel Version: 3.4 Subsystem:
Regression: No Bisected commit-id:
Attachments: Test program

Description Michael Kerrisk 2012-04-29 19:32:30 UTC
ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);

The pwrite() system call (and also pwritev()) writes bytes at the given 'offset', leaving the file offset unchanged. 

But, on Linux, if a file is opened with O_APPEND, pwrite() wrongly appends data to the end of the file. It should instead honor the offset. This was the intention in the in the original POSIX.1-2001 specification [1], and wording was added to POSIX.1-2008 to make this more explicit.

Tested on 3.0 and 3.2, but appears to be still current in 3.4.

[1] http://www.opengroup.org/sophocles2/show_mail.tpl?CALLER=show_archive.tpl&source=L&listname=austin-group-l&id=9455
[2] http://www.opengroup.org/austin/docs/austin_325.txt
Comment 1 Michael Kerrisk 2012-04-29 19:37:05 UTC
Timo Sirainen also noted this issue:
http://thread.gmane.org/gmane.linux.kernel/513390
Comment 2 Michael Kerrisk 2012-04-29 19:45:09 UTC
Created attachment 73123 [details]
Test program

Test program and example that demonstrates the issue:


$ cat > a
aaaaaaa
bbbbbbb
ccccccc
ddddddd
$ ./t_pwrite_O_APPEND a 5 XXXXX y
$ cat a
aaaaaaa
bbbbbbb
ccccccc
ddddddd
XXXXX$ 


In the above, the XXXXX string should have been placed at offset 5 of the file 'a'.
Comment 3 Alan 2012-05-12 00:41:21 UTC
Not clear what the fix is however..  we can't just ignore it because of the appendonly attribute.