Skip to main content

sync_dir

Function sync_dir 

Source
pub async fn sync_dir(path: impl AsRef<Path>) -> Result<()>
Expand description

Writes the entries of the directory at path through to the device: the names created, removed or renamed in it before the call survive a crash. A file’s own sync_all does not cover its name in the directory, so the name needs this call. It runs on the blocking pool.

To replace a file target atomically, so a crash leaves either the old contents or the new and never a mix:

  1. write the new contents to a temporary file in the same directory,
  2. sync_all the temporary file,
  3. rename it over target,
  4. sync_dir the directory they are in.

On Windows the directory is opened with backup semantics for reading and writing and flushed (FlushFileBuffers); on Apple’s systems a full flush of a directory the system refuses is made with fsync.

§Errors

The system’s own: path does not exist, cannot be opened, or is not something the system lets be synced. A path that is not a directory gets the answer the system gives for it.