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 awrite()and be subject toO_SYNC.The POSIX definition of O_SYNC says:
And the POSIX definition for "synchronized I/O file integrity completion":
And the definition for "Synchronized I/O Data Integrity Completion":
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_SYNCandftruncate()would require you to callfsync()afterftruncate()of a file opened withO_SYNC.