Search code examples
cfile-ioposix

O_APPEND flag on one file makes read() system call behave weirdly on other files


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.


Solution

  • Given destflg=O_RDWR | O_APPEND | O_CREAT;, I assume two things:

    1. You're opening the file in append mode
    2. You're running on Linux.

    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 which pwrite() writes data. However, on Linux, if a file is opened with O_APPEND, pwrite() appends data to the end of the file, regardless of the value of offset.