Files
rmcl/src/migrate.rs
T
objz a2d337397c fix(migration): recover safe upgrade conflicts for 0.6.1
Deduplicate identical files, merge nonconflicting legacy directories, and archive differing disposable caches without overwriting current data. Resume stale journals and detect local settings conflicts before moving instances.

Render migration Retry/Quit in the shared popup footer and add regression coverage for recovery and data preservation.
2026-10-06 11:58:22 +02:00

192 lines
6.5 KiB
Rust

// SPDX-FileCopyrightText: 2026 Constantin Bauer
// SPDX-License-Identifier: GPL-3.0-only
//! Legacy mcl→rmcl path migration. Runs once before any other init in main().
//! Idempotent: absence of the old dir is the sentinel.
use std::fs;
use std::io;
use std::path::Path;
const OLD_NAME: &str = "mcl";
const NEW_NAME: &str = "rmcl";
pub fn run_legacy_rename() {
if let Some(dir) = dirs::config_dir() {
rename_top_level(&dir.join(OLD_NAME), &dir.join(NEW_NAME));
}
if let Some(dir) = dirs::data_dir() {
let new_data = dir.join(NEW_NAME);
rename_top_level(&dir.join(OLD_NAME), &new_data);
cleanup_instance_leftovers(&new_data.join("instances"));
rewrite_linux_desktop_entries(&dir, &new_data.join("instances"));
}
if let Some(dir) = dirs::cache_dir() {
rename_top_level(&dir.join(OLD_NAME), &dir.join(NEW_NAME));
}
if let (Some(desk), Some(data)) = (dirs::desktop_dir(), dirs::data_dir()) {
rewrite_native_desktop_shortcuts(&desk, &data.join(NEW_NAME).join("instances"));
}
if let (Some(config_dir), Some(data_dir)) = (dirs::config_dir(), dirs::data_dir())
&& !data_dir.join(OLD_NAME).exists()
&& let Err(error) =
crate::config::migrate_legacy_data_paths(&config_dir.join(NEW_NAME).join("config.toml"))
{
eprintln!("rmcl migration: failed to update legacy config paths: {error}");
}
}
fn rename_top_level(old: &Path, new: &Path) {
if !old.exists() {
return;
}
if new.exists() {
match crate::layout_migration::validate_merge(old, new, false)
.and_then(|_| crate::layout_migration::move_or_merge(old, new, None))
{
Ok(()) => eprintln!(
"rmcl migration: merged {} -> {}",
old.display(),
new.display()
),
Err(error) => eprintln!("rmcl migration stopped safely: {error}"),
}
return;
}
match fs::rename(old, new) {
Ok(_) => eprintln!(
"rmcl migration: moved {} -> {}",
old.display(),
new.display()
),
Err(e) if e.kind() == io::ErrorKind::CrossesDevices => {
if let Err(e2) = copy_top_level_across_devices(old, new) {
eprintln!(
"rmcl migration: failed cross-device copy {} -> {}: {}",
old.display(),
new.display(),
e2
);
return;
}
eprintln!(
"rmcl migration: cross-device moved {} -> {}",
old.display(),
new.display()
);
}
Err(e) => eprintln!(
"rmcl migration: failed to rename {} -> {}: {}",
old.display(),
new.display(),
e
),
}
}
fn copy_top_level_across_devices(old: &Path, new: &Path) -> io::Result<()> {
let parent = new
.parent()
.ok_or_else(|| io::Error::other("migration destination has no parent"))?;
let staging = tempfile::Builder::new()
.prefix(".rmcl-migration-")
.tempdir_in(parent)?;
copy_dir_recursive(old, staging.path())?;
fs::rename(staging.path(), new)?;
fs::remove_dir_all(old)
}
fn copy_dir_recursive(src: &Path, dst: &Path) -> io::Result<()> {
fs::create_dir_all(dst)?;
for entry in fs::read_dir(src)? {
let entry = entry?;
let ty = entry.file_type()?;
let dst_path = dst.join(entry.file_name());
if ty.is_symlink() {
crate::storage::copy_symlink(&entry.path(), &dst_path)?;
} else if ty.is_dir() {
copy_dir_recursive(&entry.path(), &dst_path)?;
} else {
fs::copy(entry.path(), &dst_path)?;
}
}
fs::set_permissions(dst, fs::metadata(src)?.permissions())?;
Ok(())
}
fn cleanup_instance_leftovers(instances_dir: &Path) {
let Ok(entries) = fs::read_dir(instances_dir) else {
return;
};
for entry in entries.flatten() {
let mc = entry.path().join(crate::storage::MINECRAFT_DIR_NAME);
for leftover in [".mcl-shim.jar", ".mcl-log4j2.xml"] {
let p = mc.join(leftover);
if p.exists() {
let _ = fs::remove_file(&p);
}
}
}
}
fn rewrite_linux_desktop_entries(_data_dir: &Path, _instances_dir: &Path) {
#[cfg(target_os = "linux")]
{
let apps_dir = _data_dir.join("applications");
let Ok(entries) = fs::read_dir(_instances_dir) else {
return;
};
for entry in entries.flatten() {
let name = entry.file_name();
let sanitized = crate::instance::desktop::sanitize(&name.to_string_lossy());
let old = apps_dir.join(format!("mcl-{sanitized}.desktop"));
let new = apps_dir.join(format!("rmcl-{sanitized}.desktop"));
if old.exists()
&& !new.exists()
&& let Ok(content) = fs::read_to_string(&old)
{
let new_content = content.replace("Exec=mcl ", "Exec=rmcl ");
if fs::write(&new, new_content).is_ok() {
let _ = fs::remove_file(&old);
}
}
}
}
}
fn rewrite_native_desktop_shortcuts(_desktop_dir: &Path, _instances_dir: &Path) {
#[cfg(any(target_os = "windows", target_os = "macos"))]
{
// mcl-era windows shortcuts were .bat files; current rmcl writes
// .vbs, so only the legacy extension is rewritten here.
let ext = if cfg!(target_os = "windows") {
"bat"
} else {
"command"
};
let Ok(entries) = fs::read_dir(_instances_dir) else {
return;
};
for entry in entries.flatten() {
let display = entry.file_name().to_string_lossy().into_owned();
// mcl named shortcut files with the sanitized form of the
// instance name ("My Pack" -> "My_Pack"), so look those up.
let sanitized = crate::instance::desktop::sanitize(&display);
let path = _desktop_dir.join(format!("Minecraft - {sanitized}.{ext}"));
if !path.exists() {
continue;
}
if let Ok(content) = fs::read_to_string(&path) {
let new_content = content.replace("mcl instance launch", "rmcl instance launch");
if new_content != content {
let _ = fs::write(&path, new_content);
}
}
}
}
}
#[cfg(test)]
#[path = "tests/migrate.rs"]
mod tests;