use super::{BACKUP_FILE, Error, Path, Result, STATE_FILE, Terraform, ToolCx};
impl Terraform {
pub(crate) fn adopt(cx: &ToolCx<'_>, from: &Path) -> Result<Vec<&'static str>> {
let refuse = |reason: String| Error::Adopt {
run: cx.run.to_string(),
reason,
};
if cx.state_dir.join(STATE_FILE).exists() {
return Err(refuse("the run has a Terraform state already".into()));
}
if !from.join(STATE_FILE).is_file() {
return Err(refuse(format!("{} holds no {STATE_FILE}", from.display())));
}
let mut moved = Vec::new();
for file in [STATE_FILE, BACKUP_FILE] {
let (src, dst) = (from.join(file), cx.state_dir.join(file));
if !src.exists() {
continue;
}
if let Err(e) = move_file(&src, &dst) {
Terraform::give_back(cx, from, &moved);
return Err(Error::io(&src, "moved")(e));
}
moved.push(file);
}
Ok(moved)
}
pub(crate) fn give_back(cx: &ToolCx<'_>, from: &Path, files: &[&str]) {
for file in files {
if let Err(e) = move_file(&cx.state_dir.join(file), &from.join(file)) {
tracing::warn!(file, error = %e, "an adopted state file couldn't be put back");
}
}
}
}
fn move_file(from: &Path, to: &Path) -> std::io::Result<()> {
match std::fs::rename(from, to) {
Err(e) if e.kind() == std::io::ErrorKind::CrossesDevices => {
std::fs::copy(from, to)?;
std::fs::remove_file(from).inspect_err(|_| {
if let Err(gone) = std::fs::remove_file(to) {
tracing::warn!(error = %gone, file = %to.display(), "a copy of a taken-over state couldn't be removed");
}
})
}
renamed => renamed,
}
}
#[cfg(all(test, unix))]
mod tests {
use super::*;
#[test]
fn a_state_that_cant_be_moved_stays_only_where_it_was() {
use std::os::unix::fs::PermissionsExt;
let (old, run) = (tempfile::tempdir().unwrap(), tempfile::tempdir().unwrap());
let (from, to) = (old.path().join("terraform.tfstate"), run.path().join("terraform.tfstate"));
std::fs::write(&from, "{}").unwrap();
std::fs::set_permissions(old.path(), std::fs::Permissions::from_mode(0o500)).unwrap();
let moved = move_file(&from, &to);
std::fs::set_permissions(old.path(), std::fs::Permissions::from_mode(0o700)).unwrap();
if moved.is_ok() {
return; }
assert_eq!(moved.unwrap_err().kind(), std::io::ErrorKind::PermissionDenied);
assert!(!to.exists(), "no second copy in the run");
assert!(from.is_file(), "the state stays where it was");
}
}