Because you're using O_DIRECT
, you're apparently forced to write in pagesize
increments. However, that implies your file will always be padded out to a multiple of pagesize
. In your current code, it will be padded with zeros, because you zero the page each time before filling it.
One way to address this is to track the actual amount of data that should be in the file, and ftruncate()
the file to the desired size before closing it.