I am looking for ways to make sure that directory changes are (a) ordered before subsequent disk writes, and/or (b) persisted to disk. I am aware that these are two related, but not necessarily identical problems, and I need to control precisely both issues on Linux, Windows, and macOS.
On Linux, the man pages for fsync clearly state that, to ensure that changes to entries inside a directory are persisted on disk, one should call fsync
on the containing directory. For example, one can do the following:
rename("/my/dir/old-file", "/my/dir/new-file");
int dirfd = open("/my/dir", O_RDONLY | O_DIRECTORY);
fsync(dirfd);
close(dirfd);
I am wondering whether the same is needed/possible on Windows and macOS (and perhaps also other OSs in the BSD family).
On Windows, an attempt to open a directory results in ERROR_ACCESS_DENIED
, so an analogous approach with FlushFileBuffers
clearly cannot work. I could not find any system call in MSDN that would achieve the same effect as on Linux.
On macOS, no system call in the above code snippet produces an error, and in fact fcntl
with either F_BARRIERFSYNC
or F_FULLSYNC
(the macOS-specific ways to request a write barrier or a full flush to disk) works too. However, I am not sure whether this actually does anything: the man pages for fsync
and fcntl
on macOS do not say anything about syncing directory entries.