Kernel Bug Tracker – Bug 43178
pwrite() should not be affected by O_APPEND
Last modified: 2016-03-20 10:13:24 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 , 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.
Timo Sirainen also noted this issue:
Created attachment 73123 [details]
Test program and example that demonstrates the issue:
$ cat > a
$ ./t_pwrite_O_APPEND a 5 XXXXX y
$ cat a
In the above, the XXXXX string should have been placed at offset 5 of the file 'a'.
Not clear what the fix is however.. we can't just ignore it because of the appendonly attribute.