public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Christian Ebner <c.ebner@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [PATCH proxmox-backup v3 2/5] datastore: data blob: refactor decoding method
Date: Wed, 29 Jul 2026 11:07:34 +0200	[thread overview]
Message-ID: <20260729090737.135385-3-c.ebner@proxmox.com> (raw)
In-Reply-To: <20260729090737.135385-1-c.ebner@proxmox.com>

Improve code style and readability by using a single match statement
instead of chained if statements and deduplicate the common digest
verificaton, performed on the decoded blob data.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
Reviewed-by: Robert Obkircher <r.obkircher@proxmox.com>
---
 pbs-datastore/src/data_blob.rs | 62 +++++++++++++++++-----------------
 1 file changed, 31 insertions(+), 31 deletions(-)

diff --git a/pbs-datastore/src/data_blob.rs b/pbs-datastore/src/data_blob.rs
index 465bcb280..44ab41e16 100644
--- a/pbs-datastore/src/data_blob.rs
+++ b/pbs-datastore/src/data_blob.rs
@@ -180,33 +180,33 @@ impl DataBlob {
         config: Option<&CryptConfig>,
         digest: Option<&[u8; 32]>,
     ) -> Result<Vec<u8>, Error> {
-        let magic = self.magic();
+        let magic = *self.magic();
 
-        if magic == &UNCOMPRESSED_BLOB_MAGIC_1_0 {
-            let data_start = std::mem::size_of::<DataBlobHeader>();
-            let data = self.raw_data[data_start..].to_vec();
-            if let Some(digest) = digest {
-                Self::verify_digest(&data, None, digest)?;
+        let (data, crypt_config) = match magic {
+            UNCOMPRESSED_BLOB_MAGIC_1_0 => {
+                let data_start = std::mem::size_of::<DataBlobHeader>();
+                let data = self.raw_data[data_start..].to_vec();
+                (data, None)
             }
-            Ok(data)
-        } else if magic == &COMPRESSED_BLOB_MAGIC_1_0 {
-            let data_start = std::mem::size_of::<DataBlobHeader>();
-            let mut reader = &self.raw_data[data_start..];
-            let data = zstd::stream::decode_all(&mut reader)?;
-            // zstd::block::decompress is about 10% slower
-            // let data = zstd::block::decompress(&self.raw_data[data_start..], MAX_BLOB_SIZE)?;
-            if let Some(digest) = digest {
-                Self::verify_digest(&data, None, digest)?;
+            COMPRESSED_BLOB_MAGIC_1_0 => {
+                let data_start = std::mem::size_of::<DataBlobHeader>();
+                let mut reader = &self.raw_data[data_start..];
+                let data = zstd::stream::decode_all(&mut reader)?;
+                // zstd::block::decompress is about 10% slower
+                // let data = zstd::block::decompress(&self.raw_data[data_start..], MAX_BLOB_SIZE)?;
+                (data, None)
             }
-            Ok(data)
-        } else if magic == &ENCR_COMPR_BLOB_MAGIC_1_0 || magic == &ENCRYPTED_BLOB_MAGIC_1_0 {
-            let header_len = std::mem::size_of::<EncryptedDataBlobHeader>();
-            let head = unsafe {
-                (&self.raw_data[..header_len]).read_le_value::<EncryptedDataBlobHeader>()?
-            };
+            ENCR_COMPR_BLOB_MAGIC_1_0 | ENCRYPTED_BLOB_MAGIC_1_0 => {
+                let header_len = std::mem::size_of::<EncryptedDataBlobHeader>();
+                let head = unsafe {
+                    (&self.raw_data[..header_len]).read_le_value::<EncryptedDataBlobHeader>()?
+                };
 
-            if let Some(config) = config {
-                let data = if magic == &ENCR_COMPR_BLOB_MAGIC_1_0 {
+                let Some(config) = config else {
+                    bail!("unable to decrypt blob - missing CryptConfig");
+                };
+
+                let data = if magic == ENCR_COMPR_BLOB_MAGIC_1_0 {
                     Self::decode_compressed_chunk(
                         config,
                         &self.raw_data[header_len..],
@@ -221,16 +221,16 @@ impl DataBlob {
                         &head.tag,
                     )?
                 };
-                if let Some(digest) = digest {
-                    Self::verify_digest(&data, Some(config), digest)?;
-                }
-                Ok(data)
-            } else {
-                bail!("unable to decrypt blob - missing CryptConfig");
+                (data, Some(config))
             }
-        } else {
-            bail!("Invalid blob magic number.");
+            _ => bail!("Invalid blob magic number."),
+        };
+
+        if let Some(digest) = digest {
+            Self::verify_digest(&data, crypt_config, digest)?;
         }
+
+        Ok(data)
     }
 
     /// Load data blob via given sync ``reader`` and verify its CRC
-- 
2.47.3





  parent reply	other threads:[~2026-07-29  9:08 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-29  9:07 [PATCH proxmox-backup v3 0/5] restrict previous manifest reuse checks for push sync jobs Christian Ebner
2026-07-29  9:07 ` [PATCH proxmox-backup v3 1/5] datastore: data blob: refactor crypt mode method Christian Ebner
2026-07-29  9:07 ` Christian Ebner [this message]
2026-07-29  9:07 ` [PATCH proxmox-backup v3 3/5] client: backup writer: pass no crypt config to manifest blob decoder Christian Ebner
2026-07-29  9:07 ` [PATCH proxmox-backup v3 4/5] client: allow skipping signature check on previous manifest fetching Christian Ebner
2026-07-29  9:07 ` [PATCH proxmox-backup v3 5/5] sync: push: gracefully handle previous manifest signature mismatches Christian Ebner
2026-07-29 13:00 ` applied-series: [PATCH proxmox-backup v3 0/5] restrict previous manifest reuse checks for push sync jobs Fabian Grünbichler

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260729090737.135385-3-c.ebner@proxmox.com \
    --to=c.ebner@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal