diff --git a/src/rrd/cache.rs b/src/rrd/cache.rs index 0a9350a2..0a39878b 100644 --- a/src/rrd/cache.rs +++ b/src/rrd/cache.rs @@ -56,7 +56,12 @@ pub fn update_value(rel_path: &str, value: f64, dst: DST) -> Result<(), Error> { } else { let mut rrd = match RRD::load(&path) { Ok(rrd) => rrd, - Err(_) => RRD::new(dst), + Err(err) => { + if err.kind() != std::io::ErrorKind::NotFound { + eprintln!("overwriting old RRD file, because of load error: {}", err); + } + RRD::new(dst) + }, }; rrd.update(now, value); rrd.save(&path)?; diff --git a/src/rrd/rrd.rs b/src/rrd/rrd.rs index 87c435c9..dbfb98aa 100644 --- a/src/rrd/rrd.rs +++ b/src/rrd/rrd.rs @@ -1,7 +1,7 @@ use std::io::Read; use std::path::Path; -use anyhow::{bail, Error}; +use anyhow::Error; use crate::api2::types::{RRDMode, RRDTimeFrameResolution}; @@ -275,10 +275,11 @@ impl RRD { (start, reso, list.into()) } - pub fn from_raw(mut raw: &[u8]) -> Result { + pub fn from_raw(mut raw: &[u8]) -> Result { let expected_len = std::mem::size_of::(); if raw.len() != expected_len { - bail!("RRD::from_raw failed - wrong data size ({} != {})", raw.len(), expected_len); + let msg = format!("wrong data size ({} != {})", raw.len(), expected_len); + return Err(std::io::Error::new(std::io::ErrorKind::Other, msg)); } let mut rrd: RRD = unsafe { std::mem::zeroed() }; @@ -288,15 +289,21 @@ impl RRD { } if rrd.magic != PROXMOX_RRD_MAGIC_1_0 { - bail!("RRD::from_raw failed - wrong magic number"); + let msg = format!("wrong magic number"); + return Err(std::io::Error::new(std::io::ErrorKind::Other, msg)); } Ok(rrd) } - pub fn load(filename: &Path) -> Result { - let raw = proxmox::tools::fs::file_get_contents(filename)?; - Self::from_raw(&raw) + pub fn load(path: &Path) -> Result { + proxmox::try_block!({ + let raw = std::fs::read(path)?; + Self::from_raw(&raw) + }).map_err(|err| { + let msg = format!("RRD load {:?} failed - {}", path, err); + std::io::Error::new(std::io::ErrorKind::Other, msg) + }) } pub fn save(&self, filename: &Path) -> Result<(), Error> {