fsync: sync parent directories on POSIX systems

Summary:
On POSIX systems it's a good practice to fsync directories to ensure metadata
about a file is properly written.

Reviewed By: DurhamG

Differential Revision: D26822211

fbshipit-source-id: fca10c702b480f22020ad184eb55c8879999f0ee
This commit is contained in:
Jun Wu 2021-03-04 11:57:40 -08:00 committed by Facebook GitHub Bot
parent f10466c9f1
commit fd1bbe92f8

View File

@ -68,6 +68,27 @@ pub fn fsync_glob(dir: &Path, patterns: &[&str], newer_than: Option<SystemTime>)
}
}
result.sort_unstable();
// Also fsync parent directories on *nix. This syncs metadata about the file.
// On Windows directories cannot be opened.
#[cfg(unix)]
{
let dirs: Vec<&Path> = {
let mut dirs: Vec<_> = result.iter().filter_map(|p| p.parent()).collect();
dirs.dedup();
dirs
};
for path in dirs {
let path = dir.join(path);
match fs::OpenOptions::new().read(true).open(&path) {
Ok(file) => match file.sync_all() {
Ok(_) => debug!("fsynced dir: {}", path.display()),
Err(e) => warn!("cannot fsync dir {}: {}", path.display(), e),
},
Err(e) => warn!("cannot open dir {}: {}", path.display(), e),
}
}
}
result
}