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
+1
-10
@@ -268,16 +268,7 @@ fn profile_instance(matches: &ArgMatches) -> CliResult {
|
||||
} else {
|
||||
Some(profile.as_str())
|
||||
};
|
||||
let instance_dir = manager.instances_dir.join(&config.name);
|
||||
let selected = crate::instance::config_sync::switch_profile(
|
||||
&config.name,
|
||||
config.config_sync_profile.as_deref(),
|
||||
target,
|
||||
&manager.meta_dir,
|
||||
&instance_dir,
|
||||
)?;
|
||||
config.config_sync_profile = selected;
|
||||
manager.save(&config)?;
|
||||
crate::instance::config_sync::switch_profile_and_save(&manager, &mut config, target)?;
|
||||
|
||||
println!(
|
||||
"Updated '{}' config profile to {}.",
|
||||
|
||||
@@ -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();
|
||||
|
||||
+10
-22
@@ -716,23 +716,15 @@ impl App {
|
||||
);
|
||||
return Ok(());
|
||||
}
|
||||
let instance_dir = self.instance_manager.instances_dir.join(&instance.name);
|
||||
match crate::instance::config_sync::switch_profile(
|
||||
&instance.name,
|
||||
instance.config_sync_profile.as_deref(),
|
||||
let mut updated = instance.clone();
|
||||
match crate::instance::config_sync::switch_profile_and_save(
|
||||
&self.instance_manager,
|
||||
&mut updated,
|
||||
profile.as_deref(),
|
||||
&self.instance_manager.meta_dir,
|
||||
&instance_dir,
|
||||
) {
|
||||
Ok(selected) => {
|
||||
let mut updated = instance.clone();
|
||||
updated.config_sync_profile = selected;
|
||||
if let Err(error) = self.instance_manager.save(&updated) {
|
||||
tracing::error!("Failed to save config profile: {error}");
|
||||
} else {
|
||||
self.instances_state
|
||||
.replace_instance(&instance.name, updated);
|
||||
}
|
||||
Ok(()) => {
|
||||
self.instances_state
|
||||
.replace_instance(&instance.name, updated);
|
||||
}
|
||||
Err(error) => {
|
||||
error_buffer::push_error(error_buffer::ErrorEvent {
|
||||
@@ -2071,16 +2063,12 @@ impl App {
|
||||
.into_iter()
|
||||
.filter(|instance| instance.config_sync_profile.as_deref() == Some(profile))
|
||||
{
|
||||
let instance_dir = self.instance_manager.instances_dir.join(&instance.name);
|
||||
let mut updated = instance.clone();
|
||||
updated.config_sync_profile = crate::instance::config_sync::switch_profile(
|
||||
&instance.name,
|
||||
instance.config_sync_profile.as_deref(),
|
||||
crate::instance::config_sync::switch_profile_and_save(
|
||||
&self.instance_manager,
|
||||
&mut updated,
|
||||
None,
|
||||
&self.instance_manager.meta_dir,
|
||||
&instance_dir,
|
||||
)?;
|
||||
self.instance_manager.save(&updated)?;
|
||||
self.instances_state
|
||||
.replace_instance(&instance.name, updated);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user