Do I have to flush a directory on Windows?

200 views Asked by At

On Linux, it's well known that after:

  • creating a new file (creat(path,..), open(new_path,O_CREAT..)), or
  • deleting a file (unlink(path)), or
  • renaming a file (rename(src_path,dst_path))

One should also make sure that the changes to the directory (which in some sense is yet another file which maps human-readable names to inodes) also have been persisted to the disc in a durable way (otherwise, in case of power failure, or taking a snapshot of the volume, the directory might appear to not have the new content). In other words, these functions return before the change to the directory is made durable (which kinda makes sense, given that FS is more like DAG than a tree, so it's not even clear which directories need updating).

One can use open(path_to_parent_dir) to obtain a file descriptor of the directory, then fsync(..) it (and close(..) it).

On Windows, I see that in case of MoveFileExA(src_path,dst_path,flags), I can provide the MOVEFILE_WRITE_THROUGH flag, which on the one hand achieves the durability of renaming, on the other is a proof that the problem, in general, applies to Windows as well.

Thus, I wonder: what about the other operations such as:

  • creating a file (CreateFile(path,...) with OS_FILE_CREATE), or
  • deleting a file (DeleteFile(path)).

Do these functions ensure that changes to the directory containing the path are durable before return, or should I do call some additional, function to achieve durability?

If so, what should I do? Something like this?

const auto dir_handle = CreateFile(path_to_parent_dir,...,FILE_FLAG_BACKUP_SEMANTICS,...);
FlushFileBuffers(dir_handle);
CloseHandle(dir_handle);
0

There are 0 answers