public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Filip Schauer <f.schauer@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH v7 vma-to-pbs 6/9] refactor error handling
Date: Tue,  9 Apr 2024 14:14:20 +0200	[thread overview]
Message-ID: <20240409121423.168627-7-f.schauer@proxmox.com> (raw)
In-Reply-To: <20240409121423.168627-1-f.schauer@proxmox.com>

Signed-off-by: Filip Schauer <f.schauer@proxmox.com>
---
 src/main.rs |  4 ++--
 src/vma.rs  | 36 ++++++++++++++++++------------------
 2 files changed, 20 insertions(+), 20 deletions(-)

diff --git a/src/main.rs b/src/main.rs
index cce9718..e2aed8e 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,4 +1,4 @@
-use anyhow::{Context, Result};
+use anyhow::{Context, Error};
 use clap::{command, Arg, ArgAction};
 use proxmox_sys::linux::tty;
 
@@ -6,7 +6,7 @@ mod vma;
 mod vma2pbs;
 use vma2pbs::{backup_vma_to_pbs, BackupVmaToPbsArgs};
 
-fn main() -> Result<()> {
+fn main() -> Result<(), Error> {
     let matches = command!()
         .arg(
             Arg::new("repository")
diff --git a/src/vma.rs b/src/vma.rs
index 14f6e95..518de8a 100644
--- a/src/vma.rs
+++ b/src/vma.rs
@@ -3,7 +3,7 @@ use std::io::Read;
 use std::mem::size_of;
 use std::str;
 
-use anyhow::{anyhow, bail, Result};
+use anyhow::{bail, format_err, Context, Error};
 use bincode::Options;
 use serde::{Deserialize, Serialize};
 use serde_big_array::BigArray;
@@ -111,7 +111,7 @@ pub struct VmaReader<T> {
 }
 
 impl<T: Read> VmaReader<T> {
-    pub fn new(mut vma_file: T) -> Result<Self> {
+    pub fn new(mut vma_file: T) -> Result<Self, Error> {
         let vma_header = Self::read_header(&mut vma_file)?;
         let configs = Self::read_configs(&vma_header)?;
 
@@ -124,7 +124,7 @@ impl<T: Read> VmaReader<T> {
         Ok(instance)
     }
 
-    fn read_header(vma_file: &mut T) -> Result<VmaHeader> {
+    fn read_header(vma_file: &mut T) -> Result<VmaHeader, Error> {
         let mut buffer = vec![0; VMA_HEADER_SIZE_NO_BLOB_BUFFER];
         vma_file.read_exact(&mut buffer)?;
 
@@ -135,11 +135,11 @@ impl<T: Read> VmaReader<T> {
         let mut vma_header: VmaHeader = bincode_options.deserialize(&buffer)?;
 
         if vma_header.magic != VMA_HEADER_MAGIC {
-            return Err(anyhow!("Invalid magic number"));
+            bail!("Invalid magic number");
         }
 
         if vma_header.version != 1 {
-            return Err(anyhow!("Invalid VMA version {}", vma_header.version));
+            bail!("Invalid VMA version {}", vma_header.version);
         }
 
         buffer.resize(vma_header.header_size as usize, 0);
@@ -150,7 +150,7 @@ impl<T: Read> VmaReader<T> {
         let computed_md5sum: [u8; 16] = md5::compute(&buffer).into();
 
         if vma_header.md5sum != computed_md5sum {
-            return Err(anyhow!("Wrong VMA header checksum"));
+            bail!("Wrong VMA header checksum");
         }
 
         let blob_buffer = &buffer[VMA_HEADER_SIZE_NO_BLOB_BUFFER..vma_header.header_size as usize];
@@ -159,7 +159,7 @@ impl<T: Read> VmaReader<T> {
         Ok(vma_header)
     }
 
-    fn read_string(buffer: &[u8]) -> Result<String> {
+    fn read_string(buffer: &[u8]) -> Result<String, Error> {
         let size_bytes: [u8; 2] = buffer[0..2].try_into()?;
         let size = u16::from_le_bytes(size_bytes) as usize;
         let string_bytes: &[u8] = &buffer[2..1 + size];
@@ -168,7 +168,7 @@ impl<T: Read> VmaReader<T> {
         Ok(String::from(string))
     }
 
-    fn read_configs(vma_header: &VmaHeader) -> Result<HashSet<VmaConfig>> {
+    fn read_configs(vma_header: &VmaHeader) -> Result<HashSet<VmaConfig>, Error> {
         let mut configs = HashSet::new();
 
         for i in 0..VMA_MAX_CONFIGS {
@@ -199,7 +199,7 @@ impl<T: Read> VmaReader<T> {
         self.vma_header.dev_info[device_id as usize].device_name_offset != 0
     }
 
-    pub fn get_device_name(&self, device_id: VmaDeviceId) -> Result<String> {
+    pub fn get_device_name(&self, device_id: VmaDeviceId) -> Result<String, Error> {
         let device_name_offset =
             self.vma_header.dev_info[device_id as usize].device_name_offset as usize;
 
@@ -212,7 +212,7 @@ impl<T: Read> VmaReader<T> {
         Ok(device_name)
     }
 
-    pub fn get_device_size(&self, device_id: VmaDeviceId) -> Result<VmaDeviceSize> {
+    pub fn get_device_size(&self, device_id: VmaDeviceId) -> Result<VmaDeviceSize, Error> {
         let dev_info = &self.vma_header.dev_info[device_id as usize];
 
         if dev_info.device_name_offset == 0 {
@@ -222,7 +222,7 @@ impl<T: Read> VmaReader<T> {
         Ok(dev_info.device_size)
     }
 
-    fn read_extent_header(mut vma_file: impl Read) -> Result<VmaExtentHeader> {
+    fn read_extent_header(mut vma_file: impl Read) -> Result<VmaExtentHeader, Error> {
         let mut buffer = vec![0; size_of::<VmaExtentHeader>()];
         vma_file.read_exact(&mut buffer)?;
 
@@ -233,7 +233,7 @@ impl<T: Read> VmaReader<T> {
         let vma_extent_header: VmaExtentHeader = bincode_options.deserialize(&buffer)?;
 
         if vma_extent_header.magic != VMA_EXTENT_HEADER_MAGIC {
-            return Err(anyhow!("Invalid magic number"));
+            bail!("Invalid magic number");
         }
 
         // Fill the MD5 sum field with zeros to compute the MD5 sum
@@ -241,15 +241,15 @@ impl<T: Read> VmaReader<T> {
         let computed_md5sum: [u8; 16] = md5::compute(&buffer).into();
 
         if vma_extent_header.md5sum != computed_md5sum {
-            return Err(anyhow!("Wrong VMA extent header checksum"));
+            bail!("Wrong VMA extent header checksum");
         }
 
         Ok(vma_extent_header)
     }
 
-    fn restore_extent<F>(&mut self, callback: F) -> Result<()>
+    fn restore_extent<F>(&mut self, callback: F) -> Result<(), Error>
     where
-        F: Fn(VmaDeviceId, VmaDeviceOffset, Option<Vec<u8>>) -> Result<()>,
+        F: Fn(VmaDeviceId, VmaDeviceOffset, Option<Vec<u8>>) -> Result<(), Error>,
     {
         let vma_extent_header = Self::read_extent_header(&mut self.vma_file)?;
 
@@ -291,9 +291,9 @@ impl<T: Read> VmaReader<T> {
         Ok(())
     }
 
-    pub fn restore<F>(&mut self, callback: F) -> Result<()>
+    pub fn restore<F>(&mut self, callback: F) -> Result<(), Error>
     where
-        F: Fn(VmaDeviceId, VmaDeviceOffset, Option<Vec<u8>>) -> Result<()>,
+        F: Fn(VmaDeviceId, VmaDeviceOffset, Option<Vec<u8>>) -> Result<(), Error>,
     {
         loop {
             match self.restore_extent(&callback) {
@@ -303,7 +303,7 @@ impl<T: Read> VmaReader<T> {
                         if ioerr.kind() == std::io::ErrorKind::UnexpectedEof {
                             break; // Break out of the loop since the end of the file was reached.
                         } else {
-                            return Err(anyhow!("Failed to read VMA file: {}", ioerr));
+                            return Err(format_err!(e)).context("Failed to read VMA file");
                         }
                     }
                     _ => bail!(e),
-- 
2.39.2





  parent reply	other threads:[~2024-04-09 12:15 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-09 12:14 [pbs-devel] [PATCH v7 vma-to-pbs 0/9] Implement vma-to-pbs tool Filip Schauer
2024-04-09 12:14 ` [pbs-devel] [PATCH v7 vma-to-pbs 1/9] add the ability to provide credentials via files Filip Schauer
2024-04-09 12:14 ` [pbs-devel] [PATCH v7 vma-to-pbs 2/9] bump proxmox-backup-qemu Filip Schauer
2024-04-09 12:14 ` [pbs-devel] [PATCH v7 vma-to-pbs 3/9] remove unnecessary "extern crate" declarations Filip Schauer
2024-04-09 12:14 ` [pbs-devel] [PATCH v7 vma-to-pbs 4/9] add support for streaming the VMA file via stdin Filip Schauer
2024-04-09 12:14 ` [pbs-devel] [PATCH v7 vma-to-pbs 5/9] add a fallback for the --fingerprint argument Filip Schauer
2024-04-09 12:14 ` Filip Schauer [this message]
2024-04-09 12:14 ` [pbs-devel] [PATCH v7 vma-to-pbs 7/9] makefile: remove reference to unused submodule Filip Schauer
2024-04-09 12:14 ` [pbs-devel] [PATCH v7 vma-to-pbs 8/9] switch argument handling from clap to pico-args Filip Schauer
2024-04-09 12:14 ` [pbs-devel] [PATCH v7 vma-to-pbs 9/9] reformat command line arguments to kebab-case Filip Schauer
2024-04-09 13:00 ` [pbs-devel] applied-series: [PATCH v7 vma-to-pbs 0/9] Implement vma-to-pbs tool Wolfgang Bumiller
2024-04-09 13:20 ` [pbs-devel] " Max Carrara

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=20240409121423.168627-7-f.schauer@proxmox.com \
    --to=f.schauer@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