use std::io;
use std::path::Path;
use super::ops::blocking;
pub async fn sync_dir(path: impl AsRef<Path>) -> io::Result<()> {
let path = path.as_ref().to_path_buf();
blocking(move || sync_dir_now(&path)).await
}
#[cfg(unix)]
fn sync_dir_now(path: &Path) -> io::Result<()> {
let dir = std::fs::File::open(path)?;
let synced = dir.sync_all();
#[cfg(any(target_os = "macos", target_os = "ios"))]
if let Err(err) = &synced
&& matches!(err.raw_os_error(), Some(libc::ENOTSUP | libc::EINVAL))
{
use std::os::fd::AsRawFd;
return match unsafe { libc::fsync(dir.as_raw_fd()) } {
0 => Ok(()),
_ => Err(io::Error::last_os_error()),
};
}
synced
}
#[cfg(windows)]
fn sync_dir_now(path: &Path) -> io::Result<()> {
use std::os::windows::fs::OpenOptionsExt;
std::fs::OpenOptions::new()
.read(true)
.write(true)
.custom_flags(crate::syscore::windows::FILE_FLAG_BACKUP_SEMANTICS)
.open(path)?
.sync_all()
}