use std::fs::{File, OpenOptions};
use std::io::Write;
use std::path::{Path, PathBuf};
use rand::TryRng;
use rand::rngs::SysRng;
use snafu::Snafu;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NewFileMode {
Default,
Exactly(u32),
KeepExisting,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReplaceStep {
Create,
SetPermissions,
Write,
Replace,
SyncParent,
}
impl ReplaceStep {
fn verb(self) -> &'static str {
match self {
Self::Create => "create",
Self::SetPermissions => "set the permissions of",
Self::Write => "write",
Self::Replace => "replace",
Self::SyncParent => "sync the directory of",
}
}
}
#[derive(Debug, Snafu)]
#[snafu(display("failed to {} `{}`", step.verb(), path.display()))]
pub struct ReplaceError {
pub step: ReplaceStep,
pub path: PathBuf,
source: std::io::Error,
}
impl ReplaceError {
pub fn io(&self) -> &std::io::Error {
&self.source
}
}
impl From<ReplaceError> for std::io::Error {
fn from(error: ReplaceError) -> Self {
error.source
}
}
pub fn replace_contents(path: &Path, bytes: &[u8], mode: NewFileMode) -> Result<(), ReplaceError> {
let Some(file_name) = path.file_name() else {
return Err(ReplaceError {
step: ReplaceStep::Create,
path: path.to_path_buf(),
source: std::io::Error::new(std::io::ErrorKind::InvalidInput, "no file name"),
});
};
let suffix = SysRng.try_next_u64().map_err(|error| ReplaceError {
step: ReplaceStep::Create,
path: path.to_path_buf(),
source: std::io::Error::other(error),
})?;
let temporary = path.with_file_name(format!(
".{}.tmp-{}-{suffix:016x}",
file_name.to_string_lossy(),
std::process::id()
));
let mut options = OpenOptions::new();
options.write(true).create_new(true);
create_with_mode(&mut options, mode);
let file = options.open(&temporary).map_err(|source| ReplaceError {
step: ReplaceStep::Create,
path: temporary.clone(),
source,
})?;
let result = write_and_replace(file, &temporary, path, bytes, mode);
if result.is_err() {
let _ = std::fs::remove_file(&temporary);
}
result
}
fn write_and_replace(
mut file: File,
temporary: &Path,
path: &Path,
bytes: &[u8],
mode: NewFileMode,
) -> Result<(), ReplaceError> {
let failed = |step, at: &Path| {
let path = at.to_path_buf();
move |source| ReplaceError { step, path, source }
};
set_mode(&file, path, mode).map_err(failed(ReplaceStep::SetPermissions, temporary))?;
file.write_all(bytes)
.and_then(|()| file.sync_all())
.map_err(failed(ReplaceStep::Write, temporary))?;
drop(file);
replace_file(temporary, path).map_err(failed(ReplaceStep::Replace, path))?;
sync_parent_directory(path).map_err(failed(ReplaceStep::SyncParent, path))
}
#[cfg(unix)]
fn create_with_mode(options: &mut OpenOptions, mode: NewFileMode) {
use std::os::unix::fs::OpenOptionsExt;
if let NewFileMode::Exactly(mode) = mode {
options.mode(mode);
}
}
#[cfg(not(unix))]
fn create_with_mode(_: &mut OpenOptions, _: NewFileMode) {}
#[cfg(unix)]
fn set_mode(file: &File, target: &Path, mode: NewFileMode) -> std::io::Result<()> {
use std::os::unix::fs::PermissionsExt;
let permissions = match mode {
NewFileMode::Default => return Ok(()),
NewFileMode::Exactly(mode) => std::fs::Permissions::from_mode(mode),
NewFileMode::KeepExisting => match std::fs::metadata(target) {
Ok(metadata) => metadata.permissions(),
Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(()),
Err(error) => return Err(error),
},
};
file.set_permissions(permissions)
}
#[cfg(not(unix))]
fn set_mode(_: &File, _: &Path, _: NewFileMode) -> std::io::Result<()> {
Ok(())
}
pub fn replace_file(from: &Path, to: &Path) -> std::io::Result<()> {
std::fs::rename(from, to)
}
pub fn sync_parent_directory(path: &Path) -> std::io::Result<()> {
let Some(parent) = path.parent() else {
return Ok(());
};
sync_directory(parent)
}
fn sync_directory(directory: &Path) -> std::io::Result<()> {
open_directory_for_sync(directory).and_then(|directory| directory.sync_all())
}
fn open_directory_for_sync(path: &Path) -> std::io::Result<File> {
#[cfg(windows)]
{
use std::os::windows::fs::OpenOptionsExt;
const GENERIC_READ: u32 = 0x8000_0000;
const GENERIC_WRITE: u32 = 0x4000_0000;
const FILE_FLAG_BACKUP_SEMANTICS: u32 = 0x0200_0000;
OpenOptions::new()
.access_mode(GENERIC_READ | GENERIC_WRITE)
.custom_flags(FILE_FLAG_BACKUP_SEMANTICS)
.open(path)
}
#[cfg(not(windows))]
File::open(path)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_support::TempPath;
fn scratch(label: &str) -> TempPath {
let directory = TempPath::new(label);
std::fs::create_dir(&directory).unwrap();
directory
}
fn entries(directory: &Path) -> Vec<std::ffi::OsString> {
let mut names = std::fs::read_dir(directory)
.unwrap()
.map(|entry| entry.unwrap().file_name())
.collect::<Vec<_>>();
names.sort();
names
}
#[test]
fn replace_contents_creates_then_replaces_and_leaves_only_the_target() {
let scratch = scratch("replace");
let path = scratch.join("state.json");
replace_contents(&path, b"first", NewFileMode::Default).unwrap();
assert_eq!(std::fs::read(&path).unwrap(), b"first");
replace_contents(&path, b"second", NewFileMode::KeepExisting).unwrap();
assert_eq!(std::fs::read(&path).unwrap(), b"second");
assert_eq!(entries(&scratch), ["state.json"]);
}
#[test]
fn a_failed_replace_reports_its_step_and_removes_the_temporary() {
let scratch = scratch("replace-fails");
let path = scratch.join("occupied");
std::fs::create_dir(&path).unwrap();
std::fs::write(path.join("kept"), b"kept").unwrap();
let error = replace_contents(&path, b"new", NewFileMode::Default).unwrap_err();
assert_eq!(error.step, ReplaceStep::Replace);
assert_eq!(error.path, path);
assert_eq!(entries(&scratch), ["occupied"]);
assert_eq!(std::fs::read(path.join("kept")).unwrap(), b"kept");
}
#[test]
fn sync_parent_directory_succeeds_for_a_local_file() {
let scratch = scratch("dirsync");
let path = scratch.join("probe");
std::fs::write(&path, b"x").unwrap();
sync_parent_directory(&path).unwrap();
}
#[cfg(unix)]
#[test]
fn replace_contents_sets_the_requested_mode() {
use std::os::unix::fs::PermissionsExt;
let scratch = scratch("replace-mode");
let path = scratch.join("secret");
let mode = |path: &Path| std::fs::metadata(path).unwrap().permissions().mode() & 0o777;
replace_contents(&path, b"key", NewFileMode::Exactly(0o600)).unwrap();
assert_eq!(mode(&path), 0o600);
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o640)).unwrap();
replace_contents(&path, b"key", NewFileMode::KeepExisting).unwrap();
assert_eq!(mode(&path), 0o640);
}
}