Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does java.io.FileDescriptor.sync() fsync directories on Linux?

fsync(2) manpage tells syncing directory is explicitly needed if a file is synced.

How about Java's sync method in io package? Does is take care about that? Does it depend on OS and/or file system?

I found nothing helpful in http://docs.oracle.com/javase/7/docs/api/java/io/FileDescriptor.html#sync...

like image 438
ebelcrom Avatar asked Nov 10 '22 16:11

ebelcrom


1 Answers

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:

  1. The directory used to open the file might not be known to the OS anymore.
  2. 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.
  3. 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.

like image 96
horstr Avatar answered Nov 14 '22 22:11

horstr