use std::fs::{self, OpenOptions};
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub enum Access {
#[default]
Default,
OwnerOnly,
}
pub fn write_new(path: &Path, contents: &[u8], access: Access) -> io::Result<()> {
let temporary = written(path, contents, access)?;
let result = retried(|| fs::hard_link(&temporary, path));
let _ = fs::remove_file(&temporary);
result
}
pub fn replace(path: &Path, contents: &[u8], access: Access) -> io::Result<()> {
let temporary = written(path, contents, access)?;
let result = retried(|| fs::rename(&temporary, path));
if result.is_err() {
let _ = fs::remove_file(&temporary);
}
result
}
pub(crate) fn retried(mut operation: impl FnMut() -> io::Result<()>) -> io::Result<()> {
let mut wait = core::time::Duration::from_millis(2);
loop {
match operation() {
Err(error)
if cfg!(windows)
&& error.kind() == io::ErrorKind::PermissionDenied
&& wait <= core::time::Duration::from_millis(512) =>
{
std::thread::sleep(wait);
wait *= 2;
}
result => return result,
}
}
}
fn written(path: &Path, contents: &[u8], access: Access) -> io::Result<PathBuf> {
let parent = path
.parent()
.ok_or_else(|| io::Error::from(io::ErrorKind::InvalidInput))?;
if !parent.as_os_str().is_empty() {
fs::create_dir_all(parent)?;
}
let temporary = temporary_path(path);
let mut options = OpenOptions::new();
options.write(true).create_new(true);
#[cfg(unix)]
if access == Access::OwnerOnly {
use std::os::unix::fs::OpenOptionsExt;
options.mode(0o600);
}
#[cfg(not(unix))]
let _ = access;
let mut file = options.open(&temporary)?;
let result = file.write_all(contents).and_then(|()| file.sync_all());
drop(file);
match result {
Ok(()) => Ok(temporary),
Err(error) => {
let _ = fs::remove_file(&temporary);
Err(error)
}
}
}
pub(crate) fn temporary_path(path: &Path) -> PathBuf {
static NEXT: core::sync::atomic::AtomicU64 = core::sync::atomic::AtomicU64::new(0);
let count = NEXT.fetch_add(1, core::sync::atomic::Ordering::Relaxed);
let nonce = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map_or(0, |elapsed| elapsed.as_nanos());
let mut name = path.file_name().unwrap_or_default().to_os_string();
name.push(format!(".{}.{}.{}.tmp", std::process::id(), nonce, count));
path.with_file_name(name)
}