src/rrd/rrd.rs: store/verify magic number

This commit is contained in:
Dietmar Maurer 2020-05-25 09:21:54 +02:00
parent 4f9513996c
commit 0c4344650d

View File

@ -7,6 +7,9 @@ use crate::api2::types::{RRDMode, RRDTimeFrameResolution};
pub const RRD_DATA_ENTRIES: usize = 70; pub const RRD_DATA_ENTRIES: usize = 70;
// openssl::sha::sha256(b"Proxmox Round Robin Database file v1.0")[0..8];
pub const PROXMOX_RRD_MAGIC_1_0: [u8; 8] = [206, 46, 26, 212, 172, 158, 5, 186];
use bitflags::bitflags; use bitflags::bitflags;
bitflags!{ bitflags!{
@ -157,6 +160,7 @@ impl RRA {
#[repr(C)] #[repr(C)]
// Note: Avoid alignment problems by using 8byte types only // Note: Avoid alignment problems by using 8byte types only
pub struct RRD { pub struct RRD {
magic: [u8; 8],
hour_avg: RRA, hour_avg: RRA,
hour_max: RRA, hour_max: RRA,
day_avg: RRA, day_avg: RRA,
@ -178,6 +182,7 @@ impl RRD {
}; };
Self { Self {
magic: PROXMOX_RRD_MAGIC_1_0,
hour_avg: RRA::new( hour_avg: RRA::new(
flags | RRAFlags::CF_AVERAGE, flags | RRAFlags::CF_AVERAGE,
RRDTimeFrameResolution::Hour as u64, RRDTimeFrameResolution::Hour as u64,
@ -282,6 +287,10 @@ impl RRD {
raw.read_exact(rrd_slice)?; raw.read_exact(rrd_slice)?;
} }
if rrd.magic != PROXMOX_RRD_MAGIC_1_0 {
bail!("RRD::from_raw failed - wrong magic number");
}
Ok(rrd) Ok(rrd)
} }