use std::io;
use std::path::Path;
use fs_err::File;
pub fn bulk_sync_dir(dir: &Path) -> io::Result<()> {
#[cfg(any(target_os = "linux", target_os = "android", target_os = "hurd"))]
match nix::unistd::syncfs(File::open(dir)?) {
Ok(()) => return Ok(()),
Err(e) => log::warn!("syncfs failed for {}: {e}", dir.display()),
}
sync_dir_with_fsync(dir)
}
fn sync_dir_with_fsync(dir: &Path) -> io::Result<()> {
for entry in fs_err::read_dir(dir)? {
let entry = entry?;
if entry.file_type()?.is_dir() {
sync_dir_with_fsync(&entry.path())?;
} else {
File::open(entry.path())?.sync_all()?;
}
}
#[cfg(unix)]
File::open(dir)?.sync_all()?;
Ok(())
}