2024-10-05 13:50:46 +00:00
|
|
|
use crate::app_config::AppConfig;
|
2024-10-05 17:35:23 +00:00
|
|
|
use crate::ota::ota_update::{OTAPlatform, OTAUpdate};
|
2024-10-05 13:50:46 +00:00
|
|
|
use crate::utils::files_utils;
|
2024-10-05 17:35:23 +00:00
|
|
|
use std::os::unix::fs::MetadataExt;
|
|
|
|
use std::str::FromStr;
|
2024-10-05 13:50:46 +00:00
|
|
|
|
|
|
|
/// 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(())
|
|
|
|
}
|
2024-10-05 17:35:23 +00:00
|
|
|
|
2024-10-05 18:00:57 +00:00
|
|
|
/// 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(())
|
|
|
|
}
|
|
|
|
|
2024-10-05 17:35:23 +00:00
|
|
|
/// 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)
|
|
|
|
}
|
2024-10-08 19:53:21 +00:00
|
|
|
|
|
|
|
/// 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)
|
|
|
|
}
|