SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
//! Crash-safe file writes: the new content goes to a temporary file next to the target, is
//! flushed to disk, and replaces the target in one rename. A crash or power cut at any moment
//! leaves the old file or the new one, never a torn mix. The previous version can be kept as
//! `<file>.bak`: the old file itself under that name (a hard link) where the file system allows,
//! else a copy written the same way.
use std::fs::{self, OpenOptions};
use std::io::Write;
use std::path::{Path, PathBuf};
/// The backup of a file written with `backup: true`.
pub fn backup_path(path: &Path) -> PathBuf {
let mut s = path.as_os_str().to_owned();
s.push(".bak");
PathBuf::from(s)
}
fn temp_beside(path: &Path, tag: &str) -> PathBuf {
let name = path.file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_else(|| "file".into());
let nanos = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.subsec_nanos()).unwrap_or(0);
path.with_file_name(format!(".{name}.{tag}.{}.{nanos}.tmp", std::process::id()))
}
fn write_synced(path: &Path, bytes: &[u8]) -> std::io::Result<()> {
let mut f = OpenOptions::new().write(true).create_new(true).open(path)?;
f.write_all(bytes)?;
flush(&f)
}
/// Flush a written file to the disk. On Apple systems `sync_all` asks the drive itself to flush
/// (`F_FULLFSYNC`), which a network share refuses (an SMB share: "Operation not supported", os
/// error 45), so a save there failed before it replaced anything. The plain `fsync` the share
/// does offer is used then, as SQLite and git do.
fn flush(f: &fs::File) -> std::io::Result<()> {
flush_with(f, fs::File::sync_all)
}
fn flush_with(f: &fs::File, full: impl FnOnce(&fs::File) -> std::io::Result<()>) -> std::io::Result<()> {
match full(f) {
#[cfg(target_vendor = "apple")]
Err(e) if e.kind() == std::io::ErrorKind::Unsupported || e.raw_os_error() == Some(45) => rustix::fs::fsync(f).map_err(std::io::Error::from),
r => r,
}
}
/// Flush the directory entry (the rename) to disk where the platform allows it.
fn sync_dir(path: &Path) {
#[cfg(unix)]
if let Some(dir) = path.parent().filter(|d| !d.as_os_str().is_empty())
&& let Ok(d) = fs::File::open(dir)
{
let _ = d.sync_all();
}
#[cfg(not(unix))]
let _ = path;
}
/// Steps of a save, so tests can stop one part way (as a crash would).
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
enum Step {
TempWritten,
BackupWritten,
#[allow(dead_code)] // stopping after the last step is a whole save (tests name it)
Replaced,
}
/// Most links followed before a path is called a loop.
const MAX_LINK_HOPS: usize = 40;
/// The file a save has to replace: `path`, or where a symbolic link at `path` (or a chain of
/// them) leads, so a design linked into a project from a synced folder is saved there and the
/// link survives. A link whose target is missing resolves to the place the target would be.
fn resolve_links(path: &Path) -> std::io::Result<PathBuf> {
let mut at = path.to_path_buf();
for _ in 0..MAX_LINK_HOPS {
if !fs::symlink_metadata(&at).is_ok_and(|m| m.file_type().is_symlink()) {
return Ok(at);
}
let to = fs::read_link(&at)?;
at = if to.is_absolute() { to } else { at.parent().map(|d| d.join(&to)).unwrap_or(to) };
}
Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, format!("{}: too many levels of symbolic links", path.display())))
}
/// Give the file that replaces `old` the permissions `old` had (a private `0600` design must not
/// come back readable by everyone, a group's `0664` must not lose the group's write access).
fn keep_permissions(old: &Option<fs::Permissions>, new: &Path) {
if let Some(p) = old {
let _ = fs::set_permissions(new, p.clone());
}
}
// Retrying only the already-synced atomic rename avoids rewriting a large image
// or deleting/truncating its existing target. ACL/read-only/other permanent
// refusals still fail. No file lock is broken and no security setting is changed.
fn replace_synced(source: &Path, destination: &Path) -> std::io::Result<()> {
// A read-only target is refused for good: fail at once rather than wait for it.
let read_only = fs::metadata(destination).is_ok_and(|m| m.permissions().readonly());
let windows = cfg!(windows);
rename_with_retry(|| fs::rename(source, destination), windows && !read_only, std::thread::sleep)
.map_err(|error| explain_refusal(error, windows, read_only))
}
/// The waits between attempts at a rename Windows refuses: doubling from 1 ms, then 250 ms at a
/// time, about 2 s in all. An antivirus or indexer scanning a file written a moment earlier can
/// hold it for several hundred milliseconds when the machine is busy (#8104); 127 ms was too short.
const RENAME_WAITS_MS: [u64; 15] = [1, 2, 4, 8, 16, 32, 64, 128, 250, 250, 250, 250, 250, 250, 250];
/// Whether `error` is Windows refusing a rename (access denied, os error 5, or a sharing
/// violation, os error 32): another program has the file open, or it is read-only or denied.
fn refused_by_windows(error: &std::io::Error) -> bool {
matches!(error.raw_os_error(), Some(5 | 32))
}
/// Say why Windows would not replace the file rather than only the bare "Access is denied", and
/// keep the system's own words.
fn explain_refusal(error: std::io::Error, windows: bool, read_only: bool) -> std::io::Error {
if !windows || !refused_by_windows(&error) {
return error;
}
let why =
if read_only { "the file is read-only" } else { "Windows would not replace the file: another program may have it open, or it is read-only" };
std::io::Error::new(error.kind(), format!("{why} ({error})"))
}
fn rename_with_retry(
mut rename: impl FnMut() -> std::io::Result<()>,
windows: bool,
mut wait: impl FnMut(std::time::Duration),
) -> std::io::Result<()> {
// Windows can refuse rename while an independently opened handle withholds
// FILE_SHARE_DELETE. Access denied may also be permanent, so keep an attempt
// bound and the last error. Requested sleeps total 2005 ms; scheduling/OS
// operation time is not a wall-clock latency guarantee.
let mut result = rename();
if !windows {
return result;
}
for milliseconds in RENAME_WAITS_MS {
if !result.as_ref().is_err_and(refused_by_windows) {
return result;
}
wait(std::time::Duration::from_millis(milliseconds));
result = rename();
}
result
}
/// Keep the file a save is about to replace as its backup without copying it: a second name for
/// the same file (a hard link) under a temporary name beside it, renamed over the old backup in
/// one step. The target keeps its name at every moment, and a save never writes into a file it
/// replaces (it renames a new one over it), so the backup stays the version it was. `Ok(false)`:
/// this file system refuses links (FAT and exFAT drives, many network shares and synced cloud
/// folders) or the old file could not be flushed first; the caller copies it then, as before. A
/// refused rename fails the save, as it does for a copy.
///
/// What differs from a copy: the backup is the old file itself, so it keeps that file's
/// modification time (the previous save's, where a copy has the time of this save), its owner
/// and its permissions (a copy is given the same permissions), and until the target is replaced
/// the two names share one file.
fn link_backup(path: &Path) -> std::io::Result<bool> {
#[cfg(test)]
if tests::LINKS_REFUSED.with(std::cell::Cell::get) {
return Ok(false);
}
// Flushed first, as a copy is: data still only in memory would be lost with it in a power cut.
if open_to_flush(path).and_then(|f| flush(&f)).is_err() {
return Ok(false);
}
let bak_tmp = temp_beside(path, "bak");
if fs::hard_link(path, &bak_tmp).is_err() {
return Ok(false);
}
if let Err(e) = replace_synced(&bak_tmp, &backup_path(path)) {
// Only the second name this call just made: the old file keeps its own.
let _ = fs::remove_file(&bak_tmp);
return Err(e);
}
Ok(true)
}
/// Open the file a save replaces so it can be flushed. Windows flushes only a file opened for
/// writing (nothing is written, and it is not truncated); elsewhere reading is enough.
fn open_to_flush(path: &Path) -> std::io::Result<fs::File> {
OpenOptions::new().read(true).write(cfg!(windows)).open(path)
}
fn write_steps(path: &Path, bytes: &[u8], backup: bool, stop: Option<Step>) -> std::io::Result<()> {
let permissions = fs::metadata(path).ok().filter(|m| m.is_file()).map(|m| m.permissions());
let tmp = temp_beside(path, "new");
if let Err(e) = write_synced(&tmp, bytes) {
let _ = fs::remove_file(&tmp);
return Err(e);
}
keep_permissions(&permissions, &tmp);
if stop == Some(Step::TempWritten) {
return Ok(());
}
let linked = if backup && path.is_file() { link_backup(path) } else { Ok(false) };
let linked = match linked {
Ok(linked) => linked,
Err(e) => {
let _ = fs::remove_file(&tmp);
return Err(e);
}
};
if backup && !linked && path.is_file() {
// Copy (not move) the old file, so the target exists at every moment.
let old = fs::read(path);
let bak_tmp = temp_beside(path, "bak");
let r = old
.and_then(|old| write_synced(&bak_tmp, &old))
.map(|_| keep_permissions(&permissions, &bak_tmp))
.and_then(|_| replace_synced(&bak_tmp, &backup_path(path)));
if let Err(e) = r {
let _ = fs::remove_file(&bak_tmp);
let _ = fs::remove_file(&tmp);
return Err(e);
}
}
if stop == Some(Step::BackupWritten) {
return Ok(());
}
if let Err(e) = replace_synced(&tmp, path) {
let _ = fs::remove_file(&tmp);
return Err(e);
}
sync_dir(path);
remove_old_temps(path);
Ok(())
}
/// Temporary files of saves that crashed part way (a minute old or more) go.
fn remove_old_temps(path: &Path) {
for t in temps_beside(path) {
if fs::symlink_metadata(&t).and_then(|m| m.modified()).ok().and_then(|m| m.elapsed().ok()).is_some_and(|age| age.as_secs() >= 60) {
let _ = fs::remove_file(t);
}
}
}
/// Write `bytes` to `path` atomically; with `backup`, the file being replaced is kept as
/// `<path>.bak` first. A symbolic link at `path` is followed (the file it points to is replaced,
/// the link stays), and the file keeps the permissions it had. The app's own folders (autosave,
/// crash reports) write through this without a [`crate::vfs::PathScope`]; a command writing a
/// path it was given calls [`crate::vfs::check`] first, which judges where the link leads.
pub fn write_atomic(path: &Path, bytes: &[u8], backup: bool) -> std::io::Result<()> {
// In the browser the host stores files (one write is already atomic there).
if cfg!(target_arch = "wasm32") {
let _ = backup;
return crate::vfs::write(&path.to_string_lossy(), bytes);
}
write_steps(&resolve_links(path)?, bytes, backup, None)
}
/// Write `bytes` to the file named `path` itself, atomically: a link at `path` is never followed,
/// and what it points to is never touched. Anything there that isn't a regular file is refused. For
/// the app's own small files beside a design, which never live anywhere else.
pub fn write_atomic_no_follow(path: &Path, bytes: &[u8]) -> std::io::Result<()> {
if cfg!(target_arch = "wasm32") {
return crate::vfs::write(&path.to_string_lossy(), bytes);
}
let there = fs::symlink_metadata(path).ok();
if there.as_ref().is_some_and(|m| !m.is_file()) {
return Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, format!("{}: not a regular file", path.display())));
}
let tmp = temp_beside(path, "new");
if let Err(e) = write_synced(&tmp, bytes) {
let _ = fs::remove_file(&tmp);
return Err(e);
}
keep_permissions(&there.map(|m| m.permissions()), &tmp);
// A rename replaces the name itself, so a link put there since the check above goes, and the
// file it pointed to stays as it was.
if let Err(e) = replace_synced(&tmp, path) {
let _ = fs::remove_file(&tmp);
return Err(e);
}
sync_dir(path);
remove_old_temps(path);
Ok(())
}
/// Publish a complete new file without replacing an existing destination, even if another
/// writer creates it after this call starts. Unsupported filesystems return an error.
pub fn create_atomic(path: &Path, bytes: &[u8]) -> std::io::Result<()> {
if cfg!(target_arch = "wasm32") {
let name = path.to_string_lossy();
if crate::vfs::exists(&name) {
return Err(std::io::Error::new(std::io::ErrorKind::AlreadyExists, "destination already exists"));
}
return crate::vfs::write(&name, bytes);
}
let tmp = temp_beside(path, "create");
// Only remove a temporary file that this invocation successfully created.
// The file closes at the end of this block, before it is linked into place.
let result = {
let mut file = OpenOptions::new().write(true).create_new(true).open(&tmp)?;
file.write_all(bytes).and_then(|_| flush(&file))
};
let result = result.and_then(|_| fs::hard_link(&tmp, path));
let _ = fs::remove_file(&tmp);
if result.is_ok() {
sync_dir(path);
}
result
}
/// Leftover temporary files of saves that never finished (a crash part way), next to `path`
/// (next to the file a link at `path` points to, where a save puts them).
pub fn stale_temps(path: &Path) -> Vec<PathBuf> {
temps_beside(&resolve_links(path).unwrap_or_else(|_| path.to_path_buf()))
}
/// The leftover temporary files of the file named `path` itself.
fn temps_beside(path: &Path) -> Vec<PathBuf> {
let Some(name) = path.file_name().map(|n| n.to_string_lossy().to_string()) else { return Vec::new() };
let dir = path.parent().filter(|d| !d.as_os_str().is_empty()).unwrap_or(Path::new("."));
let prefix = format!(".{name}.");
fs::read_dir(dir)
.into_iter()
.flatten()
.flatten()
.map(|e| e.path())
.filter(|p| p.file_name().map(|n| n.to_string_lossy().to_string()).is_some_and(|n| n.starts_with(&prefix) && n.ends_with(".tmp")))
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
thread_local! {
/// This thread's saves find hard links refused ([`link_backup`]), as on a FAT drive: the
/// backup is copied, exactly as it is there.
pub(super) static LINKS_REFUSED: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
}
/// A new, empty folder of its own for one test (nothing in it is removed).
fn fresh_dir(tag: &str) -> PathBuf {
let nanos = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_nanos()).unwrap_or(0);
let d = std::env::temp_dir().join(format!("solidcraft-safe-write-{tag}-{}-{nanos}", std::process::id()));
fs::create_dir_all(&d).unwrap();
d
}
/// The time of a save with its backup, the backup linked against the backup copied (links
/// refused, as before hard links were used), taking turns. The file saved is
/// `SOLIDCRAFT_SAVE_BENCH_FILE` (a design), in a new folder in `SOLIDCRAFT_SAVE_BENCH_DIR`
/// (on the drive to measure; else the temporary folder). Run with `--ignored --nocapture`.
#[test]
#[ignore = "manual save measurement"]
fn save_time_with_the_backup_linked_or_copied() {
let Some(file) = std::env::var_os("SOLIDCRAFT_SAVE_BENCH_FILE") else { return };
let bytes = fs::read(file).unwrap();
let base = std::env::var_os("SOLIDCRAFT_SAVE_BENCH_DIR").map(PathBuf::from).unwrap_or_else(std::env::temp_dir);
let nanos = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_nanos()).unwrap_or(0);
let d = base.join(format!("save-bench-{}-{nanos}", std::process::id()));
fs::create_dir_all(&d).unwrap();
let p = d.join("design.solidcraft");
write_atomic(&p, &bytes, true).unwrap();
let rounds: usize = std::env::var("SOLIDCRAFT_SAVE_BENCH_ROUNDS").ok().and_then(|n| n.parse().ok()).unwrap_or(11);
let mut times = [Vec::new(), Vec::new()];
for round in 0..2 * rounds {
let refused = round % 2 == 1;
LINKS_REFUSED.with(|r| r.set(refused));
let t = std::time::Instant::now();
write_atomic(&p, &bytes, true).unwrap();
times[usize::from(refused)].push(t.elapsed().as_secs_f64() * 1000.0);
}
LINKS_REFUSED.with(|r| r.set(false));
for (name, mut t) in ["linked", "copied"].into_iter().zip(times) {
t.sort_by(f64::total_cmp);
println!(
"SAVEBENCH bytes={} backup={name} median_ms={:.2} min_ms={:.2} max_ms={:.2} n={}",
bytes.len(),
t[t.len() / 2],
t[0],
t[t.len() - 1],
t.len()
);
}
}
/// Saves keep the previous version as the backup, stopped after any step or not, whether the
/// file system links the old file as the backup or refuses links (a FAT drive, a share) so it
/// is copied: the design file is the old version or the new one, whole, and the backup is
/// the version before it, never the one being written.
#[test]
fn the_backup_is_the_previous_version_linked_or_copied_and_a_stopped_save_keeps_both() {
for refused in [false, true] {
LINKS_REFUSED.with(|r| r.set(refused));
let d = fresh_dir(if refused { "bak-copied" } else { "bak-linked" });
let p = d.join("a.solidcraft");
write_atomic(&p, b"one", true).unwrap();
write_atomic(&p, b"two", true).unwrap();
assert_eq!((fs::read(&p).unwrap(), fs::read(backup_path(&p)).unwrap()), (b"two".to_vec(), b"one".to_vec()), "refused {refused}");
for stop in [Step::TempWritten, Step::BackupWritten] {
write_steps(&p, b"three", true, Some(stop)).unwrap();
assert_eq!(fs::read(&p).unwrap(), b"two", "{stop:?}, refused {refused}");
let bak = fs::read(backup_path(&p)).unwrap();
let want: &[u8] = if stop == Step::TempWritten { b"one" } else { b"two" };
assert_eq!(bak, want, "{stop:?}, refused {refused}");
assert!(!stale_temps(&p).is_empty(), "the stopped save's temporary file is left over");
}
write_atomic(&p, b"four", true).unwrap();
write_atomic(&p, b"five", true).unwrap();
assert_eq!((fs::read(&p).unwrap(), fs::read(backup_path(&p)).unwrap()), (b"five".to_vec(), b"four".to_vec()), "refused {refused}");
}
LINKS_REFUSED.with(|r| r.set(false));
}
/// #5467: a lock is someone else's while it is refreshed; one left by a crash goes stale, and
/// this app's own lock never counts as the design being open elsewhere. Writing a design's
/// file changes its stamp.
#[test]
fn a_fresh_lock_from_another_app_holds_the_design_and_a_stale_or_own_one_does_not() {
use crate::design_lock as lock;
let d = dir("design-lock");
let design = d.join("Bracket.solidcraft");
std::fs::write(&design, b"one").unwrap();
assert_eq!(lock::lock_path(&design), d.join(".~Bracket.solidcraft.lock"));
assert!(!crate::is_design_path(&lock::lock_path(&design).to_string_lossy()), "never listed as a design");
let now = lock::now_secs();
let theirs = lock::Lock { host: "WORKSHOP-PC".into(), pid: std::process::id().wrapping_add(1), ..lock::Lock::mine(now - 600) };
lock::write(&design, &lock::Lock { refreshed: now - 30, ..theirs.clone() }).unwrap();
assert_eq!(lock::held_elsewhere(&design, now).map(|l| l.host), Some("WORKSHOP-PC".into()));
// Not refreshed for longer than three refreshes: left behind, ignored.
lock::write(&design, &lock::Lock { refreshed: now - lock::STALE_SECS - 1, ..theirs.clone() }).unwrap();
assert!(lock::held_elsewhere(&design, now).is_none());
lock::remove(&design);
assert!(lock::lock_path(&design).exists(), "another app's lock is left alone");
// This app's own lock: not "elsewhere", and removed on close.
lock::write(&design, &lock::Lock::mine(now)).unwrap();
assert!(lock::held_elsewhere(&design, now).is_none());
lock::remove(&design);
assert!(!lock::lock_path(&design).exists());
let known = lock::stamp(&design.to_string_lossy()).unwrap();
assert!(!lock::changed_since(&design.to_string_lossy(), &known));
std::fs::write(&design, b"written elsewhere").unwrap();
assert!(lock::changed_since(&design.to_string_lossy(), &known));
let _ = std::fs::remove_dir_all(&d);
}
/// A link at the lock's path is neither written through nor read through, and closing the
/// design leaves it alone. A save still follows a link at the design's own path.
#[cfg(unix)]
#[test]
fn a_link_at_the_lock_path_is_never_followed() {
use crate::design_lock as lock;
use std::os::unix::fs::symlink;
let d = dir("design-lock-link");
let design = d.join("Bracket.solidcraft");
std::fs::write(&design, b"one").unwrap();
let lock_file = lock::lock_path(&design);
let now = lock::now_secs();
let mine = lock::Lock::mine(now);
let victim = d.join("victim.txt");
std::fs::write(&victim, b"keep me").unwrap();
symlink(&victim, &lock_file).unwrap();
assert!(lock::write(&design, &mine).is_err());
assert_eq!(std::fs::read(&victim).unwrap(), b"keep me");
assert!(std::fs::symlink_metadata(&lock_file).unwrap().file_type().is_symlink(), "the link is refused, not replaced");
std::fs::remove_file(&lock_file).unwrap();
// A link to a place with nothing there doesn't make the file either.
let missing = d.join("made-by-the-link.txt");
symlink(&missing, &lock_file).unwrap();
assert!(lock::write(&design, &mine).is_err());
assert!(!missing.exists());
std::fs::remove_file(&lock_file).unwrap();
// A link to a real lock is no lock: nothing is read through it, and closing leaves it.
let elsewhere = d.join("elsewhere.lock");
std::fs::write(&elsewhere, serde_json::to_vec(&mine).unwrap()).unwrap();
symlink(&elsewhere, &lock_file).unwrap();
assert!(lock::read(&design).is_none());
lock::remove(&design);
assert!(std::fs::symlink_metadata(&lock_file).is_ok() && elsewhere.exists());
std::fs::remove_file(&lock_file).unwrap();
// A hard link is a regular file: its name is replaced, the other name keeps its content.
let other_name = d.join("other-name.txt");
std::fs::write(&other_name, b"keep me too").unwrap();
std::fs::hard_link(&other_name, &lock_file).unwrap();
lock::write(&design, &mine).unwrap();
assert_eq!(std::fs::read(&other_name).unwrap(), b"keep me too");
assert!(lock::read(&design).is_some_and(|l| l.is_mine()));
std::fs::remove_file(&lock_file).unwrap();
// Nothing there, and no temporary file left: a plain lock is still written.
lock::write(&design, &mine).unwrap();
assert_eq!(lock::read(&design), Some(mine));
assert!(stale_temps(&lock_file).is_empty());
// The design's own link is still followed by a save.
let real = d.join("real.solidcraft");
std::fs::write(&real, b"old").unwrap();
let linked = d.join("linked.solidcraft");
symlink(&real, &linked).unwrap();
write_atomic(&linked, b"new", false).unwrap();
assert_eq!(std::fs::read(&real).unwrap(), b"new");
assert!(std::fs::symlink_metadata(&linked).unwrap().file_type().is_symlink());
let _ = std::fs::remove_dir_all(&d);
}
/// A lock path that isn't a regular file is ignored at once. Read on a thread, so a regression
/// fails here rather than holding up the test run.
#[cfg(unix)]
#[test]
fn a_lock_path_that_is_not_a_regular_file_is_ignored_at_once() {
use crate::design_lock as lock;
use std::os::unix::fs::symlink;
let d = dir("design-lock-special");
let design = d.join("Bracket.solidcraft");
let lock_file = lock::lock_path(&design);
let read_soon = |design: &Path| {
let (tx, rx) = std::sync::mpsc::channel();
let design = design.to_path_buf();
std::thread::spawn(move || {
let _ = tx.send(lock::read(&design));
});
rx.recv_timeout(std::time::Duration::from_secs(10)).expect("read returned")
};
let cases: [&dyn Fn(&Path); 3] = [&|p| symlink("/dev/zero", p).unwrap(), &|p| std::fs::create_dir(p).unwrap(), &|p| {
assert!(std::process::Command::new("mkfifo").arg(p).status().unwrap().success())
}];
for make in cases {
make(&lock_file);
assert!(read_soon(&design).is_none());
assert!(lock::held_elsewhere(&design, lock::now_secs()).is_none());
assert!(lock::write(&design, &lock::Lock::mine(lock::now_secs())).is_err());
assert!(std::fs::symlink_metadata(&lock_file).is_ok(), "left where it is");
let _ = std::fs::remove_file(&lock_file).or_else(|_| std::fs::remove_dir(&lock_file));
}
// A regular file too large to be a lock is not read either.
std::fs::write(&lock_file, vec![b' '; 4097]).unwrap();
assert!(read_soon(&design).is_none());
let _ = std::fs::remove_dir_all(&d);
}
#[test]
fn create_does_not_replace_an_existing_destination() {
let d = dir("create");
let p = d.join("render.png");
create_atomic(&p, b"first complete image").unwrap();
assert_eq!(create_atomic(&p, b"replacement").unwrap_err().kind(), std::io::ErrorKind::AlreadyExists);
assert_eq!(fs::read(&p).unwrap(), b"first complete image");
assert!(stale_temps(&p).is_empty());
fs::remove_dir_all(d).unwrap();
}
#[test]
fn concurrent_creators_publish_exactly_one_whole_file() {
let d = dir("create-race");
let p = d.join("render.png");
let gate = std::sync::Arc::new(std::sync::Barrier::new(2));
let workers: Vec<_> = [b"first image".as_slice(), b"second image".as_slice()]
.into_iter()
.map(|bytes| {
let gate = gate.clone();
let p = p.clone();
std::thread::spawn(move || {
gate.wait();
create_atomic(&p, bytes)
})
})
.collect();
let results: Vec<_> = workers.into_iter().map(|w| w.join().unwrap()).collect();
assert_eq!(results.iter().filter(|r| r.is_ok()).count(), 1);
assert_eq!(results.iter().filter_map(|r| r.as_ref().err()).next().unwrap().kind(), std::io::ErrorKind::AlreadyExists);
let bytes = fs::read(&p).unwrap();
assert!(bytes == b"first image" || bytes == b"second image");
assert!(stale_temps(&p).is_empty());
fs::remove_dir_all(d).unwrap();
}
fn dir(tag: &str) -> PathBuf {
let d = std::env::temp_dir().join(format!("solidcraft-safe-write-{tag}-{}", std::process::id()));
let _ = fs::remove_dir_all(&d);
fs::create_dir_all(&d).unwrap();
d
}
/// A network share (SMB on macOS) refuses the drive flush `sync_all` asks for with "Operation
/// not supported" (os error 45). The file is then flushed the plain way and the save goes on;
/// any other failure still fails the save.
#[test]
fn a_share_that_refuses_the_full_flush_gets_a_plain_one() {
let d = dir("flush");
let p = d.join("a.solidcraft");
fs::write(&p, b"saved").unwrap();
let f = fs::File::open(&p).unwrap();
let refused = |_: &fs::File| Err(std::io::Error::from_raw_os_error(45));
#[cfg(target_vendor = "apple")]
flush_with(&f, refused).unwrap();
#[cfg(not(target_vendor = "apple"))]
assert!(flush_with(&f, refused).is_err(), "elsewhere sync_all is the plain flush already");
let unsupported = |_: &fs::File| Err(std::io::Error::from(std::io::ErrorKind::Unsupported));
assert_eq!(flush_with(&f, unsupported).is_ok(), cfg!(target_vendor = "apple"));
let broken = |_: &fs::File| Err(std::io::Error::from(std::io::ErrorKind::PermissionDenied));
assert_eq!(flush_with(&f, broken).unwrap_err().kind(), std::io::ErrorKind::PermissionDenied);
flush_with(&f, |_| Ok(())).unwrap();
drop(f);
let _ = fs::remove_dir_all(&d);
}
#[test]
fn replaces_and_keeps_a_backup() {
let d = dir("bak");
let p = d.join("a.solidcraft");
write_atomic(&p, b"one", true).unwrap();
assert!(!backup_path(&p).exists(), "nothing to back up the first time");
write_atomic(&p, b"two", true).unwrap();
assert_eq!(fs::read(&p).unwrap(), b"two");
assert_eq!(fs::read(backup_path(&p)).unwrap(), b"one");
write_atomic(&p, b"three", false).unwrap();
assert_eq!(fs::read(backup_path(&p)).unwrap(), b"one");
assert!(stale_temps(&p).is_empty());
let _ = fs::remove_dir_all(&d);
}
/// A save stopped after any step leaves the old file whole (or the new one).
#[test]
fn a_save_stopped_part_way_keeps_the_old_file() {
let d = dir("stop");
let p = d.join("a.solidcraft");
write_atomic(&p, b"old", true).unwrap();
for stop in [Step::TempWritten, Step::BackupWritten, Step::Replaced] {
write_steps(&p, b"new content", true, Some(stop)).unwrap();
let now = fs::read(&p).unwrap();
assert!(now == b"old" || now == b"new content", "{stop:?}: {now:?}");
if stop < Step::Replaced {
assert_eq!(now, b"old");
assert!(!stale_temps(&p).is_empty(), "the unfinished save's temporary file is left over");
for t in stale_temps(&p) {
fs::remove_file(t).unwrap();
}
}
write_atomic(&p, b"old", false).unwrap();
}
let _ = fs::remove_dir_all(&d);
}
#[test]
fn a_failed_write_leaves_the_target() {
let d = dir("fail");
let p = d.join("missing-dir").join("a.solidcraft");
assert!(write_atomic(&p, b"x", true).is_err());
assert!(!p.exists());
let _ = fs::remove_dir_all(&d);
}
#[cfg(unix)]
mod unix {
use super::*;
use std::os::unix::fs::{PermissionsExt, symlink};
fn mode(p: &Path) -> u32 {
fs::metadata(p).unwrap().permissions().mode() & 0o777
}
/// saving through a link replaces the file it points to and leaves the link.
#[test]
fn a_save_through_a_link_updates_the_target() {
let d = dir("link");
fs::create_dir_all(d.join("cloud")).unwrap();
fs::create_dir_all(d.join("work")).unwrap();
let target = d.join("cloud").join("a.solidcraft");
let link = d.join("work").join("a.solidcraft");
write_atomic(&target, b"one", false).unwrap();
symlink(&target, &link).unwrap();
write_atomic(&link, b"two", true).unwrap();
assert!(fs::symlink_metadata(&link).unwrap().file_type().is_symlink(), "the link stays a link");
assert_eq!(fs::read(&target).unwrap(), b"two");
assert_eq!(fs::read(&link).unwrap(), b"two");
assert_eq!(fs::read(backup_path(&target)).unwrap(), b"one", "the backup sits beside the real file");
assert!(!backup_path(&link).exists());
let _ = fs::remove_dir_all(&d);
}
#[test]
fn relative_and_chained_links_are_followed() {
let d = dir("chain");
fs::create_dir_all(d.join("real")).unwrap();
fs::create_dir_all(d.join("mid")).unwrap();
let target = d.join("real").join("a.solidcraft");
write_atomic(&target, b"one", false).unwrap();
symlink("../real/a.solidcraft", d.join("mid").join("b")).unwrap();
symlink("mid/b", d.join("c")).unwrap();
write_atomic(&d.join("c"), b"two", false).unwrap();
assert_eq!(fs::read(&target).unwrap(), b"two");
assert!(fs::symlink_metadata(d.join("c")).unwrap().file_type().is_symlink());
assert!(fs::symlink_metadata(d.join("mid").join("b")).unwrap().file_type().is_symlink());
let _ = fs::remove_dir_all(&d);
}
#[test]
fn a_dangling_link_is_filled_in_and_a_loop_is_an_error() {
let d = dir("dangling");
let link = d.join("a.solidcraft");
let target = d.join("later.solidcraft");
symlink(&target, &link).unwrap();
write_atomic(&link, b"new", true).unwrap();
assert_eq!(fs::read(&target).unwrap(), b"new");
assert!(fs::symlink_metadata(&link).unwrap().file_type().is_symlink());
let (x, y) = (d.join("x"), d.join("y"));
symlink(&y, &x).unwrap();
symlink(&x, &y).unwrap();
let e = write_atomic(&x, b"loop", true).unwrap_err();
assert!(e.to_string().contains("symbolic links"), "{e}");
let _ = fs::remove_dir_all(&d);
}
/// a private design stays private, a group's file keeps the group's access, and
/// the backup carries the same permissions.
#[test]
fn a_save_keeps_the_permissions() {
let d = dir("mode");
for want in [0o600, 0o640, 0o664, 0o444] {
let p = d.join(format!("a{want:o}.solidcraft"));
write_atomic(&p, b"one", false).unwrap();
fs::set_permissions(&p, fs::Permissions::from_mode(want)).unwrap();
write_atomic(&p, b"two", true).unwrap();
assert_eq!(fs::read(&p).unwrap(), b"two");
assert_eq!(mode(&p), want, "{want:o}");
assert_eq!(mode(&backup_path(&p)), want, "backup {want:o}");
let _ = fs::set_permissions(&p, fs::Permissions::from_mode(0o644));
}
let _ = fs::remove_dir_all(&d);
}
/// Where links work the backup is the old file itself, not a copy: its inode, its
/// modification time and its permissions, with one name again once the save is done;
/// where they are refused it is a copy with the old file's permissions.
#[test]
fn the_backup_is_the_old_file_linked_or_a_copy_where_links_are_refused() {
use std::os::unix::fs::MetadataExt;
for refused in [false, true] {
LINKS_REFUSED.with(|r| r.set(refused));
let d = fresh_dir(if refused { "inode-copied" } else { "inode-linked" });
let p = d.join("a.solidcraft");
write_atomic(&p, b"one", true).unwrap();
fs::set_permissions(&p, fs::Permissions::from_mode(0o640)).unwrap();
let old = fs::metadata(&p).unwrap();
write_atomic(&p, b"two", true).unwrap();
let bak = fs::metadata(backup_path(&p)).unwrap();
let new = fs::metadata(&p).unwrap();
assert_ne!(new.ino(), old.ino(), "the design file is a new file");
assert_eq!(bak.ino() == old.ino(), !refused, "linked: the backup is the old file");
if !refused {
assert_eq!(bak.modified().unwrap(), old.modified().unwrap(), "it keeps the old file's time");
}
assert_eq!((bak.nlink(), new.nlink()), (1, 1), "one name each once the save is done");
assert_eq!((mode(&p), mode(&backup_path(&p))), (0o640, 0o640), "refused {refused}");
assert_eq!(fs::read(backup_path(&p)).unwrap(), b"one");
}
LINKS_REFUSED.with(|r| r.set(false));
}
#[test]
fn a_save_through_a_link_keeps_the_targets_permissions() {
let d = dir("linkmode");
let target = d.join("a.solidcraft");
write_atomic(&target, b"one", false).unwrap();
fs::set_permissions(&target, fs::Permissions::from_mode(0o600)).unwrap();
symlink(&target, d.join("l")).unwrap();
write_atomic(&d.join("l"), b"two", true).unwrap();
assert_eq!(mode(&target), 0o600);
assert_eq!(mode(&backup_path(&target)), 0o600);
let _ = fs::remove_dir_all(&d);
}
}
}
#[cfg(test)]
mod rename_retry_tests {
use super::*;
use std::cell::Cell;
#[test]
fn only_windows_sharing_or_access_errors_retry_with_an_exact_finite_budget() {
for code in [5, 32] {
let calls = Cell::new(0);
let mut waits = Vec::new();
let result = rename_with_retry(
|| {
calls.set(calls.get() + 1);
if calls.get() < 3 { Err(std::io::Error::from_raw_os_error(code)) } else { Ok(()) }
},
true,
|delay| waits.push(delay.as_millis()),
);
assert!(result.is_ok());
assert_eq!(calls.get(), 3);
assert_eq!(waits, [1, 2]);
let calls = Cell::new(0);
let mut waits = Vec::new();
let error = rename_with_retry(
|| {
calls.set(calls.get() + 1);
Err(std::io::Error::from_raw_os_error(code))
},
true,
|delay| waits.push(delay.as_millis()),
)
.unwrap_err();
assert_eq!(error.raw_os_error(), Some(code));
assert_eq!(calls.get(), 16);
assert_eq!(waits, [1, 2, 4, 8, 16, 32, 64, 128, 250, 250, 250, 250, 250, 250, 250]);
assert_eq!(waits.iter().sum::<u128>(), 2005);
}
for (windows, code) in [(false, 5), (false, 32), (true, 2), (true, 3), (true, 87), (true, 183)] {
let calls = Cell::new(0);
let error = rename_with_retry(
|| {
calls.set(calls.get() + 1);
Err(std::io::Error::from_raw_os_error(code))
},
windows,
|_| panic!("unexpected retry"),
)
.unwrap_err();
assert_eq!(calls.get(), 1);
assert_eq!(error.raw_os_error(), Some(code));
}
let mut calls = 0;
rename_with_retry(
|| {
calls += 1;
Ok(())
},
true,
|_| panic!("success must not wait"),
)
.unwrap();
assert_eq!(calls, 1);
}
#[cfg(windows)]
#[test]
fn native_windows_rename_preserves_old_bytes_until_a_reader_releases_delete_sharing() {
use std::os::windows::fs::OpenOptionsExt;
let directory = std::env::temp_dir().join(format!("solidcraft-rename-reader-release-{}", std::process::id()));
fs::create_dir(&directory).unwrap();
let old = directory.join("image.png");
let pending = directory.join("completed.tmp");
fs::write(&old, b"old complete image").unwrap();
write_synced(&pending, b"new complete image").unwrap();
let mut reader = Some(OpenOptions::new().read(true).share_mode(1).open(&old).unwrap());
let error = fs::rename(&pending, &old).unwrap_err();
assert!(matches!(error.raw_os_error(), Some(5 | 32)), "{error}");
let calls = Cell::new(0);
rename_with_retry(
|| {
calls.set(calls.get() + 1);
fs::rename(&pending, &old)
},
true,
|delay| {
assert_eq!(fs::read(&old).unwrap(), b"old complete image");
drop(reader.take());
std::thread::sleep(delay);
},
)
.unwrap();
assert!(calls.get() >= 2 && calls.get() <= 16);
assert_eq!(fs::read(&old).unwrap(), b"new complete image");
assert!(!pending.exists());
fs::remove_dir_all(directory).unwrap();
}
/// #8104: a scanner (antivirus, indexer, thumbnailer) that opens the target without delete
/// sharing and keeps it for a few hundred milliseconds under load delays the replace; it does
/// not fail it, and the old file stays whole until the new one takes its place.
#[cfg(windows)]
#[test]
fn a_reader_holding_the_target_for_a_while_delays_the_replace_without_failing_it() {
use std::os::windows::fs::OpenOptionsExt;
let directory = std::env::temp_dir().join(format!("solidcraft-rename-slow-reader-{}", std::process::id()));
let _ = fs::remove_dir_all(&directory);
fs::create_dir(&directory).unwrap();
let path = directory.join("library.json");
fs::write(&path, b"old whole library").unwrap();
let reader = OpenOptions::new().read(true).share_mode(1).open(&path).unwrap();
let scanner = std::thread::spawn(move || {
std::thread::sleep(std::time::Duration::from_millis(400));
drop(reader);
});
write_atomic(&path, b"new whole library", false).unwrap();
scanner.join().unwrap();
assert_eq!(fs::read(&path).unwrap(), b"new whole library");
assert!(stale_temps(&path).is_empty());
fs::remove_dir_all(directory).unwrap();
}
/// A read-only target is refused for good: the export fails at once with that reason, rather
/// than after the wait for a program holding the file, and the old file is kept.
#[cfg(windows)]
#[test]
fn a_read_only_target_fails_at_once_and_says_so() {
let directory = std::env::temp_dir().join(format!("solidcraft-rename-read-only-{}", std::process::id()));
let _ = fs::remove_dir_all(&directory);
fs::create_dir(&directory).unwrap();
let path = directory.join("render.png");
fs::write(&path, b"old image").unwrap();
let writable = fs::metadata(&path).unwrap().permissions();
let mut read_only = writable.clone();
read_only.set_readonly(true);
fs::set_permissions(&path, read_only).unwrap();
let started = std::time::Instant::now();
let error = write_atomic(&path, b"new image", false).unwrap_err();
assert!(started.elapsed() < std::time::Duration::from_millis(1000), "{:?}", started.elapsed());
assert!(error.to_string().contains("the file is read-only"), "{error}");
assert!(error.to_string().contains(&std::io::Error::from_raw_os_error(5).to_string()), "{error}");
assert_eq!(fs::read(&path).unwrap(), b"old image");
assert!(stale_temps(&path).is_empty());
fs::set_permissions(&path, writable).unwrap();
fs::remove_dir_all(directory).unwrap();
}
#[cfg(windows)]
#[test]
fn persistent_native_lock_is_an_error_without_destroying_or_bypassing_the_target() {
use std::os::windows::fs::OpenOptionsExt;
let directory = std::env::temp_dir().join(format!("solidcraft-rename-persistent-reader-{}", std::process::id()));
fs::create_dir(&directory).unwrap();
let path = directory.join("image.exr");
fs::write(&path, b"old whole raw master").unwrap();
let reader = OpenOptions::new().read(true).share_mode(1).open(&path).unwrap();
let error = write_atomic(&path, b"new whole raw master", false).unwrap_err();
assert!(error.to_string().contains("another program may have it open, or it is read-only"), "{error}");
assert_eq!(error.kind(), std::io::Error::from_raw_os_error(5).kind());
assert_eq!(fs::read(&path).unwrap(), b"old whole raw master");
assert!(stale_temps(&path).is_empty());
drop(reader);
write_atomic(&path, b"new whole raw master", false).unwrap();
assert_eq!(fs::read(&path).unwrap(), b"new whole raw master");
fs::remove_dir_all(directory).unwrap();
}
}