src/rrd/cache.rs: display/log error when RRD load fails

This commit is contained in:
Dietmar Maurer 2020-05-25 10:18:53 +02:00
parent 0c4344650d
commit 84dc6adcc1
2 changed files with 20 additions and 8 deletions

View File

@ -56,7 +56,12 @@ pub fn update_value(rel_path: &str, value: f64, dst: DST) -> Result<(), Error> {
} else { } else {
let mut rrd = match RRD::load(&path) { let mut rrd = match RRD::load(&path) {
Ok(rrd) => rrd, 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.update(now, value);
rrd.save(&path)?; rrd.save(&path)?;

View File

@ -1,7 +1,7 @@
use std::io::Read; use std::io::Read;
use std::path::Path; use std::path::Path;
use anyhow::{bail, Error}; use anyhow::Error;
use crate::api2::types::{RRDMode, RRDTimeFrameResolution}; use crate::api2::types::{RRDMode, RRDTimeFrameResolution};
@ -275,10 +275,11 @@ impl RRD {
(start, reso, list.into()) (start, reso, list.into())
} }
pub fn from_raw(mut raw: &[u8]) -> Result<Self, Error> { pub fn from_raw(mut raw: &[u8]) -> Result<Self, std::io::Error> {
let expected_len = std::mem::size_of::<RRD>(); let expected_len = std::mem::size_of::<RRD>();
if raw.len() != expected_len { 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() }; let mut rrd: RRD = unsafe { std::mem::zeroed() };
@ -288,15 +289,21 @@ impl RRD {
} }
if rrd.magic != PROXMOX_RRD_MAGIC_1_0 { 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) Ok(rrd)
} }
pub fn load(filename: &Path) -> Result<Self, Error> { pub fn load(path: &Path) -> Result<Self, std::io::Error> {
let raw = proxmox::tools::fs::file_get_contents(filename)?; proxmox::try_block!({
let raw = std::fs::read(path)?;
Self::from_raw(&raw) 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> { pub fn save(&self, filename: &Path) -> Result<(), Error> {