use std::fs::{self, OpenOptions};
use std::io::Write as _;
use std::path::Path;
pub fn publish(temp: &Path, path: &Path, text: &str, create: bool) -> std::io::Result<()> {
let mut created = false;
let result = (|| -> std::io::Result<()> {
let mut options = OpenOptions::new();
options.write(true).create_new(true);
#[cfg(unix)]
{
use std::os::unix::fs::OpenOptionsExt;
options.mode(0o600);
}
let mut file = options.open(temp)?;
created = true;
file.write_all(text.as_bytes())?;
file.sync_all()?;
drop(file);
if create {
fs::hard_link(temp, path)?;
} else {
fs::rename(temp, path)?;
}
Ok(())
})();
if created {
let _ = fs::remove_file(temp);
}
result
}