fix(instance): swap mirrored config dirs atomically
mirror_dir now copies into a sibling temp directory and renames it into place only when complete, instead of deleting the destination up front where a mid-copy failure destroyed the current config.
This commit is contained in:
1 parent
c0833d4bb1
commit
41a5fe0aa9
1 file changed
+37
-7
@@ -1,3 +1,6 @@
|
|||||||
|
// SPDX-FileCopyrightText: 2026 Constantin Bauer
|
||||||
|
// SPDX-License-Identifier: GPL-3.0-only
|
||||||
|
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
@@ -228,16 +231,43 @@ fn acquire_lock(profile_dir: &Path) -> Result<ConfigSyncLock, ConfigSyncError> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn mirror_dir(src: &Path, dst: &Path) -> Result<(), ConfigSyncError> {
|
fn mirror_dir(src: &Path, dst: &Path) -> Result<(), ConfigSyncError> {
|
||||||
if dst.exists() {
|
// stage the copy in a sibling temp dir and swap it in only when complete.
|
||||||
remove_path(dst)?;
|
// deleting dst up-front (the old behaviour) meant a failure mid-copy
|
||||||
|
// destroyed the current config tree with nothing to fall back to.
|
||||||
|
let parent = dst.parent().filter(|p| !p.as_os_str().is_empty());
|
||||||
|
let Some(parent) = parent else {
|
||||||
|
return Err(std::io::Error::new(
|
||||||
|
std::io::ErrorKind::InvalidInput,
|
||||||
|
format!("no parent directory for {}", dst.display()),
|
||||||
|
)
|
||||||
|
.into());
|
||||||
|
};
|
||||||
|
std::fs::create_dir_all(parent)?;
|
||||||
|
let name = dst
|
||||||
|
.file_name()
|
||||||
|
.and_then(|s| s.to_str())
|
||||||
|
.unwrap_or("mirror");
|
||||||
|
let staging = parent.join(format!(".{name}.mirror-tmp"));
|
||||||
|
if staging.exists() {
|
||||||
|
remove_path(&staging)?;
|
||||||
}
|
}
|
||||||
std::fs::create_dir_all(dst)?;
|
std::fs::create_dir_all(&staging)?;
|
||||||
|
|
||||||
if !src.exists() {
|
let result = (|| {
|
||||||
return Ok(());
|
if src.exists() {
|
||||||
|
copy_dir_contents(src, &staging)?;
|
||||||
|
}
|
||||||
|
if dst.exists() {
|
||||||
|
remove_path(dst)?;
|
||||||
|
}
|
||||||
|
std::fs::rename(&staging, dst)?;
|
||||||
|
Ok(())
|
||||||
|
})();
|
||||||
|
|
||||||
|
if result.is_err() {
|
||||||
|
let _ = std::fs::remove_dir_all(&staging);
|
||||||
}
|
}
|
||||||
|
result
|
||||||
copy_dir_contents(src, dst)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sync_to_profile(minecraft_dir: &Path, profile_dir: &Path) -> Result<(), ConfigSyncError> {
|
fn sync_to_profile(minecraft_dir: &Path, profile_dir: &Path) -> Result<(), ConfigSyncError> {
|
||||||
|
|||||||
Reference in new issue
Block a user