use std::io;
use std::path::Path;
#[cfg(unix)]
const PRIVATE_MODE: u32 = 0o600;
pub(crate) fn write(path: &Path, contents: &str, private: bool) -> io::Result<()> {
if private {
write_private(path, contents)
} else {
std::fs::write(path, contents)
}
}
#[cfg(unix)]
fn write_private(path: &Path, contents: &str) -> io::Result<()> {
use std::io::Write;
use std::os::unix::fs::{OpenOptionsExt, PermissionsExt};
let mut file = std::fs::OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.mode(PRIVATE_MODE)
.open(path)?;
file.set_permissions(std::fs::Permissions::from_mode(PRIVATE_MODE))?;
file.write_all(contents.as_bytes())
}
#[cfg(not(unix))]
fn write_private(path: &Path, contents: &str) -> io::Result<()> {
std::fs::write(path, contents)
}