use super::super::{managed, native, Provider};
use super::*;
pub fn migrate(id: &str, directory: Option<&str>) -> Result<String, String> {
let state = store::root();
let _lock = lock(&state, id)?;
let provider = find(&state, id)
.map(|record| record.draft.provider)
.unwrap_or(Provider::System);
migrate_using(
&state,
id,
&mut *super::registration::adapter(provider),
directory,
)
}
pub(super) fn migrate_using(
state: &Path,
id: &str,
scheduler: &mut dyn super::registration::Registration,
directory: Option<&str>,
) -> Result<String, String> {
let Some(old) = store::legacy_task(state, id)? else {
let record = find(state, id)?;
let path = checked(state, &record)?;
let ascii = path.file_name().is_some_and(|name| name.is_ascii());
let unchanged = match directory {
None => true,
Some(name) => path
.file_name()
.is_some_and(|current| current.to_str() == Some(name)),
};
if ascii && unchanged {
return Ok(id.into());
}
super::lifecycle::cleanup_using(state, &record, scheduler)?;
let result = super::relocation::migrate(state, id, directory);
let mut current = find(state, id)?;
current.draft.enabled = record.draft.enabled;
let manifest = super::layout::read_manifest(&checked(state, ¤t)?)?;
super::storage::materialize(¤t, manifest.dependencies, manifest.hermes)?;
let mut local = receipt(¤t)?;
local.state = "pending".into();
write_receipt(¤t, &local)?;
super::lifecycle::prepare_using(state, ¤t, scheduler)?;
return result;
};
managed::verify(state, &old)?;
if old.draft.provider != Provider::System {
return Err("Only owned system tasks support package migration".into());
}
let next = if let Ok(package) = find(state, id) {
package
} else {
let mut draft = old.draft.clone();
if let Some(content) = native::payload::read(state, &old)? {
draft.content = content;
}
draft.files = native::bundle_files::read(&old)?;
draft.script_dir = None;
draft.enabled = false;
super::creation::stage_identity(state, draft, Vec::new(), None, Some(id))?;
find(state, id)?
};
let path = checked(state, &next)?;
let journal = path.join(".local/migration.json");
if journal.exists() {
let previous: ManagedAutomation = super::storage::json(&journal)?;
if previous.revision != old.revision || previous.id != old.id {
return Err("Original task changed during migration; its files were preserved".into());
}
} else {
json_write(&journal, &old)?;
}
let result = (|| {
scheduler.apply(state, &mut old.clone(), false)?;
let manifest: Manifest = super::layout::read_manifest(&path)?;
let mut configured = next.clone();
configured.draft.enabled = old.draft.enabled;
configured.revision = old
.revision
.checked_add(1)
.ok_or("Automation revision exhausted")?;
super::storage::materialize(&configured, manifest.dependencies, manifest.hermes)?;
let mut receipt = receipt(&configured)?;
receipt.revision = configured.revision;
receipt.fingerprint = super::storage::fingerprint(&configured.draft)?;
receipt.state = "pending".into();
receipt.archived = false;
write_receipt(&configured, &receipt)?;
let mut paused = configured.clone();
paused.draft.enabled = false;
super::lifecycle::prepare_using(state, &paused, scheduler)?;
store::retire_legacy(state, id)?;
scheduler.apply(state, &mut configured, old.draft.enabled)?;
Ok::<_, String>(())
})();
if let Err(error) = result {
let _ = std::fs::remove_file(path.join(".local/ready"));
let rollback = scheduler
.apply(state, &mut old.clone(), old.draft.enabled)
.and_then(|()| store::restore_legacy(state, &old));
let mut receipt = receipt(&next)?;
receipt.state = "prepare_failed".into();
receipt.error = Some(format!(
"Migration failed: {error}; restoration: {rollback:?}"
));
write_receipt(&next, &receipt)?;
return Err(receipt.error.unwrap_or(error));
}
std::fs::remove_file(journal).map_err(|e| e.to_string())?;
#[cfg(not(test))]
crate::core::cloud_sync::notify_central_change();
Ok(id.into())
}