Does fsync of a parent directory guarantee synchronization of meta data of all recursive sub directories?

StackOverflow https://stackoverflow.com/questions/17616485

Question

Currently I am writing an Android application which needs to work with lot of files at native side.

Recently I ran into several data loss or inconsistency problems while updating or renaming these files. (All these files are stored within several sub-directories from a common parent/root directory.)

After reading several articles about fsync(), I decided to use it. However I had one thing unclear:

Does calling fsync() on a parent directory guarantee synchronization of meta data of all sub directories, recursively?

That is, lets say I do a lot of rename operations of several files in different sub-directories within a common parent directory.

Then should I call fsync() on each sub-directory separately or calling fsync() on my parent directory is enough to make sure the meta data of all sub-directories (and their child directories recursively) are synchronized with the physical device?

Basically the idea is to save time by avoiding multiple fsync() calls. Or would it take more time for fsync() on a top level directory compared to fsync() on each individual sub-directories? (Assuming fysnc() on top level directory synchronizes all sub-directory meta data)

Was it helpful?

Solution

When fsync is called on a directory the operation treats its target more like a file than a container of files. From the file system's perspective a directory is a special kind of file that just happens to contain metadata about other files. When fsync is applied to a directory, this metadata about the contained files is synchronized but not the subdirectories/files themselves.

If you want to ensure that all of the contents are properly synchronized, then you should fsync them explicitly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top