use crate::app_config::AppConfig; use crate::ota::ota_update::{OTAPlatform, OTAUpdate}; use crate::utils::files_utils; use std::os::unix::fs::MetadataExt; use std::str::FromStr; /// Check out whether a given update exists or not pub fn update_exists(platform: OTAPlatform, version: &semver::Version) -> anyhow::Result { Ok(AppConfig::get() .path_ota_update(platform, version) .is_file()) } /// Save a new firmware update pub fn save_update( platform: OTAPlatform, version: &semver::Version, update: &[u8], ) -> anyhow::Result<()> { let path = AppConfig::get().path_ota_update(platform, version); files_utils::create_directory_if_missing(path.parent().unwrap())?; std::fs::write(path, update)?; Ok(()) } /// Get the list of OTA software updates for a platform pub fn get_ota_updates_for_platform(platform: OTAPlatform) -> anyhow::Result> { let ota_path = AppConfig::get().ota_platform_dir(platform); let mut out = Vec::new(); for e in std::fs::read_dir(ota_path)? { let e = e?; out.push(OTAUpdate { platform, version: semver::Version::from_str(e.file_name().to_str().unwrap_or("bad"))?, file_size: e.metadata()?.size(), }); } Ok(out) }