From 6eed8ed992852947000496c8dd1f3684b29ed2d0 Mon Sep 17 00:00:00 2001 From: Lukas Wagner Date: Wed, 31 Jan 2024 14:51:54 +0100 Subject: [PATCH] rrd: feature-gate support for the v1 format new users of this crate might not really need support for the v1 format. Signed-off-by: Lukas Wagner --- proxmox-rrd/Cargo.toml | 4 ++++ proxmox-rrd/src/lib.rs | 1 + proxmox-rrd/src/rrd.rs | 23 ++++++++++++----------- proxmox-rrd/tests/file_format_test.rs | 3 ++- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/proxmox-rrd/Cargo.toml b/proxmox-rrd/Cargo.toml index 630ecec3..5f8de386 100644 --- a/proxmox-rrd/Cargo.toml +++ b/proxmox-rrd/Cargo.toml @@ -25,3 +25,7 @@ serde_json.workspace = true proxmox-schema = { workspace = true, features = [ "api-macro" ] } proxmox-sys.workspace = true proxmox-time.workspace = true + +[features] +default = [ "rrd_v1" ] +rrd_v1 = [] diff --git a/proxmox-rrd/src/lib.rs b/proxmox-rrd/src/lib.rs index 80b39438..65e04247 100644 --- a/proxmox-rrd/src/lib.rs +++ b/proxmox-rrd/src/lib.rs @@ -6,6 +6,7 @@ //! * Stores data for different time resolution //! * Simple cache implementation with journal support +#[cfg(feature = "rrd_v1")] mod rrd_v1; pub mod rrd; diff --git a/proxmox-rrd/src/rrd.rs b/proxmox-rrd/src/rrd.rs index 0b8ac460..1aec9c79 100644 --- a/proxmox-rrd/src/rrd.rs +++ b/proxmox-rrd/src/rrd.rs @@ -21,8 +21,6 @@ use serde::{Deserialize, Serialize}; use proxmox_schema::api; use proxmox_sys::fs::{make_tmp_file, CreateOptions}; -use crate::rrd_v1; - /// Proxmox RRD v2 file magic number // openssl::sha::sha256(b"Proxmox Round Robin Database file v2.0")[0..8]; pub const PROXMOX_RRD_MAGIC_2_0: [u8; 8] = [224, 200, 228, 27, 239, 112, 122, 159]; @@ -366,15 +364,18 @@ impl RRD { bail!("not an rrd file - file is too small ({})", raw.len()); } - let rrd = if raw[0..8] == rrd_v1::PROXMOX_RRD_MAGIC_1_0 { - let v1 = rrd_v1::RRDv1::from_raw(raw)?; - v1.to_rrd_v2() - .map_err(|err| format_err!("unable to convert from old V1 format - {}", err))? - } else if raw[0..8] == PROXMOX_RRD_MAGIC_2_0 { - serde_cbor::from_slice(&raw[8..]) - .map_err(|err| format_err!("unable to decode RRD file - {}", err))? - } else { - bail!("not an rrd file - unknown magic number"); + let rrd: RRD = match &raw[0..8] { + #[cfg(feature = "rrd_v1")] + magic if magic == crate::rrd_v1::PROXMOX_RRD_MAGIC_1_0 => { + let v1 = crate::rrd_v1::RRDv1::from_raw(raw)?; + v1.to_rrd_v2() + .map_err(|err| format_err!("unable to convert from old V1 format - {err}"))? + } + magic if magic == PROXMOX_RRD_MAGIC_2_0 => { + serde_cbor::from_slice(&raw[8..]) + .map_err(|err| format_err!("unable to decode RRD file - {err}"))? + } + _ => bail!("not an rrd file - unknown magic number") }; if rrd.source.last_update < 0.0 { diff --git a/proxmox-rrd/tests/file_format_test.rs b/proxmox-rrd/tests/file_format_test.rs index 372a4077..e05c4853 100644 --- a/proxmox-rrd/tests/file_format_test.rs +++ b/proxmox-rrd/tests/file_format_test.rs @@ -20,12 +20,13 @@ fn compare_file(fn1: &str, fn2: &str) -> Result<(), Error> { Ok(()) } -const RRD_V1_FN: &str = "./tests/testdata/cpu.rrd_v1"; const RRD_V2_FN: &str = "./tests/testdata/cpu.rrd_v2"; // make sure we can load and convert RRD v1 #[test] +#[cfg(feature = "rrd_v1")] fn upgrade_from_rrd_v1() -> Result<(), Error> { + const RRD_V1_FN: &str = "./tests/testdata/cpu.rrd_v1"; let rrd = RRD::load(Path::new(RRD_V1_FN), true)?; const RRD_V2_NEW_FN: &str = "./tests/testdata/cpu.rrd_v2.upgraded";