I have a C program which creates a specified number of files (name- myfiles) in a directory. Then removes all the files. Then creates a very large file (name -appfile), appends it, truncates it. Several rounds of above operations are performed in a loop.
In order to verify each write
, I read destination file from the same offset, where it had written the data. This verification part(read()) goes very well if test does not use O_APPEND flag for large file. But Otherwise, read starts showing weird behavior. After completion of 1st round of the test, either bytes read by read is 0 or lesser than the buffer size Or if the number of bytes are equal to buffer size than the content of both buffers mismatch.
Given destflg=O_RDWR | O_APPEND | O_CREAT;
, I assume two things:
pwrite()
on Linux is broken.
From the Linux pwrite()
man page:
BUGS
POSIX requires that opening a file with the
O_APPEND
flag should have no effect on the location at whichpwrite()
writes data. However, on Linux, if a file is opened withO_APPEND
,pwrite()
appends data to the end of the file, regardless of the value ofoffset
.