SolarEnergy/central_backend/src/ota/ota_manager.rs

70 lines
2.0 KiB
Rust
Raw Normal View History

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<bool> {
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 content of an OTA update
pub fn get_ota_update(platform: OTAPlatform, version: &semver::Version) -> anyhow::Result<Vec<u8>> {
let path = AppConfig::get().path_ota_update(platform, version);
Ok(std::fs::read(path)?)
}
2024-10-08 20:22:57 +00:00
/// Delete an OTA update
pub fn delete_update(platform: OTAPlatform, version: &semver::Version) -> anyhow::Result<()> {
let path = AppConfig::get().path_ota_update(platform, version);
std::fs::remove_file(path)?;
Ok(())
}
/// Get the list of OTA software updates for a platform
pub fn get_ota_updates_for_platform(platform: OTAPlatform) -> anyhow::Result<Vec<OTAUpdate>> {
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)
}
/// Get all the available OTA updates
pub fn get_all_ota_updates() -> anyhow::Result<Vec<OTAUpdate>> {
let mut out = vec![];
for p in OTAPlatform::supported_platforms() {
out.append(&mut get_ota_updates_for_platform(*p)?)
}
Ok(out)
}