use std::io::{self, Write as _};
use std::path::Path;
pub fn replace_private(path: &Path, bytes: &[u8]) -> io::Result<()> {
let parent = path
.parent()
.ok_or_else(|| io::Error::other(format!("{} has no parent", path.display())))?;
let mut temporary = tempfile::NamedTempFile::new_in(parent)?;
temporary.write_all(bytes)?;
temporary.as_file().sync_all()?;
temporary.persist(path).map_err(|error| error.error)?;
sync_directory(parent)
}
pub fn sync_directory(directory: &Path) -> io::Result<()> {
#[cfg(unix)]
std::fs::File::open(directory)?.sync_all()?;
#[cfg(not(unix))]
let _ = directory;
Ok(())
}
#[cfg(test)]
mod tests;