[pbs-devel] [PATCH v7 vma-to-pbs 6/9] refactor error handling

Filip Schauer f.schauer at proxmox.com
Tue Apr 9 14:14:20 CEST 2024


Signed-off-by: Filip Schauer <f.schauer at 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





More information about the pbs-devel mailing list