The fsync
manual page states that calling fsync
on a file does not imply the associated directory will be fsynced as well. If required, fsync
has to be called for the directory.
I can see several good reasons for that definition/behavior:
- The directory used to open the file might not be known to the OS anymore.
- The file to directory relationship is not 1:1, the file in question could be referenced by multiple directories via hard links. If a file-fsync would be guaranteed to sync all referencing directories, the file system would have to know locations of all those references. Normally, only the direction directory to file is known to a file system.
- If the directory entry associated with the file was already written to disk, there'd be no point in fsyncing a directory whenever a file-fsync is requested - that'd be an unnecessary performance hit.
With that out of the way, let's look into Java's definition/behavior.
The JavaDoc does not mention anything in respect to associated filesystem objects like directories. Additionally, I don't see a way to get a FileDescriptor
instance for a directory.
Looking at the behavior of the OpenJDK implementation based on its source code, java.io.FileDescriptor.sync()
simply triggers an fsync
on UNIX and FlushFileBuffers
on Windows.
So, no, java.io.FileDescriptor.sync()
does not affect associated directories in any way.