diff --git a/src/lib.rs b/src/lib.rs index fa9c6236..c2ac4f8d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -309,7 +309,7 @@ where } } - let disks = telemetry::storage_usage::get_storage_usage()?; + let disks = telemetry::storage_usage::get_storage_usage(); for (disk_name, storage) in disks { device .send_object( @@ -551,7 +551,7 @@ mod tests { ) .returning(|_: &str, _: &str, _: AstarteType| Ok(())); - let storage_usage = get_storage_usage().unwrap(); + let storage_usage = get_storage_usage(); publisher .expect_send_object() .withf( @@ -616,7 +616,7 @@ mod tests { ) .returning(|_: &str, _: &str, _: SystemStatus| Ok(())); - let storage_usage = get_storage_usage().unwrap(); + let storage_usage = get_storage_usage(); publisher .expect_send_object() .withf( @@ -646,7 +646,7 @@ mod tests { }, ) .await; - for (path, payload) in get_storage_usage().unwrap() { + for (path, payload) in get_storage_usage() { DeviceManager::<_, MockSubscriber>::send_telemetry( &publisher, TelemetryMessage { diff --git a/src/telemetry/mod.rs b/src/telemetry/mod.rs index 16f33e9b..dec51ad7 100644 --- a/src/telemetry/mod.rs +++ b/src/telemetry/mod.rs @@ -333,7 +333,7 @@ async fn send_data( .await; } "io.edgehog.devicemanager.StorageUsage" => { - let storage_usage = storage_usage::get_storage_usage()?; + let storage_usage = storage_usage::get_storage_usage(); for (path, payload) in storage_usage { let _ = communication_channel .send(TelemetryMessage { diff --git a/src/telemetry/storage_usage.rs b/src/telemetry/storage_usage.rs index f047cd5a..663ec18d 100644 --- a/src/telemetry/storage_usage.rs +++ b/src/telemetry/storage_usage.rs @@ -18,34 +18,52 @@ * SPDX-License-Identifier: Apache-2.0 */ -use crate::error::DeviceManagerError; -use astarte_device_sdk::AstarteAggregate; +use astarte_device_sdk::{astarte_aggregate, AstarteAggregate}; +use log::{error, warn}; use std::collections::HashMap; use sysinfo::{DiskExt, System, SystemExt}; #[derive(Debug, AstarteAggregate)] -#[allow(non_snake_case)] +#[astarte_aggregate(rename_all = "camelCase")] pub struct DiskUsage { - pub totalBytes: i64, - pub freeBytes: i64, + pub total_bytes: i64, + pub free_bytes: i64, } /// get structured data for `io.edgehog.devicemanager.StorageUsage` interface /// /dev/ is excluded from the device names since it is common for all devices -pub fn get_storage_usage() -> Result, DeviceManagerError> { - let mut ret: HashMap = HashMap::new(); +pub fn get_storage_usage() -> HashMap { let mut sys = System::new_all(); sys.refresh_disks(); - for disk in sys.disks() { - let disk_name = disk.name().to_str().unwrap().replace("/dev/", ""); - ret.insert( - disk_name.to_owned() as String, - DiskUsage { - totalBytes: disk.total_space() as i64, - freeBytes: disk.available_space() as i64, - }, - ); - } - Ok(ret) + sys.disks() + .iter() + .filter_map(|disk| { + let Some(name) = disk.name().to_str() else { + warn!("non-utf8 path {}, ignoring", disk.name().to_string_lossy()); + return None; + }; + let name = name.strip_prefix("/dev/").unwrap_or(name); + // remove disks with a higher depth + if name.contains('/') { + warn!("not simple disks device, ignoring"); + return None; + } + let Ok(total_bytes) = disk.total_space().try_into() else { + error!("disk size too big, ignoring"); + return None; + }; + let Ok(free_bytes) = disk.available_space().try_into() else { + error!("available space too big, ignoring"); + return None; + }; + Some(( + name.to_string(), + DiskUsage { + total_bytes, + free_bytes, + }, + )) + }) + .collect() }