title: Is ftruncate() asynchronous?
body: <p>I am attempting to write a class in C++ that provides a means of atomically appending to a file, even for the case of power failure mid write.</p>
  
  <p>First, I write my current file position (a 64 offset from the beginning of the file, in bytes) to a separate journal file. Then, I write the requested data to the end of the date file. Finally, I call ftruncate() (setting the truncated size to 0) on the journal file.</p>
  
  <p>The main idea is that if this class is ever asked to open a file that has a non empty journal file, then you know a write was interrupted and you can read the position of the last write from the journal file and fseek to that spot. You lose the last partial write, but the file should not be corrupted.</p>
  
  <p>Unfortunately, it seems like ftruncate() is asynchronous. In practice, even if I call fflush() and fsync() after ftruncate I see the journal grow to up to hundreds of bytes while doing lots of writes. It always ultimately ends up at 0, but I expected to see it at either size 0 or size 8 at all times.</p>
  
  <p>Is it possible to make ftruncate completely synchronous? Or is there a better way to use the journal?</p>