Is ftruncate considered a "write operation" for the purposes of O_SYNC?


When writing to a file opened with O_SYNC, the data (and metadata) is guaranteed to be written to persistent storage when the write call returns, and no explicit fsync call is needed.

Is the same true for ftruncate? Or do I still need to call fsync after ftruncate even with O_SYNC?


Not all filesystems are capable of dealing with holes. There will be some filesystems that actually have to physically write 0’s when you call ftruncate().

So logically, ftruncate() be treated like a write() and be subject to O_SYNC.

The POSIX definition of O_SYNC says:

Write I/O operations on the file descriptor shall complete as defined by synchronized I/O file integrity completion.

And the POSIX definition for "synchronized I/O file integrity completion":

Identical to a synchronized I/O data integrity completion with the addition that all file attributes relative to the I/O operation […] are successfully transferred prior to returning to the calling process.

And the definition for "Synchronized I/O Data Integrity Completion":

[…] The write is complete only when the data specified in the write request is successfully transferred and all file system information required to retrieve the data is successfully transferred.

That includes the file size.
But notably it only applies to "writes" (and "reads").

However, neither POSIX nor the Linux man pages define what a "write" or "write I/O" is, and in particular, whether ftruncate() counts as one.

So if you want to get lawyery about it, it is not strictly guaranteed anywhere, although I think it’s a bug in the specification.

In practice, though, I doubt any file system that implements O_SYNC and ftruncate() would require you to call fsync() after ftruncate() of a file opened with O_SYNC.

Answered By – root

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published