use std::time::Duration;
use crate::{integrity_check::IntegrityCheck, update_workflow::UpdateError};
#[cfg(feature = "uboot")]
use crate::{traits::persistent_store::PersistentStore, uboot::UBootEnv};
pub struct Timeout {
duration: Duration,
}
impl Timeout {
pub fn new(duration: Duration) -> Self {
Timeout { duration }
}
}
impl IntegrityCheck for Timeout {
async fn system_is_ok(&mut self) -> Result<(), UpdateError> {
tokio::time::sleep(self.duration).await;
Ok(())
}
}
pub struct SystemdIsSystemRunning;
impl IntegrityCheck for SystemdIsSystemRunning {
async fn system_is_ok(&mut self) -> Result<(), UpdateError> {
let output = tokio::process::Command::new("systemctl")
.arg("is-system-running")
.arg("--wait")
.output()
.await
.map_err(|err| UpdateError::RollbackNeeded(err.to_string()))?;
if output.status.success() {
Ok(())
} else {
Err(UpdateError::RollbackNeeded(format!(
"systemctl is-system-running failed with '{}'",
String::from_utf8(output.stdout).unwrap_or("<invalid ouput>".to_string())
)))
}
}
}
#[cfg(feature = "uboot")]
pub struct UBootDidRollback {
uboot: UBootEnv,
}
#[cfg(feature = "uboot")]
impl UBootDidRollback {
pub fn new(uboot: UBootEnv) -> Self {
Self { uboot }
}
}
#[cfg(feature = "uboot")]
impl IntegrityCheck for UBootDidRollback {
async fn system_is_ok(&mut self) -> Result<(), UpdateError> {
if let Ok(Some(ustate)) = self.uboot.load("ustate").await {
if ustate == "3" {
self.uboot.save("ustate", "0").await.ok();
return Err(UpdateError::ExternalRollback);
}
}
Ok(())
}
}