fix(config-sync): roll back failed profile saves
This commit is contained in:
1 parent
297a52aed8
commit
3f2f514097
4 files changed
+111
-33
No files matched your search
@@ -13,6 +13,10 @@ pub enum ConfigSyncError {
|
||||
InstanceRunning { instance: String },
|
||||
#[error("IO error: {0}")]
|
||||
Io(#[from] std::io::Error),
|
||||
#[error("Failed to save config profile: {0}")]
|
||||
Save(String),
|
||||
#[error("Failed to save config profile ({save}) and roll back profile files ({rollback})")]
|
||||
SaveRollback { save: String, rollback: String },
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -106,7 +110,7 @@ pub fn delete_profile(meta_dir: &Path, profile: &str) -> Result<(), ConfigSyncEr
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn switch_profile(
|
||||
fn switch_profile(
|
||||
instance_name: &str,
|
||||
current_profile: Option<&str>,
|
||||
target_profile: Option<&str>,
|
||||
@@ -172,6 +176,67 @@ pub fn switch_profile(
|
||||
Ok(Some(profile.to_string()))
|
||||
}
|
||||
|
||||
pub fn switch_profile_and_save(
|
||||
manager: &crate::instance::InstanceManager,
|
||||
config: &mut crate::instance::InstanceConfig,
|
||||
target_profile: Option<&str>,
|
||||
) -> Result<(), ConfigSyncError> {
|
||||
let current_profile = config.config_sync_profile.clone();
|
||||
let instance_dir = manager.instances_dir.join(&config.name);
|
||||
let selected = switch_profile_and_persist(
|
||||
&config.name,
|
||||
current_profile.as_deref(),
|
||||
target_profile,
|
||||
&manager.meta_dir,
|
||||
&instance_dir,
|
||||
|selected| {
|
||||
let mut updated = config.clone();
|
||||
updated.config_sync_profile = selected.map(str::to_owned);
|
||||
manager.save(&updated)
|
||||
},
|
||||
)?;
|
||||
config.config_sync_profile = selected;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn switch_profile_and_persist<E>(
|
||||
instance_name: &str,
|
||||
current_profile: Option<&str>,
|
||||
target_profile: Option<&str>,
|
||||
meta_dir: &Path,
|
||||
instance_dir: &Path,
|
||||
persist: impl FnOnce(Option<&str>) -> Result<(), E>,
|
||||
) -> Result<Option<String>, ConfigSyncError>
|
||||
where
|
||||
E: std::fmt::Display,
|
||||
{
|
||||
let selected = switch_profile(
|
||||
instance_name,
|
||||
current_profile,
|
||||
target_profile,
|
||||
meta_dir,
|
||||
instance_dir,
|
||||
)?;
|
||||
let Err(save) = persist(selected.as_deref()) else {
|
||||
return Ok(selected);
|
||||
};
|
||||
let save = save.to_string();
|
||||
|
||||
if let Err(rollback) = switch_profile(
|
||||
instance_name,
|
||||
selected.as_deref(),
|
||||
current_profile,
|
||||
meta_dir,
|
||||
instance_dir,
|
||||
) {
|
||||
return Err(ConfigSyncError::SaveRollback {
|
||||
save,
|
||||
rollback: rollback.to_string(),
|
||||
});
|
||||
}
|
||||
Err(ConfigSyncError::Save(save))
|
||||
}
|
||||
|
||||
fn normalize_profile(profile: &str) -> Option<&str> {
|
||||
let trimmed = profile.trim();
|
||||
if trimmed.is_empty() {
|
||||
|
||||
@@ -282,6 +282,40 @@ fn switch_between_profiles_saves_old_and_loads_new() {
|
||||
assert!(!minecraft.join("config/a.txt").exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_profile_save_restores_previous_files() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let meta = tmp.path().join("meta");
|
||||
let instance = tmp.path().join("instance");
|
||||
let minecraft = instance.join(crate::storage::MINECRAFT_DIR_NAME);
|
||||
std::fs::create_dir_all(minecraft.join("config")).unwrap();
|
||||
std::fs::create_dir_all(meta.join("state/profiles/main/config")).unwrap();
|
||||
std::fs::write(minecraft.join("options.txt"), "local-options").unwrap();
|
||||
std::fs::write(minecraft.join("config/local.txt"), "local").unwrap();
|
||||
std::fs::write(
|
||||
meta.join("state/profiles/main/options.txt"),
|
||||
"shared-options",
|
||||
)
|
||||
.unwrap();
|
||||
std::fs::write(meta.join("state/profiles/main/config/shared.txt"), "shared").unwrap();
|
||||
|
||||
let error = switch_profile_and_persist("inst", None, Some("main"), &meta, &instance, |_| {
|
||||
Err(std::io::Error::other("disk full"))
|
||||
})
|
||||
.unwrap_err();
|
||||
|
||||
assert!(matches!(error, ConfigSyncError::Save(_)));
|
||||
assert_eq!(
|
||||
std::fs::read_to_string(minecraft.join("options.txt")).unwrap(),
|
||||
"local-options"
|
||||
);
|
||||
assert_eq!(
|
||||
std::fs::read_to_string(minecraft.join("config/local.txt")).unwrap(),
|
||||
"local"
|
||||
);
|
||||
assert!(!minecraft.join("config/shared.txt").exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn second_instance_uses_profile_options_saved_by_first_instance() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
|
||||
Reference in new issue
Block a user